/*
 * Copyright 2005-2010 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.osedu.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.kra.irb.correspondence;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.kuali.kra.bo.KraPersistableBusinessObjectBase;
import org.kuali.kra.irb.actions.ProtocolActionType;

public class BatchCorrespondence extends KraPersistableBusinessObjectBase { 
    
    public static final String SEND_CORRESPONDENCE_BEFORE_EVENT ="BEFORE";
    public static final String SEND_CORRESPONDENCE_AFTER_EVENT ="AFTER";

    private static final long serialVersionUID = 1L;
    
    private String batchCorrespondenceTypeCode; 
    private String description; 
    private String daysToEventUiText; 
    private String sendCorrespondence;
    private Integer finalActionDay;
    private String finalActionTypeCode; 
    private String finalActionCorrespType;
    
    private List<BatchCorrespondenceDetail> batchCorrespondenceDetails;
    
    private ProtocolCorrespondenceType protocolCorrespondenceType;
    private ProtocolActionType protocolActionType;
    
    public BatchCorrespondence() {
        setBatchCorrespondenceDetails(new ArrayList<BatchCorrespondenceDetail>());
    } 
    
    public String getBatchCorrespondenceTypeCode() {
        return batchCorrespondenceTypeCode;
    }

    public void setBatchCorrespondenceTypeCode(String batchCorrespondenceTypeCode) {
        this.batchCorrespondenceTypeCode = batchCorrespondenceTypeCode;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDaysToEventUiText() {
        return daysToEventUiText;
    }

    public void setDaysToEventUiText(String daysToEventUiText) {
        this.daysToEventUiText = daysToEventUiText;
    }

    public String getSendCorrespondence() {
        return sendCorrespondence;
    }

    public void setSendCorrespondence(String sendCorrespondence) {
        this.sendCorrespondence = sendCorrespondence;
    }

    public Integer getFinalActionDay() {
        return finalActionDay;
    }

    public void setFinalActionDay(Integer finalActionDay) {
        this.finalActionDay = finalActionDay;
    }

    public String getFinalActionTypeCode() {
        return finalActionTypeCode;
    }

    public void setFinalActionTypeCode(String finalActionTypeCode) {
        this.finalActionTypeCode = finalActionTypeCode;
    }

    public String getFinalActionCorrespType() {
        return finalActionCorrespType;
    }

    public void setFinalActionCorrespType(String finalActionCorrespType) {
        this.finalActionCorrespType = finalActionCorrespType;
    }

    /**
     * 
     * This method returns the batch correspondence details in the proper order depending if
     * correspondences occur before or after the event.
     * @return Sorted list of BatchCorrespondenceDetail
     */
    public List<BatchCorrespondenceDetail> getBatchCorrespondenceDetails() {
        List<BatchCorrespondenceDetail> result = this.batchCorrespondenceDetails;
        Collections.sort(result);
        if (StringUtils.equals(getSendCorrespondence(), SEND_CORRESPONDENCE_BEFORE_EVENT)) {
            Collections.reverse(result);
        }
        return result;
    }

    public void setBatchCorrespondenceDetails(List<BatchCorrespondenceDetail> batchCorrespondenceDetails) {
        this.batchCorrespondenceDetails = batchCorrespondenceDetails;
    }

    public ProtocolCorrespondenceType getProtocolCorrespondenceType() {
        return protocolCorrespondenceType;
    }

    public void setProtocolCorrespondenceType(ProtocolCorrespondenceType protocolCorrespondenceType) {
        this.protocolCorrespondenceType = protocolCorrespondenceType;
    }

    public ProtocolActionType getProtocolActionType() {
        return protocolActionType;
    }

    public void setProtocolActionType(ProtocolActionType protocolActionType) {
        this.protocolActionType = protocolActionType;
    }

    /** {@inheritDoc} */
    @Override 
    protected LinkedHashMap<String, Object> toStringMapper() {
        LinkedHashMap<String, Object> hashMap = new LinkedHashMap<String, Object>();
        hashMap.put("BatchCorresponcenceTypeCode", this.getBatchCorrespondenceTypeCode());
        hashMap.put("description", this.getDescription());
        hashMap.put("daysToEventUiText", this.getDaysToEventUiText());
        hashMap.put("sendCorrespondence", this.getSendCorrespondence());
        hashMap.put("finalActionDay", this.getFinalActionDay());
        hashMap.put("finalActionTypeCode", this.getFinalActionTypeCode());
        hashMap.put("finalActionCorrespType", this.getFinalActionCorrespType());
        return hashMap;
    }
    
}

@@ -3007,32 +3007,122 @@
 onTypeCode() %7B%0D%0A
+        if (finalActionTypeCode == null) %7B%0D%0A            return %22%22;%0D%0A        %7D else %7B%0D%0A    
         return f
@@ -3134,32 +3134,43 @@
 ctionTypeCode;%0D%0A
+        %7D%0D%0A
     %7D%0D%0A%0D%0A    pub
@@ -3224,32 +3224,157 @@
 ionTypeCode) %7B%0D%0A
+        if (StringUtils.isBlank(finalActionTypeCode)) %7B%0D%0A            this.finalActionTypeCode = null;%0D%0A        %7D else %7B%0D%0A    
         this.fin
@@ -3406,32 +3406,43 @@
 ctionTypeCode;%0D%0A
+        %7D%0D%0A
     %7D%0D%0A%0D%0A    pub
@@ -3475,32 +3475,125 @@
 orrespType() %7B%0D%0A
+        if (finalActionCorrespType == null) %7B%0D%0A            return %22%22;%0D%0A        %7D else %7B%0D%0A    
         return f
@@ -3608,32 +3608,43 @@
 onCorrespType;%0D%0A
+        %7D%0D%0A
     %7D%0D%0A%0D%0A    pub
@@ -3704,32 +3704,163 @@
 CorrespType) %7B%0D%0A
+        if (StringUtils.isBlank(finalActionCorrespType)) %7B%0D%0A            this.finalActionCorrespType = null;%0D%0A        %7D else %7B%0D%0A    
         this.fin
@@ -3902,24 +3902,35 @@
 rrespType;%0D%0A
+        %7D%0D%0A
     %7D%0D%0A%0D%0A   

Fix for MySQL - Oracle treated blank fields as nulls however with MySQL these fields need to be set to null explicitly otherwise a constraint error will occur.