KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > links > action > EditLinkAction


1 package com.dotmarketing.portlets.links.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.beans.Tree;
22 import com.dotmarketing.beans.WebAsset;
23 import com.dotmarketing.cms.factories.PublicCompanyFactory;
24 import com.dotmarketing.db.DotHibernate;
25 import com.dotmarketing.factories.HostFactory;
26 import com.dotmarketing.factories.IdentifierFactory;
27 import com.dotmarketing.factories.InodeFactory;
28 import com.dotmarketing.factories.TreeFactory;
29 import com.dotmarketing.factories.WebAssetFactory;
30 import com.dotmarketing.menubuilders.RefreshMenus;
31 import com.dotmarketing.portal.struts.DotPortletAction;
32 import com.dotmarketing.portal.struts.DotPortletActionInterface;
33 import com.dotmarketing.portlets.categories.model.Category;
34 import com.dotmarketing.portlets.contentlet.model.Contentlet;
35 import com.dotmarketing.portlets.files.model.File;
36 import com.dotmarketing.portlets.folders.model.Folder;
37 import com.dotmarketing.portlets.htmlpages.model.HTMLPage;
38 import com.dotmarketing.portlets.links.factories.LinkFactory;
39 import com.dotmarketing.portlets.links.model.Link;
40 import com.dotmarketing.portlets.links.struts.LinkForm;
41 import com.dotmarketing.services.ContentletMapServices;
42 import com.dotmarketing.services.ContentletServices;
43 import com.dotmarketing.util.Logger;
44 import com.dotmarketing.util.UtilMethods;
45 import com.dotmarketing.util.Validator;
46 import com.dotmarketing.util.WebKeys;
47 import com.liferay.portal.model.User;
48 import com.liferay.portal.util.Constants;
49 import com.liferay.portlet.ActionRequestImpl;
50 import com.liferay.util.servlet.SessionMessages;
51
52 /**
53  * @author if(working==false){ author="Maria Ahues"; }else{ author="Rocco
54  * Maglio";
55  */

56
57 public class EditLinkAction extends DotPortletAction implements
58         DotPortletActionInterface {
59
60     public void processAction(ActionMapping mapping, ActionForm form,
61             PortletConfig config, ActionRequest req, ActionResponse res)
62             throws Exception JavaDoc {
63
64         String JavaDoc cmd = req.getParameter(Constants.CMD);
65         String JavaDoc referer = req.getParameter("referer");
66
67         //wraps request to get session object
68
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
69         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
70
71         if ((referer != null) && (referer.length() != 0)) {
72             referer = URLDecoder.decode(referer, "UTF-8");
73         }
74
75         Logger.debug(this, "EditLinkAction cmd=" + cmd);
76
77         DotHibernate.startTransaction();
78
79         User user = _getUser(req);
80
81         try {
82             Logger.debug(this, "Calling Retrieve method");
83
84             _retrieveWebAsset(req, res, config, form, user, Link.class,
85                     WebKeys.LINK_EDIT);
86
87         } catch (Exception JavaDoc ae) {
88             _handleException(ae, req);
89             return;
90         }
91
92         /*
93          * We are editing the link
94          */

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

135         if ((cmd != null) && cmd.equals(Constants.ADD)) {
136             try {
137
138                 if (Validator.validate(req, form, mapping)) {
139
140                     Logger.debug(this, "Calling Save method");
141                     _saveWebAsset(req, res, config, form, user);
142
143                     String JavaDoc subcmd = req.getParameter("subcmd");
144
145                     if ((subcmd != null)
146                             && subcmd
147                                     .equals(com.dotmarketing.util.Constants.PUBLISH)) {
148                         Logger.debug(this, "Calling Publish method");
149                         _publishWebAsset(req, res, config, form, user, WebKeys.LINK_FORM_EDIT);
150
151                     }
152                     _sendToReferral(req, res, referer);
153                 }
154
155             } catch (Exception JavaDoc ae) {
156                 _handleException(ae, req);
157                 if (ae.getMessage().equals(WebKeys.USER_PERMISSIONS_EXCEPTION)) {
158                     setForward(req, "portlet.ext.links.edit_link");
159                     return;
160                 }
161                 return;
162             }
163
164         }
165         /*
166          * If we are deleteing the link, run the delete action and return to the
167          * list
168          *
169          */

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

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

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

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

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

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

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

289         else if ((cmd != null)
290                 && cmd.equals(com.dotmarketing.util.Constants.COPY)) {
291             try {
292                 Logger.debug(this, "Calling Copy Method");
293                 _copyWebAsset(req, res, config, form, user);
294             } catch (Exception JavaDoc ae) {
295                 _handleException(ae, req);
296             }
297             _sendToReferral(req, res, referer);
298         }
299         /*
300          * If we are moving the link, run the copy action and return to the list
301          */

302         else if ((cmd != null)
303                 && cmd.equals(com.dotmarketing.util.Constants.MOVE)) {
304             try {
305                 Logger.debug(this, "Calling Move Method");
306                 _moveWebAsset(req, res, config, form, user, Link.class,
307                         WebKeys.LINK_EDIT);
308             } catch (Exception JavaDoc ae) {
309                 _handleException(ae, req);
310             }
311             _sendToReferral(req, res, referer);
312         } else
313             Logger.debug(this, "Unspecified Action");
314
315         DotHibernate.commitTransaction();
316         
317         if ((cmd != null) && cmd.equals(Constants.ADD) ) {
318             RefreshMenus.deleteMenus();
319         }
320         
321         setForward(req, "portlet.ext.links.edit_link");
322     }
323
324     ///// ************** ALL METHODS HERE *************************** ////////
325

326     public void _editWebAsset(ActionRequest req, ActionResponse res,
327             PortletConfig config, ActionForm form, User user) throws Exception JavaDoc {
328
329         //calls edit method from super class that returns parent folder
330
Folder parentFolder = super._editWebAsset(req, res, config, form, user,
331                 WebKeys.LINK_EDIT);
332
333         //setting parent folder path and inode on the form bean
334
LinkForm lf = (LinkForm) form;
335         lf.setSelectedparent(parentFolder.getName());
336         lf.setParent(parentFolder.getInode());
337         lf.setSelectedparentPath(parentFolder.getPath());
338
339         //This can't be done on the WebAsset so it needs to be done here.
340
Link link = (Link) req.getAttribute(WebKeys.LINK_EDIT);
341         link.setParent(parentFolder.getInode());
342         if (link.getInode() > 0) {
343             if (link.getInternalLinkIdentifier() > 0) {
344                 Identifier ili = (Identifier) InodeFactory.getInode(String
345                         .valueOf(link.getInternalLinkIdentifier()),
346                         Identifier.class);
347
348                 HTMLPage myHTMLPage = (HTMLPage) IdentifierFactory
349                         .getWorkingChildOfClass(ili, HTMLPage.class);
350                 if (myHTMLPage.getInode() > 0) {
351                     lf.setInternallink(myHTMLPage.getInode());
352
353                 } else {
354                     File myFile = (File) IdentifierFactory
355                             .getWorkingChildOfClass(ili, File.class);
356                     if (myFile.getInode() > 0) {
357                         lf.setInternallink(myFile.getInode());
358                     }
359                 }
360             }
361         } else {
362             lf.setInternal(true);
363             link.setInternal(true);
364         }
365
366         //Permissions variable
367
if (link.getInode() > 0)
368             req.setAttribute(WebKeys.PERMISSION_INODE_EDIT, link);
369         else
370             //if the link is new the parentfolder permissions are used
371
req.setAttribute(WebKeys.PERMISSION_INODE_EDIT, parentFolder);
372
373     }
374
375     public void _saveWebAsset(ActionRequest req, ActionResponse res,
376             PortletConfig config, ActionForm form, User user) throws Exception JavaDoc {
377
378         //wraps request to get session object
379
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
380         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
381
382         //gets the new information for the link from the request object
383
req.setAttribute(WebKeys.LINK_FORM_EDIT,
384                 new com.dotmarketing.portlets.links.model.Link());
385         BeanUtils
386                 .copyProperties(req.getAttribute(WebKeys.LINK_FORM_EDIT), form);
387
388         //gets the new information for the link from the request object
389
Link link = (Link) req.getAttribute(WebKeys.LINK_FORM_EDIT);
390         
391         boolean previousShowMenu = link.isShowOnMenu();
392         
393         //gets the current link being edited from the request object
394
Link currentLink = (Link) req.getAttribute(WebKeys.LINK_EDIT);
395
396         Inode internalLink = (Inode) InodeFactory.getInode(req
397                 .getParameter("internallink"), Inode.class);
398
399
400         //parent folder or inode for this file
401
Folder parent = (Folder) InodeFactory.getInode(req
402                 .getParameter("parent"), Folder.class);
403
404         //Checking permissions
405
_checkPermissions(currentLink, parent, user, httpReq);
406
407         //gets user id from request for mod user
408
String JavaDoc userId = user.getUserId();
409
410         // take care of internal links
411
if (internalLink.getInode() > 0) {
412
413             Identifier internalLinkIdentifier = IdentifierFactory
414                     .getIdentifierByInode(internalLink);
415             link.setInternal(true);
416             link.setInternalLinkIdentifier(internalLinkIdentifier.getInode());
417
418             if (internalLink instanceof HTMLPage) {
419
420                 if (((HTMLPage) internalLink).isHttpsRequired()) {
421                     link.setProtocal("https://");
422                 } else {
423                     link.setProtocal("http://");
424                 }
425
426             } else {
427                 link.setProtocal("http://");
428             }
429
430             StringBuffer JavaDoc myURL = new StringBuffer JavaDoc();
431             if (internalLink instanceof WebAsset) {
432                 Host host = HostFactory.getParentHost((WebAsset) internalLink);
433                 myURL.append(host.getHostname());
434             } else {
435             
436                 myURL.append(PublicCompanyFactory.getDefaultCompany().getPortalURL());
437             }
438             myURL.append(internalLinkIdentifier.getURI());
439             link.setUrl(myURL.toString());
440         }
441
442         Link workingLink = null;
443         //it saves or updates the asset
444
if (currentLink.getInode() > 0) {
445             Identifier identifier = IdentifierFactory
446                     .getIdentifierByInode(currentLink);
447             WebAssetFactory
448                     .createAsset(link, userId, parent, identifier, false);
449
450             workingLink = (Link) WebAssetFactory.saveAsset(link, identifier);
451             currentLink = link;
452             if (!currentLink.getTarget().equals(link.getTarget())) {
453                 //create new identifier, with the URI
454
IdentifierFactory.updateIdentifierURI(workingLink,
455                         (Folder) parent);
456             }
457         } else {
458             WebAssetFactory.createAsset(link, userId, parent);
459             req.setAttribute(WebKeys.LINK_EDIT, link);
460             workingLink = link;
461         }
462
463         // Get parents of the old version so you can update the working
464
// information to this new version.
465

466         
467         List JavaDoc parents = InodeFactory.getParentsOfClass(currentLink, Category.class);
468         parents.addAll(InodeFactory.getParentsOfClass(currentLink, Identifier.class));
469         parents.addAll(InodeFactory.getParentsOfClass(currentLink, Folder.class));
470         parents.addAll(InodeFactory.getParentsOfClass(currentLink, Contentlet.class));
471         
472         
473         List JavaDoc children = InodeFactory.getChildrenClass(currentLink, Category.class);
474         children.addAll(InodeFactory.getChildrenClass(currentLink, Contentlet.class));
475         
476         
477         java.util.Iterator JavaDoc parentsIterator = parents.iterator();
478
479         //update parents to new version delete old versions parents if not
480
// live.
481
while (parentsIterator.hasNext()) {
482             Inode parentInode = (Inode) parentsIterator.next();
483             parentInode.addChild(workingLink);
484
485             //to keep relation types from parent only if it exists
486
Tree tree = com.dotmarketing.factories.TreeFactory.getTree(
487                     parentInode, currentLink);
488             if ((tree.getRelationType() != null)
489                     && (tree.getRelationType().length() != 0)) {
490                 Tree newTree = com.dotmarketing.factories.TreeFactory.getTree(
491                         parentInode, workingLink);
492                 newTree.setRelationType(tree.getRelationType());
493                 newTree.setTreeOrder(0);
494                 TreeFactory.saveTree(newTree);
495             }
496
497             //Republishing parents working contentlets
498
if (parentInode instanceof Contentlet) {
499                 Contentlet cont = (Contentlet) parentInode;
500
501                 if (cont.isWorking()) {
502                     //calls the asset factory edit
503
ContentletServices.writeContentletToFile(cont, true);
504                     ContentletMapServices.writeContentletMapToFile(cont, true);
505                 }
506             }
507
508             //checks type of parent and deletes child if not live version.
509
if (!currentLink.isLive()) {
510                 if (!(parentInode instanceof Identifier)) {
511                     parentInode.deleteChild(currentLink);
512                 }
513             }
514         }
515
516         //Saving page permissions
517
_applyPermissions(req, WebKeys.LINK_FORM_EDIT);
518
519         if (req.getParameter("popup") != null) {
520             req.setAttribute("inode", String.valueOf(workingLink.getInode()));
521         }
522
523         //Refreshing the menues
524
/*if (previousShowMenu != link.isShowOnMenu()) {
525             //existing folder with different show on menu ... need to regenerate menu
526             RefreshMenus.deleteMenus();
527         }*/

528         
529         SessionMessages.add(req, "message", "message.link.save");
530     }
531
532     public void _copyWebAsset(ActionRequest req, ActionResponse res,
533             PortletConfig config, ActionForm form, User user) throws Exception JavaDoc {
534
535         //wraps request to get session object
536
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
537         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
538
539         //gets the current template being edited from the request object
540
Link currentLink = (Link) req.getAttribute(WebKeys.LINK_EDIT);
541
542         //gets folder parent
543
String JavaDoc parentInode = req.getParameter("parent");
544         Folder parent = null;
545         if (parentInode != null && parentInode.length() != 0
546                 && !parentInode.equals("0")) {
547             //the parent is being passed through the request
548
parent = (Folder) InodeFactory.getInode(parentInode, Folder.class);
549             Logger.debug(this, "Parent Folder=" + parent.getInode());
550         } else {
551             parent = (Folder) InodeFactory.getParentOfClass(currentLink,
552                     Folder.class);
553             Logger.debug(this, "Parent Folder=" + parent.getInode());
554         }
555
556         //Checking permissions
557
_checkCopyAndMovePermissions(currentLink, parent, user, httpReq, "copy");
558
559         LinkFactory.copyLink(currentLink, parent);
560         
561         SessionMessages.add(req, "message", "message.link.copy");
562     }
563
564     public void _getVersionBackWebAsset(ActionRequest req, ActionResponse res,
565             PortletConfig config, ActionForm form, User user) throws Exception JavaDoc {
566
567         Link workingLink = (Link) super._getVersionBackWebAsset(req, res,
568                 config, form, user, Link.class, WebKeys.LINK_EDIT);
569         Link linkVersion = (Link) req.getAttribute(WebKeys.LINK_EDIT);
570
571         // Get parents of the old version so you can update the working
572
// information to this new version.
573
List JavaDoc parents = InodeFactory.getParentsOfClass(linkVersion, Category.class);
574         parents.addAll(InodeFactory.getParentsOfClass(linkVersion, Identifier.class));
575         parents.addAll(InodeFactory.getParentsOfClass(linkVersion, Folder.class));
576         parents.addAll(InodeFactory.getParentsOfClass(linkVersion, Contentlet.class));
577         parents.addAll(InodeFactory.getParentsOfClass(linkVersion, Host.class));
578         
579         
580         java.util.Iterator JavaDoc parentsIterator = parents.iterator();
581
582         //update parents to new version delete old versions parents if not
583
// live.
584
while (parentsIterator.hasNext()) {
585             Inode parentInode = (Inode) parentsIterator.next();
586             if(parentInode.getInode() ==0){
587                 continue;
588             }
589             parentInode.addChild(workingLink);
590
591             //to keep relation types from parent only if it exists
592
Tree tree = com.dotmarketing.factories.TreeFactory.getTree(
593                     parentInode, linkVersion);
594             if ((tree.getRelationType() != null)
595                     && (tree.getRelationType().length() != 0)) {
596                 Tree newTree = com.dotmarketing.factories.TreeFactory.getTree(
597                         parentInode, workingLink);
598                 newTree.setRelationType(tree.getRelationType());
599                 newTree.setTreeOrder(0);
600                 TreeFactory.saveTree(newTree);
601             }
602
603             // checks type of parent and deletes child if not live version.
604
if (!linkVersion.isLive()) {
605                 if (!(parentInode instanceof Identifier)) {
606                     parentInode.deleteChild(linkVersion);
607                 }
608             }
609         }
610
611         //Rewriting the parentīs contentlets of the link
612
List JavaDoc contentlets = InodeFactory.getParentsOfClass(workingLink,
613                 Contentlet.class);
614         Iterator JavaDoc it = contentlets.iterator();
615         while (it.hasNext()) {
616             Contentlet cont = (Contentlet) it.next();
617             if (cont.isWorking()) {
618                 ContentletServices.writeContentletToFile(cont, true);
619                 //writes the contentlet object to a file
620
ContentletMapServices.writeContentletMapToFile(cont, true);
621             }
622         }
623
624     }
625
626 }
Popular Tags