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 edu.sampleu.bookstore.rule; 017 018import edu.sampleu.bookstore.bo.BookOrder; 019import edu.sampleu.bookstore.document.BookOrderDocument; 020import org.kuali.rice.core.api.util.RiceKeyConstants; 021import org.kuali.rice.krad.document.Document; 022import org.kuali.rice.krad.rules.TransactionalDocumentRuleBase; 023import org.kuali.rice.krad.util.GlobalVariables; 024import org.kuali.rice.krad.util.KRADConstants; 025 026import java.util.List; 027 028 029/* 030 * Business Rule for Book Order Document that follows prior to submit action. 031 * Checks that book order/orders is/are not null or Empty and all the attributes are mentioned for each order. 032 */ 033 034public class BookOrderDocumentRule extends TransactionalDocumentRuleBase { 035 036 private static final String BOOK_ORDERS_PROPERTY_PATH = KRADConstants.DOCUMENT_PROPERTY_NAME + ".bookOrders"; 037 private static final String NO_BOOK_ORDERS_ERROR_KEY = RiceKeyConstants.ERROR_CUSTOM; 038 private static final String ERROR_MESSAGE_NO_ORDERS = "You must add at least one entry to your book order."; 039 040 private static final String BOOK_ORDERS_EMPTY_ERROR_KEY = RiceKeyConstants.ERROR_CUSTOM; 041 private static final String ERROR_MESSAGE_EMPTY_ORDERS = "You must add attributes to your book order."; 042 043 @Override 044 protected boolean processCustomRouteDocumentBusinessRules(Document document) { 045 046 System.out.println("@@@@ IN RULE CHECK"); 047 048 // cast the document to a BookOrderDocument 049 BookOrderDocument bookOrderDocument = (BookOrderDocument)document; 050 051 // get the list of book orders of the book order document 052 List<BookOrder> bookOrders = bookOrderDocument.getBookOrders(); 053 054 // make sure that the list is not empty 055 if (bookOrders == null || bookOrders.isEmpty()) { 056 GlobalVariables.getMessageMap().putError(BOOK_ORDERS_PROPERTY_PATH, NO_BOOK_ORDERS_ERROR_KEY, ERROR_MESSAGE_NO_ORDERS); 057 System.out.println("@@@@ FALSE RULE CHECK"); 058 return false; 059 } else { 060 for(BookOrder bookOrder : bookOrders){ 061 if(bookOrder.getBookId() == null || bookOrder.getDiscount() == null || bookOrder.getQuantity() == null){ 062 GlobalVariables.getMessageMap().putError(BOOK_ORDERS_PROPERTY_PATH, BOOK_ORDERS_EMPTY_ERROR_KEY, ERROR_MESSAGE_EMPTY_ORDERS); 063 System.out.println("@@@@ FALSE RULE CHECK"); 064 return false; 065 } 066 } 067 068 } 069 System.out.println("@@@@ TRUE RULE CHECK"); 070 071 return super.processCustomRouteDocumentBusinessRules(document); 072 } 073 074 075 076 077 078}