001    package org.kuali.ole.catalog.rule;
002    
003    import org.kuali.ole.OLEConstants;
004    import org.kuali.ole.catalog.bo.OlePrivacy;
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     * OlePrivacyRule validates maintenance object for Privacy Maintenance Document
014     */
015    public class OlePrivacyRule extends MaintenanceDocumentRuleBase {
016    
017        @Override
018        protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
019            boolean isValid = true;
020            OlePrivacy OlePrivacy = (OlePrivacy) document.getNewMaintainableObject().getDataObject();
021    
022            isValid &= validatePrivacyCode(OlePrivacy);
023            return isValid;
024        }
025    
026        /**
027         * This method  validates duplicate privacy Id and return boolean value.
028         * @param olePrivacy
029         * @return  boolean
030         */
031        private boolean validatePrivacyCode(OlePrivacy olePrivacy) {
032    
033            if (olePrivacy.getPrivacyCode() != null) {
034    
035                Map<String, String> criteria = new HashMap<String, String>();
036    
037                criteria.put(OLEConstants.OlePrivacy.PRIVACY_CD, olePrivacy.getPrivacyCode());
038    
039                List<OlePrivacy> privacyCodeInDatabase = (List<OlePrivacy>) getBoService().findMatching(OlePrivacy.class, criteria);
040    
041                if ((privacyCodeInDatabase.size() > 0)) {
042    
043                    for(OlePrivacy privacyCodeObj:privacyCodeInDatabase){
044                        String privacyId = privacyCodeObj.getPrivacyId();
045                        if(null == olePrivacy.getPrivacyId() ||
046                                !(olePrivacy.getPrivacyId().equalsIgnoreCase(privacyId))){
047                            this.putFieldError(OLEConstants.OlePrivacy.PRIVACY_CODE, "error.duplicate.code");
048                            return false;
049                        }
050                    }
051                }
052                return true;
053            }
054            return false;
055        }
056    }