KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.clipbuilder.html.struts;
2
3 import org.jahia.clipbuilder.html.struts.Util.*;
4 import org.apache.struts.action.*;
5 import javax.servlet.http.*;
6 import java.util.*;
7 import org.jahia.clipbuilder.html.bean.*;
8
9 import org.apache.struts.action.*;
10 import org.jahia.clipbuilder.html.struts.Util.*;
11 import javax.servlet.http.*;
12 import org.jahia.clipbuilder.html.bean.*;
13 import java.util.*;
14 import org.jahia.clipbuilder.html.web.Constant.WebConstants;
15 import org.jahia.clipbuilder.html.SessionManager;
16 import org.jahia.clipbuilder.html.struts.webBrowser.*;
17 import org.jahia.clipbuilder.html.struts.webBrowser.*;
18 import org.jahia.clipbuilder.html.RequestParameterManager;
19
20 /**
21  * Description of the Class
22  *
23  *@author Tlili Khaled
24  */

25 public class TestClipperAction extends AbstractWizardAction {
26     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(TestClipperAction.class);
27
28
29     /**
30      * Gets the KeyMethodMap attribute of the TestClipperAction object
31      *
32      *@return The KeyMethodMap value
33      */

34     public Map getKeyMethodMap() {
35         Map map = super.getKeyMethodMap();
36         map.put("testClipper.view.change", "changeView");
37         map.put("testClipper.button.go", "go");
38         return map;
39     }
40
41
42     /**
43      * Gets the FormId attribute of the TestClipperAction object
44      *
45      *@return The FormId value
46      */

47     public int getFormId() {
48         return Constants.TEST;
49     }
50
51
52     /**
53      * Description of the Method
54      *
55      *@param actionMapping Description of Parameter
56      *@param actionForm Description of Parameter
57      *@param request Description of Parameter
58      *@param response Description of Parameter
59      *@return Description of the Returned Value
60      */

61     public ActionForward init(ActionMapping actionMapping, ActionForm actionForm,
62             HttpServletRequest request,
63             HttpServletResponse response) {
64         try {
65             logger.debug("[ Init ]");
66             //super.init(actionMapping, actionForm, httpServletRequest, httpServletResponse);
67
TestClipperForm form = (TestClipperForm) actionForm;
68
69             // remove the preview bean
70
removeBeanForm(request, Constants.PREVIEW);
71                         SessionManager.removeHTMLDocumentBuilder(request);
72
73             //init the webBrowserForm
74
WebBrowserForm wbf = SessionManager.getWebBrowserForm(request);
75             if (wbf != null) {
76                 wbf = new WebBrowserForm();
77                 SessionManager.setWebBrowserForm(request, wbf);
78             }
79             else {
80                 SessionManager.setWebBrowserForm(request, new WebBrowserForm());
81             }
82
83             // set the list of parameter to be show
84
activateParamsList(form, request);
85         }
86         catch (Exception JavaDoc ex) {
87             ex.printStackTrace();
88         }
89
90         return actionMapping.getInputForward();
91     }
92
93
94     /**
95      * Description of the Method
96      *
97      *@param actionMapping Description of Parameter
98      *@param actionForm Description of Parameter
99      *@param httpServletRequest Description of Parameter
100      *@param httpServletResponse Description of Parameter
101      *@return Description of the Returned Value
102      *@exception Exception Description of Exception
103      */

104     public ActionForward changeView(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception JavaDoc {
105         try {
106             logger.debug("[ changeView ]");
107
108             TestClipperForm form = (TestClipperForm) actionForm;
109
110             //process old selected url
111
int posUrl = Integer.parseInt(form.getOldSelectedUrl());
112             UrlBean uBean = SessionManager.getClipperBean(httpServletRequest).getUrlBean(posUrl);
113             if (uBean == null) {
114                 logger.debug("[ Last url ]");
115             }
116             else {
117                 updateQueryParamsValue(httpServletRequest, uBean);
118             }
119             form.setOldSelectedUrl(form.getSelectedUrl());
120
121             // process new selected url
122
activateParamsList(form, httpServletRequest);
123         }
124         catch (NumberFormatException JavaDoc ex) {
125             ex.printStackTrace();
126         }
127         catch (Exception JavaDoc ex) {
128             ex.printStackTrace();
129         }
130         finally {
131             return actionMapping.getInputForward();
132         }
133
134     }
135
136
137     /**
138      * Description of the Method
139      *
140      *@param actionMapping Description of Parameter
141      *@param actionForm Description of Parameter
142      *@param request Description of Parameter
143      *@param httpServletResponse Description of Parameter
144      *@return Description of the Returned Value
145      *@exception Exception Description of Exception
146      */

147     public ActionForward go(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse httpServletResponse) throws Exception JavaDoc {
148         logger.debug("[ DOVIEW ] new Session");
149         TestClipperForm form = (TestClipperForm) actionForm;
150
151         try {
152             // Struts don't manage very well checkbox when form is in Session Scope
153
String JavaDoc resetCache = request.getParameter("resetCache");
154             form.setResetCache(resetCache);
155             if (resetCache != null && resetCache.equalsIgnoreCase("true")) {
156                 String JavaDoc nameSpace = RequestParameterManager.getNameSpace(request);
157                 String JavaDoc remoteUser = request.getRemoteUser();
158                 ClipperBean cBean = SessionManager.getClipperBean(request);
159                 WebBrowserAction.removeObjectFromCache(nameSpace, remoteUser, cBean);
160             }
161
162             String JavaDoc url = form.getSelectedUrl();
163             int posUrl = Integer.parseInt(url);
164             updateQueryParamsValue(request, SessionManager.getClipperBean(request).getUrlBean(posUrl));
165
166             // handle input user parameter errors
167
if (checkUserQueryParameters(request)) {
168                 return actionMapping.getInputForward();
169             }
170
171             // set the simulator mode
172
SessionManager.getWebBrowserForm(request).setWebBrowserSimulatorMode(form.getWebBrowserSimulatorMode());
173
174         }
175         catch (Exception JavaDoc ex) {
176             ex.printStackTrace();
177         }
178         logger.debug("[ Go to preview ]");
179         return actionMapping.findForward("previewTest");
180     }
181
182
183     /**
184      * Description of the Method
185      *
186      *@param request Description of Parameter
187      *@return Description of the Returned Value
188      */

189     private boolean checkUserQueryParameters(HttpServletRequest request) {
190         boolean foundErros = false;
191         ActionErrors errors = new ActionErrors();
192         ClipperBean cBean = SessionManager.getClipperBean(request);
193         List urlBeanList = cBean.getUrlListBean();
194
195         // check all params of all urls
196
for (int i = 0; i < urlBeanList.size(); i++) {
197             List queryParamBeanList = cBean.getAllQueryParam(i);
198
199             // all query param of the curertn url
200
for (int j = 0; j < queryParamBeanList.size(); j++) {
201                 QueryParamBean currentQueryBean = (QueryParamBean) queryParamBeanList.get(j);
202
203                 if (currentQueryBean.getDefaultValue() == null) {
204                     // Errors: param value is missing --> add error mesasge
205
UrlBean uBean = (UrlBean) cBean.getUrlBean(i);
206                     String JavaDoc urlMessage[] = {"URL: " + uBean.getPosition() + ":" + uBean.getAbsoluteURL()};
207                     ActionError urlActionMessage = new ActionError("test.param.urlMessage", urlMessage);
208                     errors.add("test.param.urlMessage", urlActionMessage);
209
210                     String JavaDoc[] queryParamMessage = {"Parameter :" + currentQueryBean.getName()};
211                     ActionError queryParamActionMessage = new ActionError("test.param.urlMessage", queryParamMessage);
212                     errors.add("test.param.urlMessage", queryParamActionMessage);
213
214                     // update found Errors value
215
foundErros = true;
216                 }
217             }
218         }
219
220         // save all message
221
saveErrors(request, errors);
222         return foundErros;
223     }
224
225
226     /**
227      * Sets the QueryParsmsValue attribute of the TestClipperAction object
228      *
229      *@param request The new QueryParsmsValue value
230      *@param uBean The new QueryParsmsValue value
231      */

232     private void updateQueryParamsValue(HttpServletRequest request, UrlBean uBean) {
233         logger.debug("[ Set query param ]");
234         Map queryParamDefaultValue = buildQueryMap(request, uBean);
235
236         //get the list of all query parameter of th next url
237
UrlBean nextUrlBean = SessionManager.getClipperBean(request).getNextRecordedUrlBean(uBean);
238         // test that there is a next url
239
if (nextUrlBean != null) {
240             List queryParamBeanList = nextUrlBean.getQueryParamBeanList();
241             logger.debug("[ Query param size: " + queryParamBeanList.size() + " ]");
242             for (int i = 0; i < queryParamBeanList.size(); i++) {
243                 QueryParamBean qBean = (QueryParamBean) queryParamBeanList.get(i);
244                 String JavaDoc value = (String JavaDoc) queryParamDefaultValue.get(qBean.getName());
245                 //update value
246
if (value != null) {
247                     qBean.setDefaultValue(value);
248                     logger.debug("[ Query param " + qBean.getName() + " is set whith value " + value + " ]");
249                 }
250                 else {
251                     logger.error("[ No value found for Query param " + qBean.getName() + ".]");
252                 }
253             }
254         }
255     }
256
257
258
259     /**
260      * Description of the Method
261      *
262      *@param request Description of Parameter
263      *@param uBean Description of Parameter
264      *@return Description of the Returned Value
265      */

266     private Map buildQueryMap(HttpServletRequest request, UrlBean uBean) {
267         Map queryParamDefaultValue = new TreeMap();
268         Map params = getParam(request);
269         // Get value of all parameter
270
String JavaDoc[] values = ((String JavaDoc[]) params.get("usedValue"));
271         if (values == null) {
272             return queryParamDefaultValue;
273         }
274         logger.debug("[ URL: " + uBean.getAbsoluteUrlValue() + " ]");
275         logger.debug("[ Request param size " + values.length + " ]");
276         logger.debug("[ Form param size " + uBean.getFormParamBeanList().size() + " ]");
277         for (int j = 0; j < values.length; j++) {
278             logger.debug("[ Looking for param at position " + j + " ]");
279             // retrieve the name of parameter
280
FormParamBean fBean = (FormParamBean) uBean.getFormParamBeanList().get(j);
281             String JavaDoc name = fBean.getName();
282             logger.debug("[ Param found whith name " + name + " ]");
283             //get the value
284
String JavaDoc paramValues = values[j];
285             // put to the map
286
queryParamDefaultValue.put(name, paramValues);
287         }
288         return queryParamDefaultValue;
289     }
290
291
292     /**
293      * Description of the Method
294      *
295      *@param form Description of Parameter
296      *@param request Description of Parameter
297      *@exception Exception Description of Exception
298      */

299     private void activateParamsList(TestClipperForm form, HttpServletRequest request) throws Exception JavaDoc {
300         ClipperBean bean = SessionManager.getClipperBean(request);
301
302         logger.debug("[ Url list size == " + bean.getUrlListBean().size() + " ]");
303
304         String JavaDoc position = form.getSelectedUrl();
305
306         // this can occur the first time the edit page is accesed
307
if (position == null) {
308             //there is at leat one element in the urlListBean
309
position = "0";
310             form.setOldSelectedUrl(position);
311             form.setSelectedUrl(position);
312             logger.debug("[ First url selected autommatically]");
313         }
314
315         //Activate form list
316
int posUrl = Integer.parseInt(position);
317         List fList = bean.getAllFormParam(posUrl);
318         if (fList.isEmpty()) {
319             logger.debug("[ Form Param list is empty ]");
320         }
321         for (int i = 0; i < fList.size(); i++) {
322             logger.debug(" [ Form Param value " + fList.get(i) + " ] ");
323         }
324         form.setActifFormParamsList(fList);
325         logger.debug("[ Actif Parameters List is set from first url at position " + posUrl + " ]");
326
327         //Activate query list
328
List qList = bean.getAllQueryParam(posUrl);
329         if (qList.isEmpty()) {
330             logger.debug("[ Query param list is empty ]");
331         }
332
333         form.setActifQueryParamsList(qList);
334
335     }
336
337 }
338
Popular Tags