1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
|
102 | |
|
103 | 0 | builder.setControl(controlBuilder); |
104 | |
|
105 | |
|
106 | 0 | return builder.build(); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
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) { |
122 | 0 | return ((String)o).compareTo((String)o1); |
123 | |
} |
124 | 0 | return compare; |
125 | |
} |
126 | 0 | return 1; |
127 | |
} |
128 | |
} |
129 | |
} |