View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package edu.sampleu.travel.workflow;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.rice.core.api.uif.RemotableAttributeErrorContract;
25  import org.kuali.rice.core.api.uif.RemotableAttributeError;
26  import org.kuali.rice.kew.rule.GenericWorkflowAttribute;
27  import org.kuali.rice.kns.web.ui.Field;
28  import org.kuali.rice.kns.web.ui.Row;
29  
30  public class DestinationRuleAttribute extends GenericWorkflowAttribute {
31  
32  	private static final String DEST_LABEL = "Destination";
33  	private static final String DEST_FIELD_KEY = "destination";
34  
35      private final List<Row> rows = new ArrayList<Row>();
36  
37  	private String destination;
38  
39      public DestinationRuleAttribute() {
40          super("destination");
41      }
42  
43      public DestinationRuleAttribute(String destination) {
44          super("destination");
45          this.destination = destination;
46      }
47  
48      /*
49  	public boolean isMatch(DocumentContent docContent, List<RuleExtension> ruleExtensions) {
50  		try {
51  			boolean foundDestRule = false;
52  			for (Iterator extensionsIterator = ruleExtensions.iterator(); extensionsIterator.hasNext();) {
53  	            RuleExtension extension = (RuleExtension) extensionsIterator.next();
54  	            if (extension.getRuleTemplateAttribute().getRuleAttribute().getClassName().equals(getClass().getName())) {
55  	                for (Iterator valuesIterator = extension.getExtensionValues().iterator(); valuesIterator.hasNext();) {
56  	                    RuleExtensionValue extensionValue = (RuleExtensionValue) valuesIterator.next();
57  	                    String key = extensionValue.getKey();
58  	                    String value = extensionValue.getValue();
59  	                    if (key.equals(DEST_FIELD_KEY)) {
60  	                    	destination = value;
61  	                    	foundDestRule = true;
62  	                    }
63  	                }
64  	            }
65  	        }
66  			if (! foundDestRule) {
67  				return false;
68  			}
69  
70  			Element element = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
71  					new InputSource(new BufferedReader(new StringReader(docContent.getDocContent())))).getDocumentElement();
72  			XPath xpath = XPathFactory.newInstance().newXPath();
73  			String docContentDest = xpath.evaluate("//destination", element);
74  			return destination.equals(docContentDest);
75  		} catch (Exception e) {
76  			throw new RuntimeException(e);
77  		}
78  	}*/
79  
80  	public List<Row> getRuleRows() {
81  		return getRows();
82  	}
83  
84  	public List<Row> getRoutingDataRows() {
85  		return getRows();
86  	}
87  
88      private List<Row> getRows() {
89          log.info("Returning rows: " + rows);
90          List<Field> fields = new ArrayList<Field>();
91          fields.add(new Field(DEST_LABEL, "", Field.TEXT, false, DEST_FIELD_KEY, "", false, false, null, null));
92          List<Row> rows = new ArrayList<Row>();
93          rows.add(new Row(fields));
94          return rows;
95      }
96  
97      /* setter for edoclite field */
98      public void setDestination(String destination) {
99          this.destination = destination;
100     }
101 
102     public Map<String, String> getProperties() {
103         Map<String, String> props = new HashMap<String, String>();
104         props.put("destination", destination);
105         return props;
106     }
107 
108 	public List<RemotableAttributeError> validateRoutingData(Map paramMap) {
109 		return validateInputMap(paramMap);
110 	}
111 
112 	public List<RemotableAttributeError> validateRuleData(Map paramMap) {
113 		return validateInputMap(paramMap);
114 	}
115 
116     private List<RemotableAttributeError> validateInputMap(Map paramMap) {
117     	List errors = new ArrayList();
118     	this.destination = (String) paramMap.get(DEST_FIELD_KEY);
119     	if (StringUtils.isBlank(destination)  && required) {
120     		errors.add(RemotableAttributeError.Builder.create(DEST_FIELD_KEY, "Destination is required.").build());
121     	}
122     	return errors;
123     }
124 }