Merge branch 'hotfix/5.45.1'
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / remoting / cache / ConversationalTransientEntityCacher.java
1 /**
2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.remoting.cache;
10
11 import java.util.HashSet;
12 import java.util.Set;
13
14 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
15 import eu.etaxonomy.cdm.api.service.ICommonService;
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 * Note: The methods of this class were originally found in the {@link CdmTransientEntityCacher}.
24 * Once that class was moved to cdmlib it was not possible to move these methods, too,
25 * because of the current dependency on CdmApplicationState.
26 * In future it should be tried to move them also to cdmlib by replacing the
27 * {@link CdmApplicationState} dependency e.g. by adding {@link ICommonService} as parameter
28 * to the method {@link #load(CdmEntityIdentifier)}
29 *
30 * @author k.luther
31 * @date 15.03.2018
32 */
33 public class ConversationalTransientEntityCacher extends CdmTransientEntityCacher {
34
35 public ConversationalTransientEntityCacher(Object sessionOwner) {
36 super(sessionOwner);
37 }
38 private CdmBase load(CdmEntityIdentifier cei) {
39 return CdmApplicationState.getCommonService().findWithUpdate(cei.getCdmClass(), cei.getId());
40 }
41
42 public UpdateResult load(UpdateResult result, boolean update) {
43 // probably a good time to broadcast to other sessions
44
45 Set<CdmBase> updatedObjects = result.getUpdatedObjects();
46 if (result.getCdmEntity()!= null){
47 updatedObjects.add(result.getCdmEntity());
48 }
49 Set<CdmBase> reloadedObjects = new HashSet<>();
50 Set<CdmEntityIdentifier> updatedCdmIds = result.getUpdatedCdmIds();
51 boolean updatedCdmIdsIsEmpty = updatedCdmIds.isEmpty();
52
53 // if the cdm identifier set contains identifiers of objects already
54 // present in the updated objects set remove them
55 for(CdmBase updatedObject : updatedObjects) {
56 if(updatedObject != null) {
57 if (exists(new CdmEntityCacheKey<>(updatedObject))){
58 CdmEntityIdentifier cdmEntityIdentifier = CdmEntityIdentifier.NewInstance(updatedObject);
59 if(!updatedCdmIdsIsEmpty && updatedCdmIds.contains(cdmEntityIdentifier)) {
60 updatedCdmIds.remove(cdmEntityIdentifier);
61 }
62 }
63 CdmBase cachedObj = load(updatedObject, update);
64 reloadedObjects.add(cachedObj);
65 }
66 }
67
68 // remote load cdm identifiers of objects which already exist
69 // in the cache
70 for(CdmEntityIdentifier id : updatedCdmIds) {
71 @SuppressWarnings({ "rawtypes", "unchecked" })
72 CdmEntityCacheKey<?> cacheKey = new CdmEntityCacheKey(id.getCdmClass(), id.getId());
73 if(exists(cacheKey)) {
74 reloadedObjects.add(load(id));
75 }
76 }
77 updatedObjects.clear();
78 result.addUpdatedObjects(reloadedObjects);
79 return result;
80 }
81 }