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