View Javadoc
1   /*
2    * Copyright 2012 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.document.workflow;
17  
18  import org.kuali.ole.module.purap.PurapConstants;
19  import org.kuali.ole.sys.OLEConstants;
20  import org.kuali.ole.sys.OLEPropertyConstants;
21  import org.kuali.ole.sys.context.SpringContext;
22  import org.kuali.ole.sys.document.workflow.OLEDocumentSearchCustomizer;
23  import org.kuali.rice.kew.api.document.Document;
24  import org.kuali.rice.kew.api.document.DocumentStatusCategory;
25  import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
26  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeString;
27  import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
28  import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
29  import org.kuali.rice.kew.framework.document.search.DocumentSearchResultValue;
30  import org.kuali.rice.kew.framework.document.search.DocumentSearchResultValues;
31  import org.kuali.rice.kim.api.KimConstants;
32  import org.kuali.rice.kim.api.services.IdentityManagementService;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  
35  import java.util.ArrayList;
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  public class OLEPurapDocumentSearchCustomizer extends OLEDocumentSearchCustomizer {
41  
42      /**
43       * @see org.kuali.ole.sys.document.workflow.OLEDocumentSearchCustomizer#customizeResults(org.kuali.rice.kew.api.document.search.DocumentSearchCriteria, java.util.List)
44       */
45      @Override
46      public DocumentSearchResultValues customizeResults(DocumentSearchCriteria documentSearchCriteria, List<DocumentSearchResult> defaultResults) {
47          // since we know we are looking up POs at this time - add the warning about disclosing them
48          GlobalVariables.getMessageMap().putWarning(OLEPropertyConstants.DOCUMENT_NUMBER, PurapConstants.WARNING_PURCHASEORDER_NUMBER_DONT_DISCLOSE);
49  
50          org.kuali.rice.kew.framework.document.search.DocumentSearchResultValues.Builder customResultsBuilder = DocumentSearchResultValues.Builder.create();
51  
52          List<DocumentSearchResultValue.Builder> customResultValueBuilders = new ArrayList<DocumentSearchResultValue.Builder>();
53  
54          boolean isAuthorizedToViewPurapDocId = false;
55          if (defaultResults.size() > 0) {
56              for (DocumentAttribute documentAttribute : defaultResults.get(0).getDocumentAttributes()) {
57                  if (OLEPropertyConstants.PURAP_DOC_ID.equals(documentAttribute.getName())) {
58                      isAuthorizedToViewPurapDocId = isAuthorizedToViewPurapDocId();
59                  }
60              }
61          }
62          for (DocumentSearchResult result : defaultResults) {
63              List<DocumentAttribute.AbstractBuilder<?>> custAttrBuilders = new ArrayList<DocumentAttribute.AbstractBuilder<?>>();
64              Document document = result.getDocument();
65  
66              for (DocumentAttribute documentAttribute : result.getDocumentAttributes()) {
67                  if (OLEPropertyConstants.PURAP_DOC_ID.equals(documentAttribute.getName())) {
68                      if (!isAuthorizedToViewPurapDocId && !document.getStatus().getCategory().equals(DocumentStatusCategory.SUCCESSFUL)) {
69                          DocumentAttributeString.Builder builder = DocumentAttributeString.Builder.create(OLEPropertyConstants.PURAP_DOC_ID);
70                          builder.setValue("********");
71                          custAttrBuilders.add(builder);
72                          break;
73                      }
74                  }
75              }
76              DocumentSearchResultValue.Builder builder = DocumentSearchResultValue.Builder.create(document.getDocumentId());
77              builder.setDocumentAttributes(custAttrBuilders);
78              customResultValueBuilders.add(builder);
79          }
80          customResultsBuilder.setResultValues(customResultValueBuilders);
81  
82          return customResultsBuilder.build();
83      }
84  
85      @Override
86      public boolean isCustomizeResultsEnabled(String documentTypeName) {
87          // do not mask the purapDocumentIdentifier field if the document is not PO or POSP..
88          if (PurapConstants.PurchaseOrderDocTypes.PURCHASE_ORDER_DOCUMENT.equalsIgnoreCase(documentTypeName)
89                  || PurapConstants.PurchaseOrderDocTypes.PURCHASE_ORDER_SPLIT_DOCUMENT.equalsIgnoreCase(documentTypeName)) {
90              return true;
91          }
92          return super.isCustomizeResultsEnabled(documentTypeName);
93      }
94  
95      protected boolean isAuthorizedToViewPurapDocId() {
96          String principalId = GlobalVariables.getUserSession().getPerson().getPrincipalId();
97          String namespaceCode = OLEConstants.CoreModuleNamespaces.KNS;
98          String permissionTemplateName = KimConstants.PermissionTemplateNames.FULL_UNMASK_FIELD;
99  
100         Map<String, String> roleQualifiers = new HashMap<String, String>();
101 
102         Map<String, String> permissionDetails = new HashMap<String, String>();
103         permissionDetails.put(KimConstants.AttributeConstants.COMPONENT_NAME, OLEPropertyConstants.PURCHASE_ORDER_DOCUMENT_SIMPLE_NAME);
104         permissionDetails.put(KimConstants.AttributeConstants.PROPERTY_NAME, OLEPropertyConstants.PURAP_DOC_ID);
105 
106         IdentityManagementService identityManagementService = SpringContext.getBean(IdentityManagementService.class);
107         boolean isAuthorized = identityManagementService.isAuthorizedByTemplateName(principalId, namespaceCode, permissionTemplateName, permissionDetails, roleQualifiers);
108         return isAuthorized;
109     }
110 
111 }