Project

General

Profile

Download (2.75 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
 * @author k.luther
24
 * @date 15.03.2018
25
 */
26
public class ConversationalTransientEntityCacher extends CdmTransientEntityCacher {
27

    
28
    public ConversationalTransientEntityCacher(Object sessionOwner) {
29
        super(sessionOwner);
30
    }
31
    private CdmBase load(CdmEntityIdentifier cei, boolean update) {
32
        return CdmApplicationState.getCommonService().findWithUpdate(cei.getCdmClass(), cei.getId());
33
    }
34

    
35
    public UpdateResult load(UpdateResult result, boolean update) {
36
        // probably a good time to broadcast to other sessions
37

    
38
        Set<CdmBase> updatedObjects = result.getUpdatedObjects();
39
        if (result.getCdmEntity()!= null){
40
            updatedObjects.add(result.getCdmEntity());
41
        }
42
        Set<CdmBase> reloadedObjects = new HashSet<>();
43
        Set<CdmEntityIdentifier> updatedCdmIds = result.getUpdatedCdmIds();
44
        boolean updatedCdmIdsIsEmpty = updatedCdmIds.isEmpty();
45

    
46
        // if the cdm identifier set contains identifiers of objects already
47
        // present in the updated objects set remove them
48
        for(CdmBase updatedObject : updatedObjects) {
49
            if(updatedObject != null && exists(new CdmEntityCacheKey<>(updatedObject))) {
50
                CdmEntityIdentifier cdmEntityIdentifier = new CdmEntityIdentifier(updatedObject.getId(), updatedObject.getClass());
51
                if(!updatedCdmIdsIsEmpty && updatedCdmIds.contains(cdmEntityIdentifier)) {
52
                    updatedCdmIds.remove(cdmEntityIdentifier);
53
                }
54
            }
55
            reloadedObjects.add(load(updatedObject, update));
56
        }
57

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