1 56 package org.opencrx.kernel.layer.application; 57 58 import java.io.Serializable ; 59 import java.util.HashMap ; 60 import java.util.Iterator ; 61 import java.util.List ; 62 import java.util.Map ; 63 import java.util.SortedMap ; 64 import java.util.TreeMap ; 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 { 76 77 public Codes( 79 RequestCollection channel, 80 Path codeSegmentIdentity 81 ) throws ServiceException { 82 this.channel = channel; 83 this.codeSegmentIdentity = codeSegmentIdentity; 84 this.codeValueContainers = new HashMap (); 85 this.codeValueContainerIds = new HashMap (); 86 this.cachedEntries = new HashMap (); 87 this.shortTexts = new HashMap (); 88 this.longTexts = new HashMap (); 89 this.refresh(); 90 } 91 92 public void markAsDirty( 94 ) { 95 this.cacheIsDirty = true; 96 } 97 98 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 if(this.cacheIsDirty && (codeSegment != null)) { 111 this.codeValueContainers.clear(); 112 this.codeValueContainerIds.clear(); 113 this.cachedEntries.clear(); 114 List 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 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 name = valueContainer.values("name"); 133 for(Iterator 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 public DataproviderObject_1_0 getCodeValueContainerByName( 148 String name 149 ) { 150 return (DataproviderObject_1_0)this.codeValueContainers.get( 151 this.codeValueContainerIds.get(name) 152 ); 153 } 154 155 public DataproviderObject_1_0 getCodeValueContainerById( 157 String id 158 ) { 159 return (DataproviderObject_1_0)this.codeValueContainers.get(id); 160 } 161 162 public List getCodeEntriesById( 164 String id 165 ) throws ServiceException { 166 if(System.currentTimeMillis() > this.cachedEntriesExpirationAt) { 167 this.refresh(); 168 this.cachedEntriesExpirationAt = System.currentTimeMillis() + this.codeCacheRefreshRate; 169 } 170 List codeEntries = (List )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 public List getCodeEntriesByName( 194 String name 195 ) throws ServiceException { 196 return this.getCodeEntriesById( 197 (String )this.codeValueContainerIds.get(name) 198 ); 199 } 200 201 public SortedMap getShortText( 203 String name, 204 short locale, 205 boolean codeAsKey 206 ) throws ServiceException { 207 SortedMap shortTexts = null; 208 if((shortTexts = (SortedMap )this.shortTexts.get(name + ":" + locale + ":" + codeAsKey)) == null) { 209 List entries = this.getCodeEntriesByName(name); 210 shortTexts = new TreeMap (); 211 for(Iterator i = entries.iterator(); i.hasNext(); ) { 212 DataproviderObject_1_0 entry = (DataproviderObject_1_0)i.next(); 213 Short code = new Short ((short)0); 214 try { 215 code = new Short (entry.path().getBase()); 216 } catch(Exception e) {} 217 Object text = ((List )entry.values("shortText")).size() > locale 218 ? ((List )entry.values("shortText")).get(locale) 219 : ((List )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 public SortedMap getLongText( 237 String name, 238 short locale, 239 boolean codeAsKey 240 ) throws ServiceException { 241 SortedMap longTexts = null; 242 if((longTexts = (SortedMap )this.longTexts.get(name + ":" + locale + ":" + codeAsKey)) == null) { 243 List entries = this.getCodeEntriesByName(name); 244 longTexts = new TreeMap (); 245 for(Iterator i = entries.iterator(); i.hasNext(); ) { 246 DataproviderObject_1_0 entry = (DataproviderObject_1_0)i.next(); 247 Short code = new Short ((short)0); 248 try { 249 code = new Short (entry.path().getBase()); 250 } catch(Exception e) {} 251 Object text = 252 ((List )entry.values("longText")).size() > locale 253 ? ((List )entry.values("longText")).get(locale) 254 : ((List )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 276 private static final long serialVersionUID = 3256719593744381497L; 277 278 private final RequestCollection channel; 279 280 private final Path codeSegmentIdentity; 281 282 private final Map codeValueContainers; 284 285 private final Map codeValueContainerIds; 287 288 private final Map shortTexts; 289 private final Map longTexts; 290 private final Map cachedEntries; 291 292 private boolean cacheIsDirty = true; 294 private long cachedEntriesExpirationAt = 0; 295 private long codeCacheRefreshRate = 60000; 296 297 } 298 299 | Popular Tags |