1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.kuali.student.lum.kim; |
20 | |
|
21 | |
import java.util.ArrayList; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.HashSet; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
import java.util.Set; |
28 | |
|
29 | |
import javax.xml.namespace.QName; |
30 | |
|
31 | |
import org.apache.commons.lang.StringUtils; |
32 | |
import org.apache.log4j.Logger; |
33 | |
import org.kuali.rice.core.resourceloader.GlobalResourceLoader; |
34 | |
import org.kuali.rice.kew.dto.DocumentDetailDTO; |
35 | |
import org.kuali.rice.kew.dto.DocumentTypeDTO; |
36 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
37 | |
import org.kuali.rice.kew.service.WorkflowUtility; |
38 | |
import org.kuali.rice.kim.bo.impl.KimAttributes; |
39 | |
import org.kuali.rice.kim.bo.types.dto.AttributeSet; |
40 | |
import org.kuali.rice.kim.service.support.impl.KimTypeAttributeValidationException; |
41 | |
import org.kuali.student.common.rice.StudentIdentityConstants; |
42 | |
import org.kuali.student.core.proposal.dto.ProposalInfo; |
43 | |
import org.kuali.student.core.proposal.service.ProposalService; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | public class KimQualificationHelper { |
50 | 0 | protected static final Logger LOG = Logger.getLogger(KimQualificationHelper.class); |
51 | |
|
52 | 0 | private static UniqueMap translationMap = new UniqueMap(); |
53 | |
|
54 | |
{ |
55 | |
|
56 | |
|
57 | |
} |
58 | |
|
59 | |
protected static WorkflowUtility getWorkflowUtility() { |
60 | 0 | return KEWServiceLocator.getWorkflowUtilityService(); |
61 | |
} |
62 | |
|
63 | |
public static void validateRequiredAttributesAgainstReceived(Set<List<String>> requiredAttributes, AttributeSet receivedAttributes, boolean checkRequiredAttributes, String commaSeparatorString) { |
64 | |
|
65 | 0 | if ( !checkRequiredAttributes ) { |
66 | 0 | return; |
67 | |
} |
68 | |
|
69 | 0 | if ( requiredAttributes == null || requiredAttributes.isEmpty() ) { |
70 | 0 | return; |
71 | |
} |
72 | |
|
73 | 0 | if ( receivedAttributes == null || receivedAttributes.isEmpty() ) { |
74 | 0 | return; |
75 | |
} |
76 | |
|
77 | 0 | Set<List<String>> totalMissingAttributes = new HashSet<List<String>>(); |
78 | 0 | for (List<String> currentReqAttributes : requiredAttributes) { |
79 | 0 | List<String> missingAttributes = new ArrayList<String>(); |
80 | 0 | for( String requiredAttribute : currentReqAttributes ) { |
81 | 0 | if( !receivedAttributes.containsKey(requiredAttribute) ) { |
82 | 0 | missingAttributes.add(requiredAttribute); |
83 | |
} |
84 | |
} |
85 | 0 | if (missingAttributes.isEmpty()) { |
86 | |
|
87 | 0 | return; |
88 | |
} |
89 | 0 | totalMissingAttributes.add(missingAttributes); |
90 | 0 | } |
91 | |
|
92 | 0 | int i = 1; |
93 | 0 | StringBuffer errorMessage = new StringBuffer("Missing Required Attributes from lists - "); |
94 | 0 | for (List<String> missingAttributes : totalMissingAttributes) { |
95 | 0 | if(missingAttributes.size()>0) { |
96 | 0 | errorMessage.append("List " + i + ": ("); |
97 | 0 | i++; |
98 | 0 | Iterator<String> attribIter = missingAttributes.iterator(); |
99 | 0 | while ( attribIter.hasNext() ) { |
100 | 0 | errorMessage.append( attribIter.next() ); |
101 | 0 | if( attribIter.hasNext() ) { |
102 | 0 | errorMessage.append( commaSeparatorString ); |
103 | |
} |
104 | |
} |
105 | 0 | errorMessage.append(")"); |
106 | 0 | } |
107 | |
} |
108 | 0 | LOG.info("Found missing attributes: " + errorMessage.toString()); |
109 | 0 | throw new KimTypeAttributeValidationException(errorMessage.toString()); |
110 | |
} |
111 | |
|
112 | |
protected static String getProposalId(AttributeSet qualification) { |
113 | 0 | for (String proposalReferenceType : StudentIdentityConstants.QUALIFICATION_PROPOSAL_ID_REF_TYPES) { |
114 | 0 | if (qualification.containsKey(proposalReferenceType)) { |
115 | 0 | return qualification.get(proposalReferenceType); |
116 | |
} |
117 | |
} |
118 | 0 | return null; |
119 | |
} |
120 | |
|
121 | |
public static AttributeSet translateInputAttributeSet(AttributeSet qualification) { |
122 | |
try { |
123 | 0 | DocumentDetailDTO docDetail = null; |
124 | |
|
125 | 0 | String documentNumber = qualification.get(KimAttributes.DOCUMENT_NUMBER); |
126 | 0 | String proposalId = getProposalId(qualification); |
127 | 0 | if (StringUtils.isBlank(documentNumber)) { |
128 | |
|
129 | 0 | if (StringUtils.isNotBlank(proposalId)) { |
130 | 0 | ProposalInfo propInfo = getProposalService().getProposal(proposalId); |
131 | 0 | documentNumber = propInfo.getWorkflowId(); |
132 | |
} |
133 | |
} |
134 | 0 | if (StringUtils.isNotBlank(documentNumber)) { |
135 | |
|
136 | 0 | docDetail = getWorkflowUtility().getDocumentDetail(Long.valueOf(documentNumber)); |
137 | |
} |
138 | |
else { |
139 | |
|
140 | 0 | String appId = qualification.get( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID ); |
141 | 0 | if (StringUtils.isNotBlank(appId)) { |
142 | 0 | String documentTypeName = qualification.get( KimAttributes.DOCUMENT_TYPE_NAME ); |
143 | 0 | if (StringUtils.isBlank(documentTypeName)) { |
144 | |
|
145 | 0 | String ksObjectType = qualification.get( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_TYPE ); |
146 | 0 | if (StringUtils.isNotBlank(ksObjectType)) { |
147 | 0 | documentTypeName = translationMap.get(ksObjectType); |
148 | |
} |
149 | |
} |
150 | |
|
151 | 0 | if (StringUtils.isNotBlank(documentTypeName)) { |
152 | |
|
153 | 0 | docDetail = getWorkflowUtility().getDocumentDetailFromAppId(documentTypeName, appId); |
154 | |
} |
155 | |
else { |
156 | |
|
157 | 0 | LOG.warn("Could not find valid document type name or KS object type using qualifications: " + qualification); |
158 | |
} |
159 | 0 | } |
160 | |
else { |
161 | |
|
162 | 0 | LOG.warn("Could not find valid document id or application id using qualifications: " + qualification); |
163 | |
} |
164 | |
} |
165 | 0 | translateQualifications(docDetail, proposalId, qualification); |
166 | 0 | return qualification; |
167 | |
} |
168 | 0 | catch (Exception e) { |
169 | 0 | LOG.error(e.getLocalizedMessage(), e); |
170 | 0 | throw new RuntimeException(e); |
171 | |
} |
172 | |
} |
173 | |
|
174 | |
protected static void translateQualifications(DocumentDetailDTO docDetail, String proposalId, AttributeSet qualifications) { |
175 | 0 | if (docDetail != null) { |
176 | |
|
177 | 0 | if (!qualifications.containsKey(KimAttributes.DOCUMENT_NUMBER)) { |
178 | 0 | qualifications.put(KimAttributes.DOCUMENT_NUMBER, docDetail.getRouteHeaderId().toString()); |
179 | |
} |
180 | |
|
181 | 0 | if (!qualifications.containsKey(StudentIdentityConstants.QUALIFICATION_KS_PROPOSAL_ID) && StringUtils.isNotBlank(proposalId)) { |
182 | 0 | qualifications.put(StudentIdentityConstants.QUALIFICATION_KS_PROPOSAL_ID, proposalId); |
183 | |
} |
184 | |
|
185 | 0 | if (!qualifications.containsKey(StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID)) { |
186 | 0 | qualifications.put(StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID, docDetail.getAppDocId()); |
187 | |
} |
188 | 0 | DocumentTypeDTO docType = KEWServiceLocator.getDocumentTypeService().getDocumentTypeVO(docDetail.getDocTypeId()); |
189 | 0 | if (docType != null) { |
190 | 0 | String documentTypeName = docType.getName(); |
191 | |
|
192 | 0 | if (!qualifications.containsKey(KimAttributes.DOCUMENT_TYPE_NAME)) { |
193 | 0 | qualifications.put(KimAttributes.DOCUMENT_TYPE_NAME, documentTypeName); |
194 | |
} |
195 | |
|
196 | 0 | if (!qualifications.containsKey(StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_TYPE)) { |
197 | 0 | qualifications.put(StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_TYPE, translationMap.getKeyForValue(documentTypeName)); |
198 | |
} |
199 | 0 | } |
200 | |
else { |
201 | 0 | String errorMsg = "Could not find valid KEW document type for document id " + docDetail.getRouteHeaderId(); |
202 | 0 | LOG.error(errorMsg); |
203 | 0 | throw new RuntimeException(errorMsg); |
204 | |
} |
205 | 0 | } |
206 | |
else { |
207 | 0 | LOG.warn("Could not find KEW document instance for qualifications: " + qualifications); |
208 | |
|
209 | 0 | if ((!qualifications.containsKey(StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_TYPE)) && |
210 | |
qualifications.containsKey(KimAttributes.DOCUMENT_TYPE_NAME)) { |
211 | 0 | qualifications.put(StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_TYPE, translationMap.getKeyForValue(qualifications.get(KimAttributes.DOCUMENT_TYPE_NAME))); |
212 | |
} |
213 | 0 | else if ((!qualifications.containsKey(KimAttributes.DOCUMENT_TYPE_NAME)) && |
214 | |
qualifications.containsKey(StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_TYPE)) { |
215 | 0 | qualifications.put(KimAttributes.DOCUMENT_TYPE_NAME, translationMap.get(qualifications.get(StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_TYPE))); |
216 | |
} |
217 | |
} |
218 | 0 | } |
219 | |
|
220 | |
protected static ProposalService getProposalService() { |
221 | 0 | return (ProposalService) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/proposal","ProposalService")); |
222 | |
} |
223 | |
|
224 | 0 | private static class UniqueMap extends HashMap<String,String> { |
225 | |
|
226 | |
private static final long serialVersionUID = 1L; |
227 | |
|
228 | |
@Override |
229 | |
public String put(String key, String value) { |
230 | 0 | if (this.containsValue(value)) { |
231 | 0 | throw new UnsupportedOperationException("Map already contains an entry with value: " + value); |
232 | |
} |
233 | 0 | return super.put(key, value); |
234 | |
} |
235 | |
|
236 | |
public String getKeyForValue(String value) { |
237 | 0 | for (Map.Entry<String, String> mapEntry : this.entrySet()) { |
238 | 0 | if (StringUtils.equals(value, mapEntry.getValue())) { |
239 | 0 | return mapEntry.getKey(); |
240 | |
} |
241 | |
} |
242 | 0 | return null; |
243 | |
} |
244 | |
} |
245 | |
|
246 | |
} |