KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > struts > EditAction


1 package org.jahia.clipbuilder.html.struts;
2
3 import org.jahia.clipbuilder.html.struts.Util.AbstractWizardAction;
4 import org.jahia.clipbuilder.html.struts.Util.*;
5 import org.apache.struts.action.*;
6 import javax.servlet.http.*;
7 import org.apache.struts.action.*;
8 import javax.servlet.http.*;
9 import org.jahia.clipbuilder.html.*;
10 import org.jahia.clipbuilder.html.bean.*;
11 import java.util.*;
12 import java.io.*;
13
14 /**
15  * Action linked whith editParams.jsp
16  *
17  *@author Tlili Khaled
18  */

19 public class EditAction extends AbstractWizardAction {
20     private boolean errorHasOccured = false;
21     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(EditAction.class);
22
23
24     /**
25      * Gets the FormId attribute of the EditAction object
26      *
27      *@return The FormId value
28      */

29     public int getFormId() {
30         return Constants.EDITPARAM;
31     }
32
33
34     /**
35      * Gets the KeyMethodMap attribute of the BrowseAction object
36      *
37      *@return The KeyMethodMap value
38      */

39     public Map getKeyMethodMap() {
40         Map map = super.getKeyMethodMap();
41         map.put("wizard", "view");
42         map.put("edit.backToSelect", "backToSelect");
43         map.put("edit.reset", "reset");
44         map.put("edit.resetAll", "resetAll");
45         map.put("edit.validate", "validate");
46         map.put("edit.view.change", "changeView");
47
48         return map;
49     }
50
51
52     /**
53      * Description of the Method
54      *
55      *@param actionMapping Description of Parameter
56      *@param actionForm Description of Parameter
57      *@param httpServletRequest Description of Parameter
58      *@param httpServletResponse Description of Parameter
59      *@return Description of the Returned Value
60      */

61     public ActionForward view(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse httpServletResponse) {
62         logger.debug("[ View ]");
63         AbstractWizardForm form = (AbstractWizardForm) actionForm;
64         SessionManager.getWebBrowserForm(request).setShow(Constants.WEB_BROWSER_SHOW_LAST_DOCUMENT);
65         checkEditableParamaters(request);
66
67         return actionMapping.getInputForward();
68     }
69
70
71
72     /**
73      * Called when back button is selected
74      *
75      *@param actionMapping Description of Parameter
76      *@param actionForm Description of Parameter
77      *@param httpServletRequest Description of Parameter
78      *@param httpServletResponse Description of Parameter
79      *@return Description of the Returned Value
80      */

81     public ActionForward backToSelect(ActionMapping actionMapping,
82             ActionForm actionForm,
83             HttpServletRequest httpServletRequest,
84             HttpServletResponse httpServletResponse) {
85         EditParamForm editParamForm = (EditParamForm) actionForm;
86         //SessionManager.getWebBrowserForm(httpServletRequest).setShow("browse");
87
logger.debug("[ Back to select page ]");
88         return actionMapping.findForward("viewSelectPart");
89     }
90
91
92
93     /**
94      * Description of the Method
95      *
96      *@param actionMapping Description of Parameter
97      *@param actionForm Description of Parameter
98      *@param request Description of Parameter
99      *@param httpServletResponse Description of Parameter
100      *@return Description of the Returned Value
101      */

102     public ActionForward init(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse httpServletResponse) {
103         logger.debug("[ Init ]");
104         super.init(actionMapping, actionForm, request, httpServletResponse);
105
106         EditParamForm editParamForm = (EditParamForm) actionForm;
107         ActionForward forward = actionMapping.getInputForward();
108
109         checkEditableParamaters(request);
110
111         // set the list of parameter to be show
112
activateParamsList(editParamForm, request);
113
114         // set the document html to be printed
115
setWebBrowserContent(request, httpServletResponse, editParamForm);
116         return forward;
117     }
118
119
120     /**
121      * Description of the Method
122      *
123      *@param actionMapping Description of Parameter
124      *@param actionForm Description of Parameter
125      *@param httpServletRequest Description of Parameter
126      *@param httpServletResponse Description of Parameter
127      *@return Description of the Returned Value
128      */

129     public ActionForward changeView(ActionMapping actionMapping,
130             ActionForm actionForm,
131             HttpServletRequest httpServletRequest,
132             HttpServletResponse httpServletResponse) {
133         logger.debug("[ changeView ]");
134
135         EditParamForm editParamForm = (EditParamForm) actionForm;
136
137         // set the list of parameter to be show
138
activateParamsList(editParamForm, httpServletRequest);
139
140         // set the document html to be printed
141
setWebBrowserContent(httpServletRequest, httpServletResponse, editParamForm);
142
143         return actionMapping.getInputForward();
144     }
145
146
147     /**
148      * Called when reset button is selected
149      *
150      *@param actionMapping Description of Parameter
151      *@param actionForm Description of Parameter
152      *@param httpServletRequest Description of Parameter
153      *@param httpServletResponse Description of Parameter
154      *@return Description of the Returned Value
155      */

156     public ActionForward reset(ActionMapping actionMapping, ActionForm actionForm,
157             HttpServletRequest httpServletRequest,
158             HttpServletResponse httpServletResponse) {
159         EditParamForm editParamForm = (EditParamForm) actionForm;
160
161         //Retrieve the clipper
162
ClipperBean bean = SessionManager.getClipperBean(httpServletRequest);
163
164         //Get the current url bean
165
int posUrl = Integer.parseInt(editParamForm.getSelectedUrl());
166         UrlBean uBean = bean.getUrlBean(posUrl);
167
168         // process only the selected url
169
resetFormParamMapping(uBean);
170
171         logger.debug("[Param of url " + uBean.getAbsoluteURL() + " has beean reseted]");
172
173         return actionMapping.findForward("editParams");
174     }
175
176
177     /**
178      * Called when resetAll button is selected
179      *
180      *@param actionMapping Description of Parameter
181      *@param actionForm Description of Parameter
182      *@param httpServletRequest Description of Parameter
183      *@param httpServletResponse Description of Parameter
184      *@return Description of the Returned Value
185      */

186     public ActionForward resetAll(ActionMapping actionMapping,
187             ActionForm actionForm,
188             HttpServletRequest httpServletRequest,
189             HttpServletResponse httpServletResponse) {
190         EditParamForm editParamForm = (EditParamForm) actionForm;
191
192         logger.debug("[ Reset all params ]");
193
194         //Retrieve the clipper
195
ClipperBean bean = SessionManager.getClipperBean(httpServletRequest);
196
197         // updtae the mapping for each form param
198
List uBeanList = bean.getUrlListBean();
199
200         // perform reset for all bean
201
for (int i = 0; i < uBeanList.size(); i++) {
202             UrlBean uBean = (UrlBean) uBeanList.get(i);
203             resetFormParamMapping(uBean);
204         }
205
206         return actionMapping.findForward("editParams");
207     }
208
209
210     /**
211      * Called when validate button is selected
212      *
213      *@param actionMapping Description of Parameter
214      *@param actionForm Description of Parameter
215      *@param httpServletRequest Description of Parameter
216      *@param httpServletResponse Description of Parameter
217      *@return Description of the Returned Value
218      */

219     public ActionForward validate(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
220         EditParamForm editParamForm = (EditParamForm) actionForm;
221
222         //Retrieve the clipper
223
ClipperBean bean = SessionManager.getClipperBean(httpServletRequest);
224
225         //get all the parameter
226
Map params = getParam(httpServletRequest);
227
228         //Get the current url bean
229
int posUrl = Integer.parseInt(editParamForm.getSelectedUrl());
230         UrlBean uBean = bean.getUrlBean(posUrl);
231
232         // updtae preferences for each form param
233
List fBeanList = uBean.getFormParamBeanList();
234         for (int i = 0; i < fBeanList.size(); i++) {
235             FormParamBean fBean = (FormParamBean) fBeanList.get(i);
236             String JavaDoc mapping = ((String JavaDoc[]) params.get("mapping"))[i];
237             String JavaDoc visibility = ((String JavaDoc[]) params.get("visibility"))[i];
238             String JavaDoc update = ((String JavaDoc[]) params.get("update"))[i];
239             String JavaDoc useAsDefaultValue = ((String JavaDoc[]) params.get("useAsDefaultValue"))[i];
240             //logger.debug("[ form param found: name," + name + " mapping " + mapping + "]");
241
fBean.setMapping(mapping);
242             fBean.setVisibility(visibility);
243             fBean.setUpdate(update);
244             fBean.setUseAsDefaultValue(useAsDefaultValue);
245         }
246
247         logger.debug("[ Validate action performed ]");
248
249         return actionMapping.findForward("editParams");
250     }
251
252
253     /**
254      * process the selected Url
255      *
256      *@param httpServletRequest Description of Parameter
257      *@param httpServletResponse The new BodyContent value
258      *@param editParamForm Description of Parameter
259      */

260     private void setWebBrowserContent(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
261             EditParamForm editParamForm) {
262
263         // Get the position of url
264
String JavaDoc position = editParamForm.getSelectedUrl();
265         int posUrl = Integer.parseInt(position);
266
267         //Show the html document
268
SessionManager.getWebBrowserForm(httpServletRequest).setPositionUrl(posUrl);
269         SessionManager.getWebBrowserForm(httpServletRequest).setShow(Constants.WEB_BROWSER_SHOW_EDIPARAMS);
270         if (editParamForm.getShowHTML() != null) {
271             // Show label is selected: Get the document whit label
272
if (editParamForm.getShowLabel() != null) {
273                 logger.debug("Show HTML Label");
274                 SessionManager.getWebBrowserForm(httpServletRequest).setShow(Constants.WEB_BROWSER_SHOW_EDIPARAMS);
275
276             }
277             // Get the document whitout the label
278
else {
279                 logger.debug("Show HTML");
280                 SessionManager.getWebBrowserForm(httpServletRequest).setShow(Constants.WEB_BROWSER_SHOW_LAST_DOCUMENT);
281
282             }
283         }
284     }
285
286
287     /**
288      * Description of the Method
289      *
290      *@param request Description of Parameter
291      */

292     private void checkEditableParamaters(HttpServletRequest request) {
293         ClipperBean cBean = SessionManager.getClipperBean(request);
294         if (cBean.getUrlListBean().size() < 2) {
295             ActionMessages actionErrors = new ActionMessages();
296             ActionMessage e = new ActionMessage("information.noParameters");
297             actionErrors.add("information.noParameters", e);
298             saveErrors(request, actionErrors);
299         }
300     }
301
302
303     /**
304      * Description of the Method
305      *
306      *@param uBean Description of Parameter
307      */

308     private void resetFormParamMapping(UrlBean uBean) {
309         List fBeanList = uBean.getFormParamBeanList();
310         for (int i = 0; i < fBeanList.size(); i++) {
311             FormParamBean fBean = (FormParamBean) fBeanList.get(i);
312             String JavaDoc name = fBean.getName();
313             fBean.setMapping(name);
314         }
315
316     }
317
318
319     /**
320      * Description of the Method
321      *
322      *@param editParamForm Description of Parameter
323      *@param request Description of Parameter
324      */

325     private void activateParamsList(EditParamForm editParamForm,
326             HttpServletRequest request) {
327         ClipperBean bean = SessionManager.getClipperBean(request);
328         String JavaDoc position = editParamForm.getSelectedUrl();
329
330         // this can occur the first time the edit page is accesed
331
if (position == null) {
332             //there is at leat one element in the urlListBean
333
position = "0";
334             editParamForm.setSelectedUrl(position);
335             logger.debug("[ First url selected autommatically]");
336         }
337
338         //Activate form list
339
int posUrl = Integer.parseInt(position);
340         List fList = bean.getAllFormParam(posUrl);
341         if (fList.isEmpty()) {
342             logger.debug("[ Form Param list is empty ]");
343         }
344         editParamForm.setActifFormParamsList(fList);
345         logger.debug("[ Actif Parameters List is set from first url at position " +
346                 posUrl + " ]");
347
348         //Activate query list
349
List qList = bean.getAllQueryParam(posUrl);
350         if (qList.isEmpty()) {
351             logger.debug("[ Query param list is empty ]");
352         }
353
354         editParamForm.setActifQueryParamsList(qList);
355
356     }
357
358 }
359
Popular Tags