Coverage Report - org.kuali.student.lum.kim.KimQualificationHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
KimQualificationHelper
0%
0/92
0%
0/68
7.125
KimQualificationHelper$1
N/A
N/A
7.125
KimQualificationHelper$UniqueMap
0%
0/8
0%
0/6
7.125
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 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  
 //import org.kuali.rice.kew.api.doctype.DocumentType;
 38  
 
 39  
 /**
 40  
  * Class to allow convenience methods to help with qualification verification and translation
 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  
             // below is example of how this could work
 50  
             // translationMap.put("referenceType.clu.proposal", "kuali.proposal.type.course.create");
 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  
                 // abort if type does not want the qualifiers to be checked
 59  0
                 if ( !checkRequiredAttributes ) {
 60  0
                         return;
 61  
                 }
 62  
                 // abort if the list is empty, no attributes need to be checked
 63  0
                 if ( requiredAttributes == null || requiredAttributes.isEmpty() ) {
 64  0
                         return;
 65  
                 }
 66  
                 // if attributes are null or empty, they're all missing
 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  
                                 // if no missing attributes from this list then we have required attributes needed
 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  
                         // first get a valid DocumentDetailDTO object if possible
 119  0
                         String documentNumber = qualification.get(KimConstants.AttributeConstants.DOCUMENT_NUMBER);
 120  0
                         String proposalId = getProposalId(qualification);
 121  0
                         if (StringUtils.isBlank(documentNumber)) {
 122  
                             // if document number is not in qualification try to get it using proposal id qualification
 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  
                                 // document id exists so look up KEW document instance using it
 130  0
                                 docDetail = getWorkflowDocumentService().getDocumentDetail(documentNumber);
 131  
                         }
 132  
                         else {
 133  
                                 // document id does not exist so attempt lookup by Document Type Name and Application ID
 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  
                                                 // could not find Document Type Name in qualification so check for KS Object Type
 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  
                                         // check for a valid Document Type Name
 145  0
                                         if (StringUtils.isNotBlank(documentTypeName)) {
 146  
                                                 // found valid Application ID and Document Type Name so KEW Document instance can be retrieved
 147  
 
 148  
 
 149  0
                         docDetail = getWorkflowDocumentService().getDocumentDetailByAppId(documentTypeName, appId);
 150  
                                         }
 151  
                                         else {
 152  
                                                 // if neither Document Type Name nor KS object type is found then KEW document instance cannot be retrieved
 153  0
                                                 LOG.warn("Could not find valid document type name or KS object type using qualifications: " + qualification);
 154  
                                         }
 155  0
                                 }
 156  
                                 else {
 157  
                                         // if application id is not found then KEW document instance cannot be retrieved
 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  
                         // add document id if necessary
 173  0
                         if (!qualifications.containsKey(KimConstants.AttributeConstants.DOCUMENT_NUMBER)) {
 174  0
                                 qualifications.put(KimConstants.AttributeConstants.DOCUMENT_NUMBER, docDetail.getDocument().getDocumentId());
 175  
                         }
 176  
                         // add KS proposal id if possible
 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  
                         // add KS object id if necessary
 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  
 //                        DocumentType docType = KEWServiceLocator.getDocumentTypeService().getDocumentType(docDetail.getDocTypeId());
 186  0
                         if (docType != null) {
 187  0
                                 String documentTypeName = docType.getName();
 188  
                                 // add document type name if necessary
 189  0
                                 if (!qualifications.containsKey(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME)) {
 190  0
                                         qualifications.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME, documentTypeName);
 191  
                                 }
 192  
                                 // add KS object type code if necessary
 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  
                         // add KS object type code if necessary
 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  
 }