001 package org.kuali.ole.catalog.rule; 002 003 import org.kuali.ole.OLEConstants; 004 import org.kuali.ole.catalog.bo.OleCountryCodes; 005 import org.kuali.rice.krad.maintenance.MaintenanceDocument; 006 import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase; 007 008 import java.util.HashMap; 009 import java.util.List; 010 import java.util.Map; 011 012 /** 013 * OleCountryCodesRule validates maintenance object for Country Codes Maintenance Document 014 */ 015 public class OleCountryCodesRule extends MaintenanceDocumentRuleBase { 016 017 @Override 018 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) { 019 boolean isValid = true; 020 OleCountryCodes oleCountryCodes = (OleCountryCodes) document.getNewMaintainableObject().getDataObject(); 021 022 isValid &= validateCountryCode(oleCountryCodes); 023 return isValid; 024 } 025 026 /** 027 * This method validates duplicate countryCode Id and return boolean value. 028 * @param oleCountryCodes 029 * @return boolean 030 */ 031 private boolean validateCountryCode(OleCountryCodes oleCountryCodes) { 032 if (oleCountryCodes.getCountryCode() != null) { 033 Map<String, String> criteria = new HashMap<String, String>(); 034 criteria.put(OLEConstants.OleCountryCodes.COUNTRY_CD, oleCountryCodes.getCountryCode()); 035 List<OleCountryCodes> countryCodeInDatabase = (List<OleCountryCodes>) getBoService().findMatching(OleCountryCodes.class, criteria); 036 if ((countryCodeInDatabase.size() > 0)) { 037 for (OleCountryCodes countryCodeObj : countryCodeInDatabase) { 038 Integer countryCodeId = countryCodeObj.getCountryCodeId(); 039 if (null == oleCountryCodes.getCountryCodeId() || (oleCountryCodes.getCountryCodeId().intValue() != countryCodeId.intValue())) { 040 this.putFieldError(OLEConstants.OleCountryCodes.COUNTRY_CODE, "error.duplicate.code"); 041 return false; 042 } 043 } 044 } 045 } 046 return true; 047 } 048 }