KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > templates > action > EditTemplateAction


1 package com.dotmarketing.portlets.templates.action;
2
3 import java.net.URLDecoder JavaDoc;
4 import java.net.URLEncoder JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 import javax.portlet.ActionRequest;
9 import javax.portlet.ActionResponse;
10 import javax.portlet.PortletConfig;
11 import javax.portlet.WindowState;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13
14 import org.apache.commons.beanutils.BeanUtils;
15 import org.apache.struts.action.ActionForm;
16 import org.apache.struts.action.ActionMapping;
17
18 import com.dotmarketing.beans.Host;
19 import com.dotmarketing.beans.Identifier;
20 import com.dotmarketing.beans.Inode;
21 import com.dotmarketing.db.DotHibernate;
22 import com.dotmarketing.factories.HostFactory;
23 import com.dotmarketing.factories.IdentifierFactory;
24 import com.dotmarketing.factories.InodeFactory;
25 import com.dotmarketing.factories.WebAssetFactory;
26 import com.dotmarketing.portal.struts.DotPortletAction;
27 import com.dotmarketing.portal.struts.DotPortletActionInterface;
28 import com.dotmarketing.portlets.files.model.File;
29 import com.dotmarketing.portlets.htmlpages.model.HTMLPage;
30 import com.dotmarketing.portlets.templates.factories.TemplateFactory;
31 import com.dotmarketing.portlets.templates.model.Template;
32 import com.dotmarketing.portlets.templates.struts.TemplateForm;
33 import com.dotmarketing.services.PageServices;
34 import com.dotmarketing.services.TemplateServices;
35 import com.dotmarketing.util.Config;
36 import com.dotmarketing.util.Logger;
37 import com.dotmarketing.util.PortletURLUtil;
38 import com.dotmarketing.util.Validator;
39 import com.dotmarketing.util.WebKeys;
40 import com.liferay.portal.model.User;
41 import com.liferay.portal.util.Constants;
42 import com.liferay.portlet.ActionRequestImpl;
43 import com.liferay.util.servlet.SessionMessages;
44
45 /**
46  * @author Maria
47  */

48
49 public class EditTemplateAction extends DotPortletAction implements
50         DotPortletActionInterface {
51
52     public void processAction(ActionMapping mapping, ActionForm form,
53             PortletConfig config, ActionRequest req, ActionResponse res)
54             throws Exception JavaDoc {
55
56         String JavaDoc cmd = req.getParameter(Constants.CMD);
57         String JavaDoc referer = req.getParameter("referer");
58
59         //wraps request to get session object
60
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
61         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
62
63         if ((referer != null) && (referer.length() != 0)) {
64             referer = URLDecoder.decode(referer, "UTF-8");
65         }
66
67         Logger.debug(this, "EditTemplateAction cmd=" + cmd);
68
69         if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.TEMPLATE_ADD_CONTAINER)) {
70             Logger.debug(this, "I'm popping up the Template selector");
71             setForward(req, "portlet.ext.templates.container_selector");
72             return;
73         }
74
75         //popup the Template selector first
76
if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.TEMPLATE_ADD_FILE)) {
77             Logger.debug(this, "I'm popping up the file selector");
78             setForward(req, "portlet.ext.templates.file_selector");
79             return;
80         }
81
82         DotHibernate.startTransaction();
83
84         User user = _getUser(req);
85
86         try {
87             Logger.debug(this, "Calling Retrieve method");
88             _retrieveWebAsset(req, res, config, form, user, Template.class,
89                     WebKeys.TEMPLATE_EDIT);
90
91         } catch (Exception JavaDoc ae) {
92             _handleException(ae, req);
93             return;
94         }
95
96         /*
97          * We are editing the Template
98          */

99         if ((cmd != null) && cmd.equals(Constants.EDIT)) {
100             try {
101                 Logger.debug(this, "Calling Edit method");
102                 _editWebAsset(req, res, config, form, user);
103
104             } catch (Exception JavaDoc ae) {
105                 if ((referer != null) && (referer.length() != 0)) {
106                     if (ae.getMessage().equals(WebKeys.EDIT_ASSET_EXCEPTION)) {
107                         //The web asset edit threw an exception because it's
108
// locked so it should redirect back with message
109
java.util.Map JavaDoc<String JavaDoc,String JavaDoc[]> params = new java.util.HashMap JavaDoc<String JavaDoc,String JavaDoc[]>();
110                         params.put("struts_action",new String JavaDoc[] { "/ext/director/direct" });
111                         params.put("cmd", new String JavaDoc[] { "editTemplate" });
112                         params.put("template", new String JavaDoc[] { req.getParameter("inode") });
113                         params.put("referer", new String JavaDoc[] { URLEncoder.encode(referer, "UTF-8") });
114
115                         String JavaDoc directorURL = com.dotmarketing.util.PortletURLUtil.getActionURL(httpReq, WindowState.MAXIMIZED.toString(), params);
116
117                         _sendToReferral(req, res, directorURL);
118                         return;
119                     }
120                 }
121                 _handleException(ae, req);
122             }
123         }
124
125         /*
126          * If we are updating the Template, copy the information
127          * from the struts bean to the hbm inode and run the
128          * update action and return to the list
129          */

130         if ((cmd != null) && cmd.equals(Constants.ADD)) {
131             try {
132
133                 if (Validator.validate(req, form, mapping)) {
134
135                     Logger.debug(this, "Calling Save method");
136                     _saveWebAsset(req, res, config, form, user);
137
138                     String JavaDoc subcmd = req.getParameter("subcmd");
139
140                     if ((subcmd != null)
141                             && subcmd
142                                     .equals(com.dotmarketing.util.Constants.PUBLISH)) {
143                         Logger.debug(this, "Calling Publish method");
144                         _publishWebAsset(req, res, config, form, user,
145                                 WebKeys.TEMPLATE_FORM_EDIT);
146                         //_publishHTMLPages(req, res, config, form);
147
}
148                     
149                     java.util.Map JavaDoc params = new java.util.HashMap JavaDoc();
150                     params.put("struts_action",new String JavaDoc[] {"/ext/templates/view_templates"});
151                     referer = PortletURLUtil.getActionURL(req,WindowState.MAXIMIZED.toString(),params);
152                     _sendToReferral(req, res, referer);
153                     return;
154                 }
155
156             } catch (Exception JavaDoc ae) {
157                 _handleException(ae, req);
158             }
159
160         }
161         /*
162          * If we are deleteing the Template,
163          * run the delete action and return to the list
164          *
165          */

166         else if ((cmd != null) && cmd.equals(Constants.DELETE)) {
167             try {
168                 Logger.debug(this, "Calling Delete method");
169                 _deleteWebAsset(req, res, config, form, user,
170                         WebKeys.TEMPLATE_EDIT);
171
172             } catch (Exception JavaDoc ae) {
173                 _handleException(ae, req);
174                 return;
175             }
176             _sendToReferral(req, res, referer);
177         }
178         /*
179          * If we are undeleting the Template,
180          * run the undelete action and return to the list
181          *
182          */

183         else if ((cmd != null)
184                 && cmd.equals(com.dotmarketing.util.Constants.UNDELETE)) {
185             try {
186                 Logger.debug(this, "Calling UnDelete method");
187                 _undeleteWebAsset(req, res, config, form, user,
188                         WebKeys.TEMPLATE_EDIT);
189
190             } catch (Exception JavaDoc ae) {
191                 _handleException(ae, req);
192                 return;
193             }
194             _sendToReferral(req, res, referer);
195
196         }
197         /*
198          * If we are deleting the Template version,
199          * run the deeleteversion action and return to the list
200          */

201         else if ((cmd != null)
202                 && cmd.equals(com.dotmarketing.util.Constants.DELETEVERSION)) {
203             try {
204                 Logger.debug(this, "Calling Delete Version Method");
205                 _deleteVersionWebAsset(req, res, config, form, user,
206                         WebKeys.TEMPLATE_EDIT);
207
208             } catch (Exception JavaDoc ae) {
209                 _handleException(ae, req);
210                 return;
211             }
212             _sendToReferral(req, res, referer);
213         }
214         /*
215          * If we are unpublishing the Template,
216          * run the unpublish action and return to the list
217          */

218         else if ((cmd != null)
219                 && cmd.equals(com.dotmarketing.util.Constants.UNPUBLISH)) {
220             try {
221                 Logger.debug(this, "Calling Unpublish Method");
222                 _unPublishWebAsset(req, res, config, form, user,
223                         WebKeys.TEMPLATE_EDIT);
224
225             } catch (Exception JavaDoc ae) {
226                 _handleException(ae, req);
227                 return;
228             }
229             _sendToReferral(req, res, referer);
230
231         }
232         /*
233          * If we are getting the Template version back,
234          * run the getversionback action and return to the list
235          */

236         else if ((cmd != null)
237                 && cmd.equals(com.dotmarketing.util.Constants.GETVERSIONBACK)) {
238             try {
239                 Logger.debug(this, "Calling Get Version Back Method");
240                 _getVersionBackWebAsset(req, res, config, form, user);
241
242             } catch (Exception JavaDoc ae) {
243                 _handleException(ae, req);
244             }
245             _sendToReferral(req, res, referer);
246         }
247         /*
248          * If we are getting the Template versions,
249          * run the assetversions action and return to the list
250          */

251         else if ((cmd != null)
252                 && cmd.equals(com.dotmarketing.util.Constants.ASSETVERSIONS)) {
253             try {
254                 Logger.debug(this, "Calling Get Versions Method");
255                 _getVersionsWebAsset(req, res, config, form, user,
256                         WebKeys.TEMPLATE_EDIT, WebKeys.TEMPLATE_VERSIONS);
257
258             } catch (Exception JavaDoc ae) {
259                 _handleException(ae, req);
260                 return;
261             }
262         }
263         /*
264          * If we are unlocking the Template,
265          * run the unlock action and return to the list
266          */

267         else if ((cmd != null)
268                 && cmd.equals(com.dotmarketing.util.Constants.UNLOCK)) {
269             try {
270                 Logger.debug(this, "Calling Unlock Method");
271                 _unLockWebAsset(req, res, config, form, user,
272                         WebKeys.TEMPLATE_EDIT);
273
274             } catch (Exception JavaDoc ae) {
275                 _handleException(ae, req);
276                 return;
277             }
278             _sendToReferral(req, res, referer);
279
280         }
281         /*
282          * If we are copying the Template,
283          * run the copy action and return to the list
284          */

285         else if ((cmd != null)
286                 && cmd.equals(com.dotmarketing.util.Constants.COPY)) {
287             try {
288                 Logger.debug(this, "Calling Copy Method");
289                 _copyWebAsset(req, res, config, form, user);
290             } catch (Exception JavaDoc ae) {
291                 _handleException(ae, req);
292             }
293             _sendToReferral(req, res, referer);
294         } else
295             Logger.debug(this, "Unspecified Action");
296         
297         DotHibernate.commitTransaction();
298         setForward(req, "portlet.ext.templates.edit_template");
299     }
300
301     ///// ************** ALL METHODS HERE *************************** ////////
302

303     @SuppressWarnings JavaDoc("unchecked")
304     public void _editWebAsset(ActionRequest req, ActionResponse res,
305             PortletConfig config, ActionForm form, User user) throws Exception JavaDoc {
306
307         //calls edit method from super class that returns parent folder
308
super._editWebAsset(req, res, config, form, user,
309                 WebKeys.TEMPLATE_EDIT);
310
311         //This can't be done on the WebAsset so it needs to be done here.
312
Template template = (Template) req.getAttribute(WebKeys.TEMPLATE_EDIT);
313
314         //gets image file --- on the image field on teh template we store the image's identifier
315
Identifier imageIdentifier = (Identifier) InodeFactory.getInode(template.getImage(), Identifier.class);
316         File imageFile = (File) IdentifierFactory.getWorkingChildOfClass(imageIdentifier, File.class);
317
318         //gets the template host
319
Host host = HostFactory.getParentHost(template);
320
321         //setting parent folder path and inode on the form bean
322
TemplateForm cf = (TemplateForm) form;
323         cf.setImage(imageFile.getInode());
324         cf.setSelectedimage(imageFile.getFileName());
325         cf.setHostInode(host.getInode());
326         
327     }
328
329     public void _publishHTMLPages(ActionRequest req, ActionResponse res,
330             PortletConfig config, ActionForm form) throws Exception JavaDoc {
331
332         User user = _getUser(req);
333
334         //gets the current template being edited from the request object
335
Template template = (Template) req.getAttribute(WebKeys.TEMPLATE_EDIT);
336         
337         String JavaDoc condition = "live=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted="
338                 + com.dotmarketing.db.DbConnectionFactory.getDBFalse();
339         List JavaDoc pagesForThisTemplate = InodeFactory.getChildrenClassByCondition(
340                 template, HTMLPage.class, condition);
341
342         Iterator JavaDoc iterPages = pagesForThisTemplate.iterator();
343         //iterates through the children pages
344
while (iterPages.hasNext()) {
345             HTMLPage page = (HTMLPage) iterPages.next();
346             try {
347                 _checkUserPermissions(page, user, Config.getIntProperty("PERMISSION_PUBLISH"));
348                 //writes the page to a file
349
PageServices.publishPageToFile(page);
350             } catch (Exception JavaDoc e) { }
351         }
352     }
353
354     @SuppressWarnings JavaDoc("unchecked")
355     public void _saveWebAsset(ActionRequest req, ActionResponse res,
356             PortletConfig config, ActionForm form, User user) throws Exception JavaDoc {
357         //wraps request to get session object
358
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
359         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
360         
361         //gets TemplateForm struts bean
362
TemplateForm cf = (TemplateForm) form;
363         
364         //gets the new information for the container from the request object
365
req.setAttribute(WebKeys.TEMPLATE_FORM_EDIT, new Template());
366         BeanUtils.copyProperties(req.getAttribute(WebKeys.TEMPLATE_FORM_EDIT),
367                 form);
368
369         //gets the new information for the template from the request object
370
Template template = (Template) req
371                 .getAttribute(WebKeys.TEMPLATE_FORM_EDIT);
372
373         //gets the current template being edited from the request object
374
Template currentTemplate = (Template) req
375                 .getAttribute(WebKeys.TEMPLATE_EDIT);
376
377         //Checking permissions
378
_checkPermissions(currentTemplate, user, httpReq);
379
380         //gets html pages children from current template
381
java.util.List JavaDoc<HTMLPage> htmlPageChildren = InodeFactory.getChildrenClass(
382                 currentTemplate, HTMLPage.class);
383
384         //gets user id from request for mod user
385
String JavaDoc userId = user.getUserId();
386
387         //gets identifier from the current asset
388
Identifier identifier = IdentifierFactory.getIdentifierByInode(currentTemplate);
389
390         //it saves or updates the asset
391
if (currentTemplate.getInode() > 0) {
392             WebAssetFactory.createAsset(template, userId, identifier, false);
393             template = (Template) WebAssetFactory.saveAsset(template, identifier);
394         } else {
395             WebAssetFactory.createAsset(template, userId);
396         }
397
398         req.setAttribute(WebKeys.TEMPLATE_FORM_EDIT, template);
399         
400         //gets file object for the thumbnail
401
File imageFile = (File) InodeFactory.getInode(cf.getImage(), File.class);
402         Identifier imageIdentifier = IdentifierFactory.getParentIdentifier(imageFile);
403         if (imageIdentifier!=null) {
404             template.setImage(imageIdentifier.getInode());
405         }
406
407         //creates new set for template children
408
java.util.Set JavaDoc<HTMLPage> templateChildrenSet = new java.util.HashSet JavaDoc<HTMLPage>();
409
410         //adds the html pages as children
411
templateChildrenSet.addAll(htmlPageChildren);
412         //sets the new set of children
413
for(HTMLPage html : templateChildrenSet ){
414             template.addChild(html);
415         }
416
417         ///parses the body tag to get all identifier ids and saves them as children
418
String JavaDoc containerTag = "#parse( $container";
419         String JavaDoc body = template.getBody();
420         int idx1 = 0;
421         int idx2 = 0;
422         String JavaDoc containerId = "";
423         while ((idx2 = body.indexOf(containerTag, idx1)) != -1) {
424             idx1 = body.indexOf(")", idx2 + containerTag.length());
425             containerId = body
426                     .substring(idx2 + containerTag.length(), idx1 - 1);
427             Identifier containerIdentifier = (Identifier) InodeFactory.getInode(
428                     containerId, Identifier.class);
429             template.addChild(containerIdentifier);
430         }
431
432         identifier = IdentifierFactory.getIdentifierByInode(template);
433
434         //Saving the host of the template
435
Host host = HostFactory.getHost(cf.getHostInode());
436         template.addParent(host);
437         identifier.setHostInode(host.getInode());
438         InodeFactory.saveInode(identifier);
439         
440         TemplateServices.writeTemplateToFile(template, true);
441         SessionMessages.add(httpReq, "message", "message.template.save");
442
443         //Saving permissions
444
_applyPermissions(req, template);
445         
446         //copies the information back into the form bean
447
BeanUtils.copyProperties(form, req
448                 .getAttribute(WebKeys.TEMPLATE_FORM_EDIT));
449         BeanUtils.copyProperties(currentTemplate, req
450                 .getAttribute(WebKeys.TEMPLATE_FORM_EDIT));
451         DotHibernate.flush();
452     }
453     
454     public void _copyWebAsset(ActionRequest req, ActionResponse res,
455             PortletConfig config, ActionForm form, User user) throws Exception JavaDoc {
456
457         //wraps request to get session object
458
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
459         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
460
461         Logger.debug(this, "I'm copying the Template");
462
463         //gets the current template being edited from the request object
464
Template currentTemplate = (Template) req
465                 .getAttribute(WebKeys.TEMPLATE_EDIT);
466
467         //Checking permissions
468
_checkCopyAndMovePermissions(currentTemplate, user, httpReq,"copy");
469         
470         //Calling the copy method from the factory
471
TemplateFactory.copyTemplate(currentTemplate);
472         
473         SessionMessages.add(httpReq, "message", "message.template.copy");
474     }
475
476     @SuppressWarnings JavaDoc("unchecked")
477     public void _getVersionBackWebAsset(ActionRequest req, ActionResponse res,
478             PortletConfig config, ActionForm form, User user) throws Exception JavaDoc {
479
480         Template versionTemplate = (Template) InodeFactory.getInode(req
481                 .getParameter("inode_version"), Template.class);
482
483         Identifier id = (Identifier)IdentifierFactory.getParentIdentifier(versionTemplate);
484
485         Template workingTemplate = (Template)IdentifierFactory.getWorkingChildOfClass(id, Template.class);
486
487         //gets html pages children from current template
488
java.util.List JavaDoc<HTMLPage> htmlPageChildren = InodeFactory.getChildrenClass(
489                 workingTemplate, HTMLPage.class);
490
491         //gets containers identifiers children from current template
492
java.util.List JavaDoc<Identifier> containersChildren = (java.util.List JavaDoc<Identifier>)InodeFactory.getChildrenClass(
493                 workingTemplate, Identifier.class);
494         
495         //creates new set for template children
496
java.util.Set JavaDoc<Inode> templateChildrenSet = new java.util.HashSet JavaDoc<Inode>();
497         
498         //adds the html pages as children
499
templateChildrenSet.addAll(htmlPageChildren);
500         templateChildrenSet.addAll(containersChildren);
501         
502         //sets the new set of children
503
//sets the new set of children
504
for(Inode inode : templateChildrenSet ){
505             versionTemplate.addChild(inode);
506         }
507         
508
509
510         Template newWorkingTemplate = (Template) super._getVersionBackWebAsset(req, res, config, form, user, Template.class, WebKeys.TEMPLATE_EDIT);
511         TemplateServices.writeTemplateToFile(newWorkingTemplate, true);
512     }
513     
514 }
Popular Tags