Coverage Report - org.kuali.rice.krms.impl.validation.RadioButtonTypeServiceUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
RadioButtonTypeServiceUtil
0%
0/34
0%
0/8
3.75
RadioButtonTypeServiceUtil$LabelSequenceComparator
0%
0/10
0%
0/6
3.75
 
 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.validation;
 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.RemotableAttributeField;
 22  
 import org.kuali.rice.core.api.uif.RemotableRadioButtonGroup;
 23  
 import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
 24  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute;
 25  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
 26  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
 27  
 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
 28  
 
 29  
 import javax.jws.WebParam;
 30  
 import java.util.ArrayList;
 31  
 import java.util.Comparator;
 32  
 import java.util.HashMap;
 33  
 import java.util.List;
 34  
 import java.util.Map;
 35  
 import java.util.TreeMap;
 36  
 
 37  
 /**
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  */
 40  0
 public class RadioButtonTypeServiceUtil {
 41  
 
 42  
     public List<RemotableAttributeField> getAttributeFields(@WebParam(name = "krmsTypeId") String krmsTypeId) throws RiceIllegalArgumentException {
 43  
 
 44  0
         if (StringUtils.isBlank(krmsTypeId)) {
 45  0
             throw new RiceIllegalArgumentException("krmsTypeId must be non-null and non-blank");
 46  
         }
 47  
 
 48  0
         List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
 49  
 
 50  
         // keep track of how to sort these
 51  0
         final Map<String, Integer> sortCodeMap = new HashMap<String, Integer>();
 52  
 
 53  0
         KrmsTypeDefinition krmsType =
 54  
                 KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().getTypeById(krmsTypeId);
 55  
 
 56  0
         if (krmsType == null) {
 57  0
             throw new RiceIllegalArgumentException("krmsTypeId must be a valid id of a KRMS type");
 58  
         } else {
 59  
             // translate attributes
 60  
 
 61  0
             List<KrmsTypeAttribute> typeAttributes = krmsType.getAttributes();
 62  0
             Map<String, Integer> attribDefIdSequenceNumbers = new TreeMap<String, Integer>();
 63  0
             Map<String, String> unsortedIdLables = new TreeMap<String, String>();
 64  0
             if (!CollectionUtils.isEmpty(typeAttributes)) {
 65  
                 // translate the attribute and store the sort code in our map
 66  0
                 for (KrmsTypeAttribute typeAttribute : typeAttributes) {
 67  
 
 68  0
                     KrmsTypeRepositoryService typeRepositoryService = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService();
 69  
 
 70  0
                     KrmsAttributeDefinition attributeDefinition =
 71  
                             typeRepositoryService.getAttributeDefinitionById(typeAttribute.getAttributeDefinitionId());
 72  
 
 73  0
                     attribDefIdSequenceNumbers.put(attributeDefinition.getId(), typeAttribute.getSequenceNumber());
 74  0
                     unsortedIdLables.put(attributeDefinition.getId(), attributeDefinition.getLabel());
 75  0
                 }
 76  
 
 77  0
                 Map<String, String> sortedKeyLabelMap = new TreeMap<String, String>(new LabelSequenceComparator(attribDefIdSequenceNumbers));
 78  0
                 sortedKeyLabelMap.putAll(unsortedIdLables);
 79  
 
 80  0
                 RemotableAttributeField attributeField = translateTypeAttribute(krmsType, sortedKeyLabelMap);
 81  0
                 results.add(attributeField);
 82  
             }
 83  
         }
 84  0
         return results;
 85  
     }
 86  
 
 87  
     private RemotableAttributeField translateTypeAttribute(KrmsTypeDefinition krmsType,
 88  
             Map<String, String> keyLabels) {
 89  
 
 90  0
         RemotableAttributeField.Builder builder = RemotableAttributeField.Builder.create(krmsType.getName());
 91  
 
 92  0
         RemotableRadioButtonGroup.Builder controlBuilder = RemotableRadioButtonGroup.Builder.create(keyLabels);
 93  
 
 94  0
         builder.setLongLabel(krmsType.getName());
 95  0
         builder.setName(krmsType.getName());
 96  0
         builder.setRequired(true);
 97  0
         List<String> defaultValue = new ArrayList<String>();
 98  0
         defaultValue.add(keyLabels.get(keyLabels.keySet().toArray()[0]));
 99  0
         builder.setDefaultValues(defaultValue);
 100  
 
 101  
 //            builder.setHelpSummary("helpSummary: " + krmsType.getDescription());
 102  
 //            builder.setHelpDescription("helpDescription: " + krmsType.getDescription());
 103  0
         builder.setControl(controlBuilder);
 104  
 //            builder.setMaxLength(400);
 105  
 
 106  0
         return builder.build();
 107  
     }
 108  
 
 109  
     /**
 110  
      * Sort the keys by their sequence number, if the sequence numbers are the same then fall back to the regular natural key order.
 111  
      */
 112  0
     class LabelSequenceComparator implements Comparator {
 113  0
         Map<String, Integer> keySequences = new TreeMap<String, Integer>();
 114  0
         public LabelSequenceComparator(Map<String, Integer> labelSequence) {
 115  0
             this.keySequences.putAll(labelSequence);
 116  0
         }
 117  
         @Override
 118  
         public int compare(Object o, Object o1) {
 119  0
             if (keySequences.get(o) != null && keySequences.get(o1) != null) {
 120  0
                 int compare = keySequences.get(o).compareTo(keySequences.get(o1));
 121  0
                 if (compare == 0) { // if the sequence number is the same, fall back to key natural order
 122  0
                     return ((String)o).compareTo((String)o1);
 123  
                 }
 124  0
                 return compare;
 125  
             }
 126  0
             return 1; // don't throw anything away (ie return 0)
 127  
         }
 128  
     }
 129  
 }