if (person.isGuest()) {
//Never persist things for the guest user, just rely on in-memory storage
return;
}
final IPortletEntityId wrapperPortletEntityId = portletEntity.getPortletEntityId();
final Lock portletEntityLock = this.getPortletEntityLock(request, wrapperPortletEntityId);
portletEntityLock.lock();
try {
final boolean shouldBePersisted = this.shouldBePersisted(portletEntity);
if (portletEntity instanceof PersistentPortletEntityWrapper) {
//Unwrap the persistent entity
final IPortletEntity persistentEntity = ((PersistentPortletEntityWrapper)portletEntity).getPersistentEntity();
//Already persistent entity that still has prefs
if (shouldBePersisted) {
try {
this.portletEntityDao.updatePortletEntity(persistentEntity);
}
catch (HibernateOptimisticLockingFailureException e) {
//Check if this exception is from the entity being deleted from under us.
final boolean exists = this.portletEntityDao.portletEntityExists(persistentEntity.getPortletEntityId());
if (!exists) {
this.logger.warn("The persistent portlet has already been deleted: " + persistentEntity + ". The passed entity should be persistent so a new persistent entity will be created");
this.deletePortletEntity(request, portletEntity, true);
this.createPersistentEntity(persistentEntity, wrapperPortletEntityId);
}
else {
throw e;
}
}
}
//Already persistent entity that should not be, DELETE!
else {
//Capture identifiers needed to recreate the entity as session persistent
final IPortletDefinitionId portletDefinitionId = portletEntity.getPortletDefinitionId();
final String layoutNodeId = portletEntity.getLayoutNodeId();
final int userId = portletEntity.getUserId();
//Delete the persistent entity
this.deletePortletEntity(request, portletEntity, false);
//Create a new entity and stick it in the cache
this.getOrCreatePortletEntity(request, portletDefinitionId, layoutNodeId, userId);
}
}
else if (portletEntity instanceof SessionPortletEntityImpl) {
//There are preferences on the interim entity, create an store it
if (shouldBePersisted) {
//Remove the session scoped entity from the request and session caches
this.deletePortletEntity(request, portletEntity, false);
final IPortletEntity persistentEntity = createPersistentEntity(portletEntity, wrapperPortletEntityId);
if (this.logger.isTraceEnabled()) {
this.logger.trace("Session scoped entity " + wrapperPortletEntityId + " should now be persistent. Deleted it from session cache and created persistent portlet entity " + persistentEntity.getPortletEntityId());
}
}
//Session scoped entity that is still session scoped,
else {
//Look for a persistent entity and delete it
final String channelSubscribeId = portletEntity.getLayoutNodeId();
final int userId = portletEntity.getUserId();
IPortletEntity existingPersistentEntity = this.portletEntityDao.getPortletEntity(channelSubscribeId, userId);
if (existingPersistentEntity != null) {
final IPortletEntityId consistentPortletEntityId = this.createConsistentPortletEntityId(existingPersistentEntity);
existingPersistentEntity = new PersistentPortletEntityWrapper(existingPersistentEntity, consistentPortletEntityId);
this.logger.warn("A persistent portlet entity already exists: " + existingPersistentEntity + ". The passed entity has no preferences so the persistent version will be deleted");
this.deletePortletEntity(request, existingPersistentEntity, false);