Fórum

How to set createDate and modifiedDate in serviceContext ?

Julien Tabouret, modificado 8 Anos atrás.

How to set createDate and modifiedDate in serviceContext ?

Junior Member Postagens: 27 Data de Entrada: 17/05/13 Postagens Recentes
Hello everyone,

I defined a custom entity with all required fields so it extends AuditedModel.

In the local service of this entity, I want to use ServiceContext to set the create and modified dates:
myEntity.setCreateDate(serviceContext.getCreateDate());
myEntity.setModifiedDate(serviceContext.getModifiedDate());

But these methods always return null.

The javadoc says about ServiceContext#setCreateDate:
Sets the date when an entity was created if this service context is being passed as a parameter to a method which creates an entity.

But it cant figure out how it works and how to use it.

Does it mean that there is a Liferay Aspect that injects appropriate values at runtime ? In that case what is the signature of my create method supposed to look like ? Currently MyEntityLocalService defines the following method:
MyEntity save(MyEntity entity, ServiceContext serviceContext);

Or am I supposed to put some value (for example command) in my JSP so that the request tells service context to initialize the dates ?

I just don't know what to do and I was not able to find the answer in the Liferay source code.

Thank you for your help.
Regards.

PS : I am using Liferay 6.2 CE ga2
thumbnail
Olaf Kock, modificado 8 Anos atrás.

RE: How to set createDate and modifiedDate in serviceContext ?

Liferay Legend Postagens: 6403 Data de Entrada: 23/09/08 Postagens Recentes
AFAIK there's no magic - if you use ServiceContext as a parameter, you'll basically hide which values you actually need (it can be any of ServiceContext's properties) and gain a smaller interface, e.g. just one additional parameter instead of 20. Unfortunately you'll have to create the ServiceContext yourself and set the values that your implementation relies on.

I'd love to be wrong here, but so far I've done well with this assumption.
Julien Tabouret, modificado 8 Anos atrás.

RE: How to set createDate and modifiedDate in serviceContext ?

Junior Member Postagens: 27 Data de Entrada: 17/05/13 Postagens Recentes
Hello Olaf,
As you say it seems that the job has to be done in the code. Even the implementation of AssetEntryLocalServiceImpl#updateAsset does not rely too much on the values returned by service context :
public AssetEntry updateEntry(
		long userId, long groupId, Date createDate, Date modifiedDate,
		String className, long classPK, String classUuid, long classTypeId,
		long[] categoryIds, String[] tagNames, boolean visible,
		Date startDate, Date endDate, Date expirationDate, String mimeType,
		String title, String description, String summary, String url,
		String layoutUuid, int height, int width, Integer priority,
		boolean sync)
	throws PortalException, SystemException {
	...
	if (modifiedDate == null) {
		modifiedDate = new Date();
	}
	...
		if (createDate == null) {
			createDate = new Date();
		}
	...
}

But in that case, it seems to me that the javadoc is quite ambiguous because it implies that the job is done automagically somewhere.
Sets the date when an entity was created if this service context is being passed as a parameter to a method which creates an entity.

Maybe it is just my interpretation.

Anyway, thank you for your answer.
thumbnail
Vishal Kumar, modificado 8 Anos atrás.

RE: How to set createDate and modifiedDate in serviceContext ?

Regular Member Postagens: 198 Data de Entrada: 12/12/12 Postagens Recentes
Thanks OLAF