Coverage Report - org.kuali.rice.krms.impl.type.KrmsTypeServiceBase
 
Classes in this File Line Coverage Branch Coverage Complexity
KrmsTypeServiceBase
0%
0/38
0%
0/12
3.143
KrmsTypeServiceBase$1
0%
0/8
0%
0/6
3.143
 
 1  
 /**
 2  
  * Copyright 2005-2011 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 org.kuali.rice.krms.impl.type;
 17  
 
 18  
 import org.apache.commons.collections.CollectionUtils;
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
 21  
 import org.kuali.rice.core.api.uif.RemotableAttributeError;
 22  
 import org.kuali.rice.core.api.uif.RemotableAttributeField;
 23  
 import org.kuali.rice.core.api.uif.RemotableTextInput;
 24  
 import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
 25  
 import org.kuali.rice.krad.service.DataDictionaryRemoteFieldService;
 26  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 27  
 import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
 28  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute;
 29  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
 30  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
 31  
 import org.kuali.rice.krms.framework.type.RemotableAttributeOwner;
 32  
 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
 33  
 
 34  
 import javax.jws.WebParam;
 35  
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 36  
 import java.util.ArrayList;
 37  
 import java.util.Collections;
 38  
 import java.util.Comparator;
 39  
 import java.util.HashMap;
 40  
 import java.util.List;
 41  
 import java.util.Map;
 42  
 
 43  
 /**
 44  
  * {@link KrmsTypeServiceBase} is an abstract class providing default implementation and hooks for
 45  
  * provisioning and validating the custom attributes of a krms type.  Is should probably be mentioned that the
 46  
  * default validation methods don't actually check anything, they just return empty error lists.
 47  
  */
 48  0
 public abstract class KrmsTypeServiceBase implements RemotableAttributeOwner {
 49  
 
 50  
     /**
 51  
      * <p>get the {@link RemotableAttributeField}s for the custom attributes of this krms type.  This implementation
 52  
      * will return any attributes mapped to the type via
 53  
      * {@link org.kuali.rice.krms.impl.repository.KrmsTypeAttributeBo}. If there is is a component name defined on the
 54  
      * related {@link org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionBo} then that will be used to generate
 55  
      * the {@link RemotableAttributeField}.  If not, then a simple text input will be produced. </p>
 56  
      *
 57  
      * <p>An extending class can also override the {@link #translateTypeAttribute(org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute, org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition)}
 58  
      * method which is called from here, and within it hand create the RemotableAttributeField for a certain attribute.
 59  
      * </p>
 60  
      *
 61  
      * @param krmsTypeId the people flow type identifier.  Must not be null or blank.
 62  
      * @return
 63  
      * @throws RiceIllegalArgumentException
 64  
      */
 65  
     @Override
 66  
     public List<RemotableAttributeField> getAttributeFields(@WebParam(name = "krmsTypeId") String krmsTypeId) throws RiceIllegalArgumentException {
 67  
 
 68  0
         if (StringUtils.isBlank(krmsTypeId)) {
 69  0
             throw new RiceIllegalArgumentException("krmsTypeId must be non-null and non-blank");
 70  
         }
 71  
 
 72  0
         List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
 73  
 
 74  
         // keep track of how to sort these
 75  0
         final Map<String, Integer> sortCodeMap = new HashMap<String, Integer>();
 76  
 
 77  0
         KrmsTypeDefinition krmsType =
 78  
                 KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().getTypeById(krmsTypeId);
 79  
 
 80  0
         if (krmsType == null) {
 81  0
             throw new RiceIllegalArgumentException("krmsTypeId must be a valid id of a KRMS type");
 82  
         } else {
 83  
             // translate attributes
 84  
 
 85  0
             List<KrmsTypeAttribute> typeAttributes = krmsType.getAttributes();
 86  
 
 87  0
             List<RemotableAttributeField> typeAttributeFields = new ArrayList<RemotableAttributeField>(10);
 88  0
             if (!CollectionUtils.isEmpty(typeAttributes)) {
 89  
                 // translate the attribute and store the sort code in our map
 90  0
                 for (KrmsTypeAttribute typeAttribute : typeAttributes) {
 91  
 
 92  0
                     KrmsTypeRepositoryService typeRepositoryService = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService();
 93  
 
 94  0
                     KrmsAttributeDefinition attributeDefinition =
 95  
                             typeRepositoryService.getAttributeDefinitionById(typeAttribute.getAttributeDefinitionId());
 96  
 
 97  0
                     RemotableAttributeField attributeField = translateTypeAttribute(typeAttribute, attributeDefinition);
 98  
 
 99  0
                     if (typeAttribute.getSequenceNumber() == null) {
 100  0
                         throw new IllegalStateException(typeAttribute.toString() + " has a null sequenceNumber");
 101  
                     } else {
 102  0
                         sortCodeMap.put(attributeField.getName(), typeAttribute.getSequenceNumber());
 103  
                     }
 104  
 
 105  0
                     results.add(attributeField);
 106  0
                 }
 107  
             }
 108  
         }
 109  
 
 110  0
         sortFields(results, sortCodeMap);
 111  
 
 112  0
         return results;
 113  
     }
 114  
 
 115  
     protected void sortFields(List<RemotableAttributeField> results,
 116  
             final Map<String, Integer> sortCodeMap) {// sort the results
 117  0
         Collections.sort(results, new Comparator<RemotableAttributeField>() {
 118  
             @Override
 119  
             public int compare(RemotableAttributeField o1, RemotableAttributeField o2) {
 120  0
                 if (o1 == o2 || o1.equals(o2))
 121  0
                     return 0;
 122  
                 // we assume that each has a sort code based on our previous check
 123  0
                 Integer o1SortCode = sortCodeMap.get(o1.getName());
 124  0
                 Integer o2SortCode = sortCodeMap.get(o2.getName());
 125  
 
 126  0
                 if (o1SortCode.compareTo(o2SortCode) != 0) {
 127  0
                     return o1SortCode.compareTo(o2SortCode);
 128  
                 } else {
 129  
                     // if sort codes are the same, we still would like a consistent order
 130  0
                     return o1.getName().compareTo(o2.getName());
 131  
                 }
 132  
             }
 133  
         });
 134  0
     }
 135  
 
 136  
     @Override
 137  
     public List<RemotableAttributeError> validateAttributes(@WebParam(name = "krmsTypeId") String krmsTypeId,
 138  
             @WebParam(name = "attributes") @XmlJavaTypeAdapter(
 139  
                     value = MapStringStringAdapter.class) Map<String, String> attributes) throws RiceIllegalArgumentException {
 140  0
         return Collections.emptyList();
 141  
     }
 142  
 
 143  
     @Override
 144  
     public List<RemotableAttributeError> validateAttributesAgainstExisting(
 145  
             @WebParam(name = "krmsTypeId") String krmsTypeId, @WebParam(name = "newAttributes") @XmlJavaTypeAdapter(
 146  
             value = MapStringStringAdapter.class) Map<String, String> newAttributes,
 147  
             @WebParam(name = "oldAttributes") @XmlJavaTypeAdapter(
 148  
                     value = MapStringStringAdapter.class) Map<String, String> oldAttributes) throws RiceIllegalArgumentException {
 149  0
         return validateAttributes(krmsTypeId, newAttributes);
 150  
     }
 151  
 
 152  
     /**
 153  
      * Translate a {@link org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute} into a {@link org.kuali.rice.core.api.uif.RemotableAttributeField}.
 154  
      * Override this method to provide custom translation of certain attributes.
 155  
      * @param inputAttribute the {@link org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute} to translate
 156  
      * @param attributeDefinition the {@link KrmsAttributeDefinition} for the given inputAttribute
 157  
      * @return a {@link org.kuali.rice.core.api.uif.RemotableAttributeField} for the given inputAttribute
 158  
      */
 159  
     public RemotableAttributeField translateTypeAttribute(KrmsTypeAttribute inputAttribute,
 160  
             KrmsAttributeDefinition attributeDefinition) {
 161  
 
 162  0
         if (StringUtils.isEmpty(attributeDefinition.getComponentName())) {
 163  0
             RemotableAttributeField.Builder builder = RemotableAttributeField.Builder.create(attributeDefinition.getName());
 164  
 
 165  0
             RemotableTextInput.Builder controlBuilder = RemotableTextInput.Builder.create();
 166  0
             controlBuilder.setSize(80);
 167  
 
 168  0
             controlBuilder.setWatermark(attributeDefinition.getDescription());
 169  
 
 170  0
             builder.setLongLabel(attributeDefinition.getName());
 171  0
             builder.setName(attributeDefinition.getName());
 172  
 
 173  
 //            builder.setHelpSummary("helpSummary: " + attributeDefinition.getDescription());
 174  
 //            builder.setHelpDescription("helpDescription: " + attributeDefinition.getDescription());
 175  
 
 176  0
             builder.setControl(controlBuilder);
 177  0
             builder.setMaxLength(400);
 178  
 
 179  0
             return builder.build();
 180  
         } else {
 181  0
             return getDataDictionaryRemoteFieldService().buildRemotableFieldFromAttributeDefinition(
 182  
                     attributeDefinition.getComponentName(),
 183  
                     attributeDefinition.getName());
 184  
         }
 185  
     }
 186  
 
 187  
     public DataDictionaryRemoteFieldService getDataDictionaryRemoteFieldService() {
 188  0
         return KRADServiceLocatorWeb.getDataDictionaryRemoteFieldService();
 189  
     }
 190  
 }