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