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