View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.wcm.publication;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.jcr.Node;
23  
24  import org.exoplatform.services.cms.CmsService;
25  import org.exoplatform.services.ecm.publication.NotInPublicationLifecycleException;
26  import org.exoplatform.services.ecm.publication.PublicationPlugin;
27  import org.exoplatform.services.ecm.publication.PublicationService;
28  import org.exoplatform.services.listener.ListenerService;
29  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
30  import org.picocontainer.Startable;
31  
32  /**
33   * Created by The eXo Platform SAS
34   * Author : Hoa Pham
35   * hoa.pham@exoplatform.com
36   * Sep 29, 2008
37   */
38  public class WCMPublicationServiceImpl implements WCMPublicationService, Startable {
39  
40    /** The Constant SIMPLE_LIFECYCLE_NAME. */
41    private static final String SIMPLE_LIFECYCLE_NAME = "Simple publication";
42  
43    /** The Constant STATEVERSION_LIFECYCLE_NAME. */
44    public static final String STATEVERSION_LIFECYCLE_NAME = "States and versions based publication";
45  
46    /** The publication plugins. */
47    private HashMap<String, WebpagePublicationPlugin> publicationPlugins =
48      new HashMap<String, WebpagePublicationPlugin>();
49  
50    /** The publication service. */
51    protected PublicationService publicationService;
52  
53    protected ListenerService listenerService;
54  
55    protected CmsService cmsService;
56  
57    /**
58     * Instantiates a new WCM publication service.
59     * This service delegate to PublicationService to manage the publication
60     */
61    public WCMPublicationServiceImpl() {
62      this.publicationService = WCMCoreUtils.getService(PublicationService.class);
63      this.listenerService = WCMCoreUtils.getService(ListenerService.class);
64      this.cmsService = WCMCoreUtils.getService(CmsService.class);
65    }
66  
67    /*
68     * (non-Javadoc)
69     * @see
70     * org.exoplatform.services.wcm.publication.WCMPublicationPresentationService
71     * #addPublicationPlugin
72     * (org.exoplatform.services.wcm.publication.WebpagePublicationPlugin)
73     */
74    public void addPublicationPlugin(WebpagePublicationPlugin p) {
75      publicationPlugins.put(p.getLifecycleName(),p);
76      publicationService.addPublicationPlugin(PublicationPlugin.class.cast(p));
77    }
78  
79    /*
80     * (non-Javadoc)
81     * @seeorg.exoplatform.services.wcm.publication.WCMPublicationService#
82     * enrollNodeInLifecycle(javax.jcr.Node, java.lang.String)
83     */
84    public void enrollNodeInLifecycle(Node node, String lifecycleName) throws Exception {
85      publicationService.enrollNodeInLifecycle(node,lifecycleName);
86    }
87  
88    /*
89     * (non-Javadoc)
90     * @see
91     * org.exoplatform.services.wcm.publication.WCMPublicationPresentationService
92     * #unsubcribeLifecycle(javax.jcr.Node)
93     */
94    public void unsubcribeLifecycle(Node node) throws NotInPublicationLifecycleException, Exception {
95      publicationService.unsubcribeLifecycle(node);
96    }
97  
98    /*
99     * (non-Javadoc)
100    * @see
101    * org.exoplatform.services.wcm.publication.WCMPublicationPresentationService
102    * #getWebpagePublicationPlugins()
103    */
104   public Map<String, WebpagePublicationPlugin> getWebpagePublicationPlugins() {
105     return publicationPlugins;
106   }
107 
108   /* (non-Javadoc)
109    * @see org.picocontainer.Startable#start()
110    */
111   public void start()   {
112   }
113 
114   /* (non-Javadoc)
115    * @see org.picocontainer.Startable#stop()
116    */
117   public void stop() {
118   }
119 
120   /* (non-Javadoc)
121    * @see org.exoplatform.services.wcm.publication.WCMPublicationService#isEnrolledInWCMLifecycle(javax.jcr.Node)
122    */
123   public boolean isEnrolledInWCMLifecycle(Node node) throws NotInPublicationLifecycleException, Exception {
124     if(!publicationService.isNodeEnrolledInLifecycle(node))
125       return false;
126     String lifecyleName = publicationService.getNodeLifecycleName(node);
127     if(publicationPlugins.containsKey(lifecyleName))
128       return true;
129     throw new NotInWCMPublicationException();
130   }
131 
132   /**
133    * This default implementation uses "States and versions based publication" as
134    * a default lifecycle for all sites and "Simple Publishing" for the root
135    * user.
136    */
137   public void enrollNodeInLifecycle(Node node, String siteName, String remoteUser) throws Exception {
138     /*
139      * lifecycle based on site (each site can define its own publication
140      * lifecycle) We choose to use a different publication plugin for testing
141      * only for now (test has to be created separetly)
142      */
143     if ("test".equals(siteName)) {
144       enrollNodeInLifecycle(node, SIMPLE_LIFECYCLE_NAME);
145     } else {
146       enrollNodeInLifecycle(node, STATEVERSION_LIFECYCLE_NAME);
147     }
148   }
149 
150   /**
151    * This default implementation simply delegates updates to the node WebpagePublicationPlugin.
152    */
153   public void updateLifecyleOnChangeContent(Node node, String siteName, String remoteUser)
154       throws Exception {
155     updateLifecyleOnChangeContent(node, siteName, remoteUser, null);
156   }
157 
158   /**
159    * This default implementation checks if the state is valid then delegates the update to the node WebpagePublicationPlugin.
160    */
161   public void updateLifecyleOnChangeContent(Node node, String siteName, String remoteUser, String newState)
162       throws Exception {
163 
164       if(!publicationService.isNodeEnrolledInLifecycle(node)) {
165         enrollNodeInLifecycle(node,siteName, remoteUser);
166       }
167       String lifecycleName = publicationService.getNodeLifecycleName(node);
168       WebpagePublicationPlugin publicationPlugin = publicationPlugins.get(lifecycleName);
169 
170       boolean hasState = false;
171       if (newState!=null) {
172         String[] states = publicationPlugin.getPossibleStates();
173         for (String state:states) {
174           if (state.equals(newState)) hasState=true;
175         }
176       }
177       if (hasState)
178         publicationPlugin.updateLifecyleOnChangeContent(node, remoteUser, newState);
179       else
180         publicationPlugin.updateLifecyleOnChangeContent(node, remoteUser);
181 
182       listenerService.broadcast(UPDATE_EVENT, cmsService, node);
183   }
184 
185   public String getContentState(Node node) throws Exception {
186     String currentState = null;
187     if(node.hasProperty("publication:currentState")) {
188       currentState = node.getProperty("publication:currentState").getString();
189     }
190     return currentState;
191   }
192 }