001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package mocks;
018:
019: import java.io.StringReader;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import mocks.elements.UniversityOrganizationElement;
026:
027: import org.jdom.Element;
028: import org.jdom.output.XMLOutputter;
029:
030: import edu.iu.uis.eden.WorkflowServiceErrorImpl;
031: import edu.iu.uis.eden.docsearch.SearchableAttributeStringValue;
032: import edu.iu.uis.eden.docsearch.SearchableAttributeValue;
033: import edu.iu.uis.eden.lookupable.Field;
034: import edu.iu.uis.eden.lookupable.Row;
035: import edu.iu.uis.eden.plugin.attributes.WorkflowAttribute;
036: import edu.iu.uis.eden.routeheader.DocumentContent;
037: import edu.iu.uis.eden.routetemplate.RuleExtension;
038: import edu.iu.uis.eden.routetemplate.RuleExtensionValue;
039: import edu.iu.uis.eden.util.XmlHelper;
040:
041: /**
042: * A Mock attribute which mimics the concept of a Chart/Org hierarchy as it exists at Indiana University.
043: *
044: * @author ewestfal
045: */
046: public class MockChartOrgAttribute implements WorkflowAttribute {
047:
048: private MockChartOrgService orgService = new MockChartOrgService();
049: private List<Row> rows;
050: private static final String FIN_COA_CD_KEY = "fin_coa_cd";
051: private static final String CHART_ORG_FIN_COA_CD_KEY = "chart_org_fin_coa_cd";
052:
053: private static final String ORG_CD_KEY = "org_cd";
054: private static final String CHART_ORG_ORG_CD_KEY = "chart_org_org_cd";
055:
056: private String finCoaCd;
057: private String orgCd;
058: private boolean required;
059:
060: public MockChartOrgAttribute(String finCoaCd, String orgCd) {
061: this ();
062: this .finCoaCd = finCoaCd;
063: this .orgCd = orgCd;
064: }
065:
066: public MockChartOrgAttribute() {
067: rows = new ArrayList<Row>();
068:
069: List<Field> fields = new ArrayList<Field>();
070: fields.add(new Field("Chart", "", Field.TEXT, true,
071: CHART_ORG_FIN_COA_CD_KEY, "", null,
072: "ChartOrgLookupableImplService", FIN_COA_CD_KEY));
073: rows.add(new Row(fields, "Chart & Org", 2));
074:
075: fields = new ArrayList<Field>();
076: fields.add(new Field("Org", "", Field.TEXT, true,
077: CHART_ORG_ORG_CD_KEY, "", null,
078: "ChartOrgLookupableImplService", ORG_CD_KEY));
079: fields.add(new Field("", "", Field.QUICKFINDER, false, "", "",
080: null, "ChartOrgLookupableImplService"));
081: rows.add(new Row(fields, "Chart & Org", 2));
082: }
083:
084: public List getRuleExtensionValues() {
085: List extensions = new ArrayList();
086:
087: if (finCoaCd != null && !finCoaCd.equals("")) {
088: RuleExtensionValue extensionFinCoaCd = new RuleExtensionValue();
089: extensionFinCoaCd.setKey(FIN_COA_CD_KEY);
090: extensionFinCoaCd.setValue(this .finCoaCd);
091: extensions.add(extensionFinCoaCd);
092: }
093: if (orgCd != null && !orgCd.equals("")) {
094: RuleExtensionValue extensionOrgCd = new RuleExtensionValue();
095: extensionOrgCd.setKey(ORG_CD_KEY);
096: extensionOrgCd.setValue(this .orgCd);
097: extensions.add(extensionOrgCd);
098: }
099: return extensions;
100: }
101:
102: public List validateRoutingData(Map paramMap) {
103: List errors = new ArrayList();
104: this .finCoaCd = (String) paramMap.get(CHART_ORG_FIN_COA_CD_KEY);
105: this .orgCd = (String) paramMap.get(CHART_ORG_ORG_CD_KEY);
106: if (isRequired()
107: && (this .finCoaCd == null || "".equals(finCoaCd) || (this .orgCd == null || ""
108: .equals(orgCd)))) {
109: errors
110: .add(new WorkflowServiceErrorImpl(
111: "Chart/org is required.",
112: "routetemplate.chartorgattribute.chartorg.required"));
113: } else if ((this .finCoaCd != null && !"".equals(finCoaCd) && ((this .orgCd == null || ""
114: .equals(orgCd))))
115: || ((this .finCoaCd == null || "".equals(finCoaCd))
116: && this .orgCd != null && !"".equals(orgCd))) {
117: errors
118: .add(new WorkflowServiceErrorImpl(
119: "Chart/org is invalid.",
120: "routetemplate.chartorgattribute.chartorg.invalid"));
121: }
122:
123: if (this .finCoaCd != null && !"".equals(finCoaCd)
124: && this .orgCd != null && !"".equals(orgCd)) {
125: MockOrganization org = getOrgService().findOrganization(
126: finCoaCd, orgCd);
127: if (org == null) {
128: errors
129: .add(new WorkflowServiceErrorImpl(
130: "Chart/org is invalid.",
131: "routetemplate.chartorgattribute.chartorg.invalid"));
132: }
133: }
134: return errors;
135: }
136:
137: public List validateRuleData(Map paramMap) {
138: return validateRoutingData(paramMap);
139: }
140:
141: public String getDocContent() {
142: if (getFinCoaCd() != null && !getFinCoaCd().equals("")
143: && getOrgCd() != null && !getOrgCd().equals("")) {
144: UniversityOrganizationElement univOrgElement = new UniversityOrganizationElement();
145: univOrgElement.setChart(getFinCoaCd());
146: univOrgElement.setOrgCode(getOrgCd());
147: return new XMLOutputter().outputString(univOrgElement
148: .getXMLContent());
149: } else {
150: throw new RuntimeException(
151: "Missing chart or org properties on attribute when getting document content");
152: }
153: }
154:
155: public List parseDocContent(String docContent) {
156: List chartOrgModels = new ArrayList();
157: UniversityOrganizationElement univOrgElement = new UniversityOrganizationElement();
158: Element rootElement = null;
159: try {
160: rootElement = XmlHelper.buildJDocument(
161: new StringReader(docContent)).getRootElement();
162: } catch (Exception e) {
163: throw new RuntimeException("Invalid XML submitted", e);
164: }
165: List chartOrgElements = XmlHelper.findElements(rootElement,
166: univOrgElement.getElementName());
167: for (Iterator iter = chartOrgElements.iterator(); iter
168: .hasNext();) {
169: Element chartOrgElement = (Element) iter.next();
170: try {
171: univOrgElement.loadFromXMLContent(chartOrgElement,
172: false);
173: } catch (Exception e) {
174: throw new RuntimeException(
175: "Problems loading chart org element from string "
176: + docContent, e);
177: }
178: chartOrgModels.add(new MockChartOrgAttribute(univOrgElement
179: .getChart(), univOrgElement.getOrgCode()));
180: }
181: return chartOrgModels;
182: }
183:
184: public boolean isMatch(DocumentContent docContent,
185: List ruleExtensions) {
186: boolean foundChartOrgExtension = false;
187: for (Iterator iter = ruleExtensions.iterator(); iter.hasNext();) {
188: RuleExtension extension = (RuleExtension) iter.next();
189: String className = getClass().getName();
190: if (extension.getRuleTemplateAttribute().getRuleAttribute()
191: .getClassName().equals(className)) {
192: for (Iterator iterator = extension.getExtensionValues()
193: .iterator(); iterator.hasNext();) {
194: RuleExtensionValue value = (RuleExtensionValue) iterator
195: .next();
196: if (value.getKey().equals(FIN_COA_CD_KEY)) {
197: foundChartOrgExtension = true;
198: setFinCoaCd(value.getValue());
199: }
200: if (value.getKey().equals(ORG_CD_KEY)) {
201: foundChartOrgExtension = true;
202: setOrgCd(value.getValue());
203: }
204: }
205: }
206: }
207:
208: List chartOrgValues = null;//populateFromDocContent(docContent);
209: for (Iterator iter = chartOrgValues.iterator(); iter.hasNext();) {
210: MockChartOrgAttribute attribute = (MockChartOrgAttribute) iter
211: .next();
212: if (attribute.getFinCoaCd().equals(this .getFinCoaCd())
213: && attribute.getOrgCd().equals(this .getOrgCd())) {
214: return true;
215: }
216: }
217:
218: if (ruleExtensions.isEmpty() || !foundChartOrgExtension) {
219: return true;
220: }
221: return false;
222: }
223:
224: // private void buildOrgReviewHierarchy(List chartOrgList, MockChartOrgAttribute chartOrg, Set visitedOrgs) {
225: //
226: // Organization org = IUServiceLocator.getFISDataService().findOrganization(chartOrg.getFinCoaCd(), chartOrg.getOrgCd());
227: //
228: // if (! org.hasParent()) {
229: // return;
230: // }
231: // String orgVisitedKey = org.getFinCoaCd() + "-" + org.getOrgCd();
232: // if (visitedOrgs.contains(orgVisitedKey)) {
233: // throw new RuntimeException("Org hierarchy loop detected. org " + orgVisitedKey);
234: // } else {
235: // visitedOrgs.add(orgVisitedKey);
236: // }
237: // MockChartOrgAttribute parent = new MockChartOrgAttribute();
238: // parent.setFinCoaCd(org.getReportsToChart());
239: // parent.setOrgCd(org.getReportsToOrg());
240: // //TODO this list could probably be whacked and the ojb mappings used instead...
241: // chartOrgList.add(parent);
242: // buildOrgReviewHierarchy(chartOrgList, parent, visitedOrgs);
243: // }
244:
245: // private List populateFromDocContent(DocumentContent docContent) {
246: // List chartOrgValues = new ArrayList();
247: // UniversityOrganizationElement univOrgElement = new UniversityOrganizationElement();
248: // Element rootElement = null;
249: // try {
250: // rootElement = XmlHelper.buildJDocument(docContent.getDocument()).getRootElement();
251: // } catch (Exception e) {
252: // throw new WorkflowServiceErrorException("Invalid XML submitted", new ArrayList());
253: // }
254: // List chartOrgElements = XmlHelper.findElements(rootElement, univOrgElement.getElementName());
255: // for (Iterator iter = chartOrgElements.iterator(); iter.hasNext();) {
256: // Element chartOrgElement = (Element) iter.next();
257: // try {
258: // univOrgElement.loadFromXMLContent(chartOrgElement, false);
259: // } catch (Exception e) {
260: // throw new WorkflowServiceErrorException("Problems loading chart org element from string " + docContent, new ArrayList());
261: // }
262: // MockChartOrgAttribute chartOrg = new MockChartOrgAttribute();
263: // chartOrg.setFinCoaCd(univOrgElement.getChart());
264: // chartOrg.setOrgCd(univOrgElement.getOrgCode());
265: // chartOrgValues.add(chartOrg);
266: // buildOrgReviewHierarchy(chartOrgValues, chartOrg, new HashSet());
267: // }
268: // return chartOrgValues;
269: // }
270:
271: public List getRuleRows() {
272: return rows;
273: }
274:
275: public List getRoutingDataRows() {
276: return rows;
277: }
278:
279: public String getFinCoaCd() {
280: return this .finCoaCd;
281: }
282:
283: public void setFinCoaCd(String finCoaCd) {
284: this .finCoaCd = finCoaCd;
285: }
286:
287: public String getOrgCd() {
288: return this .orgCd;
289: }
290:
291: public void setOrgCd(String orgCd) {
292: this .orgCd = orgCd;
293: }
294:
295: public boolean isRequired() {
296: return required;
297: }
298:
299: public void setRequired(boolean required) {
300: this .required = required;
301: }
302:
303: /* (non-Javadoc)
304: * @see edu.iu.uis.eden.docsearch.SearchableAttribute#getSearchContent()
305: */
306: public String getSearchContent() {
307: return getDocContent();
308: }
309:
310: /* (non-Javadoc)
311: * @see edu.iu.uis.eden.docsearch.SearchableAttribute#getSearchStorageValues()
312: */
313: public List getSearchStorageValues(String docContent) {
314: List searchStorageValues = new ArrayList();
315: //TODO the variables finCoaCd and orgCd may not be populated. Values may only be in the incoming doc content.
316: if (finCoaCd != null && !finCoaCd.equals("")) {
317: SearchableAttributeValue searchableFinCoaCd = new SearchableAttributeStringValue();
318: searchableFinCoaCd
319: .setSearchableAttributeKey(FIN_COA_CD_KEY);
320: searchableFinCoaCd.setupAttributeValue(this .finCoaCd);
321: searchStorageValues.add(searchableFinCoaCd);
322: }
323: if (orgCd != null && !orgCd.equals("")) {
324: SearchableAttributeValue searchableOrgCd = new SearchableAttributeStringValue();
325: searchableOrgCd.setSearchableAttributeKey(ORG_CD_KEY);
326: searchableOrgCd.setupAttributeValue(this .orgCd);
327: searchStorageValues.add(searchableOrgCd);
328: }
329: return searchStorageValues;
330: }
331:
332: public List validateClientRoutingData() {
333: List errors = new ArrayList();
334: // if (isRequired() && (this.finCoaCd == null || "".equals(finCoaCd) || (this.orgCd == null || "".equals(orgCd)))) {
335: // errors.add(new WorkflowAttributeValidationError("invalid.org", "Organization is invalid"));
336: // } else if ((this.finCoaCd != null && !"".equals(finCoaCd) && ((this.orgCd == null || "".equals(orgCd)))) || ((this.finCoaCd == null || "".equals(finCoaCd)) && this.orgCd != null && !"".equals(orgCd))) {
337: // errors.add(new WorkflowAttributeValidationError("invalid.org", "Organization is invalid"));
338: // }
339: //
340: // if (this.finCoaCd != null && !"".equals(finCoaCd) && this.orgCd != null && !"".equals(orgCd)) {
341: // Organization org = null;//IUServiceLocator.getFISDataService().findOrganization(finCoaCd, orgCd);
342: // if (org == null) {
343: // errors.add(new WorkflowAttributeValidationError("invalid.org", "Organization is invalid"));
344: // }
345: // }
346: return errors;
347: }
348:
349: public List getSearchingRows() {
350: return rows;
351: }
352:
353: public List validateUserSearchInputs(Map paramMap) {
354: return validateRoutingData(paramMap);
355: }
356:
357: protected MockChartOrgService getOrgService() {
358: return orgService;
359: }
360: }
|