Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
resourceLocalService.addResources throws PortalException
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?
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?