001: /*
002: * Copyright 2005-2006 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 edu.iu.uis.eden.edl.components;
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.w3c.dom.Element;
023:
024: import edu.iu.uis.eden.clientapp.WorkflowDocument;
025: import edu.iu.uis.eden.clientapp.vo.PropertyDefinitionVO;
026: import edu.iu.uis.eden.clientapp.vo.WorkflowAttributeDefinitionVO;
027: import edu.iu.uis.eden.clientapp.vo.WorkflowAttributeValidationErrorVO;
028: import edu.iu.uis.eden.edl.EDLContext;
029: import edu.iu.uis.eden.edl.RequestParser;
030: import edu.iu.uis.eden.exception.WorkflowException;
031: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
032:
033: /**
034: * Populates workflow rule attributes associated with the current configElement.
035: *
036: * @author ewestfal
037: * @author rkirkend
038: *
039: */
040: public class AttributeEDLConfigComponent extends
041: SimpleWorkflowEDLConfigComponent {
042:
043: public static final String[] VALIDATABLE_ACTIONS = new String[] {
044: WorkflowDocumentActions.ACTION_ROUTE,
045: WorkflowDocumentActions.ACTION_APPROVE,
046: WorkflowDocumentActions.ACTION_ACKNOWLEDGE,
047: WorkflowDocumentActions.ACTION_COMPLETE,
048: WorkflowDocumentActions.ACTION_FYI,
049: WorkflowDocumentActions.ACTION_DISAPPROVE,
050: WorkflowDocumentActions.ACTION_CANCEL,
051: WorkflowDocumentActions.ACTION_RETURN_TO_PREVIOUS };
052:
053: public static boolean isValidatableAction(String action) {
054: for (int index = 0; index < VALIDATABLE_ACTIONS.length; index++) {
055: if (VALIDATABLE_ACTIONS[index].equals(action)) {
056: return true;
057: }
058: }
059: return false;
060: }
061:
062: public List getMatchingParams(Element originalConfigElement,
063: RequestParser requestParser, EDLContext edlContext) {
064: List matchingParams = super .getMatchingParams(
065: originalConfigElement, requestParser, edlContext);
066: String attributeName = originalConfigElement
067: .getAttribute("attributeName");
068: String attributePropertyName = originalConfigElement
069: .getAttribute("name");
070:
071: WorkflowDocument document = (WorkflowDocument) requestParser
072: .getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
073:
074: try {
075: // validate if they are taking an action on the document (i.e. it's annotatable)
076: String action = requestParser
077: .getParameterValue(WorkflowDocumentActions.USER_ACTION_REQUEST_KEY);
078: if (isValidatableAction(action)) {
079: // only update content if this is a validatable action
080:
081: // clear attribute content so that duplicate attribute values are not added during submission of a new EDL form values version
082: document.clearAttributeContent();
083:
084: WorkflowAttributeDefinitionVO attributeDef = getWorkflowAttributeDefinitionVO(
085: attributeName, document);
086: for (Iterator iter = matchingParams.iterator(); iter
087: .hasNext();) {
088: MatchingParam param = (MatchingParam) iter.next();
089: PropertyDefinitionVO property = attributeDef
090: .getProperty(attributePropertyName);
091: //if the prop doesn't exist create it and add it to the definition otherwise update the property value
092: if (property == null) {
093: property = new PropertyDefinitionVO(
094: attributePropertyName, param
095: .getParamValue());
096: attributeDef.addProperty(property);
097: } else {
098: property.setValue(param.getParamValue());
099: }
100: }
101:
102: WorkflowAttributeValidationErrorVO[] errors = document
103: .validateAttributeDefinition(attributeDef);
104: if (errors.length > 0) {
105: getEdlContext().setInError(true);
106: }
107: for (int index = 0; index < errors.length; index++) {
108: WorkflowAttributeValidationErrorVO error = errors[index];
109: MatchingParam param = getMatchingParam(
110: matchingParams, error.getKey());
111: // if it doesn't match a param, then this is a global error
112: if (param == null) {
113: List globalErrors = (List) getEdlContext()
114: .getRequestParser()
115: .getAttribute(
116: RequestParser.GLOBAL_ERRORS_KEY);
117: globalErrors.add(error.getMessage());
118: } else {
119: param.setError(Boolean.TRUE);
120: param.setErrorMessage(error.getMessage());
121: }
122: }
123: }
124: } catch (WorkflowException e) {
125: throw new WorkflowRuntimeException(
126: "Encountered an error when validating form.", e);
127: }
128:
129: return matchingParams;
130: }
131:
132: private WorkflowAttributeDefinitionVO getWorkflowAttributeDefinitionVO(
133: String attributeName, WorkflowDocument document) {
134: for (int i = 0; i < document.getAttributeDefinitions().length; i++) {
135: WorkflowAttributeDefinitionVO workflowAttributeDef = (WorkflowAttributeDefinitionVO) document
136: .getAttributeDefinitions()[i];
137: if (workflowAttributeDef.getAttributeName().equals(
138: attributeName)) {
139: return workflowAttributeDef;
140: }
141: }
142: WorkflowAttributeDefinitionVO workflowAttributeDef = new WorkflowAttributeDefinitionVO(
143: attributeName);
144: document.addAttributeDefinition(workflowAttributeDef);
145: return workflowAttributeDef;
146: }
147:
148: private MatchingParam getMatchingParam(List matchingParams,
149: String name) {
150: for (Iterator iterator = matchingParams.iterator(); iterator
151: .hasNext();) {
152: MatchingParam param = (MatchingParam) iterator.next();
153: if (param.getParamName().equals(name)) {
154: return param;
155: }
156: }
157: return null;
158: }
159: }
|