- extended DnaQuality details view
[taxeditor.git] / eu.etaxonomy.taxeditor.remoting / src / main / java / eu / etaxonomy / cdm / api / cache / LazyLoadingCachingUtils.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 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.cdm.api.cache;
11
12 import java.util.Iterator;
13 import java.util.Map;
14
15 import org.hibernate.collection.internal.PersistentMap;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.stereotype.Component;
18
19 import eu.etaxonomy.cdm.model.common.CdmBase;
20
21 /**
22 * @author cmathew
23 * @date 15 Oct 2014
24 *
25 */
26 @Component
27 public class LazyLoadingCachingUtils {
28
29 @Autowired
30 private CdmEntityCacheManager cdmEntityCacheManager;
31
32 public static enum CollectionType {
33 SET,
34 LIST,
35 MAP;
36
37 @Override
38 public String toString() {
39 return this.name().toLowerCase();
40 }
41 }
42
43 public void updatePersistentCollection(Map map) {
44 if(map == null || map.isEmpty()) {
45 return;
46 }
47
48 int originalMapSize = map.size();
49 Object[] result = new Object[ map.size() * 2 ];
50 Iterator iter = map.entrySet().iterator();
51 int i=0;
52 while ( iter.hasNext() ) {
53 Map.Entry e = (Map.Entry) iter.next();
54 result[i++] = e.getKey();
55 result[i++] = e.getValue();
56 }
57
58 for(i=0; i<result.length;i++) {
59 if(result[i] instanceof CdmBase) {
60 CdmBase cdmBase = (CdmBase)result[i];
61 CdmBase cachedCdmBase = cdmEntityCacheManager.getCdmTransientEntityCacher().put(cdmBase.getClass(),cdmBase.getId(), cdmBase);
62 result[i] = cachedCdmBase;
63 }
64 }
65 map.clear();
66 for(i = 0; i < originalMapSize; i+=2 ) {
67 map.put(
68 result[i],
69 result[i+1]
70 );
71 }
72
73 }
74
75
76
77 }