001/**
002 * Copyright 2005-2016 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.krad.util;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kew.api.KewApiConstants;
020import org.kuali.rice.krad.bo.AdHocRouteRecipient;
021import org.kuali.rice.krad.document.Document;
022
023import java.util.List;
024import java.util.ListIterator;
025
026public class RouteToCompletionUtil {
027
028    /**
029     * Checks if there is atleast one Ad-Hoc Completion request for the document and based on that returns a boolean
030     * value.
031     */
032    public static boolean checkIfAtleastOneAdHocCompleteRequestExist(Document document) {
033        boolean foundAtleastOneCompleteReq = false;
034        // iterating the adhoc recpients list to check if there is atleast on complete request for the document.
035        foundAtleastOneCompleteReq = loopAndCheckValue(document.getAdHocRouteWorkgroups()) || loopAndCheckValue(
036                document.getAdHocRoutePersons());
037
038        return foundAtleastOneCompleteReq;
039    }
040
041    /**
042     * Loops and checks if the required value is present in the loop used for checking if there is atleast one adhoc
043     * completion
044     * request present for a person or work group
045     */
046    private static boolean loopAndCheckValue(List adhoc) {
047        if (adhoc == null) {
048            return false;
049        }
050
051        ListIterator<AdHocRouteRecipient> groupIter = adhoc.listIterator();
052        String valueToCheck = null;
053        AdHocRouteRecipient recipient = null;
054
055        boolean foundAtleastOneCompleteReq = false;
056        while (groupIter.hasNext()) {
057            recipient = groupIter.next();
058            valueToCheck = recipient.getActionRequested();
059            if (StringUtils.isNotEmpty(valueToCheck)) {
060                if (KewApiConstants.ACTION_REQUEST_COMPLETE_REQ.equals(valueToCheck)) {
061                    foundAtleastOneCompleteReq = true;
062                    break;
063                }
064            }
065        }
066
067        return foundAtleastOneCompleteReq;
068    }
069}