KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > kernel > layer > application > Codes


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: Codes.java,v 1.9 2006/02/03 00:35:57 wfro Exp $
5  * Description: Codes
6  * Revision: $Revision: 1.9 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2006/02/03 00:35:57 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004-2005, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.kernel.layer.application;
57
58 import java.io.Serializable JavaDoc;
59 import java.util.HashMap JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.List JavaDoc;
62 import java.util.Map JavaDoc;
63 import java.util.SortedMap JavaDoc;
64 import java.util.TreeMap JavaDoc;
65
66 import org.openmdx.base.exception.ServiceException;
67 import org.openmdx.compatibility.base.dataprovider.cci.AttributeSelectors;
68 import org.openmdx.compatibility.base.dataprovider.cci.DataproviderObject_1_0;
69 import org.openmdx.compatibility.base.dataprovider.cci.Directions;
70 import org.openmdx.compatibility.base.dataprovider.cci.RequestCollection;
71 import org.openmdx.compatibility.base.naming.Path;
72 import org.openmdx.kernel.log.SysLog;
73
74 public class Codes
75   implements Serializable JavaDoc {
76   
77     //-------------------------------------------------------------------------
78
public Codes(
79         RequestCollection channel,
80         Path codeSegmentIdentity
81     ) throws ServiceException {
82         this.channel = channel;
83         this.codeSegmentIdentity = codeSegmentIdentity;
84         this.codeValueContainers = new HashMap JavaDoc();
85         this.codeValueContainerIds = new HashMap JavaDoc();
86         this.cachedEntries = new HashMap JavaDoc();
87         this.shortTexts = new HashMap JavaDoc();
88         this.longTexts = new HashMap JavaDoc();
89         this.refresh();
90     }
91
92     //-------------------------------------------------------------------------
93
public void markAsDirty(
94     ) {
95         this.cacheIsDirty = true;
96     }
97
98     //-------------------------------------------------------------------------
99
private void refresh(
100     ) throws ServiceException {
101         DataproviderObject_1_0 codeSegment = null;
102         try {
103             codeSegment = this.channel.addGetRequest(
104                 this.codeSegmentIdentity,
105                 AttributeSelectors.ALL_ATTRIBUTES,
106                 null
107             );
108         } catch(ServiceException e) {}
109         // Only refresh if the code segment exists and if it was modified since last refresh
110
if(this.cacheIsDirty && (codeSegment != null)) {
111             this.codeValueContainers.clear();
112             this.codeValueContainerIds.clear();
113             this.cachedEntries.clear();
114             List JavaDoc valueContainers = this.channel.addFindRequest(
115               codeSegmentIdentity.getChild("valueContainer"),
116               null,
117               AttributeSelectors.ALL_ATTRIBUTES,
118               0,
119               Integer.MAX_VALUE,
120               Directions.ASCENDING
121             );
122             for(
123               Iterator JavaDoc i = valueContainers.iterator();
124               i.hasNext();
125             ) {
126               DataproviderObject_1_0 valueContainer = (DataproviderObject_1_0)i.next();
127               SysLog.detail("preparing code value container", valueContainer.path());
128               this.codeValueContainers.put(
129                   valueContainer.path().getBase(),
130                   valueContainer
131               );
132               List JavaDoc name = valueContainer.values("name");
133               for(Iterator JavaDoc j = name.iterator(); j.hasNext(); ) {
134                 this.codeValueContainerIds.put(
135                   j.next(),
136                   valueContainer.path().getBase()
137                 );
138               }
139             }
140             this.shortTexts.clear();
141             this.longTexts.clear();
142             this.cacheIsDirty = false;
143         }
144     }
145   
146   //-------------------------------------------------------------------------
147
public DataproviderObject_1_0 getCodeValueContainerByName(
148       String JavaDoc name
149   ) {
150       return (DataproviderObject_1_0)this.codeValueContainers.get(
151           this.codeValueContainerIds.get(name)
152       );
153   }
154   
155   //-------------------------------------------------------------------------
156
public DataproviderObject_1_0 getCodeValueContainerById(
157       String JavaDoc id
158   ) {
159       return (DataproviderObject_1_0)this.codeValueContainers.get(id);
160   }
161   
162   //-------------------------------------------------------------------------
163
public List JavaDoc getCodeEntriesById(
164       String JavaDoc id
165   ) throws ServiceException {
166       if(System.currentTimeMillis() > this.cachedEntriesExpirationAt) {
167           this.refresh();
168           this.cachedEntriesExpirationAt = System.currentTimeMillis() + this.codeCacheRefreshRate;
169       }
170       List JavaDoc codeEntries = (List JavaDoc)this.cachedEntries.get(id);
171       if(codeEntries == null) {
172           DataproviderObject_1_0 valueContainer = this.getCodeValueContainerById(id);
173           if(valueContainer == null) {
174             return null;
175           }
176           codeEntries = this.channel.addFindRequest(
177             valueContainer.path().getChild("entry"),
178             null,
179             AttributeSelectors.ALL_ATTRIBUTES,
180             0,
181             Integer.MAX_VALUE,
182             Directions.ASCENDING
183           );
184           this.cachedEntries.put(
185               id,
186               codeEntries
187           );
188       }
189       return codeEntries;
190   }
191   
192   //-------------------------------------------------------------------------
193
public List JavaDoc getCodeEntriesByName(
194       String JavaDoc name
195   ) throws ServiceException {
196       return this.getCodeEntriesById(
197           (String JavaDoc)this.codeValueContainerIds.get(name)
198       );
199   }
200   
201   //-------------------------------------------------------------------------
202
public SortedMap JavaDoc getShortText(
203     String JavaDoc name,
204     short locale,
205     boolean codeAsKey
206   ) throws ServiceException {
207     SortedMap JavaDoc shortTexts = null;
208     if((shortTexts = (SortedMap JavaDoc)this.shortTexts.get(name + ":" + locale + ":" + codeAsKey)) == null) {
209       List JavaDoc entries = this.getCodeEntriesByName(name);
210       shortTexts = new TreeMap JavaDoc();
211       for(Iterator JavaDoc i = entries.iterator(); i.hasNext(); ) {
212         DataproviderObject_1_0 entry = (DataproviderObject_1_0)i.next();
213         Short JavaDoc code = new Short JavaDoc((short)0);
214         try {
215           code = new Short JavaDoc(entry.path().getBase());
216         } catch(Exception JavaDoc e) {}
217         Object JavaDoc text = ((List JavaDoc)entry.values("shortText")).size() > locale
218           ? ((List JavaDoc)entry.values("shortText")).get(locale)
219           : ((List JavaDoc)entry.values("shortText")).get(0);
220         if(codeAsKey) {
221           shortTexts.put(code, text);
222         }
223         else {
224           shortTexts.put(text, code);
225         }
226       }
227       this.shortTexts.put(
228         name + ":" + locale + ":" + codeAsKey,
229         shortTexts
230       );
231     }
232     return shortTexts;
233   }
234   
235   //-------------------------------------------------------------------------
236
public SortedMap JavaDoc getLongText(
237     String JavaDoc name,
238     short locale,
239     boolean codeAsKey
240   ) throws ServiceException {
241     SortedMap JavaDoc longTexts = null;
242     if((longTexts = (SortedMap JavaDoc)this.longTexts.get(name + ":" + locale + ":" + codeAsKey)) == null) {
243       List JavaDoc entries = this.getCodeEntriesByName(name);
244       longTexts = new TreeMap JavaDoc();
245       for(Iterator JavaDoc i = entries.iterator(); i.hasNext(); ) {
246         DataproviderObject_1_0 entry = (DataproviderObject_1_0)i.next();
247         Short JavaDoc code = new Short JavaDoc((short)0);
248         try {
249           code = new Short JavaDoc(entry.path().getBase());
250         } catch(Exception JavaDoc e) {}
251         Object JavaDoc text =
252           ((List JavaDoc)entry.values("longText")).size() > locale
253             ? ((List JavaDoc)entry.values("longText")).get(locale)
254             : ((List JavaDoc)entry.values("longText")).get(0);
255         if(codeAsKey) {
256           longTexts.put(code, text);
257         }
258         else {
259           longTexts.put(text, code);
260         }
261       }
262       this.longTexts.put(
263         name + ":" + locale + ":" + codeAsKey,
264         longTexts
265       );
266     }
267     return longTexts;
268   }
269   
270   //-------------------------------------------------------------------------
271
// Variables
272
//-------------------------------------------------------------------------
273
/**
274     * Comment for <code>serialVersionUID</code>
275     */

276   private static final long serialVersionUID = 3256719593744381497L;
277
278   private final RequestCollection channel;
279   
280   private final Path codeSegmentIdentity;
281   
282   // map containing the code value containers key=id, value=CodeValueContainer
283
private final Map JavaDoc codeValueContainers;
284   
285   // map containing the attribute name -> code value container id mapping
286
private final Map JavaDoc codeValueContainerIds;
287   
288   private final Map JavaDoc shortTexts;
289   private final Map JavaDoc longTexts;
290   private final Map JavaDoc cachedEntries;
291   
292   // Refresh cache
293
private boolean cacheIsDirty = true;
294   private long cachedEntriesExpirationAt = 0;
295   private long codeCacheRefreshRate = 60000;
296   
297 }
298
299 //--- End of File -----------------------------------------------------------
300
Popular Tags