Subversion Repositories WebE

Rev

Rev 35 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 35 Rev 37
1
package ch.ffhs.webE.action;
1
package ch.ffhs.webE.action;
2
2
3
import java.util.ArrayList;
3
import java.util.ArrayList;
4
import java.util.Date;
4
import java.util.Date;
5
import java.util.List;
5
import java.util.List;
6
import java.util.Map;
6
import java.util.Map;
-
 
7
import java.util.Set;
7
8
8
import javax.servlet.http.HttpServletRequest;
9
import javax.servlet.http.HttpServletRequest;
9
10
10
import org.apache.struts2.StrutsStatics;
11
import org.apache.struts2.StrutsStatics;
11
12
-
 
13
import ch.ffhs.webE.dao.HistoryDAO;
12
import ch.ffhs.webE.dao.TermDAOImpl;
14
import ch.ffhs.webE.dao.TermDAO;
13
import ch.ffhs.webE.dao.UserDAOImpl;
15
import ch.ffhs.webE.dao.UserDAO;
-
 
16
import ch.ffhs.webE.domain.ActionType;
-
 
17
import ch.ffhs.webE.domain.History;
14
import ch.ffhs.webE.domain.ObjectEntity;
18
import ch.ffhs.webE.domain.ObjectEntity;
15
import ch.ffhs.webE.domain.ObjectType;
19
import ch.ffhs.webE.domain.ObjectType;
16
import ch.ffhs.webE.domain.Term;
20
import ch.ffhs.webE.domain.Term;
17
import ch.ffhs.webE.domain.User;
21
import ch.ffhs.webE.domain.User;
18
22
19
import com.opensymphony.xwork2.Action;
23
import com.opensymphony.xwork2.Action;
20
import com.opensymphony.xwork2.ActionContext;
24
import com.opensymphony.xwork2.ActionContext;
21
import com.opensymphony.xwork2.ActionSupport;
25
import com.opensymphony.xwork2.ActionSupport;
22
import com.opensymphony.xwork2.ModelDriven;
26
import com.opensymphony.xwork2.ModelDriven;
23
27
24
/**
28
/**
25
 * Implements actions applicable to term editing
29
 * Implements actions applicable to term editing
26
 *
30
 *
27
 * @author Thomas Lahn
31
 * @author Thomas Lahn
28
 */
32
 */
29
public class TermAction extends ActionSupport implements ModelDriven<Term>
33
public class TermAction extends ActionSupport implements ModelDriven<Term>
30
{
34
{
31
  private static final long serialVersionUID = 1L;
35
  private static final long serialVersionUID = 1L;
32
36
33
  private Term term = new Term();
37
  private Term term = new Term();
34
  private List<Term> termList = new ArrayList<Term>();
38
  private List<Term> termList = new ArrayList<Term>();
35
  private final TermDAOImpl termDAO = new TermDAOImpl();
39
  private final TermDAO termDAO = new TermDAO();
36
  private final UserDAOImpl userDAO = new UserDAOImpl();
40
  private final UserDAO userDAO = new UserDAO();
37
41
38
  /**
42
  /**
39
   * Session object
43
   * Session object
40
   */
44
   */
41
  Map<String, Object> session = ActionContext.getContext().getSession();
45
  Map<String, Object> session = ActionContext.getContext().getSession();
42
46
43
  /**
47
  /**
44
   * @var <code>true</code> if the term is edited/renamed, <code>false</code>
48
   * @var <code>true</code> if the term is edited/renamed, <code>false</code>
45
   *      otherwise
49
   *      otherwise
46
   */
50
   */
47
  public boolean edit = false;
51
  public boolean edit = false;
48
52
49
  /**
53
  /**
50
   * @var <code>true</code> if a term was added, <code>false</code> otherwise
54
   * @var <code>true</code> if a term was added, <code>false</code> otherwise
51
   */
55
   */
52
  public boolean added = false;
56
  public boolean added = false;
53
57
54
  private final HttpServletRequest request = (HttpServletRequest) ActionContext
58
  private final HttpServletRequest request = (HttpServletRequest) ActionContext
55
      .getContext().get(StrutsStatics.HTTP_REQUEST);
59
      .getContext().get(StrutsStatics.HTTP_REQUEST);
56
60
57
  /**
61
  /**
58
   * The term that was just saved (added, renamed)
62
   * The term that was just saved (added, renamed)
59
   */
63
   */
60
  public Term savedTerm;
64
  public Term savedTerm;
61
65
-
 
66
  private final HistoryDAO historyDAO = new HistoryDAO();
-
 
67
-
 
68
  private Set<History> history;
-
 
69
62
  /*
70
  /*
63
   * (non-Javadoc)
71
   * (non-Javadoc)
64
   *
72
   *
65
   * @see com.opensymphony.xwork2.ModelDriven#getModel()
73
   * @see com.opensymphony.xwork2.ModelDriven#getModel()
66
   */
74
   */
67
  public Term getModel()
75
  public Term getModel()
68
  {
76
  {
69
    return this.term;
77
    return this.term;
70
  }
78
  }
71
79
72
  /**
80
  /**
73
   * DB query for term list
81
   * DB query for term list
74
   *
82
   *
75
   * @return SUCCESS
83
   * @return SUCCESS
76
   */
84
   */
77
  public String list()
85
  public String list()
78
  {
86
  {
79
    this.termList = this.termDAO.getTerms();
87
    this.termList = this.termDAO.getList();
80
    return Action.SUCCESS;
88
    return Action.SUCCESS;
81
  }
89
  }
82
90
83
  /**
91
  /**
84
   * Executes the DB query to save the user
92
   * Executes the DB query to save the user
85
   *
93
   *
86
   * @return {@link Action#SUCCESS}
94
   * @return {@link Action#SUCCESS}
87
   */
95
   */
88
  public String save()
96
  public String save()
89
  {
97
  {
90
    User user = this.userDAO.searchUsername((String) this.session
98
    User user = this.userDAO.getByUsername((String) this.session
91
        .get("username"));
99
        .get("username"));
-
 
100
    Date now = new Date();
-
 
101
    ObjectEntity obj;
92
102
-
 
103
    int action = 0;
93
    if ("false".equals(this.request.getParameter("edit")))
104
    if ("false".equals(this.request.getParameter("edit")))
94
    {
105
    {
95
      /* Add a new term */
106
      /* Add a new term */
96
      ObjectEntity obj = new ObjectEntity(user,
-
 
97
          new ObjectType(ObjectType.TERM), user, null, new Date(), false,
107
      obj = new ObjectEntity(user, new ObjectType(ObjectType.TERM), user, null,
98
          this.term, null, null);
108
          now, false, this.term, null, null);
-
 
109
99
      this.term.setObject(obj);
110
      this.term.setObject(obj);
100
      this.added = true;
111
      this.added = true;
-
 
112
      action = ActionType.ADD;
-
 
113
    }
-
 
114
    else
-
 
115
    {
-
 
116
      obj = new ObjectEntity();
-
 
117
      obj.setId(this.term.getObjectId());
-
 
118
      action = ActionType.RENAME;
101
    }
119
    }
102
-
 
103
    this.edit = false;
-
 
104
120
105
    String result = Action.SUCCESS;
121
    String result = Action.SUCCESS;
106
    if (!this.termDAO.saveOrUpdate(this.term))
122
    if (this.termDAO.saveOrUpdate(this.term))
-
 
123
    {
-
 
124
      String comment = this.request.getParameter("comment");
-
 
125
-
 
126
      History historyRecord = new History(user, new ActionType(action), obj,
-
 
127
          this.term.getName(), comment, now);
-
 
128
-
 
129
      this.historyDAO.saveOrUpdate(historyRecord);
-
 
130
    }
-
 
131
    else
107
    {
132
    {
108
      result = Action.ERROR;
133
      result = Action.ERROR;
109
    }
134
    }
110
135
111
    this.savedTerm = this.term;
136
    this.savedTerm = this.term;
112
    this.term = null;
137
    this.term = null;
113
138
114
    this.list();
139
    this.list();
115
140
116
    return result;
141
    return result;
117
  }
142
  }
118
143
119
  /**
144
  /**
120
   * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
145
   * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
121
   *         otherwise
146
   *         otherwise
122
   */
147
   */
123
  public String edit()
148
  public String edit()
124
  {
149
  {
125
    int id = this.getIdParameter();
150
    int id = this.getIdParameter();
126
151
127
    String result = Action.ERROR;
152
    String result = Action.ERROR;
128
    if (id > 0)
153
    if (id > 0)
129
    {
154
    {
130
      this.term = this.termDAO.getTermById(id);
155
      this.term = this.termDAO.getById(id);
131
      if (this.term != null)
156
      if (this.term != null)
132
      {
157
      {
133
        this.edit = true;
158
        this.edit = true;
134
        result = Action.SUCCESS;
159
        result = Action.SUCCESS;
135
      }
160
      }
136
    }
161
    }
137
162
138
    this.list();
163
    this.list();
139
164
140
    return result;
165
    return result;
141
  }
166
  }
142
167
143
  /**
168
  /**
144
   * deletes a term, gets the ID from the "id" parameter that was submitted with
169
   * deletes a term, gets the ID from the "id" parameter that was submitted with
145
   * the HTTP request
170
   * the HTTP request
146
   *
171
   *
147
   * @return String - either SUCCESS or ERROR constant
172
   * @return String - either SUCCESS or ERROR constant
148
   */
173
   */
149
  public String delete()
174
  public String delete()
150
  {
175
  {
151
176
152
    int id = this.getIdParameter();
177
    int id = this.getIdParameter();
153
178
154
    /* Check for malicious ID values */
179
    /* Check for malicious ID values */
155
    String result = Action.SUCCESS;
180
    String result = Action.SUCCESS;
156
    if (id > 0)
181
    if (id > 0)
157
    {
182
    {
158
      this.termDAO.deleteTerm(id);
183
      this.termDAO.delete(id);
159
    }
184
    }
160
    else
185
    else
161
    {
186
    {
162
      result = Action.ERROR;
187
      result = Action.ERROR;
163
    }
188
    }
164
189
165
    this.list();
190
    this.list();
166
191
167
    return result;
192
    return result;
168
  }
193
  }
169
194
170
  /**
195
  /**
171
   * Gets the ID Parameter for update / delete requests
196
   * Gets the ID Parameter for update / delete requests
172
   *
197
   *
173
   * @return int from the ID request. If not set or wrong, it gives back -1
198
   * @return int from the ID request. If not set or wrong, it gives back -1
174
   */
199
   */
175
  private int getIdParameter()
200
  private int getIdParameter()
176
  {
201
  {
177
    int id = -1;
202
    int id = -1;
178
    try
203
    try
179
    {
204
    {
180
      id = Integer.parseInt(this.request.getParameter("id")); //$NON-NLS-1$
205
      id = Integer.parseInt(this.request.getParameter("id")); //$NON-NLS-1$
181
    }
206
    }
182
    catch (Exception e)
207
    catch (Exception e)
183
    {
208
    {
184
      /* TODO: Logging - wrong parameter set */
209
      /* TODO: Logging - wrong parameter set */
185
    }
210
    }
186
211
187
    return id;
212
    return id;
188
  }
213
  }
189
214
190
  /* Standard getters and setters */
215
  /* Standard getters and setters */
191
216
192
  /**
217
  /**
193
   * @return The term edited with this instance
218
   * @return The term edited with this instance
194
   */
219
   */
195
  public Term getTerm()
220
  public Term getTerm()
196
  {
221
  {
197
    return this.term;
222
    return this.term;
198
  }
223
  }
199
224
200
  /**
225
  /**
201
   * @param term
226
   * @param term
202
   *          The term edited with this instance
227
   *          The term edited with this instance
203
   */
228
   */
204
  public void setTerm(Term term)
229
  public void setTerm(Term term)
205
  {
230
  {
206
    this.term = term;
231
    this.term = term;
207
  }
232
  }
208
233
209
  /**
234
  /**
210
   * @return The list of terms edited with this instance
235
   * @return The list of terms edited with this instance
211
   */
236
   */
212
  public List<Term> getTermList()
237
  public List<Term> getTermList()
213
  {
238
  {
214
    return this.termList;
239
    return this.termList;
215
  }
240
  }
216
241
217
  /**
242
  /**
218
   * @param termList
243
   * @param termList
219
   *          The list of terms edited with this instance
244
   *          The list of terms edited with this instance
220
   */
245
   */
221
  public void setTermList(List<Term> termList)
246
  public void setTermList(List<Term> termList)
222
  {
247
  {
223
    this.termList = termList;
248
    this.termList = termList;
224
  }
249
  }
-
 
250
-
 
251
  /**
-
 
252
   * @return the histories
-
 
253
   */
-
 
254
  public Set<History> getHistories()
-
 
255
  {
-
 
256
    return this.history;
-
 
257
  }
-
 
258
-
 
259
  /**
-
 
260
   * @param histories
-
 
261
   *          the histories to set
-
 
262
   */
-
 
263
  public void setHistories(Set<History> histories)
-
 
264
  {
-
 
265
    this.history = histories;
-
 
266
  }
225
}
267
}
226
 
268