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.RelationshipDAOImpl;
14
import ch.ffhs.webE.dao.RelationshipDAO;
13
import ch.ffhs.webE.dao.RelationshipTypeDAOImpl;
15
import ch.ffhs.webE.dao.RelationshipTypeDAO;
14
import ch.ffhs.webE.dao.TermDAOImpl;
16
import ch.ffhs.webE.dao.TermDAO;
15
import ch.ffhs.webE.dao.UserDAOImpl;
17
import ch.ffhs.webE.dao.UserDAO;
-
 
18
import ch.ffhs.webE.domain.ActionType;
-
 
19
import ch.ffhs.webE.domain.History;
16
import ch.ffhs.webE.domain.ObjectEntity;
20
import ch.ffhs.webE.domain.ObjectEntity;
17
import ch.ffhs.webE.domain.ObjectType;
21
import ch.ffhs.webE.domain.ObjectType;
18
import ch.ffhs.webE.domain.Relationship;
22
import ch.ffhs.webE.domain.Relationship;
19
import ch.ffhs.webE.domain.RelationshipType;
23
import ch.ffhs.webE.domain.RelationshipType;
20
import ch.ffhs.webE.domain.Term;
24
import ch.ffhs.webE.domain.Term;
21
import ch.ffhs.webE.domain.User;
25
import ch.ffhs.webE.domain.User;
22
26
23
import com.opensymphony.xwork2.Action;
27
import com.opensymphony.xwork2.Action;
24
import com.opensymphony.xwork2.ActionContext;
28
import com.opensymphony.xwork2.ActionContext;
25
import com.opensymphony.xwork2.ActionSupport;
29
import com.opensymphony.xwork2.ActionSupport;
26
import com.opensymphony.xwork2.ModelDriven;
30
import com.opensymphony.xwork2.ModelDriven;
27
31
28
/**
32
/**
29
 * Implements actions applicable to relationship editing
33
 * Implements actions applicable to relationship editing
30
 *
34
 *
31
 * @author Thomas Lahn
35
 * @author Thomas Lahn
32
 */
36
 */
33
public class RelationshipAction extends ActionSupport implements
37
public class RelationshipAction extends ActionSupport implements
34
    ModelDriven<Relationship>
38
    ModelDriven<Relationship>
35
{
39
{
36
  private static final long serialVersionUID = 1L;
40
  private static final long serialVersionUID = 1L;
37
41
38
  private List<RelationshipType> relationshipTypes = new ArrayList<RelationshipType>();
42
  private List<RelationshipType> relationshipTypes = new ArrayList<RelationshipType>();
39
  private final RelationshipTypeDAOImpl relationshipTypeDAO = new RelationshipTypeDAOImpl();
43
  private final RelationshipTypeDAO relationshipTypeDAO = new RelationshipTypeDAO();
-
 
44
40
  private List<Term> terms = new ArrayList<Term>();
45
  private List<Term> terms = new ArrayList<Term>();
41
  private final TermDAOImpl termDAO = new TermDAOImpl();
46
  private final TermDAO termDAO = new TermDAO();
-
 
47
42
  private List<Relationship> relationshipList = new ArrayList<Relationship>();
48
  private List<Relationship> relationshipList = new ArrayList<Relationship>();
43
  private final RelationshipDAOImpl relationshipDAO = new RelationshipDAOImpl();
-
 
44
  private final UserDAOImpl userDAO = new UserDAOImpl();
-
 
45
  private Relationship relationship = new Relationship();
49
  private Relationship relationship = new Relationship();
46
50
47
  /**
51
  /**
-
 
52
   * The term that was just saved (added, renamed)
-
 
53
   */
-
 
54
  private Relationship modifiedRelationship;
-
 
55
-
 
56
  private final RelationshipDAO relationshipDAO = new RelationshipDAO();
-
 
57
-
 
58
  private final UserDAO userDAO = new UserDAO();
-
 
59
-
 
60
  private Set<History> history;
-
 
61
  private final HistoryDAO historyDAO = new HistoryDAO();
-
 
62
-
 
63
  /**
48
   * Session object
64
   * Session object
49
   */
65
   */
50
  Map<String, Object> session = ActionContext.getContext().getSession();
66
  Map<String, Object> session = ActionContext.getContext().getSession();
51
67
52
  /**
68
  /**
53
   * @var <code>true</code> if the relationship is to be edited/renamed,
69
   * @var <code>true</code> if the relationship is to be edited/renamed,
54
   *      <code>false</code> otherwise
70
   *      <code>false</code> otherwise
55
   */
71
   */
56
  public boolean edit = false;
72
  public boolean edit = false;
57
73
58
  /**
74
  /**
59
   * @var <code>true</code> if a relationship was added, <code>false</code>
75
   * @var <code>true</code> if a relationship was added, <code>false</code>
60
   *      otherwise
76
   *      otherwise
61
   */
77
   */
62
  public boolean added = false;
78
  public boolean added = false;
63
79
64
  private final HttpServletRequest request = (HttpServletRequest) ActionContext
80
  private final HttpServletRequest request = (HttpServletRequest) ActionContext
65
      .getContext().get(StrutsStatics.HTTP_REQUEST);
81
      .getContext().get(StrutsStatics.HTTP_REQUEST);
66
82
67
  /**
-
 
68
   * The term that was just saved (added, renamed)
-
 
69
   */
-
 
70
  private Relationship modifiedRelationship;
-
 
71
-
 
72
  /*
83
  /*
73
   * (non-Javadoc)
84
   * (non-Javadoc)
74
   *
85
   *
75
   * @see com.opensymphony.xwork2.ModelDriven#getModel()
86
   * @see com.opensymphony.xwork2.ModelDriven#getModel()
76
   */
87
   */
77
  public Relationship getModel()
88
  public Relationship getModel()
78
  {
89
  {
79
    return this.relationship;
90
    return this.relationship;
80
  }
91
  }
81
92
82
  /**
93
  /**
83
   * DB query for relationship list
94
   * DB query for relationship list
84
   *
95
   *
85
   * @return SUCCESS
96
   * @return SUCCESS
86
   */
97
   */
87
  public String list()
98
  public String list()
88
  {
99
  {
89
    this.setTerms(this.termDAO.getTerms());
100
    this.setTerms(this.termDAO.getList());
90
    this.setRelationshipTypes(this.relationshipTypeDAO.getRelTypes());
101
    this.setRelationshipTypes(this.relationshipTypeDAO.getList());
91
    this.setRelationshipList(this.relationshipDAO.getRelationshipList());
102
    this.setRelationshipList(this.relationshipDAO.getList());
92
    return Action.SUCCESS;
103
    return Action.SUCCESS;
93
  }
104
  }
94
105
95
  /**
106
  /**
96
   * Executes the DB query to save the relationship
107
   * Executes the DB query to save the relationship
97
   *
108
   *
98
   * @return {@link Action#SUCCESS}
109
   * @return {@link Action#SUCCESS}
99
   */
110
   */
100
  public String save()
111
  public String save()
101
  {
112
  {
102
    this.relationship.setTermFrom(this.termDAO.getTermById(Integer
113
    this.relationship.setTermFrom(this.termDAO.getById(Integer
103
        .parseInt(this.request.getParameter("term1"))));
114
        .parseInt(this.request.getParameter("term1"))));
104
    this.relationship.setTermTo(this.termDAO.getTermById(Integer
115
    this.relationship.setTermTo(this.termDAO.getById(Integer
105
        .parseInt(this.request.getParameter("term2"))));
116
        .parseInt(this.request.getParameter("term2"))));
106
    this.relationship.setRelationshipType(this.relationshipTypeDAO
117
    this.relationship.setRelationshipType(this.relationshipTypeDAO
107
        .getRelTypeById(Integer.parseInt(this.request.getParameter("type"))));
118
        .getById(Integer.parseInt(this.request.getParameter("type"))));
108
119
109
    User user = this.userDAO.searchUsername((String) this.session
120
    User user = this.userDAO.getByUsername((String) this.session
110
        .get("username"));
121
        .get("username"));
-
 
122
    Date now = new Date();
-
 
123
    ObjectEntity obj;
-
 
124
    int action = 0;
111
125
112
    if ("false".equals(this.request.getParameter("edit")))
126
    if ("false".equals(this.request.getParameter("edit")))
113
    {
127
    {
114
      /* Add a new relationship */
128
      /* Add a new relationship */
115
      ObjectEntity obj = new ObjectEntity(user, new ObjectType(
129
      obj = new ObjectEntity(user, new ObjectType(ObjectType.RELATIONSHIP),
116
          ObjectType.RELATIONSHIP), user, null, new Date(), false, null, null,
130
          user, null, new Date(), false, null, null, this.relationship);
117
          this.relationship);
-
 
118
      this.relationship.setObject(obj);
131
      this.relationship.setObject(obj);
119
      this.added = true;
132
      this.added = true;
-
 
133
      action = ActionType.ADD;
-
 
134
    }
-
 
135
    else
-
 
136
    {
-
 
137
      obj = new ObjectEntity();
-
 
138
      obj.setId(this.relationship.getObjectId());
-
 
139
      action = ActionType.MODIFY;
120
    }
140
    }
121
141
122
    this.edit = false;
142
    this.edit = false;
123
143
124
    String result = Action.SUCCESS;
144
    String result = Action.SUCCESS;
125
    if (!this.relationshipDAO.saveOrUpdate(this.relationship))
145
    if (this.relationshipDAO.saveOrUpdate(this.relationship))
-
 
146
    {
-
 
147
      String comment = this.request.getParameter("comment");
-
 
148
-
 
149
      History historyRecord = new History(user, new ActionType(action), obj,
-
 
150
          "(" + this.relationship.getTermFrom().getName() + ") ("
-
 
151
              + this.relationship.getRelationshipType().getNameFrom() + ") ("
-
 
152
              + this.relationship.getTermTo().getName() + ")", comment, now);
-
 
153
-
 
154
      this.historyDAO.saveOrUpdate(historyRecord);
-
 
155
    }
-
 
156
    else
126
    {
157
    {
127
      result = Action.ERROR;
158
      result = Action.ERROR;
128
    }
159
    }
129
160
130
    this.setModifiedRelationship(this.relationship);
161
    this.setModifiedRelationship(this.relationship);
131
    this.setRelationship(null);
162
    this.setRelationship(null);
132
163
133
    this.list();
164
    this.list();
134
165
135
    return result;
166
    return result;
136
  }
167
  }
137
168
138
  /**
169
  /**
139
   * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
170
   * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
140
   *         otherwise
171
   *         otherwise
141
   */
172
   */
142
  public String edit()
173
  public String edit()
143
  {
174
  {
144
    int id = this.getIdParameter();
175
    int id = this.getIdParameter();
145
176
146
    String result = Action.ERROR;
177
    String result = Action.ERROR;
147
    if (id > 0)
178
    if (id > 0)
148
    {
179
    {
149
      this.setRelationship(this.relationshipDAO.getRelationshipById(id));
180
      this.setRelationship(this.relationshipDAO.getById(id));
150
      if (this.getRelationship() != null)
181
      if (this.getRelationship() != null)
151
      {
182
      {
152
        this.edit = true;
183
        this.edit = true;
153
        result = Action.SUCCESS;
184
        result = Action.SUCCESS;
154
      }
185
      }
155
    }
186
    }
156
187
157
    this.list();
188
    this.list();
158
189
159
    return result;
190
    return result;
160
  }
191
  }
161
192
162
  /**
193
  /**
163
   * deletes a user, gets the ID from the "id" parameter that was submitted with
194
   * deletes a user, gets the ID from the "id" parameter that was submitted with
164
   * the HTTP request
195
   * the HTTP request
165
   *
196
   *
166
   * @return String - either SUCCESS or ERROR constant
197
   * @return String - either SUCCESS or ERROR constant
167
   */
198
   */
168
  public String delete()
199
  public String delete()
169
  {
200
  {
170
    int id = this.getIdParameter();
201
    int id = this.getIdParameter();
171
202
172
    /* Check for malicious ID values */
203
    /* Check for malicious ID values */
173
    String result = Action.SUCCESS;
204
    String result = Action.SUCCESS;
174
    if (id > 0)
205
    if (id > 0)
175
    {
206
    {
176
      this.relationshipDAO.deleteRelationship(id);
207
      this.relationshipDAO.delete(id);
177
    }
208
    }
178
    else
209
    else
179
    {
210
    {
180
      result = Action.ERROR;
211
      result = Action.ERROR;
181
    }
212
    }
182
213
183
    this.list();
214
    this.list();
184
215
185
    return result;
216
    return result;
186
  }
217
  }
187
218
188
  /**
219
  /**
189
   * Gets the ID Parameter for update / delete requests
220
   * Gets the ID Parameter for update / delete requests
190
   *
221
   *
191
   * @return int from the ID request. If not set or wrong, it gives back -1
222
   * @return int from the ID request. If not set or wrong, it gives back -1
192
   */
223
   */
193
  private int getIdParameter()
224
  private int getIdParameter()
194
  {
225
  {
195
    int id = -1;
226
    int id = -1;
196
    try
227
    try
197
    {
228
    {
198
      id = Integer.parseInt(this.request.getParameter("id")); //$NON-NLS-1$
229
      id = Integer.parseInt(this.request.getParameter("id")); //$NON-NLS-1$
199
    }
230
    }
200
    catch (Exception e)
231
    catch (Exception e)
201
    {
232
    {
202
      /* TODO: Logging - wrong parameter set */
233
      /* TODO: Logging - wrong parameter set */
203
    }
234
    }
204
235
205
    return id;
236
    return id;
206
  }
237
  }
207
238
208
  /* Standard getters and setters */
239
  /* Standard getters and setters */
209
240
210
  /**
241
  /**
211
   * @return The relationship edited with this instance
242
   * @return The relationship edited with this instance
212
   */
243
   */
213
  public Relationship getRelationship()
244
  public Relationship getRelationship()
214
  {
245
  {
215
    return this.relationship;
246
    return this.relationship;
216
  }
247
  }
217
248
218
  /**
249
  /**
219
   * @param relationship
250
   * @param relationship
220
   *          The relationship edited with this instance
251
   *          The relationship edited with this instance
221
   */
252
   */
222
  public void setRelationship(Relationship relationship)
253
  public void setRelationship(Relationship relationship)
223
  {
254
  {
224
    this.relationship = relationship;
255
    this.relationship = relationship;
225
  }
256
  }
226
257
227
  /**
258
  /**
228
   * @return The list of terms edited with this instance
259
   * @return The list of terms edited with this instance
229
   */
260
   */
230
  public List<Relationship> getRelationshipList()
261
  public List<Relationship> getRelationshipList()
231
  {
262
  {
232
    return this.relationshipList;
263
    return this.relationshipList;
233
  }
264
  }
234
265
235
  /**
266
  /**
236
   * @param relationshipList
267
   * @param relationshipList
237
   *          The list of terms edited with this instance
268
   *          The list of terms edited with this instance
238
   */
269
   */
239
  public void setRelationshipList(List<Relationship> relationshipList)
270
  public void setRelationshipList(List<Relationship> relationshipList)
240
  {
271
  {
241
    this.relationshipList = relationshipList;
272
    this.relationshipList = relationshipList;
242
  }
273
  }
243
274
244
  /**
275
  /**
245
   * @return the relationshipTypes
276
   * @return the relationshipTypes
246
   */
277
   */
247
  public List<RelationshipType> getRelationshipTypes()
278
  public List<RelationshipType> getRelationshipTypes()
248
  {
279
  {
249
    return this.relationshipTypes;
280
    return this.relationshipTypes;
250
  }
281
  }
251
282
252
  /**
283
  /**
253
   * @param relationshipTypes
284
   * @param relationshipTypes
254
   *          the relationshipTypes to set
285
   *          the relationshipTypes to set
255
   */
286
   */
256
  public void setRelationshipTypes(List<RelationshipType> relationshipTypes)
287
  public void setRelationshipTypes(List<RelationshipType> relationshipTypes)
257
  {
288
  {
258
    this.relationshipTypes = relationshipTypes;
289
    this.relationshipTypes = relationshipTypes;
259
  }
290
  }
260
291
261
  /**
292
  /**
262
   * @return the terms
293
   * @return the terms
263
   */
294
   */
264
  public List<Term> getTerms()
295
  public List<Term> getTerms()
265
  {
296
  {
266
    return this.terms;
297
    return this.terms;
267
  }
298
  }
268
299
269
  /**
300
  /**
270
   * @param terms
301
   * @param terms
271
   *          the terms to set
302
   *          the terms to set
272
   */
303
   */
273
  public void setTerms(List<Term> terms)
304
  public void setTerms(List<Term> terms)
274
  {
305
  {
275
    this.terms = terms;
306
    this.terms = terms;
276
  }
307
  }
277
308
278
  /**
309
  /**
279
   * @return the modifiedRelationship
310
   * @return the modifiedRelationship
280
   */
311
   */
281
  public Relationship getModifiedRelationship()
312
  public Relationship getModifiedRelationship()
282
  {
313
  {
283
    return this.modifiedRelationship;
314
    return this.modifiedRelationship;
284
  }
315
  }
285
316
286
  /**
317
  /**
287
   * @param modifiedRelationship
318
   * @param modifiedRelationship
288
   *          the modifiedRelationship to set
319
   *          the modifiedRelationship to set
289
   */
320
   */
290
  public void setModifiedRelationship(Relationship modifiedRelationship)
321
  public void setModifiedRelationship(Relationship modifiedRelationship)
291
  {
322
  {
292
    this.modifiedRelationship = modifiedRelationship;
323
    this.modifiedRelationship = modifiedRelationship;
293
  }
324
  }
-
 
325
-
 
326
  /**
-
 
327
   * @return the history
-
 
328
   */
-
 
329
  public Set<History> getHistory()
-
 
330
  {
-
 
331
    return this.history;
-
 
332
  }
-
 
333
-
 
334
  /**
-
 
335
   * @param history
-
 
336
   *          the history to set
-
 
337
   */
-
 
338
  public void setHistory(Set<History> history)
-
 
339
  {
-
 
340
    this.history = history;
-
 
341
  }
294
}
342
}
295
 
343