KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

31     public Map getKeyMethodMap() {
32         Map map = new HashMap();
33         map.put("init", "init");
34         map.put("manage.button.add", "add");
35         map.put("manage.button.delete", "delete");
36         //map.put("manage.button.load", "load");
37
map.put("manage.button.load", "test");
38         map.put("manage.button.test", "test");
39         map.put("manage.button.intDatabase", "initXML");
40         map.put("manage.button.initXML", "initXML");
41         map.put("manage.deploy", "deploy");
42         map.put("menu.back", "backToMenuBuilder");
43         return map;
44     }
45
46
47
48
49
50     /**
51      * Gets the FormId attribute of the ManageAction object
52      *
53      *@return The FormId value
54      */

55     public int getFormId() {
56         return org.jahia.clipbuilder.html.struts.Util.Constants.MANAGE;
57     }
58
59
60
61     /**
62      * Gets the ClippersDirectoryPath attribute of the ManageAction object
63      *
64      *@param request Description of Parameter
65      *@return The ClippersDirectoryPath value
66      */

67     public String JavaDoc getClippersDirectoryPath(HttpServletRequest request) {
68         return getServlet().getServletContext().getRealPath("/") + getResources(request).getMessage("clippers.repository.path");
69     }
70
71
72     /**
73      * Gets the ClippersDirectoryPathDeploy attribute of the ManageAction object
74      *
75      *@param request Description of Parameter
76      *@return The ClippersDirectoryPathDeploy value
77      */

78     public String JavaDoc getClippersDirectoryPathDeploy(HttpServletRequest request) {
79         String JavaDoc path = org.jahia.clipbuilder.util.JahiaUtils.getPortletDiretcoryPath(request);
80         if (path == null) {
81             // clip builder is not part of jahia
82
path = getResources(request).getMessage("clippers.repository.deploy.path");
83         }
84         return path;
85     }
86
87
88
89     /**
90      * Description of the Method
91      *
92      *@param actionMapping Description of Parameter
93      *@param actionForm Description of Parameter
94      *@param httpServletRequest Description of Parameter
95      *@param httpServletResponse Description of Parameter
96      *@return Description of the Returned Value
97      */

98     public ActionForward add(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
99         logger.debug("[ Add clipper ]");
100         ManageClippersForm form = (ManageClippersForm) actionForm;
101
102         //init the description clipper
103
ClipperBean cBean = new ClipperBean();
104
105         // set the defaultconfiguration
106
cBean.setConfigurationBean(getDefaultConfigurationManager().getDefaultConfigurationBean());
107         SessionManager.setClipperBean(httpServletRequest, cBean);
108
109         //httpServletRequest.getSession().setAttribute(Constants.DESCRIPTION_FORM, new DescriptionClipperForm());
110

111         return actionMapping.findForward("descriptionLoaded");
112     }
113
114
115     /**
116      * Description of the Method
117      *
118      *@param actionMapping Description of Parameter
119      *@param actionForm Description of Parameter
120      *@param httpServletRequest Description of Parameter
121      *@param httpServletResponse Description of Parameter
122      *@return Description of the Returned Value
123      */

124     public ActionForward deploy(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
125         logger.debug("[ Add clipper ]");
126         ManageClippersForm form = (ManageClippersForm) actionForm;
127
128         //get clipper name
129
String JavaDoc fileName = form.getClipper();
130         ClippersManagerBean cmBean = SessionManager.getClippersManagerBean(httpServletRequest);
131         ClipperBean cBean = cmBean.getClipperBean(fileName);
132         String JavaDoc path = getClippersDirectoryPath(httpServletRequest) + File.separator + fileName + ".xml";
133
134         // init and load the clipper
135
cBean = new ClipperBean();
136         cBean.loadFromXml(path);
137
138         //generate clipper.xml file
139
String JavaDoc htmlPatternDrirectoryPath = getClippersDirectoryPath(httpServletRequest) + File.separator + "htmlPattern" + File.separator + "htmlClipperPortletPattern.war";
140         //String clipperXmlFile = htmlPatternDrirectoryPath + File.separator + "WEB-INF" + File.separator + "clipper.xml";
141
//cBean.buildXmlDocument().saveInFile(clipperXmlFile);
142

143         //deploy
144
String JavaDoc patternWar = htmlPatternDrirectoryPath;
145         String JavaDoc warTarget = getClippersDirectoryPathDeploy(httpServletRequest) + File.separator + "jahia_clip_"+cBean.getName() + ".war";
146         String JavaDoc portletName = "html_clip_" + cBean.getName();
147         String JavaDoc portletDescription = cBean.getDescription();
148         org.jdom.Document clipperXML = cBean.buildXmlDocument().getDoc();
149         try {
150             DeployUtilities.getInstance().deploy(patternWar, warTarget, portletName, portletDescription, clipperXML);
151         }
152         catch (Exception JavaDoc ex) {
153             ex.printStackTrace();
154             logger.error("Error has occured during deuploy step: " + ex.toString());
155         }
156
157         return actionMapping.getInputForward();
158     }
159
160
161
162     /**
163      * Description of the Method
164      *
165      *@param actionMapping Description of Parameter
166      *@param actionForm Description of Parameter
167      *@param request Description of Parameter
168      *@param httpServletResponse Description of Parameter
169      *@return Description of the Returned Value
170      */

171     public ActionForward delete(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse httpServletResponse) {
172         logger.debug("[ delete clipper ]");
173         ManageClippersForm form = (ManageClippersForm) actionForm;
174         //get clipper name
175
String JavaDoc fileName = form.getClipper();
176         String JavaDoc fullPath = getClippersDirectoryPath(request) + File.separator + fileName + ".xml";
177
178         //delete it
179
boolean res = FileUtilities.deleteFile(fullPath);
180         if (res) {
181             logger.debug("File has beean deleted");
182         }
183         else {
184             logger.error("Enable to delete clipper");
185         }
186
187         //load list of clippers
188
SessionManager.initClippersManagerBean(request);
189         loadListNameClippersFromXmlDirectory(request);
190         return actionMapping.getInputForward();
191     }
192
193
194     /**
195      * Description of the Method
196      *
197      *@param actionMapping Description of Parameter
198      *@param actionForm Description of Parameter
199      *@param httpServletRequest Description of Parameter
200      *@param httpServletResponse Description of Parameter
201      *@return Description of the Returned Value
202      */

203     public ActionForward test(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
204         try {
205             logger.debug("[ Test clipper ]");
206             ManageClippersForm form = (ManageClippersForm) actionForm;
207
208             boolean templateView = form.isTemplateView();
209             if (templateView) {
210                 logger.debug(" [ load from xml ] ");
211                 loadFromXML(form, httpServletRequest);
212             }
213             else {
214                 logger.debug(" [ load from database ] ");
215                 // this.loadFromDatabase();
216
loadFromXML(form, httpServletRequest);
217             }
218             return actionMapping.findForward("testClipper");
219         }
220         catch (Exception JavaDoc ex) {
221             ex.printStackTrace();
222             return actionMapping.getInputForward();
223         }
224     }
225
226
227     /**
228      * Adds a feature to the Sql attribute of the ManageAction object
229      *
230      *@param actionMapping The feature to be added to the Sql attribute
231      *@param actionForm The feature to be added to the Sql attribute
232      *@param httpServletRequest The feature to be added to the Sql attribute
233      *@param httpServletResponse The feature to be added to the Sql attribute
234      *@return Description of the Returned Value
235      */

236     public ActionForward addSql(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
237         return actionMapping.findForward("buildSqlPortlet");
238     }
239
240
241
242     /**
243      * Description of the Method
244      *
245      *@param actionMapping Description of Parameter
246      *@param actionForm Description of Parameter
247      *@param request Description of Parameter
248      *@param response Description of Parameter
249      *@return Description of the Returned Value
250      */

251     public ActionForward load(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
252         try {
253             logger.debug("[ load clipper ]");
254             ManageClippersForm form = (ManageClippersForm) actionForm;
255             SessionManager.setClipperBean(request, new ClipperBean());
256
257             // set stuts form in the session the session
258
HttpSession session = request.getSession();
259             DescriptionClipperForm dform = new DescriptionClipperForm();
260             BrowseForm bform = new BrowseForm();
261             SelectPartForm sform = new SelectPartForm();
262                         EditParamForm eform = new EditParamForm();
263             PreviewForm pform = new PreviewForm();
264
265             session.setAttribute(org.jahia.clipbuilder.html.struts.Util.Constants.DESCRIPTION_FORM, dform);
266             session.setAttribute(org.jahia.clipbuilder.html.struts.Util.Constants.BROWSE_FORM, bform);
267             session.setAttribute(org.jahia.clipbuilder.html.struts.Util.Constants.SELECTPART_FORM, sform);
268                         session.setAttribute(org.jahia.clipbuilder.html.struts.Util.Constants.EDITPARAM_FORM, eform);
269             session.setAttribute(org.jahia.clipbuilder.html.struts.Util.Constants.PREVIEW_FORM, pform);
270             updateSessionAttributes(request, dform.getId());
271
272
273             boolean templateView = form.isTemplateView();
274             if (templateView) {
275                 logger.debug(" [ load from xml ] ");
276                 loadFromXML(form, request);
277             }
278             else {
279                 logger.debug(" [ load from database ] ");
280                 // this.loadFromDatabase();
281
loadFromXML(form, request);
282             }
283
284
285             //init all form
286
ClipperBean cBean = SessionManager.getClipperBean(request);
287             // run simulator
288
WebBrowserForm w = new WebBrowserForm();
289             SessionManager.setWebBrowserForm(request, w);
290             WebBrowserSimulator wbs = new WebBrowserSimulator(request, response, cBean);
291             wbs.executeForLoad(request, response, WebBrowserSimulator.MODE_LOOP);
292
293             // load form parameter
294
((AbstractWizardForm) session.getAttribute(org.jahia.clipbuilder.html.struts.Util.Constants.DESCRIPTION_FORM)).loadFromClipperBean(cBean);
295             ((AbstractWizardForm) session.getAttribute(org.jahia.clipbuilder.html.struts.Util.Constants.BROWSE_FORM)).loadFromClipperBean(cBean);
296             ((AbstractWizardForm) session.getAttribute(org.jahia.clipbuilder.html.struts.Util.Constants.SELECTPART_FORM)).loadFromClipperBean(cBean);
297                         ((AbstractWizardForm) session.getAttribute(org.jahia.clipbuilder.html.struts.Util.Constants.EDITPARAM_FORM)).loadFromClipperBean(cBean);
298             ((AbstractWizardForm) session.getAttribute(org.jahia.clipbuilder.html.struts.Util.Constants.PREVIEW_FORM)).loadFromClipperBean(cBean);
299
300             return actionMapping.findForward("descriptionLoaded");
301         }
302         catch (Exception JavaDoc ex) {
303             ex.printStackTrace();
304             return actionMapping.getInputForward();
305         }
306     }
307
308
309
310     /**
311      * Description of the Method
312      *
313      *@param actionMapping Description of Parameter
314      *@param actionForm Description of Parameter
315      *@param request Description of Parameter
316      *@param httpServletResponse Description of Parameter
317      *@return Description of the Returned Value
318      */

319     public ActionForward init(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse httpServletResponse) {
320         super.init(actionMapping, actionForm, request, httpServletResponse);
321
322         // remove Test bean form from session
323
removeBeanForm(request, org.jahia.clipbuilder.html.struts.Util.Constants.TEST);
324
325         ManageClippersForm form = (ManageClippersForm) actionForm;
326         form.setTemplateView(false);
327
328         try {
329             //init default configuration if needed
330
if (!getDefaultConfigurationManager().defaultConfigurationExist()) {
331                 logger.debug("[ Default configuration does'nt exist ]");
332                 return actionMapping.findForward("configure");
333             }
334             else {
335                 logger.debug("[ Default configuration exists ]");
336             }
337
338             //Init the clippers Manager
339
SessionManager.initSessionAttributes(request);
340
341             //load clippers
342
//loadListNameClippersFromDatabase(request);
343
loadListNameClippersFromXmlDirectory(request);
344         }
345         catch (Exception JavaDoc ex) {
346             logger.error("error has occured: " + ex.toString());
347             ex.printStackTrace();
348         }
349
350         return actionMapping.getInputForward();
351     }
352
353
354     /**
355      * Description of the Method
356      *
357      *@param actionMapping Description of Parameter
358      *@param actionForm Description of Parameter
359      *@param request Description of Parameter
360      *@param response Description of Parameter
361      *@return Description of the Returned Value
362      */

363     public ActionForward backToMenuBuilder(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
364         return actionMapping.findForward("menuBuilder");
365     }
366
367
368
369     /**
370      * Description of the Method
371      *
372      *@param actionMapping Description of Parameter
373      *@param actionForm Description of Parameter
374      *@param request Description of Parameter
375      *@param httpServletResponse Description of Parameter
376      *@return Description of the Returned Value
377      */

378     public ActionForward initDatabase(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse httpServletResponse) {
379         ManageClippersForm form = (ManageClippersForm) actionForm;
380         form.setTemplateView(false);
381
382         //init default configuration if needed
383
if (!getDefaultConfigurationManager().defaultConfigurationExist()) {
384             logger.debug("[ Default configuration does'nt exist ]");
385             return actionMapping.findForward("configure");
386         }
387         else {
388             logger.debug("[ Default configuration exists ]");
389         }
390
391         //Init the clippers Manager
392
SessionManager.initSessionAttributes(request);
393
394         //load clippers
395
//loadListNameClippersFromDatabase(request);
396

397         return actionMapping.getInputForward();
398     }
399
400
401
402     /**
403      * Description of the Method
404      *
405      *@param actionMapping Description of Parameter
406      *@param actionForm Description of Parameter
407      *@param request Description of Parameter
408      *@param httpServletResponse Description of Parameter
409      *@return Description of the Returned Value
410      */

411     public ActionForward initXML(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse httpServletResponse) {
412         ManageClippersForm form = (ManageClippersForm) actionForm;
413         form.setTemplateView(true);
414
415         //Init Session att
416
SessionManager.initSessionAttributes(request);
417
418         //load clippers
419
loadListNameClippersFromXmlDirectory(request);
420
421         return actionMapping.getInputForward();
422     }
423
424
425     /**
426      * Description of the Method
427      *
428      *@param form Description of Parameter
429      *@param request Description of Parameter
430      */

431     private void loadFromXML(ManageClippersForm form, HttpServletRequest request) {
432         logger.debug("[ load clipper ]");
433         String JavaDoc fileName = form.getClipper();
434
435         //load the selected clipper
436
ClippersManagerBean cmBean = SessionManager.getClippersManagerBean(request);
437         ClipperBean cBean = cmBean.getClipperBean(fileName);
438         String JavaDoc path = getClippersDirectoryPath(request) + File.separator + fileName + ".xml";
439         logger.debug("[ Selected clipper is " + path + "]");
440
441         // init clipper bean
442
cBean = new ClipperBean();
443         cBean.loadFromXml(path);
444
445         //load the clipperBean
446
SessionManager.setClipperBean(request, cBean);
447     }
448
449
450
451     /**
452      * Description of the Method
453      *
454      *@param actionMapping Description of Parameter
455      *@param actionForm Description of Parameter
456      *@param httpServletRequest Description of Parameter
457      *@param httpServletResponse Description of Parameter
458      *@return Description of the Returned Value
459      */

460     private ActionForward loadFromDatabase(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
461         logger.debug("[ load clipper from database ]");
462
463         // Get name of the clipper
464
ManageClippersForm form = (ManageClippersForm) actionForm;
465         String JavaDoc clipperName = form.getClipper();
466
467         //Get clipper by name from the database
468
ClipperBean cBean = this.getClipperManager().getClipperBean(clipperName);
469
470         int urlSize = cBean.getUrlListBean().size();
471         logger.debug("[ Url size " + urlSize + " ]");
472
473         //load the clipperBean
474
SessionManager.setClipperBean(httpServletRequest, cBean);
475
476         return actionMapping.findForward("testClipper");
477     }
478
479
480
481     /**
482      * load clippers that have been saved in the database
483      *
484      *@param request Description of Parameter
485      */

486     /*private void loadListNameClippersFromDatabase(HttpServletRequest request) {
487         //load
488         Map clipperList = getClipperManager().getClippersIdName();
489         if (clipperList == null) {
490             logger.debug("[ List clippers is empty !!! ]");
491             return;
492         }
493         Iterator nameIt = clipperList.keySet().iterator();
494         while (nameIt.hasNext()) {
495             String name = (String) nameIt.next();
496             Long id = (Long) clipperList.get(name);
497             logger.debug("[ Found clipper with name " + name + " ]");
498
499             //Build the clipper
500             SessionManager.getClippersManagerBean(request).addClipper(name, id);
501         }
502
503     }*/

504
505
506     /**
507      * load clippers that have been saved in the xml directory
508      *
509      *@param request Description of Parameter
510      */

511     private void loadListNameClippersFromXmlDirectory(HttpServletRequest request) {
512         // get the path of the clippers directory
513
String JavaDoc path = getClippersDirectoryPath(request);
514         logger.debug("[Path is: " + path + " ]");
515
516         //test
517
File directory = new File(path);
518         if (!directory.isDirectory()) {
519             logger.error("[ The specified path is not a directory: " + directory.getAbsolutePath() + " ]");
520             return;
521         }
522
523         //load
524
File[] clippersDescriptor = directory.listFiles();
525         for (int i = 0; i < clippersDescriptor.length; i++) {
526             File f = clippersDescriptor[i];
527             if (!f.isDirectory() && f.getAbsolutePath().endsWith(".xml")) {
528                 String JavaDoc name = f.getName();
529                 logger.debug("[ Found clipper with name " + name + " ]");
530                 //Build the clipper
531
ClipperBean cBean = new ClipperBean(f.getAbsolutePath());
532                 SessionManager.getClippersManagerBean(request).addClipper(cBean);
533             }
534         }
535
536     }
537
538 }
539
Popular Tags