KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > repository > SimpleRuntimeElement


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jun 15, 2005
14  * @author Marc Batchelor
15  *
16  */

17
18 package org.pentaho.core.repository;
19
20 import java.math.BigDecimal JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.pentaho.messages.Messages;
30 import org.pentaho.core.repository.IRuntimeElement;
31 import org.pentaho.core.repository.RepositoryException;
32 import org.pentaho.core.session.IPentahoSession;
33 import org.pentaho.core.system.PentahoBase;
34 import org.pentaho.core.util.XmlHelper;
35
36 public class SimpleRuntimeElement extends PentahoBase implements IRuntimeElement {
37     public static final int ClassVersionNumber = 2;
38
39     private static final long serialVersionUID = 5024690844237335928L;
40
41     private static Log logger = LogFactory.getLog(SimpleRuntimeElement.class);
42
43     private String JavaDoc instanceId;
44
45     private String JavaDoc parentId;
46
47     private String JavaDoc solutionId;
48
49     private String JavaDoc parentType;
50
51     private int revision;
52
53     private Map JavaDoc typesMap = new HashMap JavaDoc(); // The total list of properties and
54

55     // their types
56

57     private Map JavaDoc paramMapSS = new HashMap JavaDoc(); // ShortString Map ( VARCHAR(254) )
58

59     private Map JavaDoc paramMapLS = new HashMap JavaDoc(); // LongString Map ( CLOB )
60

61     private Map JavaDoc paramMapBD = new HashMap JavaDoc(); // BigDecimal Map
62

63     private Map JavaDoc paramMapDT = new HashMap JavaDoc(); // Date Map
64

65     private Map JavaDoc paramMapLong = new HashMap JavaDoc(); // Long Map
66

67     private Map JavaDoc paramMapCPLX = new HashMap JavaDoc(); // Complex Map (Serialized as a
68

69     // Blob)
70

71     private static final int MAXSSLENGH = 254;
72
73     private static final ThreadLocal JavaDoc allowableReadAttributeNames = new ThreadLocal JavaDoc();
74
75     private boolean loaded;
76
77     private boolean readOnly;
78
79     // TODO: Implement check on every set and get to make sure that the
80
// attribute is allowed to be read/written
81

82     /**
83      * Constructor for Hibernate
84      */

85     protected SimpleRuntimeElement() {
86
87     }
88
89     /**
90      * Constructor
91      *
92      * @param instId
93      * The Instance Id
94      */

95     public SimpleRuntimeElement(String JavaDoc instId) {
96         instanceId = instId;
97     }
98
99     /**
100      * Constructor
101      *
102      * @param instId
103      * The Instance Id
104      * @param parId
105      * The Parent Id
106      * @param parType
107      * The Parent Type
108      */

109     public SimpleRuntimeElement(String JavaDoc instId, String JavaDoc parId, String JavaDoc parType) {
110         instanceId = instId;
111         parentId = parId;
112         parentType = parType;
113     }
114
115     /**
116      * Constructor
117      *
118      * @param instId
119      * The Instance Id
120      * @param parId
121      * The Parent Id
122      * @param parType
123      * The Parent Type
124      * @param solnId
125      * The Solution Id
126      */

127     public SimpleRuntimeElement(String JavaDoc instId, String JavaDoc parId, String JavaDoc parType, String JavaDoc solnId) {
128         instanceId = instId;
129         parentId = parId;
130         parentType = parType;
131         solutionId = solnId;
132     }
133
134     public List JavaDoc getMessages() {
135         return null;
136     }
137
138     protected void setPentahoSession(IPentahoSession sess) {
139         genLogIdFromSession(sess);
140     }
141
142     /**
143      * @return Returns the parentId.
144      */

145     public String JavaDoc getParentId() {
146         return parentId;
147     }
148
149     /**
150      * @param parentId
151      * The parentId to set.
152      */

153     public void setParentId(String JavaDoc parentId) {
154         this.updateOk();
155         this.parentId = parentId;
156     }
157
158     /**
159      * @return Returns the parentType.
160      */

161     public String JavaDoc getParentType() {
162         return parentType;
163     }
164
165     /**
166      * @param parentType
167      * The parentType to set.
168      */

169     public void setParentType(String JavaDoc parentType) {
170         this.updateOk();
171         this.parentType = parentType;
172     }
173
174     /**
175      * @return Returns the instanceId.
176      */

177     public String JavaDoc getInstanceId() {
178         return instanceId;
179     }
180
181     /**
182      * @param instId
183      * The instanceId to set.
184      */

185     public void setInstanceId(String JavaDoc instId) {
186         this.updateOk();
187         this.instanceId = instId;
188     }
189
190     /**
191      * @return Returns the solutionId.
192      */

193     public String JavaDoc getSolutionId() {
194         return solutionId;
195     }
196
197     /**
198      * @param solutionId
199      * The solutionId to set.
200      */

201     public void setSolutionId(String JavaDoc solutionId) {
202         this.updateOk();
203         this.solutionId = solutionId;
204     }
205
206     /**
207      * Auto-handled revision mechanism.
208      *
209      * @return The current revision
210      */

211     public int getRevision() {
212         return revision;
213     }
214
215     /**
216      * Sets the revision of the class
217      *
218      * @param rev
219      * New revision to set.
220      */

221     protected void setRevision(int rev) {
222         revision = rev;
223     }
224
225     /**
226      * Uses the instanceId to distinguish equality. The instanceId will never be
227      * null, won't change, and is the primary key. Therefore, it's the perfect
228      * candidate for equals() and hashcode.
229      */

230     public boolean equals(Object JavaDoc other) {
231         if (this == other) {
232             return true;
233         }
234         if (!(other instanceof IRuntimeElement)) {
235             return false;
236         }
237         final IRuntimeElement otherRE = (IRuntimeElement) other;
238         return this.getInstanceId().equals(otherRE.getInstanceId());
239     }
240
241     public int hashCode() {
242         return this.getInstanceId().hashCode();
243     }
244
245     protected Map JavaDoc getParamMapSS() {
246         return paramMapSS;
247     }
248
249     protected Map JavaDoc getParamMapLS() {
250         return paramMapLS;
251     }
252
253     protected Map JavaDoc getParamMapDT() {
254         return paramMapDT;
255     }
256
257     protected Map JavaDoc getParamMapBD() {
258         return paramMapBD;
259     }
260
261     protected Map JavaDoc getParamMapLong() {
262         return paramMapLong;
263     }
264
265     protected Map JavaDoc getParamMapCPLX() {
266         return paramMapCPLX;
267     }
268
269     protected void setParamMapSS(Map JavaDoc ss) {
270         paramMapSS = ss;
271     }
272
273     protected void setParamMapLS(Map JavaDoc ls) {
274         paramMapLS = ls;
275     }
276
277     protected void setParamMapDT(Map JavaDoc dt) {
278         paramMapDT = dt;
279     }
280
281     protected void setParamMapBD(Map JavaDoc bd) {
282         paramMapBD = bd;
283     }
284
285     protected void setParamMapLong(Map JavaDoc lng) {
286         paramMapLong = lng;
287     }
288
289     protected void setParamMapCPLX(Map JavaDoc cplx) {
290         paramMapCPLX = cplx;
291     }
292
293     /**
294      * Gets a property from the paramMap as a string with no default value.
295      *
296      * @param key
297      * The key into the map.
298      * @return The property.
299      */

300     public String JavaDoc getStringProperty(String JavaDoc key) {
301         return getStringProperty(key, null);
302     }
303
304     /**
305      * Gets a property from the paramMap as a string, using a default value if
306      * it doesn't exist in the map.
307      *
308      * @param key
309      * The key into the map.
310      * @param defaultValue
311      * Default value returned if the key isn't already in the map.
312      * @return The property.
313      */

314     public String JavaDoc getStringProperty(String JavaDoc key, String JavaDoc defaultValue) {
315         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getString", key)); //$NON-NLS-1$ //$NON-NLS-2$
316
Object JavaDoc prop = getParamMapSS().get(key);
317         if (prop == null) {
318             prop = getParamMapLS().get(key);
319         }
320         return (prop != null) ? prop.toString() : defaultValue;
321     }
322
323     protected void checkType(String JavaDoc key, String JavaDoc type, boolean setIt) {
324         Map JavaDoc localTypesMap = getTypesMap();
325         String JavaDoc curType = (String JavaDoc) localTypesMap.get(key);
326         if (curType != null) {
327             if (!curType.equals(type)) {
328                 throw new RepositoryException(Messages.getErrorString("RTREPO.ERROR_0001_INVALIDTYPE", curType, type)); //$NON-NLS-1$
329
}
330         }
331         if (setIt) {
332             localTypesMap.put(key, type);
333         }
334     }
335
336     /**
337      * Sets a property into the paramMap. Special implementation note - Null
338      * values aren't supported in the Map. So, if a null value is passed in,
339      * this implementation will remove the entry from the map.
340      *
341      * @param key
342      * The key into the map.
343      * @param value
344      * The value to set.
345      */

346     public void setStringProperty(String JavaDoc key, String JavaDoc value) {
347         this.updateOk();
348         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setString", key)); //$NON-NLS-1$ //$NON-NLS-2$
349
checkType(key, value.getClass().getName(), true);
350         Map JavaDoc theMapSS = getParamMapSS();
351         Map JavaDoc theMapLS = getParamMapLS();
352         if (value != null) {
353             if (value.length() > MAXSSLENGH) {
354                 theMapSS.remove(key); // Make sure it's not in the short map
355
// first.
356
theMapLS.put(key, new StringBuffer JavaDoc(value));
357             } else {
358                 theMapLS.remove(key);
359                 theMapSS.put(key, value);
360             }
361         } else {
362             theMapSS.remove(key);
363             theMapLS.remove(key);
364         }
365     }
366
367     /**
368      * Gets a BigDecimal property from the paramMap.
369      *
370      * @param key
371      * Key in the paramMap.
372      * @return BigDecimal property
373      */

374     public BigDecimal JavaDoc getBigDecimalProperty(String JavaDoc key) {
375         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getBigDecimal", key)); //$NON-NLS-1$ //$NON-NLS-2$
376
return getBigDecimalProperty(key, null);
377     }
378
379     /**
380      * Gets a property from the paramMap as a BigDecimal, using a default value
381      * if it doesn't exist in the map.
382      *
383      * @param key
384      * Key in the paramMap.
385      * @param defaultValue
386      * Detault value if the property doesn't exist in the paramMap.
387      * @return Returns the property from the paramMap.
388      */

389     public BigDecimal JavaDoc getBigDecimalProperty(String JavaDoc key, BigDecimal JavaDoc defaultValue) {
390         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getBigDecimal", key)); //$NON-NLS-1$ //$NON-NLS-2$
391
Object JavaDoc prop = getParamMapBD().get(key);
392         return (prop != null) ? new BigDecimal JavaDoc((String JavaDoc) prop) : defaultValue;
393     }
394
395     /**
396      * Sets the BigDecimal property in the paramMap. Special implementation note -
397      * Null values aren't supported in the Map. So, if a null value is passed
398      * in, this implementation will remove the entry from the map.
399      *
400      * @param key
401      * Key in the paramMap.
402      * @param value
403      * The property value to set.
404      */

405     public void setBigDecimalProperty(String JavaDoc key, BigDecimal JavaDoc value) {
406         this.updateOk();
407         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setBigDecimal", key)); //$NON-NLS-1$ //$NON-NLS-2$
408
checkType(key, value.getClass().getName(), true);
409         Map JavaDoc theMap = getParamMapBD();
410         if (value != null) {
411             theMap.put(key, value.toString());
412         } else {
413             theMap.remove(key);
414         }
415     }
416
417     /**
418      * Gets a property from the paramMap as a Date, with no default value.
419      *
420      * @param key
421      * Key in the paramMap
422      * @return The property in the map.
423      */

424     public Date JavaDoc getDateProperty(String JavaDoc key) {
425         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getDate", key)); //$NON-NLS-1$ //$NON-NLS-2$
426
return getDateProperty(key, null);
427     }
428
429     /**
430      * Gets a property from the paramMap as a Date using a default value if it
431      * doesn't exist in the map
432      *
433      * @param key
434      * Key in the paramMap
435      * @param defaultValue
436      * The default value if the property doesn't exist in the
437      * paramMap.
438      * @return The property in the map.
439      */

440     public Date JavaDoc getDateProperty(String JavaDoc key, Date JavaDoc defaultValue) {
441         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getDate", key)); //$NON-NLS-1$ //$NON-NLS-2$
442
Object JavaDoc prop = getParamMapDT().get(key);
443         return (prop != null) ? (Date JavaDoc) prop : defaultValue;
444     }
445
446     /**
447      * Sets a date property in the paramMap. If null comes in, it removes the
448      * value from the map. Special implementation note - Null values aren't
449      * supported in the Map. So, if a null value is passed in, this
450      * implementation will remove the entry from the map.
451      *
452      * @param key
453      * Key in the paramMap
454      * @param value
455      * The property value to set.
456      */

457     public void setDateProperty(String JavaDoc key, Date JavaDoc value) {
458         this.updateOk();
459         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setDate", key)); //$NON-NLS-1$ //$NON-NLS-2$
460
checkType(key, value.getClass().getName(), true);
461         Map JavaDoc theMap = getParamMapDT();
462         if (value != null) {
463             theMap.put(key, value);
464         } else {
465             theMap.remove(key);
466         }
467     }
468
469     /**
470      * Gets a property from the paramMap as a Long using a default value if it
471      * doesn't exist in the map
472      *
473      * @param key
474      * Key in the paramMap
475      * @param defaultValue
476      * The default value if the property doesn't exist in the
477      * paramMap.
478      * @return The property in the map.
479      */

480     public Long JavaDoc getLongProperty(String JavaDoc key, Long JavaDoc defaultValue) {
481         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getLong", key)); //$NON-NLS-1$ //$NON-NLS-2$
482
Object JavaDoc prop = getParamMapLong().get(key);
483         return (prop != null) ? (Long JavaDoc) prop : defaultValue;
484     }
485
486     /**
487      * Gets a property from the paramMap as a long using a default value if it
488      * doesn't exist in the map
489      *
490      * @param key
491      * Key in the paramMap
492      * @param defaultValue
493      * The default value if the property doesn't exist in the
494      * paramMap.
495      * @return The property in the map.
496      */

497     public long getLongProperty(String JavaDoc key, long defaultValue) {
498         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getLong", key)); //$NON-NLS-1$ //$NON-NLS-2$
499
Object JavaDoc prop = getParamMapLong().get(key);
500         return (prop != null) ? ((Long JavaDoc) prop).longValue() : defaultValue;
501     }
502
503     /**
504      * Sets a long property in the paramMap. If null comes in, it removes the
505      * value from the map. Special implementation note - Null values aren't
506      * supported in the Map. So, if a null value is passed in, this
507      * implementation will remove the entry from the map.
508      *
509      * @param key
510      * Key in the paramMap
511      * @param value
512      * The property value to set.
513      */

514     public void setLongProperty(String JavaDoc key, Long JavaDoc value) {
515         this.updateOk();
516         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setLong", key)); //$NON-NLS-1$ //$NON-NLS-2$
517
checkType(key, value.getClass().getName(), true);
518         Map JavaDoc theMap = getParamMapLong();
519         if (value != null) {
520             theMap.put(key, value);
521         } else {
522             theMap.remove(key);
523         }
524     }
525
526     /**
527      * Sets a long property in the paramMap.
528      *
529      * @param key
530      * Key in the paramMap
531      * @param value
532      * The property value to set.
533      */

534     public void setLongProperty(String JavaDoc key, long value) {
535         this.updateOk();
536         setLongProperty(key, new Long JavaDoc(value));
537     }
538
539     /**
540      * Gets a list property from the paramMap.
541      *
542      * @param key
543      * Key in the map
544      * @return The list property in the paramMap.
545      */

546     public List JavaDoc getListProperty(String JavaDoc key) {
547         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getList", key)); //$NON-NLS-1$ //$NON-NLS-2$
548
Object JavaDoc prop = getParamMapCPLX().get(key);
549         return (List JavaDoc) prop;
550     }
551
552     /**
553      * Gets a map property from the paramMap.
554      *
555      * @param key
556      * The key in the map
557      * @return The map value in the paramMap.
558      */

559     public Map JavaDoc getMapProperty(String JavaDoc key) {
560         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "getMap", key)); //$NON-NLS-1$ //$NON-NLS-2$
561
Object JavaDoc prop = getParamMapCPLX().get(key);
562         return (Map JavaDoc) prop;
563     }
564
565     /**
566      * Sets a list property in the paramMap. Special implementation note - Null
567      * values aren't supported in the Map. So, if a null value is passed in,
568      * this implementation will remove the entry from the map.
569      *
570      * @param key
571      * The key in the map.
572      * @param value
573      * The list property to set.
574      */

575     public void setListProperty(String JavaDoc key, List JavaDoc value) {
576         this.updateOk();
577         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setList", key)); //$NON-NLS-1$ //$NON-NLS-2$
578
checkType(key, value.getClass().getName(), true);
579         Map JavaDoc theMap = getParamMapCPLX();
580         if (value != null) {
581             theMap.put(key, value);
582         } else {
583             theMap.remove(key);
584         }
585     }
586
587     /**
588      * Sets a map property in the paramMap. Special implementation note - Null
589      * values aren't supported in the Map. So, if a null value is passed in,
590      * this implementation will remove the entry from the map.
591      *
592      * @param key
593      * The key in the map.
594      * @param value
595      * The map property to set.
596      */

597     public void setMapProperty(String JavaDoc key, Map JavaDoc value) {
598         this.updateOk();
599         trace(Messages.getString("RTREPO.DEBUG_PROPERTY_GETSET", "setMap", key)); //$NON-NLS-1$ //$NON-NLS-2$
600
checkType(key, value.getClass().getName(), true);
601         Map JavaDoc theMap = getParamMapCPLX();
602         if (value != null) {
603             theMap.put(key, value);
604         } else {
605             theMap.remove(key);
606         }
607     }
608
609     /**
610      * Returns an XML representation of the RuntimeElement. Mainly for
611      * Debug/Test Cases to make sure that what goes in is what comes out during
612      * tests.
613      *
614      * @return Returns an XML representation of the RuntimeElement
615      */

616     public String JavaDoc toXML() {
617         StringBuffer JavaDoc rtn = new StringBuffer JavaDoc();
618         rtn.append("<runtime-element>\r"); //$NON-NLS-1$
619
rtn.append(getXMLString(getInstanceId(), "instance-id", " ")); //$NON-NLS-1$ //$NON-NLS-2$
620
rtn.append(getXMLString(Integer.toString(getRevision()), "revision", " ")); //$NON-NLS-1$ //$NON-NLS-2$
621
rtn.append(getXMLString(getParentId(), "parent-id", " ")); //$NON-NLS-1$ //$NON-NLS-2$
622
rtn.append(getXMLString(getParentType(), "parent-type", " ")); //$NON-NLS-1$ //$NON-NLS-2$
623
rtn.append(getXMLString(getSolutionId(), "solution-id", " ")); //$NON-NLS-1$ //$NON-NLS-2$
624
rtn.append(getMapXML(this.getParamMapSS(), "small-string-map")); //$NON-NLS-1$
625
rtn.append(getMapXML(this.getParamMapLS(), "large-string-map")); //$NON-NLS-1$
626
rtn.append(getMapXML(this.getParamMapDT(), "date-map")); //$NON-NLS-1$
627
rtn.append(getMapXML(this.getParamMapBD(), "big-decimal-map")); //$NON-NLS-1$
628
rtn.append(getMapXML(this.getParamMapLong(), "long-map")); //$NON-NLS-1$
629
rtn.append(getMapXML(this.getParamMapCPLX(), "complex-map")); //$NON-NLS-1$
630
rtn.append("</runtime-element>\r"); //$NON-NLS-1$
631
return rtn.toString();
632     }
633
634     private String JavaDoc getXMLString(String JavaDoc str, String JavaDoc tag, String JavaDoc indent) {
635         return indent + "<" + tag + "><![CDATA[" + str + "]]></" + tag + ">\r"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
636
}
637
638     private String JavaDoc getMapXML(Map JavaDoc theMap, String JavaDoc tag) {
639         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
640         sb.append(" <").append(tag).append(">\r"); //$NON-NLS-1$ //$NON-NLS-2$
641
sb.append(XmlHelper.mapToXML(theMap, " ")); //$NON-NLS-1$
642
sb.append(" </").append(tag).append(">\r"); //$NON-NLS-1$ //$NON-NLS-2$
643
return sb.toString();
644     }
645
646     /* ILogger Needs */
647     public Log getLogger() {
648         return logger;
649     }
650
651     public void setAllowableAttributeNames(Collection JavaDoc allowedReadNames) {
652         allowableReadAttributeNames.set(allowedReadNames);
653     }
654
655     /**
656      * @return Returns the typesMap.
657      */

658     protected Map JavaDoc getTypesMap() {
659         return typesMap;
660     }
661
662     /**
663      * @param typesMap
664      * The typesMap to set.
665      */

666     protected void setTypesMap(Map JavaDoc typesMap) {
667         this.typesMap = typesMap;
668     }
669
670     public Set JavaDoc getParameterNames() {
671         return getTypesMap().keySet();
672     }
673
674     public String JavaDoc getParameterType(String JavaDoc parameterName) {
675         return (String JavaDoc) getTypesMap().get(parameterName);
676     }
677
678     public void setLoaded(boolean value) {
679         this.loaded = value;
680     }
681
682     public boolean getLoaded() {
683         return this.loaded;
684     }
685
686     private void updateOk() {
687         if (!loaded) {
688             return;
689         }
690         if (readOnly) {
691             throw new IllegalStateException JavaDoc(Messages.getErrorString("RTELEMENT.ERROR_0001_INVALIDUPDATE")); //$NON-NLS-1$
692
}
693     }
694
695     public boolean getReadOnly() {
696         return readOnly;
697     }
698
699     public void setReadOnly(boolean value) {
700         this.readOnly = value;
701     }
702
703     public void forceSave() {
704     }
705 }
706
Popular Tags