KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nextime > ion > backoffice > action > content > EditSectionAction


1 package org.nextime.ion.backoffice.action.content;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import javax.servlet.ServletException JavaDoc;
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7 import javax.servlet.http.HttpServletResponse JavaDoc;
8 import org.apache.struts.action.ActionErrors;
9 import org.apache.struts.action.ActionForm;
10 import org.apache.struts.action.ActionForward;
11 import org.apache.struts.action.ActionMapping;
12 import org.nextime.ion.backoffice.action.BaseAction;
13 import org.nextime.ion.backoffice.bean.SectionTypes;
14 import org.nextime.ion.backoffice.form.EditSectionForm;
15 import org.nextime.ion.backoffice.tree.TreeControl;
16 import org.nextime.ion.backoffice.tree.TreeControlNode;
17 import org.nextime.ion.backoffice.exception.BackofficeSecurityException;
18 import org.nextime.ion.backoffice.security.SecurityManagerImpl;
19
20 import org.nextime.ion.framework.business.Section;
21 import org.nextime.ion.framework.business.User;
22 import org.nextime.ion.framework.locale.Locale;
23 import org.nextime.ion.framework.locale.LocaleList;
24 import org.nextime.ion.framework.mapping.Mapping;
25 import org.nextime.ion.framework.workflow.Workflow;
26
27 public class EditSectionAction extends BaseAction {
28
29     public ActionForward perform(
30         ActionMapping mapping,
31         ActionForm form,
32         HttpServletRequest JavaDoc request,
33         HttpServletResponse JavaDoc response)
34         throws IOException JavaDoc, ServletException JavaDoc {
35
36         // check if user is correctly logged
37
checkUser(request);
38
39         // set the locales list
40
request.setAttribute("locales", LocaleList.getInstance().getLocales());
41
42         // retrieve id
43
String JavaDoc id =
44             (request.getAttribute("id") != null)
45                 ? request.getAttribute("id").toString()
46                 : request.getParameter("id").toString();
47
48         // user need cancel
49
if (request.getParameter("cancel") != null) {
50             // on efface la section si elle n'a pas été initialisée
51
try {
52                 Mapping.begin();
53                 Section section = Section.getInstance(id);
54                 if (section.getMetaData("name") == null) {
55                     section.remove();
56                 }
57                 // retrieve selected section
58
TreeControl tree =
59                     (TreeControl) request.getSession().getAttribute(
60                         "treeControlTest");
61                 TreeControlNode node = tree.findNode(id);
62                 node.remove();
63                 Mapping.commit();
64             } catch (Exception JavaDoc e) {
65                 e.printStackTrace();
66                 Mapping.rollback();
67             }
68             // Forward to the next page
69
return (mapping.findForward("cancel"));
70         }
71
72         // check if this action is allowed
73
try {
74             Mapping.begin();
75             if (!new SecurityManagerImpl()
76                 .canEditSection(
77                     Section.getInstance(id),
78                     User.getInstance(
79                         request.getSession().getAttribute("userLogin")
80                             + ""))) {
81                 throw new Exception JavaDoc();
82             }
83         } catch (Exception JavaDoc e) {
84             throw new BackofficeSecurityException();
85         } finally {
86             Mapping.rollback();
87         }
88
89         // get the form
90
EditSectionForm sform = (EditSectionForm) form;
91         ActionErrors errors = sform.myValidate(request);
92
93         // fill data | first time
94
if (sform.getStatus() == null) {
95             try {
96                 Mapping.begin();
97                 Section section = Section.getInstance(id);
98                 Mapping.rollback();
99
100                 request.setAttribute("section", section);
101
102                 // add localized names properties
103
Enumeration JavaDoc keys = section.getMetaData().keys();
104                 while (keys.hasMoreElements()) {
105                     String JavaDoc key = keys.nextElement() + "";
106                     if (key.startsWith("name_")) {
107                         request.setAttribute(key, section.getMetaData(key));
108                     }
109                 }
110
111                 sform.setTemplate(section.getMetaData("template") + "");
112                 sform.setStatus(section.getMetaData("status") + "");
113                 sform.setWorkflow(section.getMetaData("workflow") + "");
114                 request.setAttribute(
115                     "types",
116                     SectionTypes.getSectionsBeans(servlet, request));
117                 request.setAttribute(
118                     "type",
119                     SectionTypes.getSectionBean(
120                         servlet,
121                         section.getMetaData("template") + "",
122                         request));
123                 request.setAttribute("workflows", Workflow.listTypes());
124             } catch (Exception JavaDoc e) {
125                 Mapping.rollback();
126                 throw new ServletException JavaDoc(e);
127             }
128
129             // Forward to the view page
130
return (mapping.findForward("view"));
131         }
132
133         // fill data | template has changed
134
if (request.getParameter("ok") == null) {
135             try {
136                 Mapping.begin();
137                 Section section = Section.getInstance(id);
138                 Mapping.rollback();
139
140                 // add localized names properties
141
Enumeration JavaDoc keys = section.getMetaData().keys();
142                 while (keys.hasMoreElements()) {
143                     String JavaDoc key = keys.nextElement() + "";
144                     if (key.startsWith("name_")) {
145                         request.setAttribute(key, request.getParameter(key));
146                     }
147                 }
148                 keys = request.getParameterNames();
149                 while (keys.hasMoreElements()) {
150                     String JavaDoc key = keys.nextElement() + "";
151                     if (key.startsWith("name_")) {
152                         request.setAttribute(key, request.getParameter(key));
153                     }
154                 }
155
156                 request.setAttribute("section", section);
157                 request.setAttribute(
158                     "types",
159                     SectionTypes.getSectionsBeans(servlet, request));
160                 request.setAttribute(
161                     "type",
162                     SectionTypes.getSectionBean(servlet, sform.getTemplate(), request));
163                 request.setAttribute("workflows", Workflow.listTypes());
164                 request.setAttribute(ERROR_KEY, errors);
165             } catch (Exception JavaDoc e) {
166                 Mapping.rollback();
167                 throw new ServletException JavaDoc(e);
168             }
169
170             // Forward to the view page
171
return (mapping.findForward("view"));
172         }
173
174         // fill data | errors
175
if (errors.size() > 0) {
176             try {
177                 Mapping.begin();
178                 Section section = Section.getInstance(id);
179                 Mapping.rollback();
180
181                 // add localized names properties
182
Enumeration JavaDoc keys = section.getMetaData().keys();
183                 while (keys.hasMoreElements()) {
184                     String JavaDoc key = keys.nextElement() + "";
185                     if (key.startsWith("name_")) {
186                         request.setAttribute(key, request.getParameter(key));
187                     }
188                 }
189                 keys = request.getParameterNames();
190                 while (keys.hasMoreElements()) {
191                     String JavaDoc key = keys.nextElement() + "";
192                     if (key.startsWith("name_")) {
193                         request.setAttribute(key, request.getParameter(key));
194                     }
195                 }
196
197                 request.setAttribute("section", section);
198                 request.setAttribute(
199                     "types",
200                     SectionTypes.getSectionsBeans(servlet, request));
201                 request.setAttribute(
202                     "type",
203                     SectionTypes.getSectionBean(servlet, sform.getTemplate(), request));
204                 request.setAttribute(ERROR_KEY, errors);
205                 request.setAttribute("workflows", Workflow.listTypes());
206                 // copy metadata
207
Enumeration JavaDoc names = request.getParameterNames();
208                 request.setAttribute("copyMeta", "true");
209                 while (names.hasMoreElements()) {
210                     String JavaDoc name = names.nextElement() + "";
211                     if (name.startsWith("META_")) {
212                         request.setAttribute(name, request.getParameter(name));
213                     }
214                 }
215             } catch (Exception JavaDoc e) {
216                 Mapping.rollback();
217                 throw new ServletException JavaDoc(e);
218             }
219
220             // Forward to the view page
221
return (mapping.findForward("view"));
222         }
223
224         // all it's ok : update section
225
try {
226             Mapping.begin();
227
228             Section section = Section.getInstance(id);
229
230             // set names properties
231
Enumeration JavaDoc keys = request.getParameterNames();
232             while (keys.hasMoreElements()) {
233                 String JavaDoc key = keys.nextElement() + "";
234                 if (key.startsWith("name_")) {
235                     section.setMetaData(key, request.getParameter(key));
236                 }
237             }
238             // default locale
239
String JavaDoc sDl =
240                 ((Locale) LocaleList
241                     .getInstance()
242                     .getLocales()
243                     .iterator()
244                     .next())
245                     .getLocale();
246             section.setMetaData("name", section.getMetaData("name_" + sDl));
247
248             section.setMetaData("template", sform.getTemplate());
249             section.setMetaData("status", sform.getStatus());
250             section.setMetaData("workflow", sform.getWorkflow());
251             // update metadata
252
Enumeration JavaDoc names = request.getParameterNames();
253             while (names.hasMoreElements()) {
254                 String JavaDoc name = names.nextElement() + "";
255                 if (name.startsWith("META_")) {
256                     section.setMetaData(
257                         name.substring(5),
258                         request.getParameter(name));
259                 }
260             }
261             // update the tree ...
262
TreeControlNode node =
263                 (
264                     (TreeControl) request.getSession().getAttribute(
265                         "treeControlTest")).findNode(
266                     request.getParameter("id"));
267             node.setLabel(section.getMetaData("name") + "");
268             String JavaDoc img = "section.gif";
269             if ("offline".equals(section.getMetaData("status"))) {
270                 img = "section-offline.gif";
271             }
272             node.setIcon(img);
273
274             Mapping.commit();
275         } catch (Exception JavaDoc e) {
276             Mapping.rollback();
277             throw new ServletException JavaDoc(e);
278         }
279
280         // Forward to the next page
281
return (mapping.findForward("ok"));
282     }
283
284 }
285
Popular Tags