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.ecm.publication.impl;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Locale;
24  import java.util.Map;
25  import java.util.ResourceBundle;
26  
27  import javax.jcr.Node;
28  import javax.jcr.Session;
29  import javax.jcr.Value;
30  import javax.jcr.nodetype.NoSuchNodeTypeException;
31  import javax.jcr.nodetype.NodeType;
32  
33  import org.exoplatform.services.ecm.publication.AlreadyInPublicationLifecycleException;
34  import org.exoplatform.services.ecm.publication.IncorrectStateUpdateLifecycleException;
35  import org.exoplatform.services.ecm.publication.NotInPublicationLifecycleException;
36  import org.exoplatform.services.ecm.publication.PublicationPlugin;
37  import org.exoplatform.services.ecm.publication.PublicationPresentationService;
38  import org.exoplatform.services.ecm.publication.PublicationService;
39  import org.exoplatform.services.jcr.core.ManageableRepository;
40  import org.exoplatform.services.log.ExoLogger;
41  import org.exoplatform.services.log.Log;
42  import org.exoplatform.services.cms.CmsService;
43  import org.exoplatform.services.listener.ListenerService;
44  import org.exoplatform.services.wcm.publication.WCMPublicationService;
45  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
46  
47  /**
48   * Created by The eXo Platform SAS
49   * Author : Romain Dénarié
50   *          romain.denarie@exoplatform.com
51   * 7 mai 08
52   */
53  public class PublicationServiceImpl implements PublicationService {
54  
55    private static final Log LOG = ExoLogger.getLogger(PublicationServiceImpl.class.getName());
56    private PublicationPresentationService publicationPresentationService;
57    private ListenerService listenerService;
58    private CmsService cmsService;
59  
60    private final String localeFile = "locale.portlet.publication.PublicationService";
61  
62    Map<String, PublicationPlugin> publicationPlugins_;
63  
64    public PublicationServiceImpl (PublicationPresentationService presentationService) {
65      if (LOG.isInfoEnabled()) {
66        LOG.info("# PublicationService initialization #");
67      }
68      this.publicationPresentationService = presentationService;
69      this.listenerService = WCMCoreUtils.getService(ListenerService.class);
70      this.cmsService = WCMCoreUtils.getService(CmsService.class);
71      publicationPlugins_ = new HashMap<String, PublicationPlugin>();
72    }
73  
74    /* (non-Javadoc)
75     * @see org.exoplatform.services.cms.publication.PublicationService#addLog(javax.jcr.Node, java.lang.String[])
76     */
77    public void addLog(Node node, String[] args) throws NotInPublicationLifecycleException, Exception {
78      Session session = node.getSession() ;
79      ManageableRepository repository = (ManageableRepository)session.getRepository() ;
80      Session systemSession = repository.getSystemSession(session.getWorkspace().getName()) ;
81  
82      if (!isNodeEnrolledInLifecycle(node)) {
83        throw new NotInPublicationLifecycleException();
84      }
85      List<Value> newValues = new ArrayList<Value>();
86      Value[] values = node.getProperty(HISTORY).getValues();
87      newValues.addAll(Arrays.<Value>asList(values)) ;
88      StringBuffer string2add = new StringBuffer();
89      for (int i=0; i<args.length;i++) {
90        if (i==0) string2add.append(args[i]);
91        else string2add.append(",").append(args[i]);
92      }
93      Value value2add=systemSession.getValueFactory().createValue(string2add.toString());
94      newValues.add(value2add);
95      node.setProperty(HISTORY,newValues.toArray(new Value[newValues.size()])) ;
96      systemSession.logout();
97    }
98  
99    /*
100    * (non-Javadoc)
101    * @seeorg.exoplatform.services.cms.publication.PublicationService#
102    * addPublicationPlugin
103    * (org.exoplatform.services.cms.publication.PublicationPlugin)
104    */
105   public void addPublicationPlugin(PublicationPlugin p) {
106     this.publicationPlugins_.put(p.getLifecycleName(),p);
107     publicationPresentationService.addPublicationPlugin(p);
108   }
109 
110   /*
111    * (non-Javadoc)
112    * @see
113    * org.exoplatform.services.cms.publication.PublicationService#changeState
114    * (javax.jcr.Node, java.lang.String, java.util.HashMap)
115    */
116   public void changeState(Node node, String newState, HashMap<String, String> context)
117   throws NotInPublicationLifecycleException, IncorrectStateUpdateLifecycleException, Exception {
118     if (!isNodeEnrolledInLifecycle(node)) throw new NotInPublicationLifecycleException();
119     String lifecycleName=getNodeLifecycleName(node);
120     PublicationPlugin nodePlugin = this.publicationPlugins_.get(lifecycleName);
121     nodePlugin.changeState(node, newState, context);
122     listenerService.broadcast(WCMPublicationService.UPDATE_EVENT, cmsService, node);
123 
124   }
125 
126   /*
127    * (non-Javadoc)
128    * @seeorg.exoplatform.services.cms.publication.PublicationService#
129    * enrollNodeInLifecycle(javax.jcr.Node, java.lang.String)
130    */
131   public void enrollNodeInLifecycle(Node node, String lifecycle)
132   throws AlreadyInPublicationLifecycleException, Exception {
133     if (isNodeEnrolledInLifecycle(node)) throw new AlreadyInPublicationLifecycleException();
134     //create mixin publication,
135     //with lifecycleName = lifecycle
136     //current state = default state = enrolled
137     //history : empty
138     if(publicationPlugins_.get(lifecycle).canAddMixin(node)) publicationPlugins_.get(lifecycle).addMixin(node) ;
139     else throw new NoSuchNodeTypeException() ;
140     node.setProperty(LIFECYCLE_NAME, lifecycle);
141     node.setProperty(CURRENT_STATE, "enrolled");
142     List<Value> history = new ArrayList<Value>();
143     node.setProperty(HISTORY, history.toArray(new Value[history.size()]));
144     publicationPlugins_.get(lifecycle).changeState(node, "enrolled", new HashMap<String,String>());
145   }
146 
147   public void unsubcribeLifecycle(Node node) throws NotInPublicationLifecycleException, Exception {
148     if(!isNodeEnrolledInLifecycle(node)) throw new NotInPublicationLifecycleException();
149     //remove all extended publication mixin nodetype for this node
150     String lifecycleName = getNodeLifecycleName(node);
151     if (LOG.isInfoEnabled()) {
152       LOG.info("The document: " + node.getName() + " unsubcribe publication lifecycle: " + lifecycleName);
153     }
154     for(NodeType nodeType: node.getMixinNodeTypes()) {
155       if(!nodeType.isNodeType(PUBLICATION)) continue;
156       node.removeMixin(nodeType.getName());
157     }
158     node.getSession().save();
159   }
160   /* (non-Javadoc)
161    * @see org.exoplatform.services.cms.publication.PublicationService#getCurrentState(javax.jcr.Node)
162    */
163   public String getCurrentState(Node node) throws NotInPublicationLifecycleException,Exception {
164     if (!isNodeEnrolledInLifecycle(node)) throw new NotInPublicationLifecycleException();
165     return node.getProperty(CURRENT_STATE).getString();
166   }
167 
168   /* (non-Javadoc)
169    * @see org.exoplatform.services.cms.publication.PublicationService#getLog(javax.jcr.Node)
170    */
171   public String[][] getLog(Node node) throws NotInPublicationLifecycleException, Exception {
172     if (!isNodeEnrolledInLifecycle(node)) throw new NotInPublicationLifecycleException();
173     Value[] values = node.getProperty(HISTORY).getValues();
174     String [][] result=new String[values.length][];
175     for (int i=0;i<values.length;i++) {
176       Value currentValue=values[i];
177       String currentString=currentValue.getString();
178       String [] currentStrings=currentString.split(",");
179       result[i]=currentStrings;
180     }
181     return result;
182 
183   }
184 
185   /*
186    * (non-Javadoc)
187    * @seeorg.exoplatform.services.cms.publication.PublicationService#
188    * getNodeLifecycleDesc(javax.jcr.Node)
189    */
190   public String getNodeLifecycleDesc(Node node) throws NotInPublicationLifecycleException,
191                                                Exception {
192     if (!isNodeEnrolledInLifecycle(node))
193       throw new NotInPublicationLifecycleException();
194     String lifecycleName = getNodeLifecycleName(node);
195     PublicationPlugin nodePlugin = this.publicationPlugins_.get(lifecycleName);
196     return nodePlugin.getNodeLifecycleDesc(node);
197   }
198 
199   /* (non-Javadoc)
200    * @see org.exoplatform.services.cms.publication.PublicationService#getNodeLifecycleName(javax.jcr.Node)
201    */
202   public String getNodeLifecycleName(Node node) throws NotInPublicationLifecycleException, Exception {
203     if (!isNodeEnrolledInLifecycle(node)) throw new NotInPublicationLifecycleException();
204     return node.getProperty(LIFECYCLE_NAME).getString();
205   }
206 
207   /* (non-Javadoc)
208    * @see org.exoplatform.services.cms.publication.PublicationService#getPublicationPlugins()
209    */
210   public Map<String,PublicationPlugin> getPublicationPlugins() {
211     return this.publicationPlugins_;
212   }
213 
214   /* (non-Javadoc)
215    * @see org.exoplatform.services.cms.publication.PublicationService#getStateImage(javax.jcr.Node)
216    */
217   public byte[] getStateImage(Node node,Locale locale) throws NotInPublicationLifecycleException, Exception {
218     if (!isNodeEnrolledInLifecycle(node)) {
219       throw new NotInPublicationLifecycleException();
220     }
221     String lifecycleName = getNodeLifecycleName(node);
222     PublicationPlugin nodePlugin = this.publicationPlugins_.get(lifecycleName);
223     return nodePlugin.getStateImage(node, locale);
224   }
225 
226 
227   /* (non-Javadoc)
228    * @see org.exoplatform.services.cms.publication.PublicationService#getUserInfo(javax.jcr.Node)
229    */
230   public String getUserInfo(Node node, Locale locale) throws NotInPublicationLifecycleException, Exception {
231     if (!isNodeEnrolledInLifecycle(node)) {
232       throw new NotInPublicationLifecycleException();
233     }
234     String lifecycleName=getNodeLifecycleName(node);
235     PublicationPlugin nodePlugin = this.publicationPlugins_.get(lifecycleName);
236     return nodePlugin.getUserInfo(node, locale);
237   }
238 
239   /* (non-Javadoc)
240    * @see org.exoplatform.services.cms.publication.PublicationService#isNodeEnrolledInLifecycle(javax.jcr.Node)
241    */
242   public boolean isNodeEnrolledInLifecycle(Node node) throws Exception {
243     return node.isNodeType(PUBLICATION);
244   }
245 
246   public String getLocalizedAndSubstituteLog(Locale locale, String key, String[] values){
247     ClassLoader cl=this.getClass().getClassLoader();
248     ResourceBundle resourceBundle=ResourceBundle.getBundle(localeFile,locale,cl);
249     String result = resourceBundle.getString(key);
250     return String.format(result,values);
251   }
252 
253   public String getLocalizedAndSubstituteLog(Node node,
254                                              Locale locale,
255                                              String key,
256                                              String[] values) throws NotInPublicationLifecycleException,
257                                                                                                    Exception {
258     String lifecycleName = getNodeLifecycleName(node);
259     PublicationPlugin publicationPlugin = publicationPlugins_.get(lifecycleName);
260     try {
261       return publicationPlugin.getLocalizedAndSubstituteMessage(locale, key, values);
262     } catch (Exception e) {
263       if (LOG.isWarnEnabled()) {
264         LOG.warn("Exception when get log message", e);
265       }
266       return key;
267     }
268   }
269 
270   public boolean isUnsubcribeLifecycle(Node node) throws Exception {
271     /* Check lifecycle of node */
272     if (isNodeEnrolledInLifecycle(node))
273       return false;
274     return true;
275   }
276 
277   public Node getNodePublish(Node node, String pluginName) throws Exception {
278     if (node.isNodeType(PUBLICATION)) {
279       PublicationPlugin publicationPlugin;
280       if (pluginName == null || pluginName.trim().equals("")) {
281         String lifecycleName = node.getProperty(LIFECYCLE_NAME).getString();
282         publicationPlugin = publicationPlugins_.get(lifecycleName);
283       } else {
284         publicationPlugin = publicationPlugins_.get(pluginName);
285       }
286       return publicationPlugin.getNodeView(node, null);
287     }
288     return null;
289   }
290 }