Subversion Repositories WebE

Rev

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