001package org.kuali.ole.alert.rule;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.alert.bo.AlertDocumentType;
005import org.kuali.ole.alert.bo.AlertEvent;
006import org.kuali.ole.deliver.bo.OleDeliverRequestType;
007import org.kuali.rice.krad.maintenance.MaintenanceDocument;
008import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
009import org.kuali.rice.krad.service.KRADServiceLocator;
010
011import java.util.HashMap;
012import java.util.List;
013import java.util.Map;
014
015/**
016 * Created by maheswarang on 12/27/14.
017 */
018public class AlertEventRule extends MaintenanceDocumentRuleBase {
019    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
020        boolean isValid = true;
021        AlertEvent alertEvent = (AlertEvent) document.getNewMaintainableObject().getDataObject();
022        isValid &= validateDocumentTypeName(alertEvent);
023        return isValid;
024    }
025
026    /**
027     * This method  validates duplicate completeness Id and return boolean value.
028     *
029     * @param alertEvent
030     * @return boolean
031     */
032    private boolean validateDocumentTypeName(AlertEvent alertEvent) {
033
034  if(alertEvent.getAlertEventId()==null &&  alertEvent.getAlertEventName() != null){
035    Map<String,String> alertEventMap = new HashMap<String,String>();
036      alertEventMap.put("alertDocumentTypeId",alertEvent.getAlertDocumentTypeId());
037      alertEventMap.put("alertEventName",alertEvent.getAlertEventName());
038      List<AlertEvent> alertEventList = (List<AlertEvent>)KRADServiceLocator.getBusinessObjectService().findMatching(AlertEvent.class, alertEventMap);
039      if(alertEventList!=null && alertEventList.size()>0){
040          //event name already defined for the same document type
041          this.putFieldError(OLEConstants.ALERT_EVENT_NAME, "error.duplicate.event.name");
042          return false;
043      }
044}
045  return true;
046    }
047
048}