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