KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > controllers > kernel > impl > simple > EditOnSiteBasicTemplateController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.controllers.kernel.impl.simple;
25
26 import java.io.File JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.infoglue.cms.entities.content.ContentVO;
34 import org.infoglue.cms.exception.SystemException;
35 import org.infoglue.cms.io.FileHelper;
36 import org.infoglue.cms.security.InfoGluePrincipal;
37 import org.infoglue.cms.util.CmsPropertyHandler;
38 import org.infoglue.deliver.applications.databeans.DatabaseWrapper;
39 import org.infoglue.deliver.applications.databeans.WebPage;
40
41 /**
42  * This is the most basic template controller supplying the templates using it with
43  * methods to fetch contents, structure and other suff needed for a site. Mostly this class just acts as a
44  * delegator to other more specialized classes.
45  */

46
47 public class EditOnSiteBasicTemplateController extends BasicTemplateController
48 {
49     private final static Logger logger = Logger.getLogger(EditOnSiteBasicTemplateController.class.getName());
50
51     public EditOnSiteBasicTemplateController(DatabaseWrapper databaseWrapper, InfoGluePrincipal infoGluePrincipal)
52     {
53         super(databaseWrapper, infoGluePrincipal);
54     }
55     
56     /**
57      * This method adds the neccessairy html to a output for it to be editable.
58      */

59     
60     private String JavaDoc decorateTag(Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc attributeName, String JavaDoc attributeValue)
61     {
62         if(attributeValue != null && !attributeValue.trim().equals(""))
63         {
64             String JavaDoc editOnSiteUrl = CmsPropertyHandler.getEditOnSiteUrl();
65             StringBuffer JavaDoc requestDelim = new StringBuffer JavaDoc( CmsPropertyHandler.getRequestArgumentDelimiter() );
66             StringBuffer JavaDoc decoratedAttributeValue = new StringBuffer JavaDoc();
67             decoratedAttributeValue.append("<span oncontextmenu=\"setContentItemParameters(" );
68             decoratedAttributeValue.append( contentId ).append( "," ).append( languageId );
69             decoratedAttributeValue.append( ",'").append( attributeName ).append( "'); setEditUrl('");
70             decoratedAttributeValue.append( editOnSiteUrl ).append( "?contentId=" ).append( contentId );
71             decoratedAttributeValue.append( requestDelim).append("languageId=").append( languageId );
72             decoratedAttributeValue.append( requestDelim).append("attributeName=" ).append( attributeName );
73             decoratedAttributeValue.append( requestDelim ).append( "forceWorkingChange=true');\">" );
74             decoratedAttributeValue.append( attributeValue + "</span>");
75             return decoratedAttributeValue.toString();
76         }
77         else
78         {
79             return "";
80         }
81     }
82     
83     /**
84      * This method adds the neccessairy html to a template to make it right-clickable.
85      */

86  
87     public String JavaDoc decoratePage(String JavaDoc page)
88     {
89         String JavaDoc decoratedTemplate = page;
90         
91         try
92         {
93             String JavaDoc extraHeader = FileHelper.getFileAsString(new File JavaDoc(CmsPropertyHandler.getContextRootPath() + "preview/editOnSiteHeader.vm"));
94             String JavaDoc extraBody = FileHelper.getFileAsString(new File JavaDoc(CmsPropertyHandler.getContextRootPath() + "preview/editOnSiteBody.vm"));
95             
96             String JavaDoc servletContext = request.getContextPath();
97             extraHeader = extraHeader.replaceAll("\\{applicationContext\\}", servletContext);
98             //logger.info("extraHeader:" + extraHeader);
99

100             StringBuffer JavaDoc modifiedTemplate = new StringBuffer JavaDoc(page);
101             
102             //Adding stuff in the header
103
int indexOfHeadEndTag = modifiedTemplate.indexOf("</head");
104             if(indexOfHeadEndTag == -1)
105                 indexOfHeadEndTag = modifiedTemplate.indexOf("</HEAD");
106             
107             if(indexOfHeadEndTag > -1)
108             {
109                 modifiedTemplate = modifiedTemplate.replace(indexOfHeadEndTag, modifiedTemplate.indexOf(">", indexOfHeadEndTag) + 1, extraHeader);
110             }
111             else
112             {
113                 int indexOfHTMLStartTag = modifiedTemplate.indexOf("<html");
114                 if(indexOfHTMLStartTag == -1)
115                     indexOfHTMLStartTag = modifiedTemplate.indexOf("<HTML");
116         
117                 if(indexOfHTMLStartTag > -1)
118                 {
119                     modifiedTemplate = modifiedTemplate.insert(modifiedTemplate.indexOf(">", indexOfHTMLStartTag) + 1, "<head>" + extraHeader);
120                 }
121                 else
122                 {
123                     logger.info("The current template is not a valid document. It does not comply with the simplest standards such as having a correct header.");
124                 }
125             }
126
127             //Adding stuff in the body
128
int indexOfBodyStartTag = modifiedTemplate.indexOf("<body");
129             if(indexOfBodyStartTag == -1)
130                 indexOfBodyStartTag = modifiedTemplate.indexOf("<BODY");
131                 
132             if(indexOfBodyStartTag > -1)
133             {
134                 modifiedTemplate = modifiedTemplate.insert(modifiedTemplate.indexOf(">", indexOfBodyStartTag) + 1, extraBody);
135             }
136             else
137             {
138                 logger.info("The current template is not a valid document. It does not comply with the simplest standards such as having a correct body.");
139             }
140             
141             decoratedTemplate = modifiedTemplate.toString();
142         }
143         catch(Exception JavaDoc e)
144         {
145             logger.warn("An error occurred when deliver tried to decorate your template to enable onSiteEditing. Reason " + e.getMessage(), e);
146         }
147         
148         return decoratedTemplate;
149     }
150     
151         
152     /**
153      * This method deliveres a String with the content-attribute asked for if it exists in the content
154      * defined in the url-parameter contentId. It decorates the attibute with html so the attribute can be clicked on for
155      * editing.
156      */

157      
158     public String JavaDoc getContentAttribute(String JavaDoc attributeName)
159     {
160         return decorateTag(this.getContentId(), this.getLanguageId(), attributeName, super.getContentAttribute(attributeName));
161     }
162
163     /**
164      * This method deliveres a String with the content-attribute asked for.
165      * As the siteNode can have multiple bindings as well as a content as a parameter this
166      * parameter requires a bindingName which refers to the AvailableServiceBinding.name-attribute.
167      */

168      
169     public String JavaDoc getContentAttribute(String JavaDoc contentBindningName, String JavaDoc attributeName)
170     {
171         return decorateTag(this.getContentId(), this.getLanguageId(), attributeName, super.getContentAttribute(contentBindningName, attributeName));
172         /*
173         String attributeValue = "";
174         
175         try
176         {
177             ContentVO contentVO = this.nodeDeliveryController.getBoundContent(this.getDatabase(), this.getPrincipal(), this.getSiteNodeId(), this.languageId, USE_LANGUAGE_FALLBACK, contentBindningName, this.deliveryContext);
178             if(contentVO != null)
179             {
180                 attributeValue = ContentDeliveryController.getContentDeliveryController().getContentAttribute(this.getDatabase(), contentVO.getContentId(), this.languageId, attributeName, this.siteNodeId, USE_LANGUAGE_FALLBACK, this.deliveryContext, this.infoGluePrincipal);
181                 attributeValue = decorateTag(contentVO.getContentId(), this.getLanguageId(), attributeName, attributeValue);
182             }
183         }
184         catch(Exception e)
185         {
186             logger.error("An error occurred trying to get attributeName=" + attributeName + " on contentBindning " + contentBindningName + ":" + e.getMessage(), e);
187         }
188                 
189         return attributeValue;
190         */

191     }
192
193
194     /**
195      * This method is just a dummy method used to ensure that we can ensure to not get a decorated attribute
196      * value if OnSiteEdit is on.
197      */

198     public String JavaDoc getContentAttribute(String JavaDoc contentBindningName, String JavaDoc attributeName, boolean clean)
199     {
200        return super.getContentAttribute(contentBindningName, attributeName);
201     }
202
203     /**
204      * This method is just a dummy method used to ensure that we can ensure to not get a decorated attribute
205      * value if OnSiteEdit is on.
206      */

207      
208     public String JavaDoc getContentAttribute(String JavaDoc attributeName, boolean clean)
209     {
210         return super.getContentAttribute(attributeName);
211     }
212
213  
214     /**
215      * This method deliveres a String with the content-attribute asked for a
216      * specific content and ensure not to get decorated attributes if EditOnSite is
217      * turned on.
218      *
219      * @param contentId
220      * the contentId of a content
221      * @param attributeName
222      * the attribute name in the content. (ie. Title, Leadin etc)
223      * @param clean
224      * true if the content should be decorated in the editonsite
225      * working mode. No decoration is made if content-attribute is
226      * empty.
227      * @return the contentAttribute or empty string if none found.
228      */

229     public String JavaDoc getContentAttribute(Integer JavaDoc contentId, String JavaDoc attributeName, boolean clean)
230     {
231         String JavaDoc attributeValue = super.getContentAttribute(contentId, attributeName);
232     
233         if( clean == false )
234         {
235             attributeValue = this.decorateTag( contentId, this.languageId, attributeName, attributeValue );
236         }
237         return attributeValue;
238     }
239
240     /**
241      * This method is just a dummy method used to ensure that we can ensure to not get a decorated attribute
242      * value if OnSiteEdit is on.
243      */

244  
245     public String JavaDoc getContentAttributeValue(Integer JavaDoc contentId, String JavaDoc attributeName, boolean clean, boolean escapeHTML)
246     {
247         String JavaDoc attributeValue = "";
248
249         if(clean)
250             attributeValue = super.getContentAttributeValue(contentId, this.getLanguageId(), attributeName, escapeHTML);
251         else
252             return decorateTag(this.getContentId(), this.getLanguageId(), attributeName, super.getContentAttributeValue(contentId, this.getLanguageId(), attributeName, escapeHTML));
253
254         return attributeValue;
255     }
256
257     /**
258      * This method is just a dummy method used to ensure that we can ensure to not get a decorated attribute
259      * value if OnSiteEdit is on.
260      */

261  
262     public String JavaDoc getContentAttribute(Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc attributeName, boolean clean)
263     {
264         //return super.getContentAttribute(contentId, languageId, attributeName);
265
String JavaDoc attributeValue = "";
266         
267         if(clean)
268         {
269             attributeValue = super.getContentAttribute(contentId, languageId, attributeName);
270         }
271         else
272         {
273             attributeValue = super.getContentAttribute(contentId, languageId, attributeName);
274             attributeValue = decorateTag(contentId, languageId, attributeName, attributeValue);
275         }
276         
277         return attributeValue;
278     }
279     
280     /**
281      * This method deliveres a String with the content-attribute asked for.
282      * As the siteNode can have multiple bindings as well as a content as a parameter this
283      * parameter requires a bindingName which refers to the AvailableServiceBinding.name-attribute.
284      */

285      
286     public String JavaDoc getContentAttribute(Integer JavaDoc contentId, String JavaDoc attributeName)
287     {
288         if(attributeName.equalsIgnoreCase(this.getTemplateAttributeName()))
289             return super.getContentAttribute(contentId, attributeName);
290             //return decorateTemplate(super.getContentAttribute(contentId, attributeName));
291
else
292             return decorateTag(contentId, this.getLanguageId(), attributeName, super.getContentAttribute(contentId, attributeName));
293     }
294
295
296
297     /**
298      * This method deliveres a String with the content-attribute asked for after it has been parsed and all special tags have been converted.
299      * As the siteNode can have multiple bindings as well as a content as a parameter this
300      * parameter requires a bindingName which refers to the AvailableServiceBinding.name-attribute.
301      */

302      
303     public String JavaDoc getParsedContentAttribute(String JavaDoc attributeName)
304     {
305         return decorateTag(this.getContentId(), this.getLanguageId(), attributeName, super.getParsedContentAttribute(attributeName));
306     }
307
308
309     /**
310      * This method deliveres a String with the content-attribute asked for after it has been parsed and all special tags have been converted.
311      * As the siteNode can have multiple bindings as well as a content as a parameter this
312      * parameter requires a bindingName which refers to the AvailableServiceBinding.name-attribute.
313      */

314      
315     public String JavaDoc getParsedContentAttribute(String JavaDoc contentBindningName, String JavaDoc attributeName)
316     {
317         String JavaDoc attributeValue = "";
318         
319         try
320         {
321             ContentVO contentVO = this.nodeDeliveryController.getBoundContent(this.getDatabase(), this.getPrincipal(), this.siteNodeId, this.languageId, USE_LANGUAGE_FALLBACK, contentBindningName, this.deliveryContext);
322             if(contentVO != null)
323             {
324                 attributeValue = getParsedContentAttribute(contentVO.getContentId(), attributeName);
325                 attributeValue = decorateTag(contentVO.getContentId(), this.getLanguageId(), attributeName, attributeValue);
326             }
327         }
328         catch(Exception JavaDoc e)
329         {
330             logger.error("An error occurred trying to get attributeName=" + attributeName + " on contentBindning " + contentBindningName + ":" + e.getMessage(), e);
331         }
332                 
333         return attributeValue;
334     }
335
336     /**
337      * This method is just a dummy method used to ensure that we can ensure to not get a decorated attribute
338      * value if OnSiteEdit is on.
339      */

340      
341     public String JavaDoc getParsedContentAttribute(String JavaDoc attributeName, boolean clean)
342     {
343         //return super.getParsedContentAttribute(attributeName);
344
String JavaDoc attributeValue = "";
345         
346         if(clean)
347         {
348             attributeValue = super.getParsedContentAttribute(attributeName);
349         }
350         else
351         {
352             attributeValue = super.getParsedContentAttribute(attributeName);
353             attributeValue = decorateTag(contentId, this.getLanguageId(), attributeName, attributeValue);
354         }
355         
356         return attributeValue;
357     }
358     
359     /**
360      * This method is just a dummy method used to ensure that we can ensure to not get a decorated attribute
361      * value if OnSiteEdit is on.
362      */

363      
364     public String JavaDoc getParsedContentAttribute(String JavaDoc contentBindningName, String JavaDoc attributeName, boolean clean)
365     {
366         //return super.getParsedContentAttribute(contentBindningName, attributeName);
367

368         String JavaDoc attributeValue = "";
369         
370         if(clean)
371         {
372             attributeValue = super.getParsedContentAttribute(contentBindningName, attributeName);
373         }
374         else
375         {
376             attributeValue = super.getParsedContentAttribute(contentBindningName, attributeName);
377             attributeValue = decorateTag(contentId, this.getLanguageId(), attributeName, attributeValue);
378         }
379         
380         return attributeValue;
381     }
382
383     /**
384      * This method is just a dummy method used to ensure that we can ensure to not get a decorated attribute
385      * value if OnSiteEdit is on.
386      */

387      
388     public String JavaDoc getParsedContentAttribute(Integer JavaDoc contentId, String JavaDoc attributeName, boolean clean)
389     {
390         //return super.getParsedContentAttribute(contentId, attributeName);
391

392         String JavaDoc attributeValue = "";
393         
394         if(clean)
395         {
396             attributeValue = super.getParsedContentAttribute(contentId, attributeName);
397         }
398         else
399         {
400             attributeValue = super.getParsedContentAttribute(contentId, attributeName);
401             attributeValue = decorateTag(contentId, this.getLanguageId(), attributeName, attributeValue);
402         }
403         
404         return attributeValue;
405         
406     }
407
408     /**
409      * This method is just a dummy method used to ensure that we can ensure to not get a decorated attribute
410      * value if OnSiteEdit is on.
411      */

412      
413     public String JavaDoc getParsedContentAttribute(Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc attributeName, boolean clean)
414     {
415         //return super.getParsedContentAttribute(contentId, attributeName);
416

417         String JavaDoc attributeValue = "";
418         
419         if(clean)
420         {
421             attributeValue = super.getParsedContentAttribute(contentId, languageId, attributeName);
422         }
423         else
424         {
425             attributeValue = super.getParsedContentAttribute(contentId, languageId, attributeName);
426             attributeValue = decorateTag(contentId, languageId, attributeName, attributeValue);
427         }
428         
429         return attributeValue;
430         
431     }
432
433     /**
434      * This method deliveres a String with the content-attribute asked for after it has been parsed and all special tags have been converted.
435      * The attribute is fetched from the specified content.
436      */

437      
438     public String JavaDoc getParsedContentAttribute(Integer JavaDoc contentId, String JavaDoc attributeName)
439     {
440         return decorateTag(contentId, this.getLanguageId(), attributeName, super.getParsedContentAttribute(contentId, attributeName));
441     }
442
443
444     
445     /**
446      * The method returns a list of WebPage-objects that is the bound sitenodes of named binding.
447      * The method is great for navigation-purposes on any site.
448      */

449
450     public List JavaDoc getBoundPages(String JavaDoc structureBindningName)
451     {
452         List JavaDoc boundPages = super.getBoundPages(structureBindningName);
453         Iterator JavaDoc i = boundPages.iterator();
454         while(i.hasNext())
455         {
456             WebPage webPage = (WebPage)i.next();
457             Integer JavaDoc contentId = super.getContentId(webPage.getSiteNodeId(), META_INFO_BINDING_NAME);
458             String JavaDoc navigationTitle = decorateTag(contentId, this.getLanguageId(), NAV_TITLE_ATTRIBUTE_NAME, webPage.getNavigationTitle());
459             webPage.setNavigationTitle(navigationTitle);
460         }
461         
462         return boundPages;
463     }
464     
465
466     /**
467      * The method returns a list of WebPage-objects that is the bound sitenodes of named binding.
468      * The method is great for navigation-purposes on any site.
469      */

470
471     public List JavaDoc getBoundPages(Integer JavaDoc siteNodeId, String JavaDoc structureBindningName)
472     {
473         List JavaDoc boundPages = super.getBoundPages(siteNodeId, structureBindningName);
474         Iterator JavaDoc i = boundPages.iterator();
475         while(i.hasNext())
476         {
477             WebPage webPage = (WebPage)i.next();
478             Integer JavaDoc contentId = super.getContentId(webPage.getSiteNodeId(), META_INFO_BINDING_NAME);
479             String JavaDoc navigationTitle = decorateTag(contentId, this.getLanguageId(), NAV_TITLE_ATTRIBUTE_NAME, webPage.getNavigationTitle());
480             webPage.setNavigationTitle(navigationTitle);
481         }
482         
483         return boundPages;
484     }
485
486     /**
487      * This method deliveres a String with the Navigation title the page asked for has.
488      * As the siteNode can have multiple bindings the method requires a bindingName
489      * which refers to the AvailableServiceBinding.name-attribute. The navigation-title is fetched
490      * from the meta-info-content bound to the site node.
491      */

492      
493     public String JavaDoc getPageNavTitle(String JavaDoc structureBindningName)
494     {
495         Integer JavaDoc siteNodeId = super.getSiteNodeId(structureBindningName);
496         Integer JavaDoc contentId = super.getContentId(siteNodeId, META_INFO_BINDING_NAME);
497         String JavaDoc navTitle = decorateTag(contentId, this.getLanguageId(), NAV_TITLE_ATTRIBUTE_NAME, super.getPageNavTitle(structureBindningName));
498                         
499         return navTitle;
500     }
501     
502     
503     /**
504      * This method deliveres a String with the URL to the digital asset asked for.
505      * As the siteNode can have multiple bindings as well as a content as a parameter this
506      * parameter requires a bindingName which refers to the AvailableServiceBinding.name-attribute.
507      */

508 /*
509     public String getAssetUrl(String contentBindningName)
510     {
511         return decorateTag(this.getContentId(), this.getLanguageId(), "", super.getAssetUrl(contentBindningName));
512     }
513 */

514     /**
515      * This method deliveres a String with the URL to the digital asset asked for.
516      */

517     /*
518      
519     public String getAssetUrl(Integer contentId)
520     {
521         return decorateTag(this.getContentId(), this.getLanguageId(), "", super.getAssetUrl(contentId));
522     }
523 */

524
525     /**
526      * This method deliveres a String with the URL to the digital asset asked for.
527      */

528     /*
529      
530     public String getAssetUrl(Integer contentId, String assetKey)
531     {
532         return decorateTag(this.getContentId(), this.getLanguageId(), assetKey, super.getAssetUrl(contentId, assetKey));
533     }
534 */

535
536     /**
537      * This method deliveres a String with the URL to the digital asset asked for.
538      * As the siteNode can have multiple bindings as well as a content as a parameter this
539      * parameter requires a bindingName which refers to the AvailableServiceBinding.name-attribute.
540      */

541     /*
542      
543     public String getAssetUrl(String contentBindningName, int index)
544     {
545         return decorateTag(this.getContentId(), this.getLanguageId(), "", super.getAssetUrl(contentBindningName, index));
546     }
547 */

548
549     /**
550      * This method deliveres a String with the URL to the digital asset asked for.
551      * As the siteNode can have multiple bindings as well as a content as a parameter this
552      * parameter requires a bindingName which refers to the AvailableServiceBinding.name-attribute.
553      */

554     /*
555      
556     public String getAssetUrl(String contentBindningName, String assetKey)
557     {
558         return decorateTag(this.getContentId(), this.getLanguageId(), assetKey, super.getAssetUrl(contentBindningName, assetKey));
559     }
560 */

561     
562     /**
563      * This method should be much more sophisticated later and include a check to see if there is a
564      * digital asset uploaded which is more specialized and can be used to act as serverside logic to the template.
565      */

566     
567     public TemplateController getTemplateController(Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
568     {
569         return getTemplateController(siteNodeId, languageId, contentId, this.request, infoGluePrincipal);
570     }
571     
572     public TemplateController getTemplateController(Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId, HttpServletRequest JavaDoc request, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
573     {
574         TemplateController templateController = null;
575         templateController = new EditOnSiteBasicTemplateController(this.databaseWrapper, infoGluePrincipal);
576         templateController.setStandardRequestParameters(siteNodeId, languageId, contentId);
577         templateController.setHttpRequest(request);
578         templateController.setBrowserBean(this.browserBean);
579         templateController.setDeliveryControllers(this.nodeDeliveryController, null, this.integrationDeliveryController);
580         return templateController;
581     }
582     
583
584 }
Popular Tags