/*
 * Copyright 2013 The Kuali Foundation.
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.kfs.module.tem.document.authorization;

import java.util.HashMap;
import java.util.Map;

import org.kuali.kfs.module.tem.identity.TemKimAttributes;
import org.kuali.rice.kew.api.KewApiConstants;
import org.kuali.rice.kew.doctype.bo.DocumentType;
import org.kuali.rice.kew.doctype.service.impl.KimDocumentTypeAuthorizer;
import org.kuali.rice.krad.util.KRADConstants;

/**
 *
 */
public class TravelWorkflowDocumentAuthorizer extends KimDocumentTypeAuthorizer {

    @Override
    public boolean canInitiate(String principalId, DocumentType documentType) {
        validatePrincipalId(principalId);
        validateDocumentType(documentType);

        Map<String, String> permissionDetails = buildDocumentTypePermissionDetails(documentType, null, null, null);
        Map<String, String> qualificationDetails = new HashMap<String, String>();
        qualificationDetails.put(TemKimAttributes.PROFILE_PRINCIPAL_ID, principalId);

        if (useKimPermission(KRADConstants.KUALI_RICE_SYSTEM_NAMESPACE, KewApiConstants.INITIATE_PERMISSION, permissionDetails, true)) {
            return getPermissionService().isAuthorizedByTemplate(principalId, KRADConstants.KUALI_RICE_SYSTEM_NAMESPACE,
                    KewApiConstants.INITIATE_PERMISSION, permissionDetails, qualificationDetails);
        }
        return true;
    }

}

@@ -948,16 +948,80 @@
 orizer;%0A
+import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;%0A
 import o
@@ -1059,16 +1059,16 @@
 stants;%0A
-
 %0A/**%0A *%0A
@@ -1506,111 +1506,26 @@
 s = 
-new HashMap%3CString, String%3E();%0A        qualificationDetails.put(TemKimAttributes.PROFILE_PRINCIPAL_ID, 
+getRoleQualifiers(
 prin
@@ -1930,10 +1930,2031 @@
 %0A    %7D%0A%0A
+    @Override%0A    public boolean canRoute(String principalId, DocumentRouteHeaderValue document) %7B%0A        validatePrincipalId(principalId);%0A        validateDocument(document);%0A        String documentId = document.getDocumentId();%0A        DocumentType documentType = document.getDocumentType();%0A        String documentStatus = document.getDocRouteStatus();%0A        String initiatorPrincipalId = document.getInitiatorWorkflowId();%0A        validateDocumentType(documentType);%0A        validateDocumentStatus(documentStatus);%0A        validatePrincipalId(initiatorPrincipalId);%0A%0A        if (!documentType.isPolicyDefined(org.kuali.rice.kew.api.doctype.DocumentTypePolicy.INITIATOR_MUST_ROUTE)) %7B%0A            Map%3CString, String%3E permissionDetails = buildDocumentTypePermissionDetails(documentType, documentStatus, null, null);%0A            Map%3CString, String%3E roleQualifiers = buildDocumentRoleQualifiers(document, permissionDetails.get(KewApiConstants.ROUTE_NODE_NAME_DETAIL));%0A            roleQualifiers.put(TemKimAttributes.PROFILE_PRINCIPAL_ID, principalId);%0A%0A            if (useKimPermission(KewApiConstants.KEW_NAMESPACE, KewApiConstants.ROUTE_PERMISSION, permissionDetails, true)) %7B%0A                return getPermissionService().isAuthorizedByTemplate(principalId, KewApiConstants.KEW_NAMESPACE,%0A                        KewApiConstants.ROUTE_PERMISSION, permissionDetails, roleQualifiers);%0A            %7D%0A        %7D%0A%0A        if (documentType.getInitiatorMustRoutePolicy().getPolicyValue()) %7B%0A            return super.canRoute(principalId, document);%0A        %7D%0A        return true;%0A    %7D%0A%0A    /**%0A     * Creates a role qualifiers map that will work with the KFS-TEM TEM Profile role%0A     *%0A     * @param principalId%0A     * @return%0A     */%0A    protected Map%3CString, String%3E getRoleQualifiers(String principalId) %7B%0A        Map%3CString, String%3E qualificationDetails = new HashMap%3CString, String%3E();%0A        qualificationDetails.put(TemKimAttributes.PROFILE_PRINCIPAL_ID, principalId);%0A        return qualificationDetails;%0A    %7D%0A
 %7D%0A

make sure that route permission check works with KFS-TEM TEM Profile permission