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