Project

General

Profile

Download (3.33 KB) Statistics
| Branch: | Tag: | Revision:
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.ICommonService;
17
import eu.etaxonomy.cdm.api.service.UpdateResult;
18
import eu.etaxonomy.cdm.api.service.dto.CdmEntityIdentifier;
19
import eu.etaxonomy.cdm.cache.CdmEntityCacheKey;
20
import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
21
import eu.etaxonomy.cdm.model.common.CdmBase;
22

    
23
/**
24
 * Note: The methods of this class were originally found in the {@link CdmTransientEntityCacher}.
25
 * Once that class was moved to cdmlib it was not possible to move these methods, too,
26
 * because of the current dependency on CdmApplicationState.
27
 * In future it should be tried to move them also to cdmlib by replacing the
28
 * {@link CdmApplicationState} dependency e.g. by adding {@link ICommonService} as parameter
29
 * to the method  {@link #load(CdmEntityIdentifier)}
30
 *
31
 * @author k.luther
32
 * @date 15.03.2018
33
 */
34
public class ConversationalTransientEntityCacher extends CdmTransientEntityCacher {
35

    
36
    public ConversationalTransientEntityCacher(Object sessionOwner) {
37
        super(sessionOwner);
38
    }
39
    private CdmBase load(CdmEntityIdentifier cei) {
40
        return CdmApplicationState.getCommonService().findWithUpdate(cei.getCdmClass(), cei.getId());
41
    }
42

    
43
    public UpdateResult load(UpdateResult result, boolean update) {
44
        // probably a good time to broadcast to other sessions
45

    
46
        Set<CdmBase> updatedObjects = result.getUpdatedObjects();
47
        if (result.getCdmEntity()!= null){
48
            updatedObjects.add(result.getCdmEntity());
49
        }
50
        Set<CdmBase> reloadedObjects = new HashSet<>();
51
        Set<CdmEntityIdentifier> updatedCdmIds = result.getUpdatedCdmIds();
52
        boolean updatedCdmIdsIsEmpty = updatedCdmIds.isEmpty();
53

    
54
        // if the cdm identifier set contains identifiers of objects already
55
        // present in the updated objects set remove them
56
        for(CdmBase updatedObject : updatedObjects) {
57
            if(updatedObject != null) {
58
                if (exists(new CdmEntityCacheKey<>(updatedObject))){
59
                    CdmEntityIdentifier cdmEntityIdentifier = new CdmEntityIdentifier(updatedObject.getId(), updatedObject.getClass());
60
                    if(!updatedCdmIdsIsEmpty && updatedCdmIds.contains(cdmEntityIdentifier)) {
61
                        updatedCdmIds.remove(cdmEntityIdentifier);
62
                    }
63
                }
64
                CdmBase cachedObj = load(updatedObject, update);
65
                reloadedObjects.add(cachedObj);
66
            }
67
        }
68

    
69
        // remote load cdm identifiers of objects which already exist
70
        // in the cache
71
        for(CdmEntityIdentifier id : updatedCdmIds) {
72
            @SuppressWarnings({ "rawtypes", "unchecked" })
73
            CdmEntityCacheKey<?> cacheKey = new CdmEntityCacheKey(id.getCdmClass(), id.getId());
74
            if(exists(cacheKey)) {
75
                reloadedObjects.add(load(id));
76
            }
77
        }
78
        updatedObjects.clear();
79
        result.addUpdatedObjects(reloadedObjects);
80
        return result;
81
    }
82
}
    (1-1/1)