1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
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   * TODO comment!
44   *
45   * @author The eFaps Team
46   * @version $Id$
47   */
48  public final class Store
49      extends AbstractAdminObject
50  {
51      /**
52       * Property to get the compress for this store.
53       */
54      public static final String PROPERTY_COMPRESS = "StoreCompress";
55  
56      /**
57       * Property name to get the JDNI Name.
58       */
59      public static final String PROPERTY_JNDINAME = "StoreJNDIName";
60  
61      /**
62       * Needed for serialization.
63       */
64      private static final long serialVersionUID = 1L;
65  
66      /**
67       * Logging instance used in this class.
68       */
69      private static final Logger LOG = LoggerFactory.getLogger(Store.class);
70  
71      /**
72       * Name of the Cache by UUID.
73       */
74      private static final String UUIDCACHE = Store.class.getName() + ".UUID";
75  
76      /**
77       * Name of the Cache by ID.
78       */
79      private static final String IDCACHE = Store.class.getName() + ".ID";
80  
81      /**
82       * Name of the Cache by Name.
83       */
84      private static final String NAMECACHE = Store.class.getName() + ".Name";
85  
86      /**
87       * Name of the Resource class of this store.
88       */
89      private String resource;
90  
91      /**
92       * Properties for the StoreResource.
93       */
94      private final Map<String, String> resourceProperties = new HashMap<String, String>();
95  
96      /**
97       * @param _id id of this store
98       * @param _uuid uuid of this store
99       * @param _name name of thi store
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      * {@inheritDoc}
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      * Method to get the properties for the resource.
127      *
128      * @param _id id of the resource
129      * @throws EFapsException on error
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      * @return value for instance variable {@link #resourceProperties}
148      */
149     protected Map<String, String> getResourceProperties()
150     {
151         return this.resourceProperties;
152     }
153 
154     /**
155      * Method to get a instance of the resource.
156      *
157      * @param _instance instance the resource is wanted for
158      * @return Resource
159      * @throws EFapsException on error
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      * Method to initialize the Cache of this CacheObjectInterface.
181      *
182      * @throws CacheReloadException on error during loading of cache
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      * Returns for given parameter <i>_id</i> the instance of class
206      * {@link Store}.
207      *
208      * @param _id id of the type to get
209      * @return instance of class {@link Store}
210      * @throws CacheReloadException on error
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      * Returns for given parameter <i>_name</i> the instance of class
224      * {@link Store}.
225      *
226      * @param _name name of the type to get
227      * @return instance of class {@link Store}
228      * @throws CacheReloadException  on error
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      * Returns for given parameter <i>_uuid</i> the instance of class
242      * {@link Store}.
243      *
244      * @param _uuid uuid of the type to get
245      * @return instance of class {@link Store}
246      * @throws CacheReloadException  on error
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      * @param _store Store to be cached
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      * @param _uUID      CIAttribute
275      * @param _criteria filter criteria
276      * @return true if successful
277      * @throws CacheReloadException on error
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 }