Subversion Repositories WebE

Rev

Rev 33 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
31 PointedEar 1
package ch.ffhs.webE.action;
2
 
3
import java.util.ArrayList;
4
import java.util.List;
5
 
6
import javax.servlet.http.HttpServletRequest;
7
 
8
import org.apache.struts2.StrutsStatics;
9
 
10
import ch.ffhs.webE.dao.TermDAO;
11
import ch.ffhs.webE.dao.TermDAOImpl;
12
import ch.ffhs.webE.domain.Term;
13
 
14
import com.opensymphony.xwork2.Action;
15
import com.opensymphony.xwork2.ActionContext;
16
import com.opensymphony.xwork2.ActionSupport;
17
import com.opensymphony.xwork2.ModelDriven;
18
 
19
/**
20
 * Implements actions applicable to term editing
21
 *
22
 * @author Thomas Lahn
23
 */
24
public class TermAction extends ActionSupport implements ModelDriven<Term>
25
{
26
  private static final long serialVersionUID = -6659925652584240539L;
27
 
28
  private Term term = new Term();
29
  private List<Term> termList = new ArrayList<Term>();
30
  private final TermDAO termDAO = new TermDAOImpl();
31
 
32
  /*
33
   * (non-Javadoc)
34
   *
35
   * @see com.opensymphony.xwork2.ModelDriven#getModel()
36
   */
37
  public Term getModel()
38
  {
39
    return this.term;
40
  }
41
 
42
  /**
43
   * Executes the DB query to save the user
44
   *
45
   * @return {@link Action#SUCCESS}
46
   */
47
  public String addOrUpdate()
48
  {
49
    this.termDAO.saveOrUpdate(this.term);
50
    return Action.SUCCESS;
51
  }
52
 
53
  /**
54
   * DB query for userList
55
   *
56
   * @return SUCCESS
57
   */
58
  public String list()
59
  {
60
    this.termList = this.termDAO.listTerm();
61
    return Action.SUCCESS;
62
  }
63
 
64
  /**
65
   * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
66
   *         otherwise
67
   */
68
  public String edit()
69
  {
70
    int id = this.getIdParameter();
71
 
72
    if (id > 0)
73
    {
74
      this.term = this.termDAO.listTermById(id);
75
      return Action.SUCCESS;
76
    }
77
    else
78
    {
79
      return Action.ERROR;
80
    }
81
  }
82
 
83
  /**
84
   * Gets the ID Parameter for update / delete requests
85
   *
86
   * @return int from the ID request. If not set or wrong, it gives back -1
87
   */
88
  private int getIdParameter()
89
  {
90
    HttpServletRequest request = (HttpServletRequest) ActionContext
91
        .getContext().get(StrutsStatics.HTTP_REQUEST);
92
 
93
    int id = -1;
94
    try
95
    {
96
      id = Integer.parseInt(request.getParameter("id")); //$NON-NLS-1$
97
    }
98
    catch (Exception e)
99
    {
100
      /* TODO: Logging - wrong parameter set */
101
    }
102
 
103
    return id;
104
  }
105
 
106
  /**
107
   * deletes a user, gets the ID from the "id" parameter that was submitted with
108
   * the HTTP request
109
   *
110
   * @return String - either SUCCESS or ERROR constant
111
   */
112
  public String delete()
113
  {
114
 
115
    int id = this.getIdParameter();
116
 
117
    /* Check for malicious ID values */
118
    if (id > 0)
119
    {
120
      this.termDAO.deleteTerm(id);
121
      return Action.SUCCESS;
122
    }
123
    else
124
    {
125
      return Action.ERROR;
126
    }
127
  }
128
 
129
  /*
130
   * Standard getters and setters
131
   */
132
 
133
  /**
134
   * @return The term edited with this instance
135
   */
136
  public Term getTerm()
137
  {
138
    return this.term;
139
  }
140
 
141
  /**
142
   * @param term
143
   *          The term edited with this instance
144
   */
145
  public void setTerm(Term term)
146
  {
147
    this.term = term;
148
  }
149
 
150
  /**
151
   * @return The list of terms edited with this instance
152
   */
153
  public List<Term> getTermList()
154
  {
155
    return this.termList;
156
  }
157
 
158
  /**
159
   * @param termList
160
   *          The list of terms edited with this instance
161
   */
162
  public void setTermList(List<Term> termList)
163
  {
164
    this.termList = termList;
165
  }
166
}