2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.remoting
.cache
;
11 import java
.util
.HashSet
;
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
;
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)}
33 public class ConversationalTransientEntityCacher
extends CdmTransientEntityCacher
{
35 public ConversationalTransientEntityCacher(Object sessionOwner
) {
38 private CdmBase
load(CdmEntityIdentifier cei
) {
39 return CdmApplicationState
.getCommonService().findWithUpdate(cei
.getCdmClass(), cei
.getId());
42 public UpdateResult
load(UpdateResult result
, boolean update
) {
43 // probably a good time to broadcast to other sessions
45 Set
<CdmBase
> updatedObjects
= result
.getUpdatedObjects();
46 if (result
.getCdmEntity()!= null){
47 updatedObjects
.add(result
.getCdmEntity());
49 Set
<CdmBase
> reloadedObjects
= new HashSet
<>();
50 Set
<CdmEntityIdentifier
> updatedCdmIds
= result
.getUpdatedCdmIds();
51 boolean updatedCdmIdsIsEmpty
= updatedCdmIds
.isEmpty();
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
);
63 CdmBase cachedObj
= load(updatedObject
, update
);
64 reloadedObjects
.add(cachedObj
);
68 // remote load cdm identifiers of objects which already exist
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
));
77 updatedObjects
.clear();
78 result
.addUpdatedObjects(reloadedObjects
);