KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > blogs > actions > AbstractAction


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution Sàrl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * ----- END LICENSE BLOCK -----
36  */

37
38 package org.jahia.blogs.actions;
39 import org.jahia.blogs.ServletResources;
40
41 import org.jahia.blogs.model.PostInfo;
42 import org.jahia.blogs.model.MetaPostInfo;
43
44 import org.jahia.bin.Jahia;
45
46 import org.jahia.content.ContentContainerListsXRefManager;
47 import org.jahia.content.ContentContainerKey;
48 import org.jahia.content.ContentContainerListKey;
49 import org.jahia.content.ContentObject;
50
51 import org.jahia.services.fields.ContentField;
52
53 import org.jahia.services.usermanager.JahiaSiteUserManagerService;
54 import org.jahia.services.usermanager.JahiaUser;
55
56 import org.jahia.data.fields.LoadFlags;
57 import org.jahia.data.fields.JahiaField;
58
59 import org.jahia.data.containers.JahiaContainerList;
60 import org.jahia.data.containers.JahiaContainer;
61
62 import org.jahia.services.version.ActivationTestResults;
63 import org.jahia.services.version.EntryLoadRequest;
64 import org.jahia.services.version.JahiaSaveVersion;
65 import org.jahia.services.version.StateModificationContext;
66
67 import org.jahia.services.pages.ContentPage;
68 import org.jahia.services.pages.JahiaPage;
69
70 import org.jahia.services.containers.JahiaContainersService;
71
72 import org.jahia.services.cache.HtmlCache;
73 import org.jahia.services.cache.CacheFactory;
74
75 import org.jahia.services.categories.Category;
76
77 import org.jahia.registries.ServicesRegistry;
78
79 import org.jahia.params.ParamBean;
80
81 import org.jahia.exceptions.JahiaException;
82
83 import org.apache.log4j.Logger;
84
85 import java.util.Set JavaDoc;
86 import java.util.HashSet JavaDoc;
87 import java.util.Iterator JavaDoc;
88 import java.util.Hashtable JavaDoc;
89 import java.util.Date JavaDoc;
90 import java.util.Enumeration JavaDoc;
91 import java.util.Vector JavaDoc;
92
93 /**
94  * Base Action implementing common methods and setting common resources. All
95  * Concrete Actions should subclass it and implement the execute method.
96  *
97  * @author Xavier Lawrence
98  */

99 public abstract class AbstractAction {
100     
101     protected String JavaDoc appKey;
102     protected transient String JavaDoc userName;
103     protected transient String JavaDoc password;
104     
105     protected ServicesRegistry servicesRegistry;
106     protected JahiaContainersService containerService;
107     protected ParamBean jParams;
108     
109     protected BlogDefinitionNames containerNames;
110     
111     // log4j logger
112
static Logger log = Logger.getLogger(AbstractAction.class);
113     
114     /**
115      * Execute the method in Jahia.
116      * @return The return value depends on the method specification. See
117      * Blogger API spec and MetaWeblog API spec.
118      *
119      * @throws JahiaException If something goes wrong
120      */

121     public abstract Object JavaDoc execute() throws JahiaException;
122     
123     /**
124      * Initializes common resources needed to execute the Action
125      *
126      * @throws JahiaException If something goes wrong
127      */

128     protected void init() throws JahiaException {
129         // Get ServicesRegistry Singleton object to retreive services
130
servicesRegistry = ServicesRegistry.getInstance();
131         
132         // ContainerService to get the content
133
containerService = servicesRegistry.getJahiaContainersService();
134         
135         // Create the ParamBean
136
jParams = new ParamBean(
137                 ServletResources.getCurrentRequest(),
138                 ServletResources.getCurrentResponse(),
139                 ServletResources.getCurrentConfig().getServletContext(),
140                 Jahia.getSettings(),
141                 System.currentTimeMillis(),
142                 ParamBean.GET_METHOD);
143         
144         // Load the container definition names from the Porpeties file
145
containerNames = new BlogDefinitionNames(jParams);
146         log.debug("Init sucessfull");
147     }
148     
149     /**
150      * Checks the login information of the user sending the blog request.
151      *
152      * @return The autenticated JahiaUser
153      * @throws JahiaException If the user provided wrong login info
154      */

155     protected JahiaUser checkLogin() throws JahiaException {
156         
157         // UserManagerService to authenticate the user
158
final JahiaSiteUserManagerService userManagerService = servicesRegistry.
159                 getJahiaSiteUserManagerService();
160         
161         // Check if the user has site access
162
final JahiaUser theUser = userManagerService.getMember(
163                 jParams.getSiteID(), userName);
164         if (theUser != null) {
165             if (!theUser.verifyPassword(password)) {
166                 JahiaException je = new JahiaException("Login error",
167                         "User " + userName + " entered bad password",
168                         JahiaException.SECURITY_ERROR,
169                         JahiaException.WARNING_SEVERITY);
170                 log.error("Couldn't validate password for user " +
171                         theUser.getUserKey() + "!", je);
172                 throw je;
173                 
174             }
175             jParams.purgeSession();
176             jParams.setUser(theUser);
177             
178         } else {
179             throw new JahiaException("Login error",
180                     "Login error: Unknown User "+userName,
181                     JahiaException.SECURITY_ERROR,
182                     JahiaException.WARNING_SEVERITY);
183         }
184         
185         log.debug("Login sucessfull");
186         return theUser;
187     }
188     
189     /**
190      * Loads a specific container, checks its field structure and sets its
191      * language. Basically used when editting or creating the content of a
192      * container is needed.
193      * @param id The Container ID
194      * @param languageCode The Language Code to set the loaded container
195      *
196      * @return The loaded JahiaContainer
197      * @throws JahiaException If the container does not exist
198      */

199     protected JahiaContainer getContainer(int id, String JavaDoc languageCode)
200     throws JahiaException {
201         JahiaContainer postContainer = containerService.loadContainer(
202                 id, LoadFlags.ALL, jParams, EntryLoadRequest.STAGED);
203         
204         if (postContainer == null) {
205             throw new JahiaException("Post: "+id+
206                     " does not exist", "Container: "+id+ " does not exist",
207                     JahiaException.ENTRY_NOT_FOUND,
208                     JahiaException.WARNING_SEVERITY);
209         }
210         
211         if (languageCode != null) {
212             postContainer.setLanguageCode(languageCode);
213             postContainer.fieldsStructureCheck(jParams);
214         }
215         return postContainer;
216     }
217     
218     /**
219      * Loads a specific container without any field structure check. Basically
220      * used to read the content of a container without making changes to it.
221      * @param id The Container ID
222      *
223      * @return The loaded JahiaContainer or null if it does not exist
224      * @throws JahiaException If something goes wrong
225      */

226     protected JahiaContainer getContainer(int id)
227     throws JahiaException {
228         EntryLoadRequest elr = new EntryLoadRequest(EntryLoadRequest.
229                 STAGING_WORKFLOW_STATE,
230                 0,
231                 jParams.getEntryLoadRequest().getLocales());
232         
233         jParams.setSubstituteEntryLoadRequest(elr);
234         JahiaContainer postContainer = containerService.loadContainer(
235                 id, LoadFlags.ALL, jParams, elr);
236         jParams.resetSubstituteEntryLoadRequest();
237         return postContainer;
238     }
239     
240     /**
241      * Flushes the cache of a Jahia page.
242      * @param theContainer The Container of the page to be flushed
243      *
244      * @throws JahiaException If something goes wrong
245      */

246     protected void flushPageCacheThatDisplayContainer(JahiaContainer
247             theContainer) throws JahiaException {
248         
249         log.debug("Flushing cache...");
250         
251         EntryLoadRequest loadVersion = EntryLoadRequest.CURRENT;
252         if (servicesRegistry.getJahiaVersionService().
253                 isStagingEnabled(theContainer.getJahiaID())) {
254             loadVersion = EntryLoadRequest.STAGED;
255         }
256         
257         JahiaContainerList theList = containerService.
258                 loadContainerListInfo(theContainer.
259                 getListID(), loadVersion);
260         
261         // Get the cache instance and invalidate the related page entries
262
HtmlCache htmlCache = CacheFactory.getHtmlCache();
263         if (htmlCache == null)
264             log.warn("Could not get the HTML cache instance!!");
265         
266         // since we have made modifications concerning this page, let's flush
267
// the content cache for all the users and browsers as well as all
268
// pages that display this containerList...
269
if (theList != null) {
270             Set JavaDoc containerPageRefs = ContentContainerListsXRefManager.
271                     getInstance().
272                     getAbsoluteContainerListPageIDs(
273                     theList.getID());
274             if (containerPageRefs != null) {
275                 Iterator JavaDoc pageRefIDs = containerPageRefs.iterator();
276                 while (pageRefIDs.hasNext()) {
277                     Integer JavaDoc curPageID = (Integer JavaDoc) pageRefIDs.next();
278                     
279                     if (htmlCache != null)
280                         htmlCache.invalidatePageEntries(curPageID.toString(),
281                                 jParams.getEntryLoadRequest().getWorkflowState());
282                 }
283             } else {
284                 log.debug("Why is cross ref list empty ?");
285             }
286         } else {
287             log.debug("Couldn't retrieve parent containerList, why is that ?");
288         }
289         
290         // since we have made modifications concerning this page, let's flush
291
// the content cache for all the users and browsers...
292
if (htmlCache != null) {
293             htmlCache.invalidatePageEntries(Integer.toString(
294                     jParams.getPageID()).toString(),
295                     jParams.getEntryLoadRequest().getWorkflowState());
296         }
297     }
298     
299     /**
300      * Changes the page of the ParamBean Object
301      * @param pageID The ID of the new page
302      *
303      * @return The new content page object
304      * @throws JahiaException If the new page does not exist
305      */

306     protected ContentPage changePage(int pageID) throws JahiaException {
307         ContentPage blogContentPage = ContentPage.getPage(pageID);
308         
309         if (blogContentPage == null) {
310             throw new JahiaException(
311                     "Blog: "+pageID+ " does not exist",
312                     "Page: "+pageID+ " does not exist",
313                     JahiaException.ENTRY_NOT_FOUND,
314                     JahiaException.WARNING_SEVERITY);
315         }
316         
317         jParams.changePage(blogContentPage);
318         return blogContentPage;
319     }
320     
321     /**
322      * Activates a specified container. Note that the user needs write and
323      * administration access on that container.
324      * @param containerID The ID of the container to activate
325      * @param user The JahiaUser requesting the activation
326      *
327      * @return The ActivationTestResults of the activation request
328      * @throws JahiaException If something goes wrong
329      */

330     protected ActivationTestResults activateContainer(int containerID,
331             JahiaUser user)
332     throws JahiaException {
333         log.debug("activating container: "+containerID);
334         
335         Set JavaDoc languageCodes = new HashSet JavaDoc();
336         languageCodes.add(ContentObject.SHARED_LANGUAGE);
337         languageCodes.add(jParams.getLocale().toString());
338         
339         JahiaSaveVersion saveVersion = servicesRegistry.getJahiaVersionService().
340                 getSiteSaveVersion(jParams.getSiteID());
341         
342         StateModificationContext smc = new StateModificationContext(
343                 new ContentContainerKey(containerID), languageCodes);
344         smc.setDescendingInSubPages(false);
345         
346         JahiaContainer container = this.getContainer(containerID);
347         Enumeration JavaDoc childs = container.getFields();
348            
349         while (childs.hasMoreElements()) {
350             JahiaField child = (JahiaField)childs.nextElement();
351             ContentField field = child.getContentField();
352             
353             field.activate(languageCodes, saveVersion.getVersionID(),
354                     jParams, smc);
355         }
356               
357         ActivationTestResults res = containerService.activateStagedContainer(
358                 languageCodes, containerID, user, saveVersion, jParams, smc);
359         
360         log.debug(res);
361         return res;
362     }
363     
364     /**
365      * Activates a specified containerList. Note that the user needs write and
366      * administration access on that containerList.
367      * @param containerListID The ID of the containerList to activate
368      * @param user The JahiaUser requesting the activation
369      *
370      * @return The ActivationTestResults of the activation request
371      * @throws JahiaException If something goes wrong
372      */

373     protected ActivationTestResults activateContainerList(int containerListID,
374             JahiaUser user, int pageID) throws JahiaException {
375         log.debug("activating containerList: "+containerListID);
376         
377         Set JavaDoc languageCodes = new HashSet JavaDoc();
378         languageCodes.add(ContentObject.SHARED_LANGUAGE);
379         languageCodes.add(jParams.getLocale().toString());
380         
381         JahiaSaveVersion saveVersion = servicesRegistry.getJahiaVersionService().
382                 getSiteSaveVersion(jParams.getSiteID());
383         
384         StateModificationContext smc = new StateModificationContext(
385                 new ContentContainerListKey(containerListID), languageCodes);
386         smc.setDescendingInSubPages(false);
387         
388         ActivationTestResults res = containerService.activateStagedContainerLists(
389                 languageCodes, pageID, user, saveVersion, smc);
390         
391         log.debug(res);
392         return res;
393     }
394     
395     /**
396      * Creates a Hashtable containing the information about a post according
397      * to the Blogger API
398      * @param postContainer The Container representing the post
399      *
400      * @return The post information in a Hashtable object
401      * @throws JahiaException If something goes wrong
402      */

403     protected Hashtable JavaDoc createPostInfo(JahiaContainer postContainer)
404     throws JahiaException {
405         Hashtable JavaDoc postInfo = new Hashtable JavaDoc(4);
406         String JavaDoc fieldName = containerNames.getValue(containerNames.POST_BODY);
407         
408         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
409         buffer.append("<");
410         buffer.append(containerNames.getValue(containerNames.POST_TITLE));
411         buffer.append(">");
412         buffer.append(postContainer.getFieldValue(containerNames.getValue(
413                 containerNames.POST_TITLE), "n/a"));
414         buffer.append("</");
415         buffer.append(containerNames.getValue(containerNames.POST_TITLE));
416         buffer.append(">");
417         
418         buffer.append(this.getValue(postContainer.getField(fieldName)));
419         postInfo.put(PostInfo.CONTENT, buffer.toString());
420         
421         fieldName = containerNames.getValue(containerNames.POST_AUTHOR);
422         JahiaUser author = servicesRegistry.getJahiaSiteUserManagerService().
423                 getMember(jParams.getSiteID(), postContainer.getField(fieldName).
424                 getValue());
425         
426         String JavaDoc userName;
427         if (author == null) {
428             userName = "guest";
429             
430         } else {
431             userName = author.getUsername();
432         }
433         
434         postInfo.put(MetaPostInfo.USER_ID, userName);
435         
436         postInfo.put(PostInfo.POST_ID, Integer.toString(postContainer.getID()));
437         
438         fieldName = containerNames.getValue(containerNames.POST_DATE);
439         String JavaDoc dateValue = (String JavaDoc)postContainer.getField(fieldName).getObject();
440         Date JavaDoc date = new Date JavaDoc(Long.parseLong(dateValue));
441         postInfo.put(PostInfo.DATE_CREATED, date);
442         
443         return postInfo;
444     }
445     
446     /**
447      * Creates a Hashtable containing the information about a post according
448      * to the MetaWeblog API
449      * @param postContainer The Container representing the post
450      * @param categories The Set of categories of the post
451      *
452      * @return The post information in a Hashtable object
453      * @throws JahiaException If something goes wrong
454      */

455     protected Hashtable JavaDoc createMetaPostInfo(JahiaContainer postContainer,
456             Set JavaDoc categories) throws JahiaException {
457         
458         Hashtable JavaDoc postInfo = new Hashtable JavaDoc(8);
459         
460         String JavaDoc fieldName = containerNames.getValue(containerNames.POST_TITLE);
461         postInfo.put(MetaPostInfo.TITLE,
462                 postContainer.getFieldValue(fieldName, "n/a"));
463
464         
465         fieldName = containerNames.getValue(containerNames.POST_EXCERPT);
466         JahiaField f = postContainer.getField(fieldName);
467         if (f != null) {
468             postInfo.put(MetaPostInfo.MT_EXCERPT,
469                     postContainer.getFieldValue(fieldName, "n/a"));
470         }
471         
472         
473         fieldName = containerNames.getValue(containerNames.POST_KEYWORDS);
474         f = postContainer.getField(fieldName);
475         if (f != null) {
476             postInfo.put(MetaPostInfo.MT_EXCERPT,
477                     postContainer.getFieldValue(fieldName, "n/a"));
478         }
479         
480         fieldName = containerNames.getValue(containerNames.POST_BODY);
481         postInfo.put(MetaPostInfo.DESCRIPTION,
482                 this.getValue(postContainer.getField(fieldName)));
483
484         fieldName = containerNames.getValue(containerNames.POST_AUTHOR);
485         JahiaUser author = servicesRegistry.getJahiaSiteUserManagerService().
486                 getMember(jParams.getSiteID(), postContainer.getField(fieldName).
487                 getValue());
488         
489         String JavaDoc userName;
490         if (author == null) {
491             userName = "guest";
492             
493         } else {
494             userName = author.getUsername();
495         }
496         
497         postInfo.put(MetaPostInfo.USER_ID, userName);
498         
499         postInfo.put(MetaPostInfo.POST_ID, Integer.toString(postContainer.
500                 getID()));
501         
502         fieldName = containerNames.getValue(containerNames.POST_DATE);
503         String JavaDoc dateValue = (String JavaDoc)postContainer.getField(fieldName).getObject();
504         
505         if (dateValue == null || dateValue.length() < 2) {
506             log.warn("No date for Container: "+ postContainer.getID());
507             dateValue = "0";
508         }
509         
510         final Date JavaDoc date = new Date JavaDoc(Long.parseLong(dateValue));
511         postInfo.put(MetaPostInfo.DATE_CREATED, date);
512         
513         final JahiaPage blogPage = ContentPage.getPage(postContainer.getPageID()).
514                 getPage(jParams);
515         
516         final String JavaDoc url = getContainerURL(blogPage, postContainer);
517         postInfo.put(MetaPostInfo.LINK, url);
518         
519         postInfo.put(MetaPostInfo.PERMANENT_LINK, url);
520         
521         if (categories != null && categories.size() > 0) {
522             postInfo.put(MetaPostInfo.CATEGORIES, fromSet(categories));
523         }
524         
525         return postInfo;
526     }
527     
528     /**
529      * Transforms a Set of category IDs into a Vector of IDs
530      */

531     protected Vector JavaDoc fromSet(Set JavaDoc categories) {
532         
533         if (categories == null) return new Vector JavaDoc(0);
534         
535         Vector JavaDoc cats = new Vector JavaDoc(categories.size());
536         Iterator JavaDoc ite = categories.iterator();
537         while (ite.hasNext()) {
538             Category cat = (Category)ite.next();
539             String JavaDoc catName = cat.getTitle(jParams.getLocale());
540             
541             if (catName == null || catName.length() < 1) {
542                 catName = cat.getKey();
543             }
544             cats.addElement(catName);
545         }
546         return cats;
547     }
548     
549     /**
550      * Creates a Hashtable containing the information about a post according
551      * to the MovableType API
552      * @param postContainer The Container representing the post
553      *
554      * @return The post information in a Hashtable object
555      * @throws JahiaException If something goes wrong
556      */

557     protected Hashtable JavaDoc createMovableTypePostInfo(JahiaContainer postContainer)
558     throws JahiaException {
559         
560         Hashtable JavaDoc postInfo = new Hashtable JavaDoc(4);
561         
562         String JavaDoc fieldName = containerNames.getValue(containerNames.POST_TITLE);
563         postInfo.put(MetaPostInfo.TITLE, postContainer.getField(fieldName).
564                 getValue());
565         
566         fieldName = containerNames.getValue(containerNames.POST_AUTHOR);
567         JahiaUser author = servicesRegistry.getJahiaSiteUserManagerService().
568                 getMember(jParams.getSiteID(), postContainer.getField(fieldName).
569                 getValue());
570         
571         String JavaDoc userName;
572         if (author == null) {
573             userName = "guest";
574             
575         } else {
576             userName = author.getUsername();
577         }
578         
579         postInfo.put(MetaPostInfo.USER_ID, userName);
580         
581         fieldName = containerNames.getValue(containerNames.POST_DATE);
582         String JavaDoc dateValue = (String JavaDoc)postContainer.getField(fieldName).getObject();
583         
584         if (dateValue == null || dateValue.length() < 2) {
585             log.warn("No date for Container: "+ postContainer.getID());
586             dateValue = "0";
587         }
588         
589         final Date JavaDoc date = new Date JavaDoc(Long.parseLong(dateValue));
590         postInfo.put(MetaPostInfo.DATE_CREATED, date);
591         
592         postInfo.put(MetaPostInfo.POST_ID, String.valueOf(postContainer.getID()));
593         
594         return postInfo;
595     }
596     
597     /**
598      * Constructs the pageURL of a JahiaPage
599      * @param page The page to construct the url
600      *
601      * @return A String representing the URL
602      * @throws JahiaException If something goes wrong
603      */

604     protected String JavaDoc getPageURL(JahiaPage page) throws JahiaException {
605         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
606         buffer.append(ServletResources.getCurrentRequest().
607                 getScheme());
608         buffer.append("://");
609         buffer.append(ServletResources.getCurrentRequest().
610                 getServerName());
611         buffer.append(":");
612         buffer.append(ServletResources.getCurrentRequest().
613                 getServerPort());
614         String JavaDoc savedCacheStatus = jParams.getCacheStatus();
615         jParams.setCacheStatus(ParamBean.CACHE_BYPASS);
616         buffer.append(page.getURL(jParams));
617         jParams.setCacheStatus(savedCacheStatus);
618         return buffer.toString();
619     }
620     
621     /**
622      * Constructs the URL of a JahiaContainer
623      * @param page The parent page of the Container
624      * @param container The Container to construct the URL
625      *
626      * @return A String representing the URL
627      * @throws JahiaException If something goes wrong
628      */

629     protected String JavaDoc getContainerURL(JahiaPage page, JahiaContainer container)
630     throws JahiaException {
631         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
632         buffer.append(getPageURL(page));
633         buffer.append("?entryId=");
634         buffer.append(container.getID());
635         return buffer.toString();
636     }
637     
638     /**
639      * Sets the categories for a JahiaContainer (ie a post)
640      * @param categories The Vector containing the category keys
641      * @param postContainer The container to add the categories to
642      *
643      * @throws JahiaException If something goes wrong
644      */

645      protected void setCategories(Vector JavaDoc categories,
646              JahiaContainer postContainer) throws JahiaException {
647          
648          if (categories == null) return;
649          
650          Set JavaDoc oldCats = Category.getObjectCategories(postContainer.
651                  getContentContainer().getObjectKey());
652          
653          if (categories.size() == 0) {
654             clearCategories(postContainer);
655             return;
656          }
657          
658          Iterator JavaDoc ite = categories.iterator();
659          int i = 0;
660          while (ite.hasNext()) {
661              String JavaDoc catKey = (String JavaDoc)ite.next();
662              
663              // the category is allready set for that post
664
if (oldCats.contains(catKey)) continue;
665              
666              Category cat = Category.getCategory(catKey);
667              
668              if (cat != null) {
669                  log.debug("Adding category: "+catKey);
670                  cat.addChildObjectKey(postContainer.
671                          getContentContainer().getObjectKey());
672                  i++;
673              }
674          }
675          
676          // Check if we need to remove some categories
677
if ((oldCats.size() + i) > categories.size()) {
678              Iterator JavaDoc ite2 = oldCats.iterator();
679              
680              while (ite2.hasNext()) {
681                  Category cat = (Category)ite2.next();
682                  
683                  // Don't need to remove that one
684
if (categories.contains(cat.getKey())) continue;
685                  
686                  log.debug("Removing category: "+cat.getKey());
687                  cat.removeChildObjectKey(postContainer.
688                          getContentContainer().getObjectKey());
689              }
690          }
691      }
692      
693      /**
694       * Removes all the categories of a given Container (ie post)
695       */

696      private void clearCategories(JahiaContainer postContainer)
697      throws JahiaException {
698         Set JavaDoc oldCats = Category.getObjectCategories(postContainer.
699                  getContentContainer().getObjectKey());
700         
701         Iterator JavaDoc ite = oldCats.iterator();
702         while(ite.hasNext()) {
703              String JavaDoc catKey = (String JavaDoc)ite.next();
704              Category cat = Category.getCategory(catKey);
705              
706              if (cat != null) {
707                  cat.removeChildObjectKey(postContainer.
708                          getContentContainer().getObjectKey());
709              }
710         }
711      }
712      
713      /**
714       * Sets the value og a BigText field. The value stored will be the same
715       * as the one passed as argument but wrapped between html tags.
716       * @param field The BigText field to set the value
717       * @param value The value of the field
718       *
719       * @throws JahiaException If something goes wrong
720       */

721      protected void setValue(JahiaField field, String JavaDoc value)
722      throws JahiaException {
723          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
724          buffer.append("<html>");
725          buffer.append(value);
726          buffer.append("</html>");
727          field.setValue(buffer.toString());
728      }
729      
730      /**
731       * Gets the value og a BigText field. The value returned will be the same
732       * as the one stored but minus the wrapping html tags.
733       * @param field The BigText field to get the value of
734       *
735       * @return The value of the field
736       * @throws JahiaException If something goes wrong
737       */

738      protected String JavaDoc getValue(JahiaField field)
739      throws JahiaException {
740          if (field == null) return "n/a";
741          String JavaDoc value = field.getValue();
742          if (value == null || value.length() == 0) return "n/a";
743          if (value.indexOf("<html>") != -1) {
744              String JavaDoc tag = "<html>";
745              String JavaDoc endTag = "</html>";
746              // contains the value without the html tags
747
value = value.substring(value.indexOf(tag) + tag.length(),
748                      value.indexOf(endTag));
749          }
750          return value;
751      }
752 }
753
Popular Tags