001/**
002 * Copyright 2005-2017 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.rice.kew.service.impl;
017
018import org.kuali.rice.kew.api.KewApiServiceLocator;
019import org.kuali.rice.kew.api.doctype.DocumentTypeService;
020import org.kuali.rice.kim.api.KimConstants;
021import org.kuali.rice.kim.api.responsibility.Responsibility;
022import org.kuali.rice.kim.framework.responsibility.ResponsibilityTypeService;
023import org.kuali.rice.kns.kim.responsibility.KimResponsibilityTypeServiceBase;
024
025import java.util.ArrayList;
026import java.util.Collections;
027import java.util.HashMap;
028import java.util.List;
029import java.util.Map;
030
031/**
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034public class DocumentTypeResponsibilityTypeServiceImpl extends
035        KimResponsibilityTypeServiceBase implements ResponsibilityTypeService {
036        DocumentTypeService documentTypeService;
037        protected String exactMatchStringAttributeName;
038
039    @Override
040    protected List<String> getRequiredAttributes() {
041        final List<String> attrs = new ArrayList<String>(super.getRequiredAttributes());
042        attrs.add(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME);
043        return Collections.unmodifiableList(attrs);
044    }
045
046    @Override
047    protected boolean isCheckRequiredAttributes() {
048        return true;
049    }
050        
051        @Override
052        protected List<Responsibility> performResponsibilityMatches(
053                        Map<String, String> requestedDetails,
054                        List<Responsibility> responsibilitiesList) {
055                Map<String, List<Responsibility>> potentialDocumentTypeMatches = new HashMap<String, List<Responsibility>>();
056                for (Responsibility responsibility : responsibilitiesList) {
057                        if ((exactMatchStringAttributeName == null)
058                                        || responsibility
059                                                        .getAttributes()
060                                                        .get(exactMatchStringAttributeName)
061                                                        .equals(
062                                                                        requestedDetails
063                                                                                        .get(exactMatchStringAttributeName))) {
064                                if (!potentialDocumentTypeMatches.containsKey(responsibility
065                                                .getAttributes().get(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME))) {
066                                        potentialDocumentTypeMatches.put(
067                                                        responsibility.getAttributes().get(
068                                                                        KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME),
069                                                        new ArrayList<Responsibility>());
070                                }
071                                potentialDocumentTypeMatches.get(
072                                                responsibility.getAttributes().get(
073                                                                KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME)).add(
074                                                responsibility);
075                        }
076                }
077                List<Responsibility> matchingResponsibilities = new ArrayList<Responsibility>();
078                if (potentialDocumentTypeMatches.containsKey(requestedDetails
079                                .get(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME))) {
080                        matchingResponsibilities
081                                        .addAll(potentialDocumentTypeMatches.get(requestedDetails
082                                                        .get(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME)));
083                } else {
084                        String closestParentDocumentTypeName = getClosestParentDocumentTypeName(
085                                        getDocumentTypeService().getDocumentTypeByName(
086                                                        requestedDetails
087                                                                        .get(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME)),
088                                        potentialDocumentTypeMatches.keySet());
089                        if (closestParentDocumentTypeName != null) {
090                                matchingResponsibilities.addAll(potentialDocumentTypeMatches
091                                                .get(closestParentDocumentTypeName));
092                        }
093                }
094                return matchingResponsibilities;
095        }
096
097        public DocumentTypeService getDocumentTypeService() {
098                if (documentTypeService == null) {
099                        documentTypeService = KewApiServiceLocator.getDocumentTypeService();
100                }
101                return this.documentTypeService;
102        }
103}