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