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.kns.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
026//Adhoc Document Complete functionality utility
027public class RouteToCompletionUtil {
028
029    /***
030     * Checks if there is atleast one Ad-Hoc Completion request for the document and based on that returns a boolean 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(document.getAdHocRoutePersons());
036        return foundAtleastOneCompleteReq;
037    }
038
039    /***
040     * Loops and checks if the required value is present in the loop used for checking if there is atleast one adhoc completion
041     * request present for a person or work group
042     */
043    public static boolean loopAndCheckValue(List adhoc) {
044        if (adhoc == null) {
045            return false;
046        }
047        ListIterator<AdHocRouteRecipient> groupIter = adhoc.listIterator();
048        String valueToCheck = null;
049        AdHocRouteRecipient recipient = null;
050        boolean foundAtleastOneCompleteReq = false;
051        while (groupIter.hasNext()) {
052            recipient = groupIter.next();
053            valueToCheck = recipient.getActionRequested();
054            if (StringUtils.isNotEmpty(valueToCheck)) {
055                if (KewApiConstants.ACTION_REQUEST_COMPLETE_REQ.equals(valueToCheck)) {
056                    foundAtleastOneCompleteReq = true;
057                    break;
058                }
059            }
060        }
061        return foundAtleastOneCompleteReq;
062    }
063}