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.dms;
009:
010: //base classes
011: import java.util.ArrayList;
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.security.audit.SecurityAudit;
018: import org.jfolder.workflow.model.WorkflowComponent;
019: import org.jfolder.workflow.model.WorkflowComponentApplyUpdatesContext;
020: import org.jfolder.workflow.model.WorkflowComponentIdentifyUpdatesContext;
021: import org.jfolder.workflow.model.WorkflowComponentUpdates;
022:
023: //other classes
024:
025: public class WorkflowDocumentGroup extends WorkflowComponent {
026:
027: private String name = null;
028: private boolean deletable = false;
029: //
030: private ArrayList documentInstanceArray = null;
031: private HashMap documentInstanceMap = null;
032: private HashMap originalDocumentInstanceMap = null;
033: private ArrayList annotations = null;
034:
035: protected WorkflowDocumentGroup(String inName) {
036: super ();
037: this .name = inName;
038: clearAll();
039: }
040:
041: protected WorkflowDocumentGroup(String inName, SecurityAudit inSa) {
042: super ();
043: this .name = inName;
044: clearAll();
045: }
046:
047: private void clearAll() {
048: this .deletable = false;
049: this .documentInstanceArray = new ArrayList();
050: this .documentInstanceMap = new HashMap();
051: this .originalDocumentInstanceMap = new HashMap();
052: this .annotations = new ArrayList();
053: }
054:
055: public final static WorkflowDocumentGroup newInstance(String inName) {
056:
057: WorkflowDocumentGroup outValue = null;
058:
059: outValue = new WorkflowDocumentGroup(inName);
060:
061: return outValue;
062: }
063:
064: public final static WorkflowDocumentGroup newInstance(
065: String inName, SecurityAudit inSa) {
066:
067: WorkflowDocumentGroup outValue = null;
068:
069: outValue = new WorkflowDocumentGroup(inName, inSa);
070:
071: return outValue;
072: }
073:
074: public String getName() {
075: return this .name;
076: }
077:
078: public boolean isDeletable() {
079: return this .deletable;
080: }
081:
082: public void setDeletable(boolean inDeletable) {
083: this .deletable = inDeletable;
084: }
085:
086: public int getAnnotationCount() {
087:
088: int outValue = 0;
089:
090: outValue = this .annotations.size();
091:
092: return outValue;
093: }
094:
095: public String getAnnotation(int inIndex) {
096:
097: String outValue = null;
098:
099: outValue = (String) this .annotations.get(inIndex);
100:
101: return outValue;
102: }
103:
104: public void addAnnotation(String inAnnotation) {
105: this .annotations.add(inAnnotation);
106: }
107:
108: public WorkflowDocumentInstance getDocument(int inIndex) {
109:
110: WorkflowDocumentInstance outValue = null;
111:
112: outValue = (WorkflowDocumentInstance) this .documentInstanceArray
113: .get(inIndex);
114:
115: return outValue;
116: }
117:
118: public WorkflowDocumentInstance getDocument(String inName) {
119:
120: WorkflowDocumentInstance outValue = null;
121:
122: outValue = (WorkflowDocumentInstance) this .documentInstanceMap
123: .get(inName);
124:
125: return outValue;
126: }
127:
128: public int getDocumentCount() {
129:
130: int outValue = 0;
131:
132: outValue = this .documentInstanceArray.size();
133:
134: return outValue;
135: }
136:
137: public void addDocument(String inName, byte inContent[]) {
138: if (!this .documentInstanceMap.containsKey(inName)) {
139: WorkflowDocumentInstance di = new WorkflowDocumentInstance(
140: inName, inContent);
141: this .documentInstanceMap.put(di.getName(), di);
142: this .documentInstanceArray.add(di);
143: } else {
144: throw new UnexpectedSystemException("Document instance '"
145: + inName
146: + "' exists, it cannot be added, only replaced");
147: }
148: }
149:
150: public void replaceDocument(String inName, byte inContent[]) {
151: if (this .documentInstanceMap.containsKey(inName)) {
152: WorkflowDocumentInstance di = new WorkflowDocumentInstance(
153: inName, inContent);
154: this .documentInstanceMap.put(di.getName(), di);
155: for (int i = 0; i < this .documentInstanceArray.size(); i++) {
156: WorkflowDocumentInstance nextDi = (WorkflowDocumentInstance) this .documentInstanceArray
157: .get(i);
158: if (nextDi.getName().equals(inName)) {
159: this .documentInstanceArray.set(i, di);
160: }
161: }
162: //this.documentInstanceArray.add(di);
163: } else {
164: throw new UnexpectedSystemException("Document instance '"
165: + inName + "' does not exist, add it first");
166: }
167: }
168:
169: public void removeDocument(String inName) {
170: if (this .documentInstanceMap.containsKey(inName)) {
171: this .documentInstanceMap.remove(inName);
172: for (int i = 0; i < this .documentInstanceArray.size(); i++) {
173: WorkflowDocumentInstance nextDi = (WorkflowDocumentInstance) this .documentInstanceArray
174: .get(i);
175: if (nextDi.getName().equals(inName)) {
176: this .documentInstanceArray.remove(nextDi);
177: break;
178: }
179: }
180: //this.documentInstanceArray.add(di);
181: } else {
182: throw new UnexpectedSystemException("Document instance '"
183: + inName + "' does not exist, it cannot be removed");
184: }
185: }
186:
187: public WorkflowComponentUpdates identifyUpdates(
188: WorkflowComponentIdentifyUpdatesContext inWciuc) {
189:
190: WorkflowDocumentGroupUpdates outValue = null;
191:
192: //determine updates
193: ArrayList commands = new ArrayList();
194: ArrayList instances = new ArrayList();
195:
196: HashMap removeMap = (HashMap) this .originalDocumentInstanceMap
197: .clone();
198:
199: Iterator addUpdateIter = this .documentInstanceMap.keySet()
200: .iterator();
201:
202: while (addUpdateIter.hasNext()) {
203: String nextDiName = (String) addUpdateIter.next();
204: Object nextDio = this .documentInstanceMap.get(nextDiName);
205: WorkflowDocumentInstance nextDi = (WorkflowDocumentInstance) nextDio;
206:
207: if (!removeMap.containsKey(nextDiName)) {
208: commands.add(WorkflowDocumentGroupUpdates.ADD);
209: instances.add(nextDi);
210: } else {
211: Object oldDio = removeMap.get(nextDiName);
212: WorkflowDocumentInstance oldDi = (WorkflowDocumentInstance) oldDio;
213: removeMap.remove(nextDiName);
214: if (!oldDi.isIdentical(nextDi)) {
215: commands.add(WorkflowDocumentGroupUpdates.UPDATE);
216: instances.add(nextDi);
217: } else {
218: //identical, no changes needed
219: }
220: }
221: }
222:
223: Iterator removeIter = removeMap.keySet().iterator();
224: while (removeIter.hasNext()) {
225: String nextDiName = (String) addUpdateIter.next();
226: Object nextDio = this .documentInstanceMap.get(nextDiName);
227: WorkflowDocumentInstance nextDi = (WorkflowDocumentInstance) nextDio;
228: //
229: commands.add(WorkflowDocumentGroupUpdates.REMOVE);
230: instances.add(nextDi);
231: }
232:
233: outValue = new WorkflowDocumentGroupUpdates(this .name,
234: commands, instances, this .annotations);
235:
236: //reset 'original' state
237: this .originalDocumentInstanceMap = (HashMap) this .documentInstanceMap
238: .clone();
239: this .annotations = new ArrayList();
240:
241: return outValue;
242: }
243:
244: //public WorkflowComponentUpdates identifyUpdates(
245: // WorkflowComponentIdentifyUpdatesContext inWciuc) {
246: // throw UnexpectedSystemException.notImplemented();
247: //}
248:
249: public void applyUpdates(
250: WorkflowComponentApplyUpdatesContext inWcauc) {
251: throw UnexpectedSystemException.notImplemented();
252: }
253: }
|