001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.workflow.model.attributes;
009:
010: //base classes
011: import java.io.Serializable;
012: import java.util.HashMap;
013: import java.util.Iterator;
014:
015: //project specific classes
016: import org.jfolder.common.UnexpectedSystemException;
017: import org.jfolder.common.tagging.ConceptTagHelper;
018: import org.jfolder.common.tagging.ValueAndClassForConceptTag;
019:
020: //other classes
021:
022: public class AttributeSet {
023:
024: //TO DO: consider changing how below works
025: public final static String APP = "APP";
026: public final static String APP_VERSION = "APP_VERSION";
027: public final static String ID = "ID";
028: public final static String STATUS = "STATUS";
029: public final static String STATE = "STATE";
030: public final static String WAIT = "WAIT";
031: public final static String STEP_COUNT = "STEP_COUNT";
032: public final static String CURRENT_TRACE = "CURRENT_TRACE";
033:
034: private HashMap privateAppAttrs;
035: private HashMap privateSysAttrs;
036: private HashMap publicAppAttrs;
037: private HashMap publicSysAttrs;
038:
039: public AttributeSet() {
040: this .privateAppAttrs = new HashMap();
041: this .privateSysAttrs = new HashMap();
042: this .publicAppAttrs = new HashMap();
043: this .publicSysAttrs = new HashMap();
044: }
045:
046: public final static AttributeSet newInstance() {
047: return new AttributeSet();
048: }
049:
050: private void ensureSerializable(Class inClass) {
051: if ((!(inClass.isPrimitive()))
052: && (!(Serializable.class.isAssignableFrom(inClass)))) {
053: throw new UnexpectedSystemException(
054: "Attributes must be serializable");
055: }
056: }
057:
058: public void addPublicSysAttr(String inName,
059: ValueAndClassForConceptTag inVac) {
060: if (isSysAttrPresent(inName.toUpperCase())) {
061: throw new UnexpectedSystemException("System attribute '"
062: + inName + "' cannot be declared more than once");
063: }
064: ensureSerializable(inVac.getValueClass());
065: publicSysAttrs.put(inName.toUpperCase(), inVac);
066: }
067:
068: public void addPrivateSysAttr(String inName,
069: ValueAndClassForConceptTag inVac) {
070: if (isSysAttrPresent(inName.toUpperCase())) {
071: throw new UnexpectedSystemException("System attribute '"
072: + inName + "' cannot be declared more than once");
073: }
074: ensureSerializable(inVac.getValueClass());
075: privateSysAttrs.put(inName.toUpperCase(), inVac);
076: }
077:
078: public void addPublicAppAttr(String inName,
079: ValueAndClassForConceptTag inVac) {
080: if (isAppAttrPresent(inName.toUpperCase())) {
081: throw new UnexpectedSystemException(
082: "Application attribute '" + inName
083: + "' cannot be declared more than once");
084: }
085:
086: ensureSerializable(inVac.getValueClass());
087: publicAppAttrs.put(inName.toUpperCase(), inVac);
088:
089: }
090:
091: public void addPrivateAppAttr(String inName,
092: ValueAndClassForConceptTag inVac) {
093: if (isAppAttrPresent(inName.toUpperCase())) {
094: throw new UnexpectedSystemException(
095: "Application attribute '" + inName
096: + "' cannot be declared more than once");
097: }
098: ensureSerializable(inVac.getValueClass());
099: privateAppAttrs.put(inName.toUpperCase(), inVac);
100: }
101:
102: public ValueAndClassForConceptTag getPrivateSysAttr(String inName) {
103:
104: ValueAndClassForConceptTag outValue = null;
105:
106: if (!isPrivateSysAttrPresent(inName.toUpperCase())) {
107: throw new UnexpectedSystemException(
108: "Private system attribute '" + inName
109: + "' does not exist");
110: }
111: Object o = privateSysAttrs.get(inName.toUpperCase());
112: outValue = (ValueAndClassForConceptTag) o;
113:
114: return outValue;
115: }
116:
117: public ValueAndClassForConceptTag getPublicSysAttr(String inName) {
118:
119: ValueAndClassForConceptTag outValue = null;
120:
121: if (!isPublicSysAttrPresent(inName.toUpperCase())) {
122: throw new UnexpectedSystemException(
123: "Public system attribute '" + inName
124: + "' does not exist");
125: }
126: Object o = publicSysAttrs.get(inName.toUpperCase());
127: outValue = (ValueAndClassForConceptTag) o;
128:
129: return outValue;
130: }
131:
132: public ValueAndClassForConceptTag getSysAttr(String inName) {
133:
134: ValueAndClassForConceptTag outValue = null;
135:
136: if (isPrivateSysAttrPresent(inName.toUpperCase())) {
137: outValue = getPrivateSysAttr(inName.toUpperCase());
138: } else if (isPublicSysAttrPresent(inName.toUpperCase())) {
139: outValue = getPublicSysAttr(inName.toUpperCase());
140: } else {
141: throw new UnexpectedSystemException("System attribute '"
142: + inName + "' does not exist");
143: }
144:
145: return outValue;
146: }
147:
148: public ValueAndClassForConceptTag getPrivateAppAttr(String inName) {
149:
150: ValueAndClassForConceptTag outValue = null;
151:
152: if (!isPrivateAppAttrPresent(inName.toUpperCase())) {
153: throw new UnexpectedSystemException(
154: "Private application attribute '" + inName
155: + "' does not exist");
156: }
157: Object o = privateAppAttrs.get(inName.toUpperCase());
158: outValue = (ValueAndClassForConceptTag) o;
159:
160: return outValue;
161: }
162:
163: public ValueAndClassForConceptTag getPublicAppAttr(String inName) {
164:
165: ValueAndClassForConceptTag outValue = null;
166:
167: if (!isPublicAppAttrPresent(inName.toUpperCase())) {
168: throw new UnexpectedSystemException(
169: "Public application attribute '" + inName
170: + "' does not exist");
171: }
172: Object o = publicAppAttrs.get(inName.toUpperCase());
173: outValue = (ValueAndClassForConceptTag) o;
174:
175: return outValue;
176: }
177:
178: public ValueAndClassForConceptTag getAppAttr(String inName) {
179:
180: ValueAndClassForConceptTag outValue = null;
181:
182: if (isPrivateAppAttrPresent(inName.toUpperCase())) {
183: outValue = getPrivateAppAttr(inName.toUpperCase());
184: } else if (isPublicAppAttrPresent(inName.toUpperCase())) {
185: outValue = getPublicAppAttr(inName.toUpperCase());
186: } else {
187: throw new UnexpectedSystemException(
188: "Application attribute '" + inName
189: + "' does not exist");
190: }
191:
192: return outValue;
193: }
194:
195: public void setPrivateSysAttr(String inName,
196: ValueAndClassForConceptTag inVac) {
197:
198: ValueAndClassForConceptTag origVac = getPrivateSysAttr(inName
199: .toUpperCase());
200: ValueAndClassForConceptTag newVac = ConceptTagHelper.castTo(
201: origVac.getValueClass(), inVac);
202: privateSysAttrs.put(inName.toUpperCase(), newVac);
203: }
204:
205: public void setPublicSysAttr(String inName,
206: ValueAndClassForConceptTag inVac) {
207:
208: ValueAndClassForConceptTag origVac = getPublicSysAttr(inName
209: .toUpperCase());
210: ValueAndClassForConceptTag newVac = ConceptTagHelper.castTo(
211: origVac.getValueClass(), inVac);
212: publicSysAttrs.put(inName.toUpperCase(), newVac);
213: }
214:
215: public void setSysAttr(String inName,
216: ValueAndClassForConceptTag inVac) {
217: if (isPrivateSysAttrPresent(inName.toUpperCase())) {
218: setPrivateSysAttr(inName.toUpperCase(), inVac);
219: } else if (isPublicSysAttrPresent(inName.toUpperCase())) {
220: setPublicSysAttr(inName.toUpperCase(), inVac);
221: } else {
222: throw new UnexpectedSystemException("System attribute '"
223: + inName + "' does not exist");
224: }
225: }
226:
227: public void setPrivateAppAttr(String inName,
228: ValueAndClassForConceptTag inVac) {
229:
230: ValueAndClassForConceptTag origVac = getPrivateAppAttr(inName
231: .toUpperCase());
232: ValueAndClassForConceptTag newVac = ConceptTagHelper.castTo(
233: origVac.getValueClass(), inVac);
234: privateAppAttrs.put(inName.toUpperCase(), newVac);
235: }
236:
237: public void setPublicAppAttr(String inName,
238: ValueAndClassForConceptTag inVac) {
239:
240: //java.util.Iterator iter = this.publicAppAttrs.keySet().iterator();
241: //while (iter.hasNext()) {
242: // MiscHelper.println("ATTRIBUTE_NAME = " + iter.next().toString());
243: //}
244:
245: ValueAndClassForConceptTag origVac = getPublicAppAttr(inName
246: .toUpperCase());
247: ValueAndClassForConceptTag newVac = ConceptTagHelper.castTo(
248: origVac.getValueClass(), inVac);
249: publicAppAttrs.put(inName.toUpperCase(), newVac);
250: }
251:
252: public void setAppAttr(String inName,
253: ValueAndClassForConceptTag inVac) {
254: if (isPrivateAppAttrPresent(inName.toUpperCase())) {
255: setPrivateAppAttr(inName.toUpperCase(), inVac);
256: } else if (isPublicAppAttrPresent(inName.toUpperCase())) {
257: setPublicAppAttr(inName.toUpperCase(), inVac);
258: } else {
259: throw new UnexpectedSystemException(
260: "Application attribute '" + inName
261: + "' does not exist");
262: }
263: }
264:
265: //TO DO: which of the below 3 should be present, maybe all 3
266: public boolean isPrivateSysAttrPresent(String inName) {
267: return privateSysAttrs.containsKey(inName.toUpperCase());
268: }
269:
270: public boolean isPublicSysAttrPresent(String inName) {
271: return publicSysAttrs.containsKey(inName.toUpperCase());
272: }
273:
274: public boolean isSysAttrPresent(String inName) {
275: return isPrivateSysAttrPresent(inName.toUpperCase())
276: || isPublicSysAttrPresent(inName.toUpperCase());
277: }
278:
279: public boolean isPrivateAppAttrPresent(String inName) {
280: return privateAppAttrs.containsKey(inName.toUpperCase());
281: }
282:
283: public boolean isPublicAppAttrPresent(String inName) {
284: return publicAppAttrs.containsKey(inName.toUpperCase());
285: }
286:
287: public boolean isAppAttrPresent(String inName) {
288: return isPrivateAppAttrPresent(inName.toUpperCase())
289: || isPublicAppAttrPresent(inName.toUpperCase());
290: }
291:
292: public Iterator getPublicSysAttrNames() {
293: return publicSysAttrs.keySet().iterator();
294: }
295:
296: public Iterator getPrivateSysAttrNames() {
297: return privateSysAttrs.keySet().iterator();
298: }
299:
300: public Iterator getPublicAppAttrNames() {
301: return publicAppAttrs.keySet().iterator();
302: }
303:
304: public Iterator getPrivateAppAttrNames() {
305: return privateAppAttrs.keySet().iterator();
306: }
307:
308: public int getPublicSysAttrCount() {
309: return publicSysAttrs.keySet().size();
310: }
311:
312: public int getPrivateSysAttrCount() {
313: return privateSysAttrs.keySet().size();
314: }
315:
316: public int getPublicAppAttrCount() {
317: return publicAppAttrs.keySet().size();
318: }
319:
320: public int getPrivateAppAttrCount() {
321: return privateAppAttrs.keySet().size();
322: }
323:
324: public void declarePublicSystemAttributes(HashMap inAttrDefs) {
325:
326: Iterator attrNames = inAttrDefs.keySet().iterator();
327:
328: while (attrNames.hasNext()) {
329: String nextAttrName = (String) attrNames.next();
330: Class nextAttrType = (Class) inAttrDefs.get(nextAttrName);
331:
332: addPublicSysAttr(nextAttrName.toUpperCase(),
333: ValueAndClassForConceptTag.newInstance(null,
334: nextAttrType));
335: }
336: }
337:
338: public void declarePrivateSystemAttributes(HashMap inAttrDefs) {
339:
340: Iterator attrNames = inAttrDefs.keySet().iterator();
341:
342: while (attrNames.hasNext()) {
343: String nextAttrName = (String) attrNames.next();
344: Class nextAttrType = (Class) inAttrDefs.get(nextAttrName);
345:
346: addPrivateSysAttr(nextAttrName.toUpperCase(),
347: ValueAndClassForConceptTag.newInstance(null,
348: nextAttrType));
349: }
350: }
351:
352: public void reset() {
353: privateAppAttrs = new HashMap();
354: privateSysAttrs = new HashMap();
355: publicAppAttrs = new HashMap();
356: publicSysAttrs = new HashMap();
357: }
358:
359: public void removeId() {
360: publicSysAttrs.remove(AttributeSet.ID.toUpperCase());
361: }
362:
363: }
|