001/**
002 * Copyright 2005-2016 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 org.kuali.rice.edl.impl.components;
017
018import java.util.Iterator;
019import java.util.List;
020
021import org.kuali.rice.core.api.uif.RemotableAttributeErrorContract;
022import org.kuali.rice.edl.impl.EDLContext;
023import org.kuali.rice.edl.impl.RequestParser;
024import org.kuali.rice.kew.api.WorkflowDocument;
025import org.kuali.rice.kew.api.document.PropertyDefinition;
026import org.kuali.rice.kew.api.document.attribute.WorkflowAttributeDefinition;
027import org.w3c.dom.Element;
028
029/**
030 * Populates workflow rule attributes associated with the current configElement.
031 * 
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 * 
034 */
035public class AttributeEDLConfigComponent extends SimpleWorkflowEDLConfigComponent {
036
037    public List getMatchingParams(Element originalConfigElement, RequestParser requestParser, EDLContext edlContext) {
038        List matchingParams = super.getMatchingParams(originalConfigElement, requestParser, edlContext);
039        // we don't want to clear the attribute content if they are just opening up the document to view it!
040        if (!edlContext.getUserAction().isLoadAction()) {
041            String attributeName = originalConfigElement.getAttribute("attributeName");
042            String attributePropertyName = originalConfigElement.getAttribute("name");
043
044            WorkflowDocument document = (WorkflowDocument) requestParser
045                    .getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
046            // clear attribute content so that duplicate attribute values are not added during submission of a new EDL form values version
047            document.clearAttributeContent();
048
049            WorkflowAttributeDefinition.Builder attributeDefBuilder = getWorkflowAttributeDefinitionVO(attributeName, document);
050            
051            for (Iterator iter = matchingParams.iterator(); iter.hasNext();) {
052                MatchingParam param = (MatchingParam) iter.next();
053                PropertyDefinition property = attributeDefBuilder.getPropertyDefinition(attributePropertyName);
054                //if the prop doesn't exist create it and add it to the definition otherwise update the property value
055                if (property == null) {
056                    property = PropertyDefinition.create(attributePropertyName, param.getParamValue());
057                } else {
058                    // modify the current property
059                    attributeDefBuilder.getPropertyDefinitions().remove(property);
060                    property = PropertyDefinition.create(property.getName(), param.getParamValue());
061                }
062                attributeDefBuilder.addPropertyDefinition(property);
063
064            }
065            
066            WorkflowAttributeDefinition attributeDef = attributeDefBuilder.build();
067            document.addAttributeDefinition(attributeDef);
068
069            // validate if they are taking an action on the document (i.e. it's annotatable)
070            if (edlContext.getUserAction().isValidatableAction()) {
071                List<? extends RemotableAttributeErrorContract> errors = document.validateAttributeDefinition(attributeDef);
072                if (!errors.isEmpty()) {
073                    getEdlContext().setInError(true);
074                }
075                for (RemotableAttributeErrorContract error : errors) {
076                    MatchingParam param = getMatchingParam(matchingParams, error.getAttributeName());
077                    // if it doesn't match a param, then this is a global error
078                    if (param == null) {
079                        List globalErrors = (List) getEdlContext().getRequestParser().getAttribute(
080                                    RequestParser.GLOBAL_ERRORS_KEY);
081                        globalErrors.add(error.getMessage());
082                    } else {
083                        param.setError(Boolean.TRUE);
084                        param.setErrorMessage(error.getMessage());
085                    }
086                }
087            }
088        }
089        return matchingParams;
090    }
091
092    private WorkflowAttributeDefinition.Builder getWorkflowAttributeDefinitionVO(String attributeName, WorkflowDocument document) {
093        for (WorkflowAttributeDefinition attributeDefinition : document.getAttributeDefinitions()) {
094            if (attributeDefinition.getAttributeName().equals(attributeName)) {
095                return WorkflowAttributeDefinition.Builder.create(attributeDefinition);
096            }
097        }
098        return WorkflowAttributeDefinition.Builder.create(attributeName);
099    }
100
101    private MatchingParam getMatchingParam(List matchingParams, String name) {
102        for (Iterator iterator = matchingParams.iterator(); iterator.hasNext();) {
103            MatchingParam param = (MatchingParam) iterator.next();
104            if (param.getParamName().equals(name)) {
105                return param;
106            }
107        }
108        return null;
109    }
110}