Source Code‎ > ‎src‎ > ‎in.co.sunrays.proj1‎ > ‎action‎ > ‎

BaseAction.java

package in.co.sunrays.proj1.action;
 
import in.co.sunrays.proj1.dto.BaseDTO;
import in.co.sunrays.proj1.dto.UserDTO;
import in.co.sunrays.proj1.util.DataValidator;
 
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
 
import org.springframework.web.struts.ActionSupport
import com.opensymphony.xwork2.Preparable;
 
/**
 * Base Action class of project. It contain (1) Generic operations (2) Generic
 * constants (3) Generic work flow.
 * 
 * @author SUNRAYS Technologies
 * @version 1.0
 * @Copyright (c) SUNRAYS Technologies
 */
public abstract class BaseAction extends ActionSupport implements
        ServletRequestAware, ServletResponseAware, ApplicationAware,
        SessionAware, Preparable {
 
    /**
     * Operation constants
     */
    protected static final String OP_SAVE = "Save";
    protected static final String OP_NEW = "New";
    protected static final String OP_DELETE = "Delete";
    protected static final String OP_CANCEL = "Cancel";
    protected static final String OP_ERROR = "Error";
    protected static final String OP_NEXT = "Next";
    protected static final String OP_PREVIOUS = "Previous";
    protected static final String OP_SIGNIN = "Sign In";
    protected static final String OP_SIGNUP = "Sign Up";
    protected static final String OP_LOGOUT = "Logout";
    protected static final String OP_GO = "Go";
 
    /**
     * Container objects
     */
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected Map session;
    protected Map application;
 
    /**
     * UI Operation value
     */
    protected String operation;
    /**
     * Non business primary key
     */
    protected long id;
    /*
     * User ID who created this record
     */
    protected String createdBy;
    /**
     * User ID who modified this record
     */
    protected String modifiedBy;
    /**
     * Record's created datetime
     */
    protected long createdDatetime;
    /**
     * Record's last modified datetime
     */
    protected long modifiedDatetime;
 
    /**
     * Implemented methods of Aware Intefaces
     */
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
 
    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }
 
    public void setApplication(Map<String, Object> application) {
        this.application = application;
    }
 
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }
 
    /**
     * accessor
     */
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getOperation() {
        return operation;
    }
 
    public void setOperation(String operation) {
        this.operation = operation;
    }
 
    public String getCreatedBy() {
        return createdBy;
    }
 
    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }
 
    public String getModifiedBy() {
        return modifiedBy;
    }
 
    public void setModifiedBy(String modifiedBy) {
        this.modifiedBy = modifiedBy;
    }
 
    public long getCreatedDatetime() {
        return createdDatetime;
    }
 
    public void setCreatedDatetime(long createdDatetime) {
        this.createdDatetime = createdDatetime;
    }
 
    public long getModifiedDatetime() {
        return modifiedDatetime;
    }
 
    public void setModifiedDatetime(long modifiedDatetime) {
        this.modifiedDatetime = modifiedDatetime;
    }
 
    /**
     * Performs display operations
     */
    @Override
    public abstract String input() throws Exception;
 
    /**
     * Performs submit operations
     */
    @Override
    public abstract String execute() throws Exception;
 
    /**
     * Restrict manual validation
     */
    @Override
    public final void validate() {
        super.validate();
    }
 
    /**
     * Loads pre-loaded data
     */
    public void prepare() throws Exception {
 
    }
 
    /**
     * Populates generic attributes into DTO from Action attributes
     */
    protected <T extends BaseDTO> T populateDTO(T dto) {
 
        if (dto == null) {
            return dto;
        }
 
        modifiedDatetime = new Date().getTime();
        if (createdDatetime == 0) {
            createdDatetime = modifiedDatetime;
        }
        UserDTO userDTO = (UserDTO) session.get("user");
        if (userDTO != null) {
            modifiedBy = userDTO.getLogin();
        }
        if (DataValidator.isNull(createdBy)) {
            createdBy = modifiedBy;
        }
        dto.setCreatedDatetime(new Timestamp(createdDatetime));
        dto.setModifiedDatetime(new Timestamp(modifiedDatetime));
        dto.setCreatedBy(createdBy);
        dto.setModifiedBy(modifiedBy);
        return dto;
    }
}
Comments