| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | 11 | public abstract class KrmsTypeServiceBase implements RemotableAttributeOwner { |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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) { |
| 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 | |
|
| 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 | |
|
| 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 | 3 | 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 | 3 | return validateAttributes(krmsTypeId, newAttributes); |
| 150 | |
} |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 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 | |
|
| 174 | |
|
| 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 | |
} |