resourceLocalService.addResources throws PortalException

thumbnail
Mirto Silvio Busico, modified 7 Years ago. Regular Member Posts: 240 Join Date: 1/18/12 Recent Posts
UPDATE: changed the code but the result is the same

Hi all,

I'm trying to manage categories and tags in an MVC portlet in Liferay 7.1

 

In the interface aui is able to select categories and tags in the add form for a "foo" entity

In FooLocalServiceImpl.java (line 47) I have:
 

    public Foo addFooWithoutId(Foo foo, ServiceContext serviceContext) {
        
        long resourcePrimKey = counterLocalService.increment();
        foo.setFooId(resourcePrimKey);        
        
        long userId = serviceContext.getUserId();
        long groupId = serviceContext.getScopeGroupId();
        long companyId = serviceContext.getCompanyId();
        long fooId = foo.getFooId();
        
        User user = null;
        try {
            user = userLocalService.getUser(userId);
        } catch (PortalException pe) {
            System.out.println(pe.getLocalizedMessage());
        }            
            
        foo.setGroupId(groupId);
        foo.setUserId(userId);
        foo.setUserName(user.getFullName());
        
        Foo myFoo = addFoo(foo);
                
        try {
            resourceLocalService.addResources(companyId, groupId, userId, Foo.class.getName(), fooId, false, true, true);
        } catch (PortalException pe) {
            System.out.println(pe.getLocalizedMessage());
        } catch (SystemException se) {
            System.out.println(se.getLocalizedMessage());
        }
        
        try {
            AssetEntry assetEntry = assetEntryLocalService.updateEntry(
                userId, groupId, myFoo.getCreateDate(),
                myFoo.getModifiedDate(), Foo.class.getName(), fooId,
                myFoo.getUuid(), 0, serviceContext.getAssetCategoryIds(),
                serviceContext.getAssetTagNames(), true, true, null, null, null,
                null, ContentTypes.TEXT_HTML, myFoo.getField1(), null, null, null,
                null, 0, 0, null);
            
            assetLinkLocalService.updateLinks(
                userId, assetEntry.getEntryId(),
                serviceContext.getAssetLinkEntryIds(),
                AssetLinkConstants.TYPE_RELATED);
    
        } catch (PortalException pe) {
            System.out.println(pe.getLocalizedMessage());
        } catch (SystemException se) {
            System.out.println(se.getLocalizedMessage());
        }
            
        return myFoo;
    }

The resourceLocalService.addResources throws a PortalException error

Debugging the code I see in the java.lang.Class (line 2124) this code

    @CallerSensitive
    public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
        throws NoSuchMethodException, SecurityException {
        checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
        Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
        if (method == null) {
            throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
        }
        return method;
    }

throws a NoSuchMethodException  and says:

There are no actions associated with the resource msb.db.model.Foo

 

What have I to do to write/read categories and tags for an entity generated by service builder?

 

 

thumbnail
Mirto Silvio Busico, modified 7 Years ago. Regular Member Posts: 240 Join Date: 1/18/12 Recent Posts

Hi all,

after some tries I've found that this code seems to work:

    @Indexable(type = IndexableType.REINDEX)
    @Override
    public Foo addFooWithoutId(Foo foo, ServiceContext serviceContext) {

        long resourcePrimKey = counterLocalService.increment();
        foo.setFooId(resourcePrimKey);

        long userId = serviceContext.getUserId();
        long groupId = serviceContext.getScopeGroupId();
        long companyId = serviceContext.getCompanyId();
        long fooId = foo.getFooId();

        User user = null;
        try {
            user = userLocalService.getUser(userId);
        } catch (PortalException pe) {
            System.out.println(pe.getLocalizedMessage());
        }

        foo.setGroupId(groupId);
        foo.setUserId(userId);
        foo.setUserName(user.getFullName());

        foo.setNew(true);
        Foo myFoo = fooPersistence.update(foo);

        try {
            resourceLocalService.addResources(companyId, groupId, userId, Foo.class.getName(), fooId, false, true,
                    true);
        } catch (PortalException pe) {
            System.out.println(pe.getLocalizedMessage());
        }

        try {
            AssetEntry assetEntry = assetEntryLocalService.updateEntry(userId, groupId, myFoo.getCreateDate(),
                    myFoo.getModifiedDate(), Foo.class.getName(), fooId, myFoo.getUuid(), 0,
                    serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames(), true, true, null, null,
                    null, null, ContentTypes.TEXT_HTML, myFoo.getField1(), null, null, null, null, 0, 0, null);
            
            Indexer<Foo> indexer = IndexerRegistryUtil.nullSafeGetIndexer(
                    Foo.class);

            indexer.reindex(foo);

            assetLinkLocalService.updateLinks(userId, assetEntry.getEntryId(), serviceContext.getAssetLinkEntryIds(),
                    AssetLinkConstants.TYPE_RELATED);

        } catch (PortalException pe) {
            System.out.println(pe.getLocalizedMessage());
        }

        return myFoo;
    }

 

But I don't know how to verify that this works as expected.

Anyone knows how to verify if this code is correct?