Subversion Repositories WebE

Rev

Rev 35 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

1
package ch.ffhs.webE.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;

import ch.ffhs.webE.dao.HistoryDAO;
import ch.ffhs.webE.dao.TermDAO;
import ch.ffhs.webE.dao.UserDAO;
import ch.ffhs.webE.domain.ActionType;
import ch.ffhs.webE.domain.History;
import ch.ffhs.webE.domain.ObjectEntity;
import ch.ffhs.webE.domain.ObjectType;
import ch.ffhs.webE.domain.Term;
import ch.ffhs.webE.domain.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * Implements actions applicable to term editing
 *
 * @author Thomas Lahn
 */

public class TermAction extends ActionSupport implements ModelDriven<Term>
{
  private static final long serialVersionUID = 1L;

  private Term term = new Term();
  private List<Term> termList = new ArrayList<Term>();
  private final TermDAO termDAO = new TermDAO();
  private final UserDAO userDAO = new UserDAO();

  /**
   * Session object
   */

  Map<String, Object> session = ActionContext.getContext().getSession();

  /**
   * @var <code>true</code> if the term is edited/renamed, <code>false</code>
   *      otherwise
   */

  public boolean edit = false;

  /**
   * @var <code>true</code> if a term was added, <code>false</code> otherwise
   */

  public boolean added = false;

  private final HttpServletRequest request = (HttpServletRequest) ActionContext
      .getContext().get(StrutsStatics.HTTP_REQUEST);

  /**
   * The term that was just saved (added, renamed)
   */

  public Term savedTerm;

  private final HistoryDAO historyDAO = new HistoryDAO();

  private Set<History> history;

  /*
   * (non-Javadoc)
   *
   * @see com.opensymphony.xwork2.ModelDriven#getModel()
   */

  public Term getModel()
  {
    return this.term;
  }

  /**
   * DB query for term list
   *
   * @return SUCCESS
   */

  public String list()
  {
    this.termList = this.termDAO.getList();
    return Action.SUCCESS;
  }

  /**
   * Executes the DB query to save the user
   *
   * @return {@link Action#SUCCESS}
   */

  public String save()
  {
    User user = this.userDAO.getByUsername((String) this.session
        .get("username"));
    Date now = new Date();
    ObjectEntity obj;

    int action = 0;
    if ("false".equals(this.request.getParameter("edit")))
    {
      /* Add a new term */
      obj = new ObjectEntity(user, new ObjectType(ObjectType.TERM), user, null,
          now, false, this.term, null, null);

      this.term.setObject(obj);
      this.added = true;
      action = ActionType.ADD;
    }
    else
    {
      obj = new ObjectEntity();
      obj.setId(this.term.getObjectId());
      action = ActionType.RENAME;
    }

    String result = Action.SUCCESS;
    if (this.termDAO.saveOrUpdate(this.term))
    {
      String comment = this.request.getParameter("comment");

      History historyRecord = new History(user, new ActionType(action), obj,
          this.term.getName(), comment, now);

      this.historyDAO.saveOrUpdate(historyRecord);
    }
    else
    {
      result = Action.ERROR;
    }

    this.savedTerm = this.term;
    this.term = null;

    this.list();

    return result;
  }

  /**
   * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
   *         otherwise
   */

  public String edit()
  {
    int id = this.getIdParameter();

    String result = Action.ERROR;
    if (id > 0)
    {
      this.term = this.termDAO.getById(id);
      if (this.term != null)
      {
        this.edit = true;
        result = Action.SUCCESS;
      }
    }

    this.list();

    return result;
  }

  /**
   * deletes a term, gets the ID from the "id" parameter that was submitted with
   * the HTTP request
   *
   * @return String - either SUCCESS or ERROR constant
   */

  public String delete()
  {

    int id = this.getIdParameter();

    /* Check for malicious ID values */
    String result = Action.SUCCESS;
    if (id > 0)
    {
      this.termDAO.delete(id);
    }
    else
    {
      result = Action.ERROR;
    }

    this.list();

    return result;
  }

  /**
   * Gets the ID Parameter for update / delete requests
   *
   * @return int from the ID request. If not set or wrong, it gives back -1
   */

  private int getIdParameter()
  {
    int id = -1;
    try
    {
      id = Integer.parseInt(this.request.getParameter("id")); //$NON-NLS-1$
    }
    catch (Exception e)
    {
      /* TODO: Logging - wrong parameter set */
    }

    return id;
  }

  /* Standard getters and setters */

  /**
   * @return The term edited with this instance
   */

  public Term getTerm()
  {
    return this.term;
  }

  /**
   * @param term
   *          The term edited with this instance
   */

  public void setTerm(Term term)
  {
    this.term = term;
  }

  /**
   * @return The list of terms edited with this instance
   */

  public List<Term> getTermList()
  {
    return this.termList;
  }

  /**
   * @param termList
   *          The list of terms edited with this instance
   */

  public void setTermList(List<Term> termList)
  {
    this.termList = termList;
  }

  /**
   * @return the histories
   */

  public Set<History> getHistories()
  {
    return this.history;
  }

  /**
   * @param histories
   *          the histories to set
   */

  public void setHistories(Set<History> histories)
  {
    this.history = histories;
  }
}