1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
49
50
51
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
75
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
101
102
103
104
105 public void addPublicationPlugin(PublicationPlugin p) {
106 this.publicationPlugins_.put(p.getLifecycleName(),p);
107 publicationPresentationService.addPublicationPlugin(p);
108 }
109
110
111
112
113
114
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
128
129
130
131 public void enrollNodeInLifecycle(Node node, String lifecycle)
132 throws AlreadyInPublicationLifecycleException, Exception {
133 if (isNodeEnrolledInLifecycle(node)) throw new AlreadyInPublicationLifecycleException();
134
135
136
137
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
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
161
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
169
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
187
188
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
200
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
208
209
210 public Map<String,PublicationPlugin> getPublicationPlugins() {
211 return this.publicationPlugins_;
212 }
213
214
215
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
228
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
240
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
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 }