001 /*
002 * Copyright 2011 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 */
016 package org.kuali.kfs.module.purap.util;
017
018 import java.util.ArrayList;
019 import java.util.List;
020
021 import org.kuali.kfs.module.purap.businessobject.PurApItem;
022 import org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem;
023 import org.kuali.rice.kns.util.ObjectUtils;
024
025 /**
026 * Purchasing Accounts Payable Item Utilities.
027 * This class contains item utilities.
028 */
029 public class PurApItemUtils {
030
031 /**
032 * Checks if an item is active. It is used mainly when were dealing with generic items (which may be po) And need to
033 * make sure the active rules are applied if it is a poitem
034 *
035 * @param item the purap item passed in
036 * @return true if item is active
037 */
038 public static boolean checkItemActive(PurApItem item) {
039 boolean active = true;
040 if (item instanceof PurchaseOrderItem) {
041 PurchaseOrderItem poi = (PurchaseOrderItem) item;
042 active = poi.isItemActiveIndicator();
043 }
044 return active;
045 }
046
047 public static boolean isNonZeroExtended(PurApItem item) {
048 return (ObjectUtils.isNotNull(item) && ObjectUtils.isNotNull(item.getExtendedPrice()) && !item.getExtendedPrice().isZero());
049 }
050
051 /**
052 * Helper to get aboveTheLineItems only from an item list
053 *
054 * @param items a list of items including above and below the line
055 * @return below the line items only
056 */
057 public static List<PurApItem> getAboveTheLineOnly(List<PurApItem> items) {
058 List<PurApItem> returnItems = new ArrayList<PurApItem>();
059 for (PurApItem item : items) {
060 if (item.getItemType().isLineItemIndicator()) {
061 returnItems.add((PurApItem) ObjectUtils.deepCopy(item));
062 }
063 }
064 return returnItems;
065 }
066
067 /**
068 * Counts the below the line, currently it relies on below the line being at the bottom
069 *
070 * @return a count of below the line items
071 */
072 public static int countBelowTheLineItems(List<PurApItem> items) {
073 int count = 0;
074 for (int i = items.size() - 1; i > 0; i--) {
075 PurApItem item = items.get(i);
076 // will have to change if we stop putting below the line at bottom
077 if (item.getItemType().isLineItemIndicator()) {
078 break;
079 }
080 else {
081 count++;
082 }
083 }
084 return count;
085 }
086
087 }