1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.document.rule;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.core.api.uif.RemotableAttributeError;
21 import org.kuali.rice.kim.api.KimConstants;
22 import org.kuali.rice.kim.api.type.KimTypeAttribute;
23 import org.kuali.rice.kim.bo.ui.KimDocumentAttributeDataBusinessObjectBase;
24 import org.kuali.rice.kim.impl.common.attribute.KimAttributeBo;
25 import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
26 import org.kuali.rice.krad.service.BusinessObjectService;
27 import org.kuali.rice.krad.service.KRADServiceLocator;
28 import org.kuali.rice.krad.util.GlobalVariables;
29 import org.kuali.rice.krad.util.KRADConstants;
30 import org.kuali.rice.krad.util.KRADPropertyConstants;
31
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37
38
39
40
41
42
43 public class AttributeValidationHelper {
44 private static final Logger LOG = Logger.getLogger(AttributeValidationHelper.class);
45
46 protected BusinessObjectService businessObjectService;
47 protected Map<String,KimAttributeBo> attributeDefinitionMap = new HashMap<String,KimAttributeBo>();
48
49 protected KimAttributeBo getAttributeDefinition( String id ) {
50 KimAttributeBo attributeImpl = attributeDefinitionMap.get( id );
51
52 if ( attributeImpl == null ) {
53 Map<String,String> criteria = new HashMap<String,String>();
54 criteria.put( KimConstants.PrimaryKeyConstants.KIM_ATTRIBUTE_ID, id );
55 attributeImpl = (KimAttributeBo)getBusinessObjectService().findByPrimaryKey( KimAttributeBo.class, criteria );
56 attributeDefinitionMap.put( id, attributeImpl );
57 }
58 return attributeImpl;
59 }
60
61 public Map<String, String> convertAttributesToMap(List<? extends KimAttributeDataBo> attributes) {
62 Map<String, String> m = new HashMap<String, String>();
63 for(KimAttributeDataBo data: attributes) {
64 KimAttributeBo attrib = getAttributeDefinition(data.getKimAttributeId());
65 if(attrib != null){
66 m.put(attrib.getAttributeName(), data.getAttributeValue());
67 } else {
68 LOG.error("Unable to get attribute name for ID:" + data.getKimAttributeId());
69 }
70 }
71 return m;
72 }
73
74 public Map<String, String> convertQualifiersToMap( List<? extends KimDocumentAttributeDataBusinessObjectBase> qualifiers ) {
75 Map<String, String> m = new HashMap<String, String>();
76 for ( KimDocumentAttributeDataBusinessObjectBase data : qualifiers ) {
77 KimAttributeBo attrib = getAttributeDefinition( data.getKimAttrDefnId() );
78 if ( attrib != null ) {
79 m.put( attrib.getAttributeName(), data.getAttrVal() );
80 } else {
81 LOG.error("Unable to get attribute name for ID:" + data.getKimAttrDefnId() );
82 }
83 }
84 return m;
85 }
86
87 public Map<String, String> getBlankValueQualifiersMap(List<KimTypeAttribute> attributes) {
88 Map<String, String> m = new HashMap<String, String>();
89 for(KimTypeAttribute attribute: attributes){
90 KimAttributeBo attrib = getAttributeDefinition(attribute.getId());
91 if ( attrib != null ) {
92 m.put( attrib.getAttributeName(), "" );
93 } else {
94 LOG.error("Unable to get attribute name for ID:" + attribute.getId());
95 }
96 }
97 return m;
98 }
99
100 public Map<String, String> convertQualifiersToAttrIdxMap( List<? extends KimDocumentAttributeDataBusinessObjectBase> qualifiers ) {
101 Map<String, String> m = new HashMap<String, String>();
102 int i = 0;
103 for ( KimDocumentAttributeDataBusinessObjectBase data : qualifiers ) {
104 KimAttributeBo attrib = getAttributeDefinition( data.getKimAttrDefnId() );
105 if ( attrib != null ) {
106 m.put( attrib.getAttributeName(), Integer.toString(i) );
107 } else {
108 LOG.error("Unable to get attribute name for ID:" + data.getKimAttrDefnId() );
109 }
110 i++;
111 }
112 return m;
113 }
114
115 public BusinessObjectService getBusinessObjectService() {
116 if(businessObjectService == null){
117 businessObjectService = KRADServiceLocator.getBusinessObjectService();
118 }
119 return businessObjectService;
120 }
121
122 public void moveValidationErrorsToErrorMap(List<RemotableAttributeError> validationErrors) {
123
124
125 for ( RemotableAttributeError error : validationErrors) {
126 for (String errMsg : error.getErrors()) {
127 String[] splitMsg = StringUtils.split(errMsg, ":");
128 GlobalVariables.getMessageMap().putError( error.getAttributeName(), splitMsg[0], splitMsg.length > 1 ? StringUtils.split(splitMsg[1], ";") : new String[] {} );
129 }
130 }
131 }
132
133 public List<RemotableAttributeError> convertErrorsForMappedFields(String errorPath, List<RemotableAttributeError> localErrors) {
134 List<RemotableAttributeError> errors = new ArrayList<RemotableAttributeError>();
135 if (errorPath == null) {
136 errorPath = KRADConstants.EMPTY_STRING;
137 }
138 else if (StringUtils.isNotEmpty(errorPath)) {
139 errorPath = errorPath + ".";
140 }
141 for ( RemotableAttributeError error : localErrors) {
142 Map<String,String> criteria = new HashMap<String,String>();
143 criteria.put(KRADPropertyConstants.ATTRIBUTE_NAME, error.getAttributeName());
144 KimAttributeBo attribute = getBusinessObjectService().findByPrimaryKey(KimAttributeBo.class, criteria);
145 String attributeDefnId = attribute==null?"":attribute.getId();
146 errors.add(RemotableAttributeError.Builder.create(errorPath+"qualifier("+attributeDefnId+").attrVal", error.getErrors()).build());
147 }
148 return errors;
149 }
150
151 public List<RemotableAttributeError> convertErrors(String errorPath, Map<String, String> attrIdxMap, List<RemotableAttributeError> localErrors) {
152 List<RemotableAttributeError> errors = new ArrayList<RemotableAttributeError>();
153 if (errorPath == null) {
154 errorPath = KRADConstants.EMPTY_STRING;
155 }
156 else if (StringUtils.isNotEmpty(errorPath)) {
157 errorPath = errorPath + ".";
158 }
159 for ( RemotableAttributeError error : localErrors ) {
160 for (String errMsg : error.getErrors()) {
161 errors.add(RemotableAttributeError.Builder.create(errorPath+"qualifiers["+attrIdxMap.get(error.getAttributeName())+"].attrVal", errMsg).build());
162 }
163 }
164 return errors;
165 }
166 }