TaskSaveAction.java

package com.mycim.webapp.actions.setting.system.task;

import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.logging.Logger;
import com.mycim.framework.logging.LoggerFactory;
import com.mycim.framework.utils.MiscUtils;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.sys.Task;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.SystemSetupAction;
import com.mycim.webapp.forms.RootForm;
import com.mycim.webapp.forms.system.TaskInfoForm;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TaskSaveAction extends SystemSetupAction {

    private static final Logger log = LoggerFactory.getLogger(TaskSaveAction.class);

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {

        // Populate the form bean instance
        if (form == null) {
            log.debug(" Creating new LabelInfoForm bean under key " + mapping.getAttribute());
            form = new TaskInfoForm();
            request.setAttribute(mapping.getAttribute(), form);
        }
        TaskInfoForm theform = (TaskInfoForm) form;
        String id = theform.getInstanceId().trim().toUpperCase();
        Task task = new Task(id, getNamedSpace(ObjectList.TASK_KEY, LocalContext.getFacilityRrn()),
                             ObjectList.TASK_KEY);

        task = (Task) getInstance(task);

        // Copy value object properties to form bean.
        PropertyUtils.copyProperties(theform, task);

        theform.setInputOutputFlag1(MiscUtils.parseCheckBoxValueFromDatabase(task.getInputOutputFlag1()));
        theform.setInputOutputFlag2(MiscUtils.parseCheckBoxValueFromDatabase(task.getInputOutputFlag2()));
        theform.setInputOutputFlag3(MiscUtils.parseCheckBoxValueFromDatabase(task.getInputOutputFlag3()));
        theform.setInputOutputFlag4(MiscUtils.parseCheckBoxValueFromDatabase(task.getInputOutputFlag4()));
        theform.setInputOutputFlag5(MiscUtils.parseCheckBoxValueFromDatabase(task.getInputOutputFlag5()));
        theform.setInputOutputFlag6(MiscUtils.parseCheckBoxValueFromDatabase(task.getInputOutputFlag6()));
        theform.setInputOutputFlag7(MiscUtils.parseCheckBoxValueFromDatabase(task.getInputOutputFlag7()));
        theform.setInputOutputFlag8(MiscUtils.parseCheckBoxValueFromDatabase(task.getInputOutputFlag8()));
        theform.setInputOutputFlag9(MiscUtils.parseCheckBoxValueFromDatabase(task.getInputOutputFlag9()));
        theform.setInputOutputFlag10(MiscUtils.parseCheckBoxValueFromDatabase(task.getInputOutputFlag10()));
        // if a value object does not exist, its instanceRrn will be set to 0
        if (task.getInstanceRrn() == 0) {
            theform.setTransId(Constants.CREATE_KEY);
            request.setAttribute(SessionNames.TASK_KEY, task);
            return (mapping.findForward("modify"));
        }
        theform.setTransId(Constants.MODIFY_KEY);
        request.setAttribute(SessionNames.TASK_KEY, task);
        return (mapping.findForward("modify"));
    }

    public ActionForward cancel(ActionMapping mapping, RootForm form, HttpServletRequest request,
                                HttpServletResponse response) {
        TaskInfoForm theform = (TaskInfoForm) form;
        theform.setInstanceId("");
        return (mapping.findForward("setup"));
    }

    public ActionForward copy(ActionMapping mapping, RootForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        TaskInfoForm theform = (TaskInfoForm) form;
        // Validate the transactional control token
        if (form == null) {
            return (mapping.findForward("login"));
        }

        String id = request.getParameter(Constants.COPY_KEY);
        Task task = new Task(id, getNamedSpace(ObjectList.TASK_KEY, LocalContext.getFacilityRrn()),
                             ObjectList.TASK_KEY);
        doCopy(theform, task);
        theform.setInputOutputFlag1(MiscUtils.parseCheckBoxValueFromDatabase(theform.getInputOutputFlag1()));
        theform.setInputOutputFlag2(MiscUtils.parseCheckBoxValueFromDatabase(theform.getInputOutputFlag2()));
        theform.setInputOutputFlag3(MiscUtils.parseCheckBoxValueFromDatabase(theform.getInputOutputFlag3()));
        theform.setInputOutputFlag4(MiscUtils.parseCheckBoxValueFromDatabase(theform.getInputOutputFlag4()));
        theform.setInputOutputFlag5(MiscUtils.parseCheckBoxValueFromDatabase(theform.getInputOutputFlag5()));
        theform.setInputOutputFlag6(MiscUtils.parseCheckBoxValueFromDatabase(theform.getInputOutputFlag6()));
        theform.setInputOutputFlag7(MiscUtils.parseCheckBoxValueFromDatabase(theform.getInputOutputFlag7()));
        theform.setInputOutputFlag8(MiscUtils.parseCheckBoxValueFromDatabase(theform.getInputOutputFlag8()));
        theform.setInputOutputFlag9(MiscUtils.parseCheckBoxValueFromDatabase(theform.getInputOutputFlag9()));
        theform.setInputOutputFlag10(MiscUtils.parseCheckBoxValueFromDatabase(theform.getInputOutputFlag10()));
        // for logic equal tag, copy is the same action as create
        theform.setTransId(Constants.CREATE_KEY);
        request.setAttribute(SessionNames.TASK_KEY, task);
        return (mapping.findForward("modify"));
    }

    public ActionForward delete(ActionMapping mapping, RootForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        TaskInfoForm theform = (TaskInfoForm) form;
        if (form == null) {
            return (mapping.findForward("login"));
        }

        Task task = new Task();
        task.setObject(ObjectList.TASK_KEY);
        // who operate this object?
        task.setTransPerformedby(LocalContext.getUserId());
        PropertyUtils.copyProperties(task, theform);
        if (theform.getFieldTag1().equals("")) {
            theform.setFieldTag1(null);
        }
        if (theform.getFieldTag2().equals("")) {
            theform.setFieldTag2(null);
        }
        if (theform.getFieldTag3().equals("")) {
            theform.setFieldTag3(null);
        }

        if (theform.getFieldTag4().equals("")) {
            theform.setFieldTag4(null);
        }
        if (theform.getFieldTag5().equals("")) {
            theform.setFieldTag5(null);
        }
        if (theform.getFieldTag6().equals("")) {
            theform.setFieldTag6(null);
        }
        if (theform.getFieldTag7().equals("")) {
            theform.setFieldTag7(null);
        }
        if (theform.getFieldTag8().equals("")) {
            theform.setFieldTag8(null);
        }
        if (theform.getFieldTag9().equals("")) {
            theform.setFieldTag9(null);
        }
        if (theform.getFieldTag10().equals("")) {
            theform.setFieldTag10(null);
        }
        task.setInputOutputFlag1(
                (task.getFieldTag1() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag1()) : null);
        task.setInputOutputFlag2(
                (task.getFieldTag2() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag2()) : null);
        task.setInputOutputFlag3(
                (task.getFieldTag3() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag3()) : null);
        task.setInputOutputFlag4(
                (task.getFieldTag4() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag4()) : null);
        task.setInputOutputFlag5(
                (task.getFieldTag5() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag5()) : null);
        task.setInputOutputFlag6(
                (task.getFieldTag6() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag6()) : null);
        task.setInputOutputFlag7(
                (task.getFieldTag7() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag7()) : null);
        task.setInputOutputFlag8(
                (task.getFieldTag8() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag8()) : null);
        task.setInputOutputFlag9(
                (task.getFieldTag9() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag9()) : null);
        task.setInputOutputFlag10(
                (task.getFieldTag10() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag10()) : null);
        task.setTransId(Constants.DELETE_KEY);
        theform.setInstanceId("");

        // no matter add or modify, they all execute the same function
        process(task);
        // just for logic equals tag
        theform.setTransId(Constants.MODIFY_KEY);
        request.setAttribute(SessionNames.TASK_KEY, task);
        return (mapping.findForward("setup"));
    }

    public ActionForward create(ActionMapping mapping, RootForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        TaskInfoForm theform = (TaskInfoForm) form;
        // Validate the transactional control token
        if (form == null) {
            return (mapping.findForward("login"));
        }

        Task task = new Task();
        task.setObject(ObjectList.TASK_KEY);
        // who operate this object?
        task.setTransPerformedby(LocalContext.getUserId());
        PropertyUtils.copyProperties(task, theform);
        if (theform.getFieldTag1().equals("")) {
            theform.setFieldTag1(null);
        }
        if (theform.getFieldTag2().equals("")) {
            theform.setFieldTag2(null);
        }
        if (theform.getFieldTag3().equals("")) {
            theform.setFieldTag3(null);
        }

        if (theform.getFieldTag4().equals("")) {
            theform.setFieldTag4(null);
        }
        if (theform.getFieldTag5().equals("")) {
            theform.setFieldTag5(null);
        }
        if (theform.getFieldTag6().equals("")) {
            theform.setFieldTag6(null);
        }
        if (theform.getFieldTag7().equals("")) {
            theform.setFieldTag7(null);
        }
        if (theform.getFieldTag8().equals("")) {
            theform.setFieldTag8(null);
        }
        if (theform.getFieldTag9().equals("")) {
            theform.setFieldTag9(null);
        }
        if (theform.getFieldTag10().equals("")) {
            theform.setFieldTag10(null);
        }
        task.setInputOutputFlag1(
                (task.getFieldTag1() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag1()) : null);
        task.setInputOutputFlag2(
                (task.getFieldTag2() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag2()) : null);
        task.setInputOutputFlag3(
                (task.getFieldTag3() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag3()) : null);
        task.setInputOutputFlag4(
                (task.getFieldTag4() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag4()) : null);
        task.setInputOutputFlag5(
                (task.getFieldTag5() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag5()) : null);
        task.setInputOutputFlag6(
                (task.getFieldTag6() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag6()) : null);
        task.setInputOutputFlag7(
                (task.getFieldTag7() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag7()) : null);
        task.setInputOutputFlag8(
                (task.getFieldTag8() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag8()) : null);
        task.setInputOutputFlag9(
                (task.getFieldTag9() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag9()) : null);
        task.setInputOutputFlag10(
                (task.getFieldTag10() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag10()) : null);

        task.setTransId(Constants.CREATE_KEY);

        // no matter add or modify, they all execute the same function
        process(task);
        // just for logic equals tag
        theform.setTransId(Constants.MODIFY_KEY);
        request.setAttribute(SessionNames.TASK_KEY, task);
        return (mapping.findForward("modify"));
    }

    public ActionForward modify(ActionMapping mapping, RootForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        if (form == null) {
            return (mapping.findForward("login"));
        }
        TaskInfoForm theform = (TaskInfoForm) form;
        // Validate the transactional control token
        Task task = new Task();
        task.setObject(ObjectList.TASK_KEY);
        // who operate this object?
        task.setTransPerformedby(LocalContext.getUserId());
        PropertyUtils.copyProperties(task, theform);
        if (theform.getFieldTag1().equals("")) {
            theform.setFieldTag1(null);
        }
        if (theform.getFieldTag2().equals("")) {
            theform.setFieldTag2(null);
        }
        if (theform.getFieldTag3().equals("")) {
            theform.setFieldTag3(null);
        }

        if (theform.getFieldTag4().equals("")) {
            theform.setFieldTag4(null);
        }
        if (theform.getFieldTag5().equals("")) {
            theform.setFieldTag5(null);
        }
        if (theform.getFieldTag6().equals("")) {
            theform.setFieldTag6(null);
        }
        if (theform.getFieldTag7().equals("")) {
            theform.setFieldTag7(null);
        }
        if (theform.getFieldTag8().equals("")) {
            theform.setFieldTag8(null);
        }
        if (theform.getFieldTag9().equals("")) {
            theform.setFieldTag9(null);
        }
        if (theform.getFieldTag10().equals("")) {
            theform.setFieldTag10(null);
        }
        task.setInputOutputFlag1(
                (task.getFieldTag1() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag1()) : null);
        task.setInputOutputFlag2(
                (task.getFieldTag2() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag2()) : null);
        task.setInputOutputFlag3(
                (task.getFieldTag3() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag3()) : null);
        task.setInputOutputFlag4(
                (task.getFieldTag4() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag4()) : null);
        task.setInputOutputFlag5(
                (task.getFieldTag5() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag5()) : null);
        task.setInputOutputFlag6(
                (task.getFieldTag6() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag6()) : null);
        task.setInputOutputFlag7(
                (task.getFieldTag7() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag7()) : null);
        task.setInputOutputFlag8(
                (task.getFieldTag8() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag8()) : null);
        task.setInputOutputFlag9(
                (task.getFieldTag9() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag9()) : null);
        task.setInputOutputFlag10(
                (task.getFieldTag10() != null) ? MiscUtils.parseCheckBox(task.getInputOutputFlag10()) : null);

        task.setTransId(Constants.MODIFY_KEY);

        // no matter add or modify, they all execute the same function
        process(task);
        // just for logic equals tag
        theform.setTransId(Constants.MODIFY_KEY);
        request.setAttribute(SessionNames.TASK_KEY, task);
        return (mapping.findForward("modify"));
    }

}