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.krad.workflow;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
020import org.kuali.rice.core.api.uif.RemotableAttributeError;
021import org.kuali.rice.core.api.uif.RemotableAttributeField;
022import org.kuali.rice.core.api.uif.RemotableTextInput;
023import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
024import org.kuali.rice.kew.api.KEWPropertyConstants;
025import org.kuali.rice.kew.api.KewApiServiceLocator;
026import org.kuali.rice.kew.api.document.Document;
027import org.kuali.rice.kew.api.document.DocumentContent;
028import org.kuali.rice.kew.api.repository.type.KewAttributeDefinition;
029import org.kuali.rice.kew.api.repository.type.KewTypeAttribute;
030import org.kuali.rice.kew.api.repository.type.KewTypeDefinition;
031import org.kuali.rice.kew.framework.peopleflow.PeopleFlowTypeService;
032import org.kuali.rice.krad.service.DataDictionaryRemoteFieldService;
033import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
034import org.kuali.rice.krad.util.BeanPropertyComparator;
035
036import javax.jws.WebParam;
037import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
038import java.util.ArrayList;
039import java.util.Collections;
040import java.util.HashMap;
041import java.util.List;
042import java.util.Map;
043
044/**
045 * @author Kuali Rice Team (rice.collab@kuali.org)
046 */
047public class DataDictionaryPeopleFlowTypeServiceImpl implements PeopleFlowTypeService {
048
049    /**
050     * @see org.kuali.rice.kew.framework.peopleflow.PeopleFlowTypeService#filterToSelectableRoleIds(java.lang.String,
051     *      java.util.List<java.lang.String>)
052     */
053    public List<String> filterToSelectableRoleIds(@WebParam(name = "kewTypeId") String kewTypeId,
054            @WebParam(name = "roleIds") List<String> roleIds) {
055        return roleIds;
056    }
057
058    /**
059     * @see org.kuali.rice.kew.framework.peopleflow.PeopleFlowTypeService#resolveRoleQualifiers(java.lang.String,
060     *      java.lang.String, org.kuali.rice.kew.api.document.Document, org.kuali.rice.kew.api.document.DocumentContent)
061     */
062    public Map<String, String> resolveRoleQualifiers(@WebParam(name = "kewTypeId") String kewTypeId,
063            @WebParam(name = "roleId") String roleId, @WebParam(name = "document") Document document,
064            @WebParam(name = "documentContent") DocumentContent documentContent) {
065        return new HashMap<String, String>();
066    }
067
068    /**
069     * @see org.kuali.rice.kew.framework.peopleflow.PeopleFlowTypeService#getAttributeFields(java.lang.String)
070     */
071    public List<RemotableAttributeField> getAttributeFields(@WebParam(name = "kewTypeId") String kewTypeId) {
072        List<RemotableAttributeField> fields = new ArrayList<RemotableAttributeField>();
073
074        KewTypeDefinition typeDefinition = KewApiServiceLocator.getKewTypeRepositoryService().getTypeById(kewTypeId);
075        List<KewTypeAttribute> typeAttributes = new ArrayList<KewTypeAttribute>(typeDefinition.getAttributes());
076
077        // sort type attributes by sort code
078        List<String> sortAttributes = new ArrayList<String>();
079        sortAttributes.add(KEWPropertyConstants.SEQUENCE_NUMBER);
080        Collections.sort(typeAttributes, new BeanPropertyComparator(sortAttributes));
081
082        // build a remotable field for each active type attribute
083        for (KewTypeAttribute typeAttribute : typeAttributes) {
084            if (!typeAttribute.isActive()) {
085                continue;
086            }
087
088            RemotableAttributeField attributeField = null;
089            if (StringUtils.isBlank(typeAttribute.getAttributeDefinition().getComponentName())) {
090                attributeField = buildRemotableFieldWithoutDataDictionary(typeAttribute.getAttributeDefinition());
091            } else {
092                attributeField = getDataDictionaryRemoteFieldService().buildRemotableFieldFromAttributeDefinition(
093                        typeAttribute.getAttributeDefinition().getComponentName(),
094                        typeAttribute.getAttributeDefinition().getName());
095            }
096
097            fields.add(attributeField);
098        }
099
100        return fields;
101    }
102
103    /**
104     * Builds a {@link RemotableAttributeField} instance when there is no component configured (and therefore we can
105     * not lookup the data dictionary)
106     *
107     * <p>
108     * Very basic field, should have labels configured and defaults to using text control
109     * </p>
110     *
111     * @param attributeDefinition - KEW attribute definition configured from which the name, label, and description
112     * will be pulled
113     * @return RemotableAttributeField instance built from the given KEW attribute definition
114     */
115    protected RemotableAttributeField buildRemotableFieldWithoutDataDictionary(
116            KewAttributeDefinition attributeDefinition) {
117        RemotableAttributeField.Builder definition = RemotableAttributeField.Builder.create(
118                attributeDefinition.getName());
119
120        definition.setLongLabel(attributeDefinition.getLabel());
121        definition.setShortLabel(attributeDefinition.getLabel());
122        definition.setHelpDescription(attributeDefinition.getDescription());
123
124        // default control to text
125        RemotableTextInput.Builder controlBuilder = RemotableTextInput.Builder.create();
126        controlBuilder.setSize(30);
127        definition.setControl(controlBuilder);
128
129        return definition.build();
130    }
131
132    /**
133     * @see org.kuali.rice.kew.framework.peopleflow.PeopleFlowTypeService#validateAttributes(java.lang.String,
134     *      java.util.Map<java.lang.String,java.lang.String>)
135     */
136    public List<RemotableAttributeError> validateAttributes(@WebParam(name = "kewTypeId") String kewTypeId,
137            @WebParam(name = "attributes") @XmlJavaTypeAdapter(
138                    value = MapStringStringAdapter.class) Map<String, String> attributes) throws RiceIllegalArgumentException {
139        return null;
140    }
141
142    /**
143     * @see org.kuali.rice.kew.framework.peopleflow.PeopleFlowTypeService#validateAttributesAgainstExisting(java.lang.String,
144     *      java.util.Map<java.lang.String,java.lang.String>, java.util.Map<java.lang.String,java.lang.String>)
145     */
146    public List<RemotableAttributeError> validateAttributesAgainstExisting(
147            @WebParam(name = "kewTypeId") String kewTypeId, @WebParam(name = "newAttributes") @XmlJavaTypeAdapter(
148            value = MapStringStringAdapter.class) Map<String, String> newAttributes,
149            @WebParam(name = "oldAttributes") @XmlJavaTypeAdapter(
150                    value = MapStringStringAdapter.class) Map<String, String> oldAttributes) throws RiceIllegalArgumentException {
151        return null;
152    }
153
154    public DataDictionaryRemoteFieldService getDataDictionaryRemoteFieldService() {
155        return KRADServiceLocatorWeb.getDataDictionaryRemoteFieldService();
156    }
157}