掲示板

Why are my asset tags not in the serviceContext?

thumbnail
6年前 に Linda van der Pal によって更新されました。

Why are my asset tags not in the serviceContext?

New Member 投稿: 21 参加年月日: 12/02/20 最新の投稿
I've used the service builder to create an object called Event and created a portlet to create and update these events. Now I want to add tags to my events. I've read all the documentation and tried to implement it, but somehow my tags don't end up in the service context.

I'm using Liferay DXP.

Here's the jsp code to edit the events:

<%@ include file="/init.jsp"%>
<liferay-portlet:actionurl name="addEvent" var="editAction">
	<portlet:param name="mvcActionCommand" value="addEvent" />
</liferay-portlet:actionurl>
...
<aui:form action="<%=editAction%>" method="post" name="fm">
	<aui:fieldset>
	...
	<aui:input name="tags" type="assetTags" />
		<aui:button type="submit"></aui:button>
		...
	</aui:fieldset>
</aui:form>
...


Here's my AddEventActionCommand code:

@Override
	protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
		ServiceContext serviceContext = ServiceContextFactory.getInstance(Event.class.getName(), actionRequest);
		
		long eventId = ParamUtil.getLong(actionRequest, "eventId");
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
		String name = ParamUtil.getString(actionRequest, "name");
		...
		
		Event event = null;
		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
		if (newEvent) {
		    long groupId = themeDisplay.getScopeGroupId();
		    
		    eventService.addEvent(name, themeDisplay.getRealUserId(), groupId,
		                            themeDisplay.getCompanyId(), city, countryId, startDate, endDate, url, cfpUrl, duchessEvent, description, 
		                            cfpStartDate, cfpEndDate, serviceContext);
		} else {
			event = eventService.getEvent(eventId);
			event.setName(name);
			event.setCity(city);
			event.setUrl(url);
			event.setStartDate(startDate);
			event.setEndDate(endDate);
			event.setCountryId(countryId);
			event.setDuchessEvent(duchessEvent);
			event.setDescription(description);
			event.setCfpUrl(cfpUrl);
			event.setCfpStartDate(cfpStartDate);
			event.setCfpEndDate(cfpEndDate);
			eventService.updateEvent(themeDisplay.getRealUserId(), event, serviceContext);
		}
	}


And finally there's my EventLocalServiceImpl code:

@Indexable(type = IndexableType.REINDEX)
	public Event addEvent(String name, long userId, long groupId, long companyId, 
			String city, long countryId, Date startDate, Date endDate, String url, 
			String cfpUrl, boolean duchessEvent, String description, Date cfpStartDate, Date cfpEndDate, ServiceContext serviceContext) throws PortalException {
		
		long eventId = counterLocalService.increment(Event.class.getName());
		Event event = eventLocalService.createEvent(eventId);
		
		event.setGroupId(groupId);
		event.setCompanyId(companyId);
		event.setUserId(userId);
		event.setName(name);
		...
		
		event = eventLocalService.addEvent(event);
		
		String[] assetTagNames = serviceContext.getAssetTagNames();
		AssetEntry assetEntry = assetEntryLocalService.updateEntry(
			    userId, event.getGroupId(), event.getCreateDate(),
			    event.getModifiedDate(), Event.class.getName(),
			    event.getEventId(), event.getUuid(), 0,
			    serviceContext.getAssetCategoryIds(), assetTagNames, true, true, null, null,
			    event.getCreateDate(), null, ContentTypes.TEXT_HTML,
			    event.getName(), null, null, null, null, 0, 0, null);

		Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(Event.class);
		indexer.reindex(event);

		return event;
	}


For some reason serviceContext.getAssetTagNames() returns an empty list. Can anyone tell me what I am doing wrong?
thumbnail
6年前 に Olaf Kock によって更新されました。

RE: Why are my asset tags not in the serviceContext?

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
Linda van der Pal:
For some reason serviceContext.getAssetTagNames() returns an empty list. Can anyone tell me what I am doing wrong?


As far as I see, you're passing in an empty serviceContext. There's no magic in it - in fact, I like to pooh-pooh it as "the missing 7-15 parameters in an service's API method". Unless I'm missing something, I'd expect them required to be extracted from the actionRequest and explicitly set in the serviceContext by yourself, in your action handler.
thumbnail
6年前 に Linda van der Pal によって更新されました。

RE: Why are my asset tags not in the serviceContext?

New Member 投稿: 21 参加年月日: 12/02/20 最新の投稿
Ah, all the sample code I could find led me to believe otherwise. However, I had already tried setting the asset tags onto the serviceContext manually, but when I did that, I got exceptions whenever I tried to add a tag that was already in the database to an asset. For some reason the underlying code then insisted on adding the tag to the database as if it were new. I would expect it to be able to handle that.
thumbnail
6年前 に Linda van der Pal によって更新されました。

RE: Why are my asset tags not in the serviceContext?

New Member 投稿: 21 参加年月日: 12/02/20 最新の投稿
Diving into the ServiceContextFactory, I found out that I should have used assetTagNames as name instead of tags as the examples in the documentation (https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/implementing-asset-categorization-and-tagging) suggest.

<aui:input name="assetTagNames" type="assetTags" />

However, this still leaves the problem of not being able to save the tags if they already exist in the database.
thumbnail
6年前 に Linda van der Pal によって更新されました。

RE: Why are my asset tags not in the serviceContext?

New Member 投稿: 21 参加年月日: 12/02/20 最新の投稿
I've moved this follow-up question to a topic of it's own: https://web.liferay.com/community/forums/-/message_boards/message/90361476