Subversion Repositories WebE

Rev

Rev 35 | Details | Compare with Previous | Last modification | View Log | RSS feed

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