001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.workflow.attribute;
017:
018: import java.util.ArrayList;
019: import java.util.Collections;
020: import java.util.List;
021: import java.util.Map;
022:
023: import javax.xml.xpath.XPath;
024: import javax.xml.xpath.XPathExpressionException;
025:
026: import org.apache.commons.lang.StringUtils;
027: import org.apache.log4j.Logger;
028: import org.kuali.core.bo.user.PersonTaxId;
029: import org.kuali.core.bo.user.UniversalUser;
030: import org.kuali.core.exceptions.UserNotFoundException;
031: import org.kuali.core.service.UniversalUserService;
032: import org.kuali.core.util.FieldUtils;
033: import org.kuali.core.workflow.attribute.WorkflowLookupableImpl;
034: import org.kuali.kfs.context.SpringContext;
035: import org.kuali.module.financial.bo.Payee;
036: import org.kuali.workflow.KualiWorkflowUtils;
037:
038: import edu.iu.uis.eden.WorkflowServiceErrorImpl;
039: import edu.iu.uis.eden.doctype.DocumentType;
040: import edu.iu.uis.eden.lookupable.Field;
041: import edu.iu.uis.eden.lookupable.Row;
042: import edu.iu.uis.eden.plugin.attributes.WorkflowAttribute;
043: import edu.iu.uis.eden.routeheader.DocumentContent;
044:
045: /**
046: * This class...
047: */
048: public class KualiPayeeTaxIdAttribute implements WorkflowAttribute {
049:
050: private static Logger LOG = Logger
051: .getLogger(KualiPayeeTaxIdAttribute.class);
052:
053: private static final String PAYEE_TAX_ID = "taxIdNumber";
054: private static final String PAYEE_TAX_ID_KEY = "payee_review_tax_id";
055: private static final String MAINTAINABLE_PREFIX = "//newMaintainableObject/businessObject";
056: private static final String PAYEE_MAINTENANCE_DOC_TYPE = "KualiPayeeMaintenanceDocument";
057:
058: private boolean required;
059: private String payeeTaxId;
060: private List rows;
061:
062: /**
063: * Constructs a KualiPayeeTaxIdAttribute.java.
064: */
065: public KualiPayeeTaxIdAttribute() {
066: rows = new ArrayList();
067: required = false;
068:
069: List fields = new ArrayList();
070: org.kuali.core.web.ui.Field kualiPayeeTaxId;
071: kualiPayeeTaxId = FieldUtils.getPropertyField(Payee.class,
072: PAYEE_TAX_ID, false);
073: fields.add(new Field(kualiPayeeTaxId.getFieldLabel(),
074: KualiWorkflowUtils.getHelpUrl(kualiPayeeTaxId),
075: Field.TEXT, true, PAYEE_TAX_ID_KEY, kualiPayeeTaxId
076: .getPropertyValue(), kualiPayeeTaxId
077: .getFieldValidValues(), WorkflowLookupableImpl
078: .getLookupableImplName(Payee.class),
079: PAYEE_TAX_ID_KEY));
080: rows.add(new Row(fields));
081: }
082:
083: public KualiPayeeTaxIdAttribute(String payeeTaxId) {
084: this ();
085: this .payeeTaxId = payeeTaxId;
086: }
087:
088: /**
089: * @see edu.iu.uis.eden.plugin.attributes.WorkflowAttribute#isMatch(edu.iu.uis.eden.routeheader.DocumentContent, java.util.List)
090: */
091: public boolean isMatch(DocumentContent docContent,
092: List ruleExtensions) {
093:
094: // only Payee Maintenance documents will route on this rule.
095: DocumentType documentType = docContent.getRouteContext()
096: .getDocument().getDocumentType();
097: if (!PAYEE_MAINTENANCE_DOC_TYPE.equalsIgnoreCase(documentType
098: .getName())) {
099: return false;
100: }
101:
102: // extract the payeeTaxId from the XML doc
103: String docPayeeTaxId = getPayeeTaxIdFromDocContent(
104: documentType, docContent);
105: if (StringUtils.isBlank(docPayeeTaxId)) {
106: return false; // no taxId found, nothing to see here
107: }
108:
109: // see if the payeeTaxId is in the DB table
110: return isTaxIdInEmployeesTable(docPayeeTaxId);
111: }
112:
113: /**
114: * @see edu.iu.uis.eden.plugin.attributes.WorkflowAttribute#getRuleRows()
115: */
116: public List getRuleRows() {
117: // Since this rule only matches if a value on the PayeeMaintenance document is present
118: // in the Employees table, there is no UI component to enter matching values.
119: return Collections.EMPTY_LIST;
120: }
121:
122: /**
123: * @see edu.iu.uis.eden.plugin.attributes.WorkflowAttribute#getRoutingDataRows()
124: */
125: public List getRoutingDataRows() {
126: return rows;
127: }
128:
129: /**
130: * @see edu.iu.uis.eden.plugin.attributes.WorkflowAttribute#getDocContent()
131: */
132: public String getDocContent() {
133: return KualiWorkflowUtils.XML_REPORT_DOC_CONTENT_PREFIX
134: + "<taxIdNumber>" + this .payeeTaxId + "</taxIdNumber>"
135: + KualiWorkflowUtils.XML_REPORT_DOC_CONTENT_SUFFIX;
136: }
137:
138: /**
139: * @see edu.iu.uis.eden.plugin.attributes.WorkflowAttribute#getRuleExtensionValues()
140: */
141: public List getRuleExtensionValues() {
142: return Collections.EMPTY_LIST;
143: }
144:
145: /**
146: * @see edu.iu.uis.eden.plugin.attributes.WorkflowAttribute#validateRoutingData(java.util.Map)
147: */
148: public List validateRoutingData(Map paramMap) {
149: List errors = new ArrayList();
150: this .payeeTaxId = (String) paramMap.get(PAYEE_TAX_ID_KEY);
151: if (!this .payeeTaxId.matches("[/d]{9}")) {
152: errors
153: .add(new WorkflowServiceErrorImpl(
154: "Payee TaxID must be exactly nine characters, all numbers. Do not use dashes or spaces.",
155: ""));
156: }
157: return errors;
158: }
159:
160: /**
161: * @see edu.iu.uis.eden.plugin.attributes.WorkflowAttribute#validateRuleData(java.util.Map)
162: */
163: public List validateRuleData(Map paramMap) {
164: return Collections.EMPTY_LIST;
165: }
166:
167: /**
168: * @see edu.iu.uis.eden.plugin.attributes.WorkflowAttribute#setRequired(boolean)
169: */
170: public void setRequired(boolean required) {
171: this .required = required;
172: }
173:
174: /**
175: * @see edu.iu.uis.eden.plugin.attributes.WorkflowAttribute#isRequired()
176: */
177: public boolean isRequired() {
178: return required;
179: }
180:
181: /**
182: * Extracts the PayeeTaxId from the document contents passed in.
183: *
184: * @param docType
185: * @param docContent
186: * @return
187: */
188: protected String getPayeeTaxIdFromDocContent(DocumentType docType,
189: DocumentContent docContent) {
190: String payeeTaxId = "";
191: XPath xpath = KualiWorkflowUtils.getXPath(docContent
192: .getDocument());
193:
194: // updated to work for the test, a little less typesafe
195: String xpathExpression = KualiWorkflowUtils.XSTREAM_SAFE_PREFIX
196: + MAINTAINABLE_PREFIX
197: + "/taxIdNumber"
198: + KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX
199: + " | /"
200: + KualiWorkflowUtils.XML_REPORT_DOC_CONTENT_XPATH_PREFIX
201: + "/taxIdNumber";
202:
203: // extract the payeeTaxId value
204: try {
205: payeeTaxId = xpath.evaluate(xpathExpression, docContent
206: .getDocument());
207: } catch (XPathExpressionException e) {
208: throw new RuntimeException(
209: "Error evaluating xpath expression ["
210: + xpathExpression
211: + "] to locate PayeeTaxId.", e);
212: }
213: return payeeTaxId;
214: }
215:
216: /**
217: * Given the taxId passed in, this method tests whether anyone with that taxId is present in the employees database, and returns
218: * the result of that search.
219: *
220: * @param payeeTaxId A String containing the payeeTaxId to test for.
221: * @return True if the payeeTaxId is found for any employee in the system, False otherwise.
222: */
223: protected static boolean isTaxIdInEmployeesTable(String payeeTaxId) {
224: if (StringUtils.isBlank(payeeTaxId)) {
225: throw new IllegalArgumentException(
226: "A blank or null payeeTaxId parameter has been passed in.");
227: }
228: UniversalUser user = null;
229: try {
230: user = SpringContext.getBean(UniversalUserService.class)
231: .getUniversalUser(new PersonTaxId(payeeTaxId));
232: } catch (UserNotFoundException e) {
233: }
234: return user != null;
235: }
236: }
|