adapt taxeditor cache to cdmlib-cache
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / remoting / cache / ConversationalTransientEntityCacher.java
1 // $Id$
2 /**
3 * Copyright (C) 2018 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.remoting.cache;
11
12 import java.util.HashSet;
13 import java.util.Set;
14
15 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
16 import eu.etaxonomy.cdm.api.service.UpdateResult;
17 import eu.etaxonomy.cdm.api.service.dto.CdmEntityIdentifier;
18 import eu.etaxonomy.cdm.cache.CdmEntityCacheKey;
19 import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
20 import eu.etaxonomy.cdm.model.common.CdmBase;
21
22 /**
23 * @author k.luther
24 * @date 15.03.2018
25 *
26 */
27 public class ConversationalTransientEntityCacher extends CdmTransientEntityCacher {
28
29 /**
30 * @param sessionOwner
31 */
32 public ConversationalTransientEntityCacher(Object sessionOwner) {
33 super(sessionOwner);
34
35 }
36 private CdmBase load(CdmEntityIdentifier cei, boolean update) {
37 return CdmApplicationState.getCommonService().findWithUpdate(cei.getCdmClass(), cei.getId());
38 }
39
40
41 public UpdateResult load(UpdateResult result, boolean update) {
42 // probably a good time to broadcast to other sessions
43
44 Set<CdmBase> updatedObjects = result.getUpdatedObjects();
45 Set<CdmBase> reloadedObjects = new HashSet<CdmBase>();
46 Set<CdmEntityIdentifier> updatedCdmIds = result.getUpdatedCdmIds();
47 boolean updatedCdmIdsIsEmpty = updatedCdmIds.isEmpty();
48
49 // if the cdm identifier set contains identifiers of objects already
50 // present in the updated objects set reomve them
51 for(CdmBase updatedObject : updatedObjects) {
52 if(updatedObject != null && super.exists(new CdmEntityCacheKey(updatedObject.getClass(), updatedObject.getId()))) {
53 CdmEntityIdentifier cdmEntityIdentifier = new CdmEntityIdentifier(updatedObject.getId(), updatedObject.getClass());
54 if(!updatedCdmIdsIsEmpty && updatedCdmIds.contains(cdmEntityIdentifier)) {
55 updatedCdmIds.remove(cdmEntityIdentifier);
56 }
57 reloadedObjects.add(load(updatedObject, update));
58 }
59 }
60
61 // remote load cdm identifiers of objects which already exist
62 // in the cache
63
64 for(CdmEntityIdentifier cei : updatedCdmIds) {
65 if(exists(new CdmEntityCacheKey(cei.getCdmClass(), cei.getId()))) {
66 reloadedObjects.add(load(cei, update));
67 }
68
69 }
70 updatedObjects.clear();
71 result.addUpdatedObjects(reloadedObjects);
72 return result;
73 }
74
75 }