Project

General

Profile

« Previous | Next » 

Revision c27ecf00

Added by Cherian Mathew over 8 years ago

#5299 Handle session caching of new entities

View differences:

eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/cache/CdmTransientEntityCacher.java
11 11

  
12 12
import java.util.ArrayList;
13 13
import java.util.Collection;
14
import java.util.HashMap;
14 15
import java.util.HashSet;
15 16
import java.util.List;
16 17
import java.util.Map;
17 18
import java.util.Set;
19
import java.util.UUID;
18 20

  
19 21
import net.sf.ehcache.Cache;
20 22
import net.sf.ehcache.CacheManager;
......
63 65

  
64 66
    private final CacheLoader cacheLoader;
65 67

  
66

  
68
    private final Map<UUID, CdmBase> newEntitiesMap = new HashMap<UUID, CdmBase>();
67 69

  
68 70
    public CdmTransientEntityCacher(String cacheId, ICdmEntitySessionManager cdmEntitySessionManager) {
69 71
        this.cacheId = cacheId;
......
75 77
        this.cdmEntitySessionManager = cdmEntitySessionManager;
76 78

  
77 79
        cacheLoader = new CacheLoader(this);
80

  
78 81
    }
79 82

  
80 83
    public CdmTransientEntityCacher(Object sessionOwner, ICdmEntitySessionManager cdmEntitySessionManager) {
......
186 189
    }
187 190

  
188 191

  
189

  
192
    public void addNewEntity(CdmBase newEntity) {
193
        if(newEntity != null && newEntity.getId() == 0 && newEntity.getUuid() != null) {
194
            newEntitiesMap.put(newEntity.getUuid(), newEntity);
195
        }
196
    }
190 197

  
191 198
    @Override
192 199
    public void put(CdmBase cdmEntity) {
......
200 207

  
201 208
        cachedCdmEntity = getFromCache(id);
202 209
        if(cachedCdmEntity == null) {
203
            getCache().put(new Element(id, cdmEntity));
204
            cdmEntity.initListener();
205
            logger.info(" - object of type " + cdmEntity.getClass().getName() + " with id " + cdmEntity.getId() + " put in cache");
210
            CdmBase cdmEntityToCache = cdmEntity;
211
            CdmBase newEntity = newEntitiesMap.get(cdmEntity.getUuid());
212
            if(newEntity != null) {
213
                newEntity.setId(cdmEntity.getId());
214
                cdmEntityToCache = newEntity;
215
            }
216
            getCache().put(new Element(id, cdmEntityToCache));
217
            cdmEntityToCache.initListener();
218
            newEntitiesMap.remove(cdmEntity.getUuid());
219
            logger.info(" - object of type " + cdmEntityToCache.getClass().getName() + " with id " + cdmEntityToCache.getId() + " put in cache");
206 220
            return;
207 221
        }
208 222
        logger.info(" - object of type " + cdmEntity.getClass().getName() + " with id " + cdmEntity.getId() + " already exists");
......
274 288
    public void dispose() {
275 289
        CacheManager.create().removeCache(cache.getName());
276 290
        cache.dispose();
291
        newEntitiesMap.clear();
277 292

  
278 293
    }
279 294

  

Also available in: Unified diff