001/**
002 * Copyright 2005-2015 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.Address;
019import edu.sampleu.bookstore.bo.Author;
020import org.kuali.rice.core.api.util.RiceKeyConstants;
021import org.kuali.rice.krad.maintenance.MaintenanceDocument;
022import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
023import org.kuali.rice.krad.util.GlobalVariables;
024import org.kuali.rice.krad.util.KRADConstants;
025
026import java.util.List;
027
028/*
029 * Business Rule for Author Document that follows prior to submit action.
030 * Checks that Author Document has at least one address and no two address should be of same type. 
031 */
032
033public class AuthorMaintenanceDocumentRule extends MaintenanceDocumentRuleBase {
034        private static final String AUTHOR_ENTRIES_PROPERTY_PATH = KRADConstants.DOCUMENT_PROPERTY_NAME
035                        + ".authorEntries";
036        private static final String NO_ADDRESS_TYPE_ERROR_KEY = RiceKeyConstants.ERROR_CUSTOM;
037        private static final String ERROR_MESSAGE_NO_ADDTYPE_FOUND = "You must add atleast one address for Author.";
038        private static final String SAME_ADDRESS_TYPE_ERROR_KEY = RiceKeyConstants.ERROR_CUSTOM;
039        private static final String ERROR_MESSAGE_SAME_ADDTYPR_FOUND = "You must not repeat address type for addresses of Author.";
040
041        @Override
042        protected boolean processGlobalRouteDocumentBusinessRules(
043                        MaintenanceDocument document) {
044                // TODO Auto-generated method stub
045                System.out.println("****Inside Global*****");
046                // cast the document to a Author Document
047                Author author = (Author)document.getDocumentDataObject();
048                System.out.println("Inside Global Aothor Got is : " + author);
049                System.out.println("****Inside Global*****");
050                // get the list of book order entries off of the book order document
051                List<Address> addressEntries = author.getAddresses();
052                System.out.println("Inside Global Aothor address is : " + addressEntries);
053                
054                // make sure that the list is not empty
055                if (addressEntries == null || addressEntries.isEmpty()) {
056                        System.out.println("--Got addressEntries Null--");
057                        GlobalVariables.getMessageMap().putError(
058                                        AUTHOR_ENTRIES_PROPERTY_PATH,NO_ADDRESS_TYPE_ERROR_KEY,
059                                        ERROR_MESSAGE_NO_ADDTYPE_FOUND);
060                        return false;
061                } else {
062                        System.out.println("--Got addressEntries Obj--");
063                        System.out.println("Inside Global Aothor address Size is : " + addressEntries.size());
064                        Address tempAddress = null;
065                        for(Address address : addressEntries){
066                                tempAddress = address;
067                                for(Address address2 : addressEntries){
068                                        if(!tempAddress.equals(address2)){
069                                                if(tempAddress.getAddressType()!= null && tempAddress.getAddressType() != null && tempAddress.getAddressType().getType().equals(address2.getAddressType().getType())){
070                                                        System.out.println("Got Same Address Typr");
071                                                        GlobalVariables.getMessageMap().putError(
072                                                                        AUTHOR_ENTRIES_PROPERTY_PATH,SAME_ADDRESS_TYPE_ERROR_KEY,
073                                                                        ERROR_MESSAGE_SAME_ADDTYPR_FOUND);
074                                                        return false;
075                                                }
076                                        }else {
077                                                System.out.println("Got Same Address Object");
078                                        } 
079                                }
080                        }
081                        
082                }
083                
084                
085                
086                return super.processGlobalRouteDocumentBusinessRules(document);
087        }
088
089//      @Override
090//      protected boolean processCustomRouteDocumentBusinessRules(Document document) {
091//              System.out.println("****Inside Custom*****");
092//              // cast the document to a Author Document
093//              AuthorDocument author = (AuthorDocument) document;
094//
095//              // get the list of book order entries off of the book order document
096//              List<Address> addressEntries = author.getAuthorAddEntries();
097//
098//              // make sure that the list is not empty
099//              if (addressEntries == null && addressEntries.isEmpty()) {
100//                      System.out.println("Got address Null");
101//                      GlobalVariables.getMessageMap().putError(
102//                                      AUTHOR_ENTRIES_PROPERTY_PATH, SAME_ADDRESS_TYPE_ERROR_KEY,
103//                                      ERROR_MESSAGE);
104//                      return false;
105//              }
106//
107//              return super.processCustomRouteDocumentBusinessRules(document);
108//      }
109
110}