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.TermDAOImpl;
12
import ch.ffhs.webE.dao.TermDAOImpl;
13
import ch.ffhs.webE.dao.UserDAOImpl;
13
import ch.ffhs.webE.dao.UserDAOImpl;
14
import ch.ffhs.webE.domain.ObjectEntity;
14
import ch.ffhs.webE.domain.ObjectEntity;
15
import ch.ffhs.webE.domain.ObjectType;
15
import ch.ffhs.webE.domain.ObjectType;
16
import ch.ffhs.webE.domain.Term;
16
import ch.ffhs.webE.domain.Term;
17
import ch.ffhs.webE.domain.User;
17
import ch.ffhs.webE.domain.User;
18
18
19
import com.opensymphony.xwork2.Action;
19
import com.opensymphony.xwork2.Action;
20
import com.opensymphony.xwork2.ActionContext;
20
import com.opensymphony.xwork2.ActionContext;
21
import com.opensymphony.xwork2.ActionSupport;
21
import com.opensymphony.xwork2.ActionSupport;
22
import com.opensymphony.xwork2.ModelDriven;
22
import com.opensymphony.xwork2.ModelDriven;
23
23
24
/**
24
/**
25
 * Implements actions applicable to term editing
25
 * Implements actions applicable to term editing
26
 *
26
 *
27
 * @author Thomas Lahn
27
 * @author Thomas Lahn
28
 */
28
 */
29
public class TermAction extends ActionSupport implements ModelDriven<Term>
29
public class TermAction extends ActionSupport implements ModelDriven<Term>
30
{
30
{
31
  private static final long serialVersionUID = 1L;
31
  private static final long serialVersionUID = 1L;
32
32
33
  private Term term = new Term();
33
  private Term term = new Term();
34
  private List<Term> termList = new ArrayList<Term>();
34
  private List<Term> termList = new ArrayList<Term>();
35
  private final TermDAOImpl termDAO = new TermDAOImpl();
35
  private final TermDAOImpl termDAO = new TermDAOImpl();
36
  private final UserDAOImpl userDAO = new UserDAOImpl();
36
  private final UserDAOImpl userDAO = new UserDAOImpl();
37
37
38
  /**
38
  /**
39
   * Session object
39
   * Session object
40
   */
40
   */
41
  Map<String, Object> session = ActionContext.getContext().getSession();
41
  Map<String, Object> session = ActionContext.getContext().getSession();
42
42
-
 
43
  /**
-
 
44
   * @var <code>true</code> if the term is edited/renamed, <code>false</code>
-
 
45
   *      otherwise
-
 
46
   */
-
 
47
  public boolean edit = false;
-
 
48
-
 
49
  /**
-
 
50
   * @var <code>true</code> if a term was added, <code>false</code> otherwise
-
 
51
   */
-
 
52
  public boolean added = false;
-
 
53
43
  private final HttpServletRequest request = (HttpServletRequest) ActionContext
54
  private final HttpServletRequest request = (HttpServletRequest) ActionContext
44
      .getContext().get(StrutsStatics.HTTP_REQUEST);
55
      .getContext().get(StrutsStatics.HTTP_REQUEST);
45
56
-
 
57
  /**
-
 
58
   * The term that was just saved (added, renamed)
-
 
59
   */
-
 
60
  public Term savedTerm;
-
 
61
46
  /*
62
  /*
47
   * (non-Javadoc)
63
   * (non-Javadoc)
48
   *
64
   *
49
   * @see com.opensymphony.xwork2.ModelDriven#getModel()
65
   * @see com.opensymphony.xwork2.ModelDriven#getModel()
50
   */
66
   */
51
  public Term getModel()
67
  public Term getModel()
52
  {
68
  {
53
    return this.term;
69
    return this.term;
54
  }
70
  }
55
71
56
  /**
72
  /**
-
 
73
   * DB query for term list
-
 
74
   *
-
 
75
   * @return SUCCESS
-
 
76
   */
-
 
77
  public String list()
-
 
78
  {
-
 
79
    this.termList = this.termDAO.getTerms();
-
 
80
    return Action.SUCCESS;
-
 
81
  }
-
 
82
-
 
83
  /**
57
   * Executes the DB query to save the user
84
   * Executes the DB query to save the user
58
   *
85
   *
59
   * @return {@link Action#SUCCESS}
86
   * @return {@link Action#SUCCESS}
60
   */
87
   */
61
  public String save()
88
  public String save()
62
  {
89
  {
-
 
90
    User user = this.userDAO.searchUsername((String) this.session
-
 
91
        .get("username"));
-
 
92
63
    if (!"1".equals(this.request.getParameter("edit")))
93
    if ("false".equals(this.request.getParameter("edit")))
64
    {
94
    {
65
      User user = this.userDAO.searchUsername((String) this.session
-
 
66
          .get("username"));
95
      /* Add a new term */
67
      ObjectEntity obj = new ObjectEntity(user,
96
      ObjectEntity obj = new ObjectEntity(user,
68
          new ObjectType(ObjectType.TERM), user, null, new Date(), false,
97
          new ObjectType(ObjectType.TERM), user, null, new Date(), false,
69
          this.term, null, null);
98
          this.term, null, null);
70
      this.term.setObject(obj);
99
      this.term.setObject(obj);
-
 
100
      this.added = true;
71
    }
101
    }
72
102
-
 
103
    this.edit = false;
-
 
104
-
 
105
    String result = Action.SUCCESS;
73
    if (this.termDAO.saveOrUpdate(this.term))
106
    if (!this.termDAO.saveOrUpdate(this.term))
74
    {
107
    {
75
      return Action.SUCCESS;
108
      result = Action.ERROR;
76
    }
109
    }
77
110
78
    return Action.ERROR;
111
    this.savedTerm = this.term;
79
  }
112
    this.term = null;
80
113
81
  /**
-
 
82
   * DB query for term list
-
 
83
   *
-
 
84
   * @return SUCCESS
-
 
85
   */
-
 
86
  public String list()
114
    this.list();
87
  {
115
88
    this.termList = this.termDAO.getTerms();
-
 
89
    return Action.SUCCESS;
116
    return result;
90
  }
117
  }
91
118
92
  /**
119
  /**
93
   * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
120
   * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
94
   *         otherwise
121
   *         otherwise
95
   */
122
   */
96
  public String edit()
123
  public String edit()
97
  {
124
  {
98
    int id = this.getIdParameter();
125
    int id = this.getIdParameter();
99
126
-
 
127
    String result = Action.ERROR;
100
    if (id > 0)
128
    if (id > 0)
101
    {
129
    {
102
      this.term = this.termDAO.getTermById(id);
130
      this.term = this.termDAO.getTermById(id);
103
      if (this.term != null)
131
      if (this.term != null)
104
      {
132
      {
105
        this.term.edit = true;
133
        this.edit = true;
106
        return Action.SUCCESS;
134
        result = Action.SUCCESS;
107
      }
135
      }
108
    }
136
    }
109
137
110
    return Action.ERROR;
-
 
111
  }
-
 
112
-
 
113
  /**
-
 
114
   * Gets the ID Parameter for update / delete requests
-
 
115
   *
-
 
116
   * @return int from the ID request. If not set or wrong, it gives back -1
-
 
117
   */
-
 
118
  private int getIdParameter()
-
 
119
  {
-
 
120
    int id = -1;
138
    this.list();
121
    try
-
 
122
    {
-
 
123
      id = Integer.parseInt(this.request.getParameter("id")); //$NON-NLS-1$
-
 
124
    }
-
 
125
    catch (Exception e)
-
 
126
    {
-
 
127
      /* TODO: Logging - wrong parameter set */
-
 
128
    }
-
 
129
139
130
    return id;
140
    return result;
131
  }
141
  }
132
142
133
  /**
143
  /**
134
   * deletes a user, gets the ID from the "id" parameter that was submitted with
144
   * deletes a term, gets the ID from the "id" parameter that was submitted with
135
   * the HTTP request
145
   * the HTTP request
136
   *
146
   *
137
   * @return String - either SUCCESS or ERROR constant
147
   * @return String - either SUCCESS or ERROR constant
138
   */
148
   */
139
  public String delete()
149
  public String delete()
140
  {
150
  {
141
151
142
    int id = this.getIdParameter();
152
    int id = this.getIdParameter();
143
153
144
    /* Check for malicious ID values */
154
    /* Check for malicious ID values */
-
 
155
    String result = Action.SUCCESS;
145
    if (id > 0)
156
    if (id > 0)
146
    {
157
    {
147
      this.termDAO.deleteTerm(id);
158
      this.termDAO.deleteTerm(id);
148
      return Action.SUCCESS;
-
 
149
    }
159
    }
150
    else
160
    else
151
    {
161
    {
152
      return Action.ERROR;
162
      result = Action.ERROR;
153
    }
163
    }
-
 
164
-
 
165
    this.list();
-
 
166
-
 
167
    return result;
154
  }
168
  }
155
169
156
  /*
170
  /**
157
   * Standard getters and setters
171
   * Gets the ID Parameter for update / delete requests
-
 
172
   *
-
 
173
   * @return int from the ID request. If not set or wrong, it gives back -1
158
   */
174
   */
-
 
175
  private int getIdParameter()
-
 
176
  {
-
 
177
    int id = -1;
-
 
178
    try
-
 
179
    {
-
 
180
      id = Integer.parseInt(this.request.getParameter("id")); //$NON-NLS-1$
-
 
181
    }
-
 
182
    catch (Exception e)
-
 
183
    {
-
 
184
      /* TODO: Logging - wrong parameter set */
-
 
185
    }
-
 
186
-
 
187
    return id;
-
 
188
  }
-
 
189
-
 
190
  /* Standard getters and setters */
159
191
160
  /**
192
  /**
161
   * @return The term edited with this instance
193
   * @return The term edited with this instance
162
   */
194
   */
163
  public Term getTerm()
195
  public Term getTerm()
164
  {
196
  {
165
    return this.term;
197
    return this.term;
166
  }
198
  }
167
199
168
  /**
200
  /**
169
   * @param term
201
   * @param term
170
   *          The term edited with this instance
202
   *          The term edited with this instance
171
   */
203
   */
172
  public void setTerm(Term term)
204
  public void setTerm(Term term)
173
  {
205
  {
174
    this.term = term;
206
    this.term = term;
175
  }
207
  }
176
208
177
  /**
209
  /**
178
   * @return The list of terms edited with this instance
210
   * @return The list of terms edited with this instance
179
   */
211
   */
180
  public List<Term> getTermList()
212
  public List<Term> getTermList()
181
  {
213
  {
182
    return this.termList;
214
    return this.termList;
183
  }
215
  }
184
216
185
  /**
217
  /**
186
   * @param termList
218
   * @param termList
187
   *          The list of terms edited with this instance
219
   *          The list of terms edited with this instance
188
   */
220
   */
189
  public void setTermList(List<Term> termList)
221
  public void setTermList(List<Term> termList)
190
  {
222
  {
191
    this.termList = termList;
223
    this.termList = termList;
192
  }
224
  }
193
}
225
}
194
 
226