Project

General

Profile

Download (2.88 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.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: Currently this class can not be moved to CdmApplicationState
24
 * due to dependency on CdmApplicationState.
25
 *
26
 * @author k.luther
27
 * @date 15.03.2018
28
 */
29
public class ConversationalTransientEntityCacher extends CdmTransientEntityCacher {
30

    
31
    public ConversationalTransientEntityCacher(Object sessionOwner) {
32
        super(sessionOwner);
33
    }
34
    private CdmBase load(CdmEntityIdentifier cei) {
35
        return CdmApplicationState.getCommonService().find(cei.getCdmClass(), cei.getId());
36
    }
37

    
38
    public UpdateResult load(UpdateResult result, boolean update) {
39
        // probably a good time to broadcast to other sessions
40

    
41
        Set<CdmBase> updatedObjects = result.getUpdatedObjects();
42
        if (result.getCdmEntity()!= null){
43
            updatedObjects.add(result.getCdmEntity());
44
        }
45
        Set<CdmBase> reloadedObjects = new HashSet<>();
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 remove them
51
        for(CdmBase updatedObject : updatedObjects) {
52
            if(updatedObject != null) {
53
                if (exists(new CdmEntityCacheKey<>(updatedObject))){
54
                    CdmEntityIdentifier cdmEntityIdentifier = new CdmEntityIdentifier(updatedObject.getId(), updatedObject.getClass());
55
                    if(!updatedCdmIdsIsEmpty && updatedCdmIds.contains(cdmEntityIdentifier)) {
56
                        updatedCdmIds.remove(cdmEntityIdentifier);
57
                    }
58
                }
59
                reloadedObjects.add(load(updatedObject, update));
60
            }
61
        }
62

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