Rev 35 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
31 | PointedEar | 1 | package ch.ffhs.webE.action; |
2 | |||
3 | import java.util.ArrayList; |
||
33 | PointedEar | 4 | import java.util.Date; |
31 | PointedEar | 5 | import java.util.List; |
33 | PointedEar | 6 | import java.util.Map; |
37 | PointedEar | 7 | import java.util.Set; |
31 | 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.TermDAO; |
||
15 | import ch.ffhs.webE.dao.UserDAO; |
||
16 | import ch.ffhs.webE.domain.ActionType; |
||
17 | import ch.ffhs.webE.domain.History; |
||
33 | PointedEar | 18 | import ch.ffhs.webE.domain.ObjectEntity; |
19 | import ch.ffhs.webE.domain.ObjectType; |
||
31 | PointedEar | 20 | import ch.ffhs.webE.domain.Term; |
33 | PointedEar | 21 | import ch.ffhs.webE.domain.User; |
31 | PointedEar | 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 term editing |
||
30 | * |
||
31 | * @author Thomas Lahn |
||
32 | */ |
||
33 | public class TermAction extends ActionSupport implements ModelDriven<Term> |
||
34 | { |
||
33 | PointedEar | 35 | private static final long serialVersionUID = 1L; |
31 | PointedEar | 36 | |
37 | private Term term = new Term(); |
||
38 | private List<Term> termList = new ArrayList<Term>(); |
||
37 | PointedEar | 39 | private final TermDAO termDAO = new TermDAO(); |
40 | private final UserDAO userDAO = new UserDAO(); |
||
31 | PointedEar | 41 | |
33 | PointedEar | 42 | /** |
43 | * Session object |
||
44 | */ |
||
45 | Map<String, Object> session = ActionContext.getContext().getSession(); |
||
46 | |||
35 | PointedEar | 47 | /** |
48 | * @var <code>true</code> if the term is edited/renamed, <code>false</code> |
||
49 | * otherwise |
||
50 | */ |
||
51 | public boolean edit = false; |
||
52 | |||
53 | /** |
||
54 | * @var <code>true</code> if a term was added, <code>false</code> otherwise |
||
55 | */ |
||
56 | public boolean added = false; |
||
57 | |||
34 | PointedEar | 58 | private final HttpServletRequest request = (HttpServletRequest) ActionContext |
59 | .getContext().get(StrutsStatics.HTTP_REQUEST); |
||
60 | |||
35 | PointedEar | 61 | /** |
62 | * The term that was just saved (added, renamed) |
||
63 | */ |
||
64 | public Term savedTerm; |
||
65 | |||
37 | PointedEar | 66 | private final HistoryDAO historyDAO = new HistoryDAO(); |
67 | |||
68 | private Set<History> history; |
||
69 | |||
31 | PointedEar | 70 | /* |
71 | * (non-Javadoc) |
||
72 | * |
||
73 | * @see com.opensymphony.xwork2.ModelDriven#getModel() |
||
74 | */ |
||
75 | public Term getModel() |
||
76 | { |
||
77 | return this.term; |
||
78 | } |
||
79 | |||
80 | /** |
||
35 | PointedEar | 81 | * DB query for term list |
82 | * |
||
83 | * @return SUCCESS |
||
84 | */ |
||
85 | public String list() |
||
86 | { |
||
37 | PointedEar | 87 | this.termList = this.termDAO.getList(); |
35 | PointedEar | 88 | return Action.SUCCESS; |
89 | } |
||
90 | |||
91 | /** |
||
31 | PointedEar | 92 | * Executes the DB query to save the user |
93 | * |
||
94 | * @return {@link Action#SUCCESS} |
||
95 | */ |
||
34 | PointedEar | 96 | public String save() |
31 | PointedEar | 97 | { |
37 | PointedEar | 98 | User user = this.userDAO.getByUsername((String) this.session |
35 | PointedEar | 99 | .get("username")); |
37 | PointedEar | 100 | Date now = new Date(); |
101 | ObjectEntity obj; |
||
35 | PointedEar | 102 | |
37 | PointedEar | 103 | int action = 0; |
35 | PointedEar | 104 | if ("false".equals(this.request.getParameter("edit"))) |
34 | PointedEar | 105 | { |
35 | PointedEar | 106 | /* Add a new term */ |
37 | PointedEar | 107 | obj = new ObjectEntity(user, new ObjectType(ObjectType.TERM), user, null, |
108 | now, false, this.term, null, null); |
||
109 | |||
34 | PointedEar | 110 | this.term.setObject(obj); |
35 | PointedEar | 111 | this.added = true; |
37 | PointedEar | 112 | action = ActionType.ADD; |
34 | PointedEar | 113 | } |
37 | PointedEar | 114 | else |
115 | { |
||
116 | obj = new ObjectEntity(); |
||
117 | obj.setId(this.term.getObjectId()); |
||
118 | action = ActionType.RENAME; |
||
119 | } |
||
34 | PointedEar | 120 | |
35 | PointedEar | 121 | String result = Action.SUCCESS; |
37 | PointedEar | 122 | if (this.termDAO.saveOrUpdate(this.term)) |
33 | PointedEar | 123 | { |
37 | PointedEar | 124 | String comment = this.request.getParameter("comment"); |
125 | |||
126 | History historyRecord = new History(user, new ActionType(action), obj, |
||
127 | this.term.getName(), comment, now); |
||
128 | |||
129 | this.historyDAO.saveOrUpdate(historyRecord); |
||
130 | } |
||
131 | else |
||
132 | { |
||
35 | PointedEar | 133 | result = Action.ERROR; |
33 | PointedEar | 134 | } |
135 | |||
35 | PointedEar | 136 | this.savedTerm = this.term; |
137 | this.term = null; |
||
31 | PointedEar | 138 | |
35 | PointedEar | 139 | this.list(); |
140 | |||
141 | return result; |
||
31 | PointedEar | 142 | } |
143 | |||
144 | /** |
||
145 | * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR} |
||
146 | * otherwise |
||
147 | */ |
||
148 | public String edit() |
||
149 | { |
||
150 | int id = this.getIdParameter(); |
||
151 | |||
35 | PointedEar | 152 | String result = Action.ERROR; |
31 | PointedEar | 153 | if (id > 0) |
154 | { |
||
37 | PointedEar | 155 | this.term = this.termDAO.getById(id); |
33 | PointedEar | 156 | if (this.term != null) |
157 | { |
||
35 | PointedEar | 158 | this.edit = true; |
159 | result = Action.SUCCESS; |
||
33 | PointedEar | 160 | } |
31 | PointedEar | 161 | } |
33 | PointedEar | 162 | |
35 | PointedEar | 163 | this.list(); |
31 | PointedEar | 164 | |
35 | PointedEar | 165 | return result; |
31 | PointedEar | 166 | } |
167 | |||
168 | /** |
||
35 | PointedEar | 169 | * deletes a term, gets the ID from the "id" parameter that was submitted with |
31 | PointedEar | 170 | * the HTTP request |
171 | * |
||
172 | * @return String - either SUCCESS or ERROR constant |
||
173 | */ |
||
174 | public String delete() |
||
175 | { |
||
176 | |||
177 | int id = this.getIdParameter(); |
||
178 | |||
179 | /* Check for malicious ID values */ |
||
35 | PointedEar | 180 | String result = Action.SUCCESS; |
31 | PointedEar | 181 | if (id > 0) |
182 | { |
||
37 | PointedEar | 183 | this.termDAO.delete(id); |
31 | PointedEar | 184 | } |
185 | else |
||
186 | { |
||
35 | PointedEar | 187 | result = Action.ERROR; |
31 | PointedEar | 188 | } |
35 | PointedEar | 189 | |
190 | this.list(); |
||
191 | |||
192 | return result; |
||
31 | PointedEar | 193 | } |
194 | |||
35 | PointedEar | 195 | /** |
196 | * Gets the ID Parameter for update / delete requests |
||
197 | * |
||
198 | * @return int from the ID request. If not set or wrong, it gives back -1 |
||
31 | PointedEar | 199 | */ |
35 | PointedEar | 200 | private int getIdParameter() |
201 | { |
||
202 | int id = -1; |
||
203 | try |
||
204 | { |
||
205 | id = Integer.parseInt(this.request.getParameter("id")); //$NON-NLS-1$ |
||
206 | } |
||
207 | catch (Exception e) |
||
208 | { |
||
209 | /* TODO: Logging - wrong parameter set */ |
||
210 | } |
||
31 | PointedEar | 211 | |
35 | PointedEar | 212 | return id; |
213 | } |
||
214 | |||
215 | /* Standard getters and setters */ |
||
216 | |||
31 | PointedEar | 217 | /** |
218 | * @return The term edited with this instance |
||
219 | */ |
||
220 | public Term getTerm() |
||
221 | { |
||
222 | return this.term; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param term |
||
227 | * The term edited with this instance |
||
228 | */ |
||
229 | public void setTerm(Term term) |
||
230 | { |
||
231 | this.term = term; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @return The list of terms edited with this instance |
||
236 | */ |
||
237 | public List<Term> getTermList() |
||
238 | { |
||
239 | return this.termList; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @param termList |
||
244 | * The list of terms edited with this instance |
||
245 | */ |
||
246 | public void setTermList(List<Term> termList) |
||
247 | { |
||
248 | this.termList = termList; |
||
249 | } |
||
37 | PointedEar | 250 | |
251 | /** |
||
252 | * @return the histories |
||
253 | */ |
||
254 | public Set<History> getHistories() |
||
255 | { |
||
256 | return this.history; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @param histories |
||
261 | * the histories to set |
||
262 | */ |
||
263 | public void setHistories(Set<History> histories) |
||
264 | { |
||
265 | this.history = histories; |
||
266 | } |
||
31 | PointedEar | 267 | } |