package ch.ffhs.webE.action; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.StrutsStatics; import ch.ffhs.webE.dao.RelationshipDAOImpl; import ch.ffhs.webE.dao.RelationshipTypeDAOImpl; import ch.ffhs.webE.dao.TermDAOImpl; import ch.ffhs.webE.dao.UserDAOImpl; import ch.ffhs.webE.domain.ObjectEntity; import ch.ffhs.webE.domain.ObjectType; import ch.ffhs.webE.domain.Relationship; import ch.ffhs.webE.domain.RelationshipType; 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 relationship editing * * @author Thomas Lahn */ public class RelationshipAction extends ActionSupport implements ModelDriven { private static final long serialVersionUID = 1L; private List relationshipTypes = new ArrayList(); private final RelationshipTypeDAOImpl relationshipTypeDAO = new RelationshipTypeDAOImpl(); private List terms = new ArrayList(); private final TermDAOImpl termDAO = new TermDAOImpl(); private List relationshipList = new ArrayList(); private final RelationshipDAOImpl relationshipDAO = new RelationshipDAOImpl(); private final UserDAOImpl userDAO = new UserDAOImpl(); private Relationship relationship = new Relationship(); /** * Session object */ Map session = ActionContext.getContext().getSession(); private final HttpServletRequest request = (HttpServletRequest) ActionContext .getContext().get(StrutsStatics.HTTP_REQUEST); /* * (non-Javadoc) * * @see com.opensymphony.xwork2.ModelDriven#getModel() */ public Relationship getModel() { return this.relationship; } /** * Prepares to add a relationship * * @return */ public String add() { this.setTerms(this.termDAO.getTerms()); this.setRelationshipTypes(this.relationshipTypeDAO.getRelTypes()); return Action.SUCCESS; } /** * Executes the DB query to save the user * * @return {@link Action#SUCCESS} */ public String save() { this.relationship.setTermFrom(this.termDAO.getTermById(Integer .parseInt(this.request.getParameter("term1")))); this.relationship.setTermTo(this.termDAO.getTermById(Integer .parseInt(this.request.getParameter("term2")))); this.relationship.setRelationshipType(this.relationshipTypeDAO .getRelTypeById(Integer.parseInt(this.request.getParameter("type")))); if (!"1".equals(this.request.getParameter("edit"))) { User user = this.userDAO.searchUsername((String) this.session .get("username")); ObjectEntity obj = new ObjectEntity(user, new ObjectType( ObjectType.RELATIONSHIP), user, null, new Date(), false, null, null, this.relationship); this.relationship.setObject(obj); } if (this.relationshipDAO.saveOrUpdate(this.relationship)) { return Action.SUCCESS; } return Action.ERROR; } /** * DB query for relationship list * * @return SUCCESS */ public String list() { this.relationshipList = this.relationshipDAO.listRelationships(); return Action.SUCCESS; } /** * @return {@link Action#SUCCESS} if id > 0, {@link Action#ERROR} * otherwise */ public String edit() { int id = this.getIdParameter(); if (id > 0) { this.relationship = this.relationshipDAO.getRelationshipById(id); if (this.relationship != null) { return Action.SUCCESS; } } return Action.ERROR; } /** * 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; } /** * deletes a user, 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 */ if (id > 0) { this.relationshipDAO.deleteRelationship(id); return Action.SUCCESS; } else { return Action.ERROR; } } /* * Standard getters and setters */ /** * @return The relationship edited with this instance */ public Relationship getRelationship() { return this.relationship; } /** * @param relationship * The relationship edited with this instance */ public void setRelationship(Relationship relationship) { this.relationship = relationship; } /** * @return The list of terms edited with this instance */ public List getRelationshipList() { return this.relationshipList; } /** * @param relationshipList * The list of terms edited with this instance */ public void setRelationshipList(List relationshipList) { this.relationshipList = relationshipList; } /** * @return the relationshipTypes */ public List getRelationshipTypes() { return this.relationshipTypes; } /** * @param relationshipTypes * the relationshipTypes to set */ public void setRelationshipTypes(List relationshipTypes) { this.relationshipTypes = relationshipTypes; } /** * @return the terms */ public List getTerms() { return this.terms; } /** * @param terms * the terms to set */ public void setTerms(List terms) { this.terms = terms; } }