1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
34
35
36
37
38 public class WCMPublicationServiceImpl implements WCMPublicationService, Startable {
39
40
41 private static final String SIMPLE_LIFECYCLE_NAME = "Simple publication";
42
43
44 public static final String STATEVERSION_LIFECYCLE_NAME = "States and versions based publication";
45
46
47 private HashMap<String, WebpagePublicationPlugin> publicationPlugins =
48 new HashMap<String, WebpagePublicationPlugin>();
49
50
51 protected PublicationService publicationService;
52
53 protected ListenerService listenerService;
54
55 protected CmsService cmsService;
56
57
58
59
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
69
70
71
72
73
74 public void addPublicationPlugin(WebpagePublicationPlugin p) {
75 publicationPlugins.put(p.getLifecycleName(),p);
76 publicationService.addPublicationPlugin(PublicationPlugin.class.cast(p));
77 }
78
79
80
81
82
83
84 public void enrollNodeInLifecycle(Node node, String lifecycleName) throws Exception {
85 publicationService.enrollNodeInLifecycle(node,lifecycleName);
86 }
87
88
89
90
91
92
93
94 public void unsubcribeLifecycle(Node node) throws NotInPublicationLifecycleException, Exception {
95 publicationService.unsubcribeLifecycle(node);
96 }
97
98
99
100
101
102
103
104 public Map<String, WebpagePublicationPlugin> getWebpagePublicationPlugins() {
105 return publicationPlugins;
106 }
107
108
109
110
111 public void start() {
112 }
113
114
115
116
117 public void stop() {
118 }
119
120
121
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
134
135
136
137 public void enrollNodeInLifecycle(Node node, String siteName, String remoteUser) throws Exception {
138
139
140
141
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
152
153 public void updateLifecyleOnChangeContent(Node node, String siteName, String remoteUser)
154 throws Exception {
155 updateLifecyleOnChangeContent(node, siteName, remoteUser, null);
156 }
157
158
159
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 }