001/*
002 * Copyright 2012 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.module.purap.document.workflow;
017
018import org.kuali.ole.module.purap.PurapConstants;
019import org.kuali.ole.sys.OLEConstants;
020import org.kuali.ole.sys.OLEPropertyConstants;
021import org.kuali.ole.sys.context.SpringContext;
022import org.kuali.ole.sys.document.workflow.OLEDocumentSearchCustomizer;
023import org.kuali.rice.kew.api.document.Document;
024import org.kuali.rice.kew.api.document.DocumentStatusCategory;
025import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
026import org.kuali.rice.kew.api.document.attribute.DocumentAttributeString;
027import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
028import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
029import org.kuali.rice.kew.framework.document.search.DocumentSearchResultValue;
030import org.kuali.rice.kew.framework.document.search.DocumentSearchResultValues;
031import org.kuali.rice.kim.api.KimConstants;
032import org.kuali.rice.kim.api.services.IdentityManagementService;
033import org.kuali.rice.krad.util.GlobalVariables;
034
035import java.util.ArrayList;
036import java.util.HashMap;
037import java.util.List;
038import java.util.Map;
039
040public class OLEPurapDocumentSearchCustomizer extends OLEDocumentSearchCustomizer {
041
042    /**
043     * @see org.kuali.ole.sys.document.workflow.OLEDocumentSearchCustomizer#customizeResults(org.kuali.rice.kew.api.document.search.DocumentSearchCriteria, java.util.List)
044     */
045    @Override
046    public DocumentSearchResultValues customizeResults(DocumentSearchCriteria documentSearchCriteria, List<DocumentSearchResult> defaultResults) {
047        // since we know we are looking up POs at this time - add the warning about disclosing them
048        GlobalVariables.getMessageMap().putWarning(OLEPropertyConstants.DOCUMENT_NUMBER, PurapConstants.WARNING_PURCHASEORDER_NUMBER_DONT_DISCLOSE);
049
050        org.kuali.rice.kew.framework.document.search.DocumentSearchResultValues.Builder customResultsBuilder = DocumentSearchResultValues.Builder.create();
051
052        List<DocumentSearchResultValue.Builder> customResultValueBuilders = new ArrayList<DocumentSearchResultValue.Builder>();
053
054        boolean isAuthorizedToViewPurapDocId = false;
055        if (defaultResults.size() > 0) {
056            for (DocumentAttribute documentAttribute : defaultResults.get(0).getDocumentAttributes()) {
057                if (OLEPropertyConstants.PURAP_DOC_ID.equals(documentAttribute.getName())) {
058                    isAuthorizedToViewPurapDocId = isAuthorizedToViewPurapDocId();
059                }
060            }
061        }
062        for (DocumentSearchResult result : defaultResults) {
063            List<DocumentAttribute.AbstractBuilder<?>> custAttrBuilders = new ArrayList<DocumentAttribute.AbstractBuilder<?>>();
064            Document document = result.getDocument();
065
066            for (DocumentAttribute documentAttribute : result.getDocumentAttributes()) {
067                if (OLEPropertyConstants.PURAP_DOC_ID.equals(documentAttribute.getName())) {
068                    if (!isAuthorizedToViewPurapDocId && !document.getStatus().getCategory().equals(DocumentStatusCategory.SUCCESSFUL)) {
069                        DocumentAttributeString.Builder builder = DocumentAttributeString.Builder.create(OLEPropertyConstants.PURAP_DOC_ID);
070                        builder.setValue("********");
071                        custAttrBuilders.add(builder);
072                        break;
073                    }
074                }
075            }
076            DocumentSearchResultValue.Builder builder = DocumentSearchResultValue.Builder.create(document.getDocumentId());
077            builder.setDocumentAttributes(custAttrBuilders);
078            customResultValueBuilders.add(builder);
079        }
080        customResultsBuilder.setResultValues(customResultValueBuilders);
081
082        return customResultsBuilder.build();
083    }
084
085    @Override
086    public boolean isCustomizeResultsEnabled(String documentTypeName) {
087        // do not mask the purapDocumentIdentifier field if the document is not PO or POSP..
088        if (PurapConstants.PurchaseOrderDocTypes.PURCHASE_ORDER_DOCUMENT.equalsIgnoreCase(documentTypeName)
089                || PurapConstants.PurchaseOrderDocTypes.PURCHASE_ORDER_SPLIT_DOCUMENT.equalsIgnoreCase(documentTypeName)) {
090            return true;
091        }
092        return super.isCustomizeResultsEnabled(documentTypeName);
093    }
094
095    protected boolean isAuthorizedToViewPurapDocId() {
096        String principalId = GlobalVariables.getUserSession().getPerson().getPrincipalId();
097        String namespaceCode = OLEConstants.CoreModuleNamespaces.KNS;
098        String permissionTemplateName = KimConstants.PermissionTemplateNames.FULL_UNMASK_FIELD;
099
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}