001/**
002 * Copyright 2005-2015 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 */
016package edu.sampleu.travel.workflow;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.commons.lang.StringUtils;
024import org.kuali.rice.core.api.uif.RemotableAttributeErrorContract;
025import org.kuali.rice.core.api.uif.RemotableAttributeError;
026import org.kuali.rice.kew.rule.GenericWorkflowAttribute;
027import org.kuali.rice.kns.web.ui.Field;
028import org.kuali.rice.kns.web.ui.Row;
029
030public class DestinationRuleAttribute extends GenericWorkflowAttribute {
031
032        private static final String DEST_LABEL = "Destination";
033        private static final String DEST_FIELD_KEY = "destination";
034
035    private final List<Row> rows = new ArrayList<Row>();
036
037        private String destination;
038
039    public DestinationRuleAttribute() {
040        super("destination");
041    }
042
043    public DestinationRuleAttribute(String destination) {
044        super("destination");
045        this.destination = destination;
046    }
047
048    /*
049        public boolean isMatch(DocumentContent docContent, List<RuleExtension> ruleExtensions) {
050                try {
051                        boolean foundDestRule = false;
052                        for (Iterator extensionsIterator = ruleExtensions.iterator(); extensionsIterator.hasNext();) {
053                    RuleExtension extension = (RuleExtension) extensionsIterator.next();
054                    if (extension.getRuleTemplateAttribute().getRuleAttribute().getClassName().equals(getClass().getName())) {
055                        for (Iterator valuesIterator = extension.getExtensionValues().iterator(); valuesIterator.hasNext();) {
056                            RuleExtensionValue extensionValue = (RuleExtensionValue) valuesIterator.next();
057                            String key = extensionValue.getKey();
058                            String value = extensionValue.getValue();
059                            if (key.equals(DEST_FIELD_KEY)) {
060                                destination = value;
061                                foundDestRule = true;
062                            }
063                        }
064                    }
065                }
066                        if (! foundDestRule) {
067                                return false;
068                        }
069
070                        Element element = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
071                                        new InputSource(new BufferedReader(new StringReader(docContent.getDocContent())))).getDocumentElement();
072                        XPath xpath = XPathFactory.newInstance().newXPath();
073                        String docContentDest = xpath.evaluate("//destination", element);
074                        return destination.equals(docContentDest);
075                } catch (Exception e) {
076                        throw new RuntimeException(e);
077                }
078        }*/
079
080        public List<Row> getRuleRows() {
081                return getRows();
082        }
083
084        public List<Row> getRoutingDataRows() {
085                return getRows();
086        }
087
088    private List<Row> getRows() {
089        log.info("Returning rows: " + rows);
090        List<Field> fields = new ArrayList<Field>();
091        fields.add(new Field(DEST_LABEL, "", Field.TEXT, false, DEST_FIELD_KEY, "", false, false, null, null));
092        List<Row> rows = new ArrayList<Row>();
093        rows.add(new Row(fields));
094        return rows;
095    }
096
097    /* setter for edoclite field */
098    public void setDestination(String destination) {
099        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}