Subversion Repositories WebE

Rev

Rev 34 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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