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