Subversion Repositories WebE

Rev

Rev 35 | Go to most recent revision | Details | 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
 
52
  private final HttpServletRequest request = (HttpServletRequest) ActionContext
53
      .getContext().get(StrutsStatics.HTTP_REQUEST);
54
 
55
  /*
56
   * (non-Javadoc)
57
   *
58
   * @see com.opensymphony.xwork2.ModelDriven#getModel()
59
   */
60
  public Relationship getModel()
61
  {
62
    return this.relationship;
63
  }
64
 
65
  /**
66
   * Prepares to add a relationship
67
   *
68
   * @return
69
   */
70
  public String add()
71
  {
72
    this.setTerms(this.termDAO.getTerms());
73
    this.setRelationshipTypes(this.relationshipTypeDAO.getRelTypes());
74
    return Action.SUCCESS;
75
  }
76
 
77
  /**
78
   * Executes the DB query to save the user
79
   *
80
   * @return {@link Action#SUCCESS}
81
   */
82
  public String save()
83
  {
84
    this.relationship.setTermFrom(this.termDAO.getTermById(Integer
85
        .parseInt(this.request.getParameter("term1"))));
86
    this.relationship.setTermTo(this.termDAO.getTermById(Integer
87
        .parseInt(this.request.getParameter("term2"))));
88
    this.relationship.setRelationshipType(this.relationshipTypeDAO
89
        .getRelTypeById(Integer.parseInt(this.request.getParameter("type"))));
90
 
91
    if (!"1".equals(this.request.getParameter("edit")))
92
    {
93
      User user = this.userDAO.searchUsername((String) this.session
94
          .get("username"));
95
      ObjectEntity obj = new ObjectEntity(user, new ObjectType(
96
          ObjectType.RELATIONSHIP), user, null, new Date(), false, null, null,
97
          this.relationship);
98
      this.relationship.setObject(obj);
99
    }
100
 
101
    if (this.relationshipDAO.saveOrUpdate(this.relationship))
102
    {
103
      return Action.SUCCESS;
104
    }
105
 
106
    return Action.ERROR;
107
  }
108
 
109
  /**
110
   * DB query for relationship list
111
   *
112
   * @return SUCCESS
113
   */
114
  public String list()
115
  {
116
    this.relationshipList = this.relationshipDAO.listRelationships();
117
    return Action.SUCCESS;
118
  }
119
 
120
  /**
121
   * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
122
   *         otherwise
123
   */
124
  public String edit()
125
  {
126
    int id = this.getIdParameter();
127
 
128
    if (id > 0)
129
    {
130
      this.relationship = this.relationshipDAO.getRelationshipById(id);
131
      if (this.relationship != null)
132
      {
133
        return Action.SUCCESS;
134
      }
135
    }
136
 
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;
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
 
157
    return id;
158
  }
159
 
160
  /**
161
   * deletes a user, gets the ID from the "id" parameter that was submitted with
162
   * the HTTP request
163
   *
164
   * @return String - either SUCCESS or ERROR constant
165
   */
166
  public String delete()
167
  {
168
 
169
    int id = this.getIdParameter();
170
 
171
    /* Check for malicious ID values */
172
    if (id > 0)
173
    {
174
      this.relationshipDAO.deleteRelationship(id);
175
      return Action.SUCCESS;
176
    }
177
    else
178
    {
179
      return Action.ERROR;
180
    }
181
  }
182
 
183
  /*
184
   * Standard getters and setters
185
   */
186
 
187
  /**
188
   * @return The relationship edited with this instance
189
   */
190
  public Relationship getRelationship()
191
  {
192
    return this.relationship;
193
  }
194
 
195
  /**
196
   * @param relationship
197
   *          The relationship edited with this instance
198
   */
199
  public void setRelationship(Relationship relationship)
200
  {
201
    this.relationship = relationship;
202
  }
203
 
204
  /**
205
   * @return The list of terms edited with this instance
206
   */
207
  public List<Relationship> getRelationshipList()
208
  {
209
    return this.relationshipList;
210
  }
211
 
212
  /**
213
   * @param relationshipList
214
   *          The list of terms edited with this instance
215
   */
216
  public void setRelationshipList(List<Relationship> relationshipList)
217
  {
218
    this.relationshipList = relationshipList;
219
  }
220
 
221
  /**
222
   * @return the relationshipTypes
223
   */
224
  public List<RelationshipType> getRelationshipTypes()
225
  {
226
    return this.relationshipTypes;
227
  }
228
 
229
  /**
230
   * @param relationshipTypes
231
   *          the relationshipTypes to set
232
   */
233
  public void setRelationshipTypes(List<RelationshipType> relationshipTypes)
234
  {
235
    this.relationshipTypes = relationshipTypes;
236
  }
237
 
238
  /**
239
   * @return the terms
240
   */
241
  public List<Term> getTerms()
242
  {
243
    return this.terms;
244
  }
245
 
246
  /**
247
   * @param terms
248
   *          the terms to set
249
   */
250
  public void setTerms(List<Term> terms)
251
  {
252
    this.terms = terms;
253
  }
254
}