1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.db.store;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.UUID;
26
27 import org.efaps.admin.AbstractAdminObject;
28 import org.efaps.ci.CIAdminCommon;
29 import org.efaps.ci.CIAttribute;
30 import org.efaps.ci.CIDB;
31 import org.efaps.db.Instance;
32 import org.efaps.db.MultiPrintQuery;
33 import org.efaps.db.QueryBuilder;
34 import org.efaps.util.EFapsException;
35 import org.efaps.util.cache.CacheLogListener;
36 import org.efaps.util.cache.CacheReloadException;
37 import org.efaps.util.cache.InfinispanCache;
38 import org.infinispan.Cache;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43
44
45
46
47
48 public final class Store
49 extends AbstractAdminObject
50 {
51
52
53
54 public static final String PROPERTY_COMPRESS = "StoreCompress";
55
56
57
58
59 public static final String PROPERTY_JNDINAME = "StoreJNDIName";
60
61
62
63
64 private static final long serialVersionUID = 1L;
65
66
67
68
69 private static final Logger LOG = LoggerFactory.getLogger(Store.class);
70
71
72
73
74 private static final String UUIDCACHE = Store.class.getName() + ".UUID";
75
76
77
78
79 private static final String IDCACHE = Store.class.getName() + ".ID";
80
81
82
83
84 private static final String NAMECACHE = Store.class.getName() + ".Name";
85
86
87
88
89 private String resource;
90
91
92
93
94 private final Map<String, String> resourceProperties = new HashMap<String, String>();
95
96
97
98
99
100
101 private Store(final long _id,
102 final String _uuid,
103 final String _name)
104 {
105 super(_id, _uuid, _name);
106 }
107
108
109
110
111 @Override
112 protected void setLinkProperty(final UUID _linkTypeUUID,
113 final long _toId,
114 final UUID _toTypeUUID,
115 final String _toName)
116 throws EFapsException
117 {
118 if (_linkTypeUUID.equals(CIDB.Store2Resource.uuid)) {
119 this.resource = _toName;
120 loadResourceProperties(_toId);
121 }
122 super.setLinkProperty(_linkTypeUUID, _toId, _toTypeUUID, _toName);
123 }
124
125
126
127
128
129
130
131 private void loadResourceProperties(final long _id)
132 throws EFapsException
133 {
134 this.resourceProperties.clear();
135 final QueryBuilder queryBldr = new QueryBuilder(CIAdminCommon.Property);
136 queryBldr.addWhereAttrEqValue(CIAdminCommon.Property.Abstract, _id);
137 final MultiPrintQuery multi = queryBldr.getPrint();
138 multi.addAttribute(CIAdminCommon.Property.Name, CIAdminCommon.Property.Value);
139 multi.executeWithoutAccessCheck();
140 while (multi.next()) {
141 this.resourceProperties.put(multi.<String>getAttribute(CIAdminCommon.Property.Name),
142 multi.<String>getAttribute(CIAdminCommon.Property.Value));
143 }
144 }
145
146
147
148
149 protected Map<String, String> getResourceProperties()
150 {
151 return this.resourceProperties;
152 }
153
154
155
156
157
158
159
160
161 public Resource getResource(final Instance _instance)
162 throws EFapsException
163 {
164 Resource ret = null;
165 try {
166 Store.LOG.debug("Getting resource for: {} with properties: {}", this.resource, this.resourceProperties);
167 ret = (Resource) (Class.forName(this.resource).newInstance());
168 ret.initialize(_instance, this);
169 } catch (final InstantiationException e) {
170 throw new EFapsException(Store.class, "getResource.InstantiationException", e, this.resource);
171 } catch (final IllegalAccessException e) {
172 throw new EFapsException(Store.class, "getResource.IllegalAccessException", e, this.resource);
173 } catch (final ClassNotFoundException e) {
174 throw new EFapsException(Store.class, "getResource.ClassNotFoundException", e, this.resource);
175 }
176 return ret;
177 }
178
179
180
181
182
183
184 public static void initialize()
185 throws CacheReloadException
186 {
187 if (InfinispanCache.get().exists(Store.UUIDCACHE)) {
188 InfinispanCache.get().<UUID, Store>getCache(Store.UUIDCACHE).clear();
189 } else {
190 InfinispanCache.get().<UUID, Store>getCache(Store.UUIDCACHE).addListener(new CacheLogListener(Store.LOG));
191 }
192 if (InfinispanCache.get().exists(Store.IDCACHE)) {
193 InfinispanCache.get().<Long, Store>getCache(Store.IDCACHE).clear();
194 } else {
195 InfinispanCache.get().<Long, Store>getCache(Store.IDCACHE).addListener(new CacheLogListener(Store.LOG));
196 }
197 if (InfinispanCache.get().exists(Store.NAMECACHE)) {
198 InfinispanCache.get().<String, Store>getCache(Store.NAMECACHE).clear();
199 } else {
200 InfinispanCache.get().<String, Store>getCache(Store.NAMECACHE).addListener(new CacheLogListener(Store.LOG));
201 }
202 }
203
204
205
206
207
208
209
210
211
212 public static Store get(final long _id)
213 throws CacheReloadException
214 {
215 final Cache<Long, Store> cache = InfinispanCache.get().<Long, Store>getCache(Store.IDCACHE);
216 if (!cache.containsKey(_id)) {
217 Store.getStoreFromDB(CIDB.Store.ID, _id);
218 }
219 return cache.get(_id);
220 }
221
222
223
224
225
226
227
228
229
230 public static Store get(final String _name)
231 throws CacheReloadException
232 {
233 final Cache<String, Store> cache = InfinispanCache.get().<String, Store>getCache(Store.NAMECACHE);
234 if (!cache.containsKey(_name)) {
235 Store.getStoreFromDB(CIDB.Store.Name, _name);
236 }
237 return cache.get(_name);
238 }
239
240
241
242
243
244
245
246
247
248 public static Store get(final UUID _uuid)
249 throws CacheReloadException
250 {
251 final Cache<UUID, Store> cache = InfinispanCache.get().<UUID, Store>getCache(Store.UUIDCACHE);
252 if (!cache.containsKey(_uuid)) {
253 Store.getStoreFromDB(CIDB.Store.UUID, String.valueOf(_uuid));
254 }
255 return cache.get(_uuid);
256 }
257
258
259
260
261 private static void cacheStore(final Store _store)
262 {
263 final Cache<UUID, Store> cache4UUID = InfinispanCache.get().<UUID, Store>getIgnReCache(Store.UUIDCACHE);
264 cache4UUID.putIfAbsent(_store.getUUID(), _store);
265
266 final Cache<String, Store> nameCache = InfinispanCache.get().<String, Store>getIgnReCache(Store.NAMECACHE);
267 nameCache.putIfAbsent(_store.getName(), _store);
268
269 final Cache<Long, Store> idCache = InfinispanCache.get().<Long, Store>getIgnReCache(Store.IDCACHE);
270 idCache.putIfAbsent(_store.getId(), _store);
271 }
272
273
274
275
276
277
278
279 private static boolean getStoreFromDB(final CIAttribute _uUID,
280 final Object _criteria)
281 throws CacheReloadException
282 {
283 final boolean ret = false;
284 try {
285 final QueryBuilder queryBldr = new QueryBuilder(CIDB.Store);
286 queryBldr.addWhereAttrEqValue(CIDB.Store.ID, _criteria);
287 final MultiPrintQuery multi = queryBldr.getPrint();
288 multi.addAttribute(CIDB.Store.Name, CIDB.Store.ID, CIDB.Store.UUID);
289 multi.executeWithoutAccessCheck();
290 if (multi.next()) {
291 final long id = multi.<Long>getAttribute(CIDB.Store.ID);
292 final String name = multi.<String>getAttribute(CIDB.Store.Name);
293 final String uuid = multi.<String>getAttribute(CIDB.Store.UUID);
294 final Store store = new Store(id, uuid, name);
295 store.readFromDB4Properties();
296 store.readFromDB4Links();
297 Store.cacheStore(store);
298 }
299 } catch (final EFapsException e) {
300 throw new CacheReloadException("Could not read Store.", e);
301 }
302 return ret;
303 }
304
305 @Override
306 public boolean equals(final Object _obj)
307 {
308 boolean ret;
309 if (_obj instanceof Store) {
310 ret = ((Store) _obj).getId() == getId();
311 } else {
312 ret = super.equals(_obj);
313 }
314 return ret;
315 }
316
317 @Override
318 public int hashCode()
319 {
320 return Long.valueOf(getId()).intValue();
321 }
322 }