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 private static final String DOCUMENT_PROPERTY_PREFIX = KRADConstants.DOCUMENT_PROPERTY_NAME + ".";
47
48 protected BusinessObjectService businessObjectService;
49 protected Map<String,KimAttributeBo> attributeDefinitionMap = new HashMap<String,KimAttributeBo>();
50
51 protected KimAttributeBo getAttributeDefinition( String id ) {
52 KimAttributeBo attributeImpl = attributeDefinitionMap.get( id );
53
54 if ( attributeImpl == null ) {
55 Map<String,String> criteria = new HashMap<String,String>();
56 criteria.put( KimConstants.PrimaryKeyConstants.KIM_ATTRIBUTE_ID, id );
57 attributeImpl = (KimAttributeBo)getBusinessObjectService().findByPrimaryKey( KimAttributeBo.class, criteria );
58 attributeDefinitionMap.put( id, attributeImpl );
59 }
60 return attributeImpl;
61 }
62
63 public Map<String, String> convertAttributesToMap(List<? extends KimAttributeDataBo> attributes) {
64 Map<String, String> m = new HashMap<String, String>();
65 for(KimAttributeDataBo data: attributes) {
66 KimAttributeBo attrib = getAttributeDefinition(data.getKimAttributeId());
67 if(attrib != null){
68 m.put(attrib.getAttributeName(), data.getAttributeValue());
69 } else {
70 LOG.error("Unable to get attribute name for ID:" + data.getKimAttributeId());
71 }
72 }
73 return m;
74 }
75
76 public Map<String, String> convertQualifiersToMap( List<? extends KimDocumentAttributeDataBusinessObjectBase> qualifiers ) {
77 Map<String, String> m = new HashMap<String, String>();
78 for ( KimDocumentAttributeDataBusinessObjectBase data : qualifiers ) {
79 KimAttributeBo attrib = getAttributeDefinition( data.getKimAttrDefnId() );
80 if ( attrib != null ) {
81 m.put( attrib.getAttributeName(), data.getAttrVal() );
82 } else {
83 LOG.error("Unable to get attribute name for ID:" + data.getKimAttrDefnId() );
84 }
85 }
86 return m;
87 }
88
89 public Map<String, String> getBlankValueQualifiersMap(List<KimTypeAttribute> attributes) {
90 Map<String, String> m = new HashMap<String, String>();
91 for(KimTypeAttribute attribute: attributes){
92 KimAttributeBo attrib = getAttributeDefinition(attribute.getKimAttribute().getId());
93 if ( attrib != null ) {
94 m.put( attrib.getAttributeName(), "" );
95 } else {
96 LOG.error("Unable to get attribute name for ID:" + attribute.getId());
97 }
98 }
99 return m;
100 }
101
102 public Map<String, String> convertQualifiersToAttrIdxMap( List<? extends KimDocumentAttributeDataBusinessObjectBase> qualifiers ) {
103 Map<String, String> m = new HashMap<String, String>();
104 int i = 0;
105 for ( KimDocumentAttributeDataBusinessObjectBase data : qualifiers ) {
106 KimAttributeBo attrib = getAttributeDefinition( data.getKimAttrDefnId() );
107 if ( attrib != null ) {
108 m.put( attrib.getAttributeName(), Integer.toString(i) );
109 } else {
110 LOG.error("Unable to get attribute name for ID:" + data.getKimAttrDefnId() );
111 }
112 i++;
113 }
114 return m;
115 }
116
117 public BusinessObjectService getBusinessObjectService() {
118 if(businessObjectService == null){
119 businessObjectService = KRADServiceLocator.getBusinessObjectService();
120 }
121 return businessObjectService;
122 }
123
124 public void moveValidationErrorsToErrorMap(List<RemotableAttributeError> validationErrors) {
125
126 for ( RemotableAttributeError error : validationErrors) {
127 for (String errMsg : error.getErrors()) {
128 String[] splitMsg = StringUtils.split(errMsg, ":");
129
130
131 if (error.getAttributeName().startsWith(DOCUMENT_PROPERTY_PREFIX)) {
132 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath( error.getAttributeName(), splitMsg[0], splitMsg.length > 1 ? StringUtils.split(splitMsg[1], ";") : new String[] {} );
133 } else {
134 GlobalVariables.getMessageMap().putError( error.getAttributeName(), splitMsg[0], splitMsg.length > 1 ? StringUtils.split(splitMsg[1], ";") : new String[] {} );
135 }
136 }
137 }
138 }
139
140 public List<RemotableAttributeError> convertErrorsForMappedFields(String errorPath, List<RemotableAttributeError> localErrors) {
141 List<RemotableAttributeError> errors = new ArrayList<RemotableAttributeError>();
142 if (errorPath == null) {
143 errorPath = KRADConstants.EMPTY_STRING;
144 }
145 else if (StringUtils.isNotEmpty(errorPath)) {
146 errorPath = errorPath + ".";
147 }
148 for ( RemotableAttributeError error : localErrors) {
149 Map<String,String> criteria = new HashMap<String,String>();
150 criteria.put(KRADPropertyConstants.ATTRIBUTE_NAME, error.getAttributeName());
151 KimAttributeBo attribute = getBusinessObjectService().findByPrimaryKey(KimAttributeBo.class, criteria);
152 String attributeDefnId = attribute==null?"":attribute.getId();
153 errors.add(RemotableAttributeError.Builder.create(errorPath+"qualifier("+attributeDefnId+").attrVal", error.getErrors()).build());
154 }
155 return errors;
156 }
157
158 public List<RemotableAttributeError> convertErrors(String errorPath, Map<String, String> attrIdxMap, List<RemotableAttributeError> localErrors) {
159 List<RemotableAttributeError> errors = new ArrayList<RemotableAttributeError>();
160 if (errorPath == null) {
161 errorPath = KRADConstants.EMPTY_STRING;
162 }
163 else if (StringUtils.isNotEmpty(errorPath)) {
164 errorPath = errorPath + ".";
165 }
166 for ( RemotableAttributeError error : localErrors ) {
167 for (String errMsg : error.getErrors()) {
168 errors.add(RemotableAttributeError.Builder.create(errorPath+"qualifiers["+attrIdxMap.get(error.getAttributeName())+"].attrVal", errMsg).build());
169 }
170 }
171 return errors;
172 }
173 }