001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.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/ecl2.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 edu.sampleu.travel.workflow;
017    
018    import java.util.ArrayList;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.kuali.rice.core.api.uif.RemotableAttributeErrorContract;
024    import org.kuali.rice.core.api.uif.RemotableAttributeError;
025    import org.kuali.rice.kew.rule.GenericWorkflowAttribute;
026    import org.kuali.rice.kns.web.ui.Field;
027    import org.kuali.rice.kns.web.ui.Row;
028    
029    public class DestinationRuleAttribute extends GenericWorkflowAttribute {
030    
031            private static final String DEST_LABEL = "Destination";
032            private static final String DEST_FIELD_KEY = "destination";
033    
034        private final List<Row> rows = new ArrayList<Row>();
035    
036            private String destination;
037    
038        public DestinationRuleAttribute() {
039            super("destination");
040        }
041    
042        public DestinationRuleAttribute(String destination) {
043            super("destination");
044            this.destination = destination;
045        }
046    
047        /*
048            public boolean isMatch(DocumentContent docContent, List<RuleExtension> ruleExtensions) {
049                    try {
050                            boolean foundDestRule = false;
051                            for (Iterator extensionsIterator = ruleExtensions.iterator(); extensionsIterator.hasNext();) {
052                        RuleExtension extension = (RuleExtension) extensionsIterator.next();
053                        if (extension.getRuleTemplateAttribute().getRuleAttribute().getClassName().equals(getClass().getName())) {
054                            for (Iterator valuesIterator = extension.getExtensionValues().iterator(); valuesIterator.hasNext();) {
055                                RuleExtensionValue extensionValue = (RuleExtensionValue) valuesIterator.next();
056                                String key = extensionValue.getKey();
057                                String value = extensionValue.getValue();
058                                if (key.equals(DEST_FIELD_KEY)) {
059                                    destination = value;
060                                    foundDestRule = true;
061                                }
062                            }
063                        }
064                    }
065                            if (! foundDestRule) {
066                                    return false;
067                            }
068    
069                            Element element = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
070                                            new InputSource(new BufferedReader(new StringReader(docContent.getDocContent())))).getDocumentElement();
071                            XPath xpath = XPathFactory.newInstance().newXPath();
072                            String docContentDest = xpath.evaluate("//destination", element);
073                            return destination.equals(docContentDest);
074                    } catch (Exception e) {
075                            throw new RuntimeException(e);
076                    }
077            }*/
078    
079            public List<Row> getRuleRows() {
080                    return getRows();
081            }
082    
083            public List<Row> getRoutingDataRows() {
084                    return getRows();
085            }
086    
087        private List<Row> getRows() {
088            log.info("Returning rows: " + rows);
089            List<Field> fields = new ArrayList<Field>();
090            fields.add(new Field(DEST_LABEL, "", Field.TEXT, false, DEST_FIELD_KEY, "", false, false, null, null));
091            List<Row> rows = new ArrayList<Row>();
092            rows.add(new Row(fields));
093            return rows;
094        }
095    
096        /* setter for edoclite field */
097        public void setDestination(String destination) {
098            this.destination = destination;
099        }
100    
101        public Map<String, String> getProperties() {
102            Map<String, String> props = new HashMap<String, String>();
103            props.put("destination", destination);
104            return props;
105        }
106    
107            public List<? extends RemotableAttributeErrorContract> validateRoutingData(Map paramMap) {
108                    return validateInputMap(paramMap);
109            }
110    
111            public List<? extends RemotableAttributeErrorContract> validateRuleData(Map paramMap) {
112                    return validateInputMap(paramMap);
113            }
114    
115        private List<? extends RemotableAttributeErrorContract> validateInputMap(Map paramMap) {
116            List errors = new ArrayList();
117            this.destination = (String) paramMap.get(DEST_FIELD_KEY);
118            if (this.destination == null  && required) {
119                    errors.add(RemotableAttributeError.Builder.create(DEST_FIELD_KEY, "Destination is required.").build());
120            }
121            return errors;
122        }
123    }