net.kamhon.ieagle.struts2.BaseUnsecureFlowAction.java Source code

Java tutorial

Introduction

Here is the source code for net.kamhon.ieagle.struts2.BaseUnsecureFlowAction.java

Source

/*
 * Copyright 2012 Eng Kam Hon (kamhon@gmail.com)
 * 
 * Licensed under the Apache 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.apache.org/licenses/LICENSE-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 net.kamhon.ieagle.struts2;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Preparable;

public class BaseUnsecureFlowAction extends BaseAction implements Preparable {
    private static final long serialVersionUID = 1L;

    private static final Log log = LogFactory.getLog(BaseUnsecureFlowAction.class);

    public void prepare() throws Exception {
        if (StringUtils.isNotBlank(ServletActionContext.getActionMapping().getName())) {
            String actionName = ServletActionContext.getActionMapping().getName();
            if (actionName.endsWith(RELOAD_ADD)) {
                prepareReloadAdd();
            } else if (actionName.endsWith(RELOAD_EDIT)) {
                prepareReloadEdit();
            } else if (actionName.endsWith(ADD)) {
                prepareAdd();
            } else if (actionName.endsWith(ADD_DETAIL)) {
                prepareAddDetail();
            } else if (actionName.endsWith(DESTROY)) {
                prepareDestroy();
            } else if (actionName.endsWith(EDIT)) {
                prepareEdit();
            } else if (actionName.endsWith(JSON)) {
                prepareJson();
            } else if (actionName.endsWith(LIST)) {
                prepareList();
            } else if (actionName.endsWith(MOVE_DOWN)) {
                prepareMoveDown();
            } else if (actionName.endsWith(MOVE_UP)) {
                prepareMoveUp();
            } else if (actionName.endsWith(SEARCH)) {
                prepareSearch();
            } else if (actionName.endsWith(PRINT)) {
                preparePrint();
            } else if (actionName.endsWith(REMOVE)) {
                prepareRemove();
            } else if (actionName.endsWith(REMOVE_DETAIL)) {
                prepareRemoveDetail();
            } else if (actionName.endsWith(RETRIEVE_DETAIL)) {
                prepareRetrieveDetail();
            } else if (actionName.endsWith(SAVE)) {
                prepareSave();
            } else if (actionName.endsWith(SHOW)) {
                prepareShow();
            } else if (actionName.endsWith(UPDATE)) {
                prepareUpdate();
            } else if (actionName.endsWith(EDIT_DETAIL)) {
                prepareEditDetail();
            } else {
                prepareOtherMethod(actionName);
            }
        }
    }

    protected void prepareReloadAdd() {
    }

    protected void prepareReloadEdit() {
    }

    /**
     * used for action which is not within the general method
     * 
     * @param actionName
     */
    protected void prepareOtherMethod(String actionName) {
    }

    protected void prepareUpdate() {
    }

    protected void prepareShow() {
    }

    protected void prepareSave() {
    }

    protected void prepareRemove() {
    }

    protected void preparePrint() {
    }

    protected void prepareSearch() {
    }

    protected void prepareList() {
    }

    protected void prepareJson() {
    }

    protected void prepareEdit() {
    }

    protected void prepareDestroy() {
    }

    protected void prepareAdd() {
    }

    protected void prepareAddDetail() {
    }

    protected void prepareEditDetail() {
    }

    protected void prepareRemoveDetail() {
    }

    protected void prepareRetrieveDetail() {
    }

    protected void prepareMoveUp() {
    }

    protected void prepareMoveDown() {
    }

    @Override
    public void validate() {
        super.validate();

        if (StringUtils.isNotBlank(ServletActionContext.getActionMapping().getName())) {
            String actionName = ServletActionContext.getActionMapping().getName().toLowerCase();
            if (actionName.endsWith(ADD)) {
                validateAdd();
            } else if (actionName.endsWith(ADD_DETAIL)) {
                validateAddDetail();
            } else if (actionName.endsWith(DESTROY)) {
                validateDestroy();
            } else if (actionName.endsWith(EDIT)) {
                validateEdit();
            } else if (actionName.endsWith(JSON)) {
                validateJson();
            } else if (actionName.endsWith(LIST)) {
                validateList();
            } else if (actionName.endsWith(MOVE_DOWN)) {
                validateMoveDown();
            } else if (actionName.endsWith(MOVE_UP)) {
                validateMoveUp();
            } else if (actionName.endsWith(SEARCH)) {
                validateSearch();
            } else if (actionName.endsWith(PRINT)) {
                validatePrint();
            } else if (actionName.endsWith(REMOVE)) {
                validateRemove();
            } else if (actionName.endsWith(REMOVE_DETAIL)) {
                validateRemoveDetail();
            } else if (actionName.endsWith(RETRIEVE_DETAIL)) {
                validateRetrieveDetail();
            } else if (actionName.endsWith(SAVE)) {
                validateSave();
            } else if (actionName.endsWith(SHOW)) {
                validateShow();
            } else if (actionName.endsWith(UPDATE)) {
                validateUpdate();
            } else if (actionName.endsWith(EDIT_DETAIL)) {
                validateEditDetail();
            } else if (actionName.endsWith(RELOAD_ADD)) {
                validateReloadAdd();
            } else if (actionName.endsWith(RELOAD_EDIT)) {
                validateReloadEdit();
            } else {
                validateOtherMethod(actionName);
            }
        }
    }

    /**
     * used for action which is not within the general method
     * 
     * @param actionName
     */
    protected void validateOtherMethod(String actionName) {
    }

    protected void validateUpdate() {
    }

    protected void validateReloadAdd() {
    }

    protected void validateReloadEdit() {
    }

    protected void validateEditDetail() {
    }

    protected void validateShow() {
    }

    protected void validateSave() {
    }

    protected void validateRemove() {
    }

    protected void validateRemoveDetail() {
    }

    protected void validateRetrieveDetail() {
    }

    protected void validatePrint() {
    }

    protected void validateList() {
    }

    protected void validateSearch() {
    }

    protected void validateJson() {
    }

    protected void validateEdit() {
    }

    protected void validateDestroy() {
    }

    protected void validateAdd() {
    }

    protected void validateAddDetail() {
    }

    protected void validateMoveUp() {
    }

    protected void validateMoveDown() {
    }

    public String add() {
        log.debug("Call add()");
        return ADD;
    }

    /**
     * if the Add Detail is submit AJAX, the result (return) should set to ADD_DETAIL. Else the result(return) should
     * set to ADD.
     * 
     * @return
     */
    public String addDetail() {
        log.debug("Call addDetail()");
        return ADD_DETAIL;
    }

    /**
     * if the Remove Detail is submit AJAX, the result (return) should set to REMOVE_DETAIL. Else the result(return)
     * should set to ADD.
     * 
     * @return
     */
    public String removeDetail() {
        log.debug("Call removeDetail()");
        return REMOVE_DETAIL;
    }

    /**
     * if the Edit Detail is submit AJAX, the result (return) should set to EDIT_DETAIL. Else the result(return) should
     * set to ADD.
     * 
     * @return
     */
    public String editDetail() {
        log.debug("Call editDetail()");
        return EDIT_DETAIL;
    }

    /**
     * if the Retrieve Detail is submit AJAX, the result (return) should set to RETRIEVE_DETAIL. Else the result(return)
     * should set to ADD.
     * 
     * @return
     */
    public String retrieveDetail() {
        log.debug("Call retrieveDetail()");
        return RETRIEVE_DETAIL;
    }

    /**
     * if the Remove Detail is submit AJAX, the result (return) should set to MOVE_UP. Else the result(return) should
     * set to ADD.
     * 
     * @return
     */
    public String moveUp() {
        log.debug("Call moveUp()");
        return MOVE_UP;
    }

    /**
     * if the Remove Detail is submit AJAX, the result (return) should set to MOVE_DOWN. Else the result(return) should
     * set to ADD.
     * 
     * @return
     */
    public String moveDown() {
        log.debug("Call moveDown()");
        return MOVE_DOWN;
    }

    public String destroy() {
        log.debug("Call destroy()");
        return DESTROY;
    }

    public String edit() {
        log.debug("Call edit()");
        return EDIT;
    }

    public String json() {
        log.debug("Call json()");
        return JSON;
    }

    public String list() {
        log.debug("Call list()");
        return LIST;
    }

    public String search() {
        log.debug("Call search()");
        return SEARCH;
    }

    public String print() {
        log.debug("Call print()");
        return PRINT;
    }

    public String remove() {
        log.debug("Call remove()");
        return SUCCESS;
    }

    public String save() {
        log.debug("Call save()");
        return SUCCESS;
    }

    public String show() {
        log.debug("Call show()");
        return SHOW;
    }

    public String update() {
        log.debug("Call update()");
        return SUCCESS;
    }

    public String reloadAdd() {
        log.debug("Call reloadAdd()");
        return ADD;
    }

    public String reloadEdit() {
        log.debug("Call reloadEdit()");
        return EDIT;
    }
}