Rev 33 | Rev 35 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 33 | Rev 34 | ||
---|---|---|---|
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 | private final HttpServletRequest request = (HttpServletRequest) ActionContext |
|
- | 44 | .getContext().get(StrutsStatics.HTTP_REQUEST); |
|
- | 45 | ||
43 | /*
|
46 | /*
|
44 | * (non-Javadoc)
|
47 | * (non-Javadoc)
|
45 | *
|
48 | *
|
46 | * @see com.opensymphony.xwork2.ModelDriven#getModel()
|
49 | * @see com.opensymphony.xwork2.ModelDriven#getModel()
|
47 | */
|
50 | */
|
48 | public Term getModel() |
51 | public Term getModel() |
49 | {
|
52 | {
|
50 | return this.term; |
53 | return this.term; |
51 | }
|
54 | }
|
52 | 55 | ||
53 | /**
|
56 | /**
|
54 | * Executes the DB query to save the user
|
57 | * Executes the DB query to save the user
|
55 | *
|
58 | *
|
56 | * @return {@link Action#SUCCESS}
|
59 | * @return {@link Action#SUCCESS}
|
57 | */
|
60 | */
|
58 | public String add() |
61 | public String save() |
59 | {
|
62 | {
|
- | 63 | if (!"1".equals(this.request.getParameter("edit"))) |
|
- | 64 | {
|
|
60 | User user = this.userDAO.searchUsername((String) this.session |
65 | User user = this.userDAO.searchUsername((String) this.session |
61 | .get("username")); |
66 | .get("username")); |
62 | ObjectEntity obj = new ObjectEntity(user, new ObjectType(ObjectType.TERM), |
67 | ObjectEntity obj = new ObjectEntity(user, |
- | 68 | new ObjectType(ObjectType.TERM), user, null, new Date(), false, |
|
63 | user, null, new Date(), false, this.term, null, null); |
69 | this.term, null, null); |
64 | this.term.setObject(obj); |
70 | this.term.setObject(obj); |
- | 71 | }
|
|
- | 72 | ||
65 | if (this.termDAO.saveOrUpdate(this.term)) |
73 | if (this.termDAO.saveOrUpdate(this.term)) |
66 | {
|
74 | {
|
67 | return Action.SUCCESS; |
75 | return Action.SUCCESS; |
68 | }
|
76 | }
|
69 | 77 | ||
70 | return Action.ERROR; |
78 | return Action.ERROR; |
71 | }
|
79 | }
|
72 | 80 | ||
73 | /**
|
81 | /**
|
74 | * DB query for term list
|
82 | * DB query for term list
|
75 | *
|
83 | *
|
76 | * @return SUCCESS
|
84 | * @return SUCCESS
|
77 | */
|
85 | */
|
78 | public String list() |
86 | public String list() |
79 | {
|
87 | {
|
80 | this.termList = this.termDAO.listTerm(); |
88 | this.termList = this.termDAO.getTerms(); |
81 | return Action.SUCCESS; |
89 | return Action.SUCCESS; |
82 | }
|
90 | }
|
83 | 91 | ||
84 | /**
|
92 | /**
|
85 | * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
|
93 | * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
|
86 | * otherwise
|
94 | * otherwise
|
87 | */
|
95 | */
|
88 | public String edit() |
96 | public String edit() |
89 | {
|
97 | {
|
90 | int id = this.getIdParameter(); |
98 | int id = this.getIdParameter(); |
91 | 99 | ||
92 | if (id > 0) |
100 | if (id > 0) |
93 | {
|
101 | {
|
94 | this.term = this.termDAO.getTermById(id); |
102 | this.term = this.termDAO.getTermById(id); |
95 | if (this.term != null) |
103 | if (this.term != null) |
96 | {
|
104 | {
|
- | 105 | this.term.edit = true; |
|
97 | return Action.SUCCESS; |
106 | return Action.SUCCESS; |
98 | }
|
107 | }
|
99 | }
|
108 | }
|
100 | 109 | ||
101 | return Action.ERROR; |
110 | return Action.ERROR; |
102 | }
|
111 | }
|
103 | 112 | ||
104 | /**
|
113 | /**
|
105 | * Gets the ID Parameter for update / delete requests
|
114 | * Gets the ID Parameter for update / delete requests
|
106 | *
|
115 | *
|
107 | * @return int from the ID request. If not set or wrong, it gives back -1
|
116 | * @return int from the ID request. If not set or wrong, it gives back -1
|
108 | */
|
117 | */
|
109 | private int getIdParameter() |
118 | private int getIdParameter() |
110 | {
|
119 | {
|
111 | HttpServletRequest request = (HttpServletRequest) ActionContext |
- | |
112 | .getContext().get(StrutsStatics.HTTP_REQUEST); |
- | |
113 | - | ||
114 | int id = -1; |
120 | int id = -1; |
115 | try
|
121 | try
|
116 | {
|
122 | {
|
117 | id = Integer.parseInt(request.getParameter("id")); //$NON-NLS-1$ |
123 | id = Integer.parseInt(this.request.getParameter("id")); //$NON-NLS-1$ |
118 | }
|
124 | }
|
119 | catch (Exception e) |
125 | catch (Exception e) |
120 | {
|
126 | {
|
121 | /* TODO: Logging - wrong parameter set */
|
127 | /* TODO: Logging - wrong parameter set */
|
122 | }
|
128 | }
|
123 | 129 | ||
124 | return id; |
130 | return id; |
125 | }
|
131 | }
|
126 | 132 | ||
127 | /**
|
133 | /**
|
128 | * deletes a user, gets the ID from the "id" parameter that was submitted with
|
134 | * deletes a user, gets the ID from the "id" parameter that was submitted with
|
129 | * the HTTP request
|
135 | * the HTTP request
|
130 | *
|
136 | *
|
131 | * @return String - either SUCCESS or ERROR constant
|
137 | * @return String - either SUCCESS or ERROR constant
|
132 | */
|
138 | */
|
133 | public String delete() |
139 | public String delete() |
134 | {
|
140 | {
|
135 | 141 | ||
136 | int id = this.getIdParameter(); |
142 | int id = this.getIdParameter(); |
137 | 143 | ||
138 | /* Check for malicious ID values */
|
144 | /* Check for malicious ID values */
|
139 | if (id > 0) |
145 | if (id > 0) |
140 | {
|
146 | {
|
141 | this.termDAO.deleteTerm(id); |
147 | this.termDAO.deleteTerm(id); |
142 | return Action.SUCCESS; |
148 | return Action.SUCCESS; |
143 | }
|
149 | }
|
144 | else
|
150 | else
|
145 | {
|
151 | {
|
146 | return Action.ERROR; |
152 | return Action.ERROR; |
147 | }
|
153 | }
|
148 | }
|
154 | }
|
149 | 155 | ||
150 | /*
|
156 | /*
|
151 | * Standard getters and setters
|
157 | * Standard getters and setters
|
152 | */
|
158 | */
|
153 | 159 | ||
154 | /**
|
160 | /**
|
155 | * @return The term edited with this instance
|
161 | * @return The term edited with this instance
|
156 | */
|
162 | */
|
157 | public Term getTerm() |
163 | public Term getTerm() |
158 | {
|
164 | {
|
159 | return this.term; |
165 | return this.term; |
160 | }
|
166 | }
|
161 | 167 | ||
162 | /**
|
168 | /**
|
163 | * @param term
|
169 | * @param term
|
164 | * The term edited with this instance
|
170 | * The term edited with this instance
|
165 | */
|
171 | */
|
166 | public void setTerm(Term term) |
172 | public void setTerm(Term term) |
167 | {
|
173 | {
|
168 | this.term = term; |
174 | this.term = term; |
169 | }
|
175 | }
|
170 | 176 | ||
171 | /**
|
177 | /**
|
172 | * @return The list of terms edited with this instance
|
178 | * @return The list of terms edited with this instance
|
173 | */
|
179 | */
|
174 | public List<Term> getTermList() |
180 | public List<Term> getTermList() |
175 | {
|
181 | {
|
176 | return this.termList; |
182 | return this.termList; |
177 | }
|
183 | }
|
178 | 184 | ||
179 | /**
|
185 | /**
|
180 | * @param termList
|
186 | * @param termList
|
181 | * The list of terms edited with this instance
|
187 | * The list of terms edited with this instance
|
182 | */
|
188 | */
|
183 | public void setTermList(List<Term> termList) |
189 | public void setTermList(List<Term> termList) |
184 | {
|
190 | {
|
185 | this.termList = termList; |
191 | this.termList = termList; |
186 | }
|
192 | }
|
187 | }
|
193 | }
|
188 | 194 |