1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.wcm.portal.impl;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.Set;
23 import java.util.concurrent.ConcurrentHashMap;
24
25 import javax.jcr.Node;
26 import javax.jcr.NodeIterator;
27 import javax.jcr.PathNotFoundException;
28 import javax.jcr.Session;
29 import javax.jcr.query.Query;
30 import javax.jcr.query.QueryResult;
31
32 import org.exoplatform.portal.config.model.PortalConfig;
33 import org.exoplatform.services.jcr.RepositoryService;
34 import org.exoplatform.services.jcr.core.ExtendedNode;
35 import org.exoplatform.services.jcr.core.ManageableRepository;
36 import org.exoplatform.services.jcr.ext.common.SessionProvider;
37 import org.exoplatform.services.log.ExoLogger;
38 import org.exoplatform.services.log.Log;
39 import org.exoplatform.services.wcm.core.NodeLocation;
40 import org.exoplatform.services.wcm.core.WCMConfigurationService;
41 import org.exoplatform.services.wcm.core.WebSchemaConfigService;
42 import org.exoplatform.services.wcm.portal.LivePortalManagerService;
43 import org.picocontainer.Startable;
44
45
46
47
48
49
50
51
52
53
54
55 public class LivePortalManagerServiceImpl implements LivePortalManagerService, Startable {
56
57 private final String PORTAL_FOLDER = "exo:portalFolder";
58
59 private static final Log LOG = ExoLogger.getLogger(LivePortalManagerServiceImpl.class.getName());
60
61 private ConcurrentHashMap<String, String> livePortalPaths = new ConcurrentHashMap<String, String>();
62
63 private RepositoryService repositoryService;
64
65 private WCMConfigurationService wcmConfigService;
66
67
68
69
70
71
72
73
74 public LivePortalManagerServiceImpl(
75 WebSchemaConfigService webSchemaConfigService,
76 WCMConfigurationService wcmConfigurationService,
77 RepositoryService repositoryService) {
78 this.wcmConfigService = wcmConfigurationService;
79 this.repositoryService = repositoryService;
80 }
81
82
83
84
85
86
87
88 public final Node getLivePortal(final SessionProvider sessionProvider, final String portalName) throws Exception {
89 String currentRepository = repositoryService.getCurrentRepository().getConfiguration().getName();
90 return getLivePortal(sessionProvider, currentRepository, portalName);
91 }
92
93
94
95
96
97
98
99 public final List<Node> getLivePortals(final SessionProvider sessionProvider) throws Exception {
100 String currentRepository = repositoryService.getCurrentRepository().getConfiguration().getName();
101 return getLivePortals(sessionProvider, currentRepository);
102 }
103
104
105
106
107
108
109
110 public final Node getLiveSharedPortal(final SessionProvider sessionProvider) throws Exception {
111 String currentRepository = repositoryService.getCurrentRepository().getConfiguration().getName();
112 return getLiveSharedPortal(sessionProvider, currentRepository);
113 }
114
115
116
117
118
119
120
121
122 public final Node getLivePortal(final SessionProvider sessionProvider,
123 final String repository,
124 final String portalName) throws Exception {
125 Node portalsStorage = getLivePortalsStorage(sessionProvider);
126 return portalsStorage.getNode(portalName);
127 }
128
129
130
131
132
133
134
135 public final List<Node> getLivePortals(final SessionProvider sessionProvider, final String repository) throws Exception {
136 List<Node> list = new ArrayList<Node>();
137 Node portalsStorage = getLivePortalsStorage(sessionProvider);
138 for (NodeIterator iterator = portalsStorage.getNodes(); iterator.hasNext(); ) {
139 Node node = iterator.nextNode();
140 if (node.isNodeType(PORTAL_FOLDER)) {
141 list.add(node);
142 }
143 }
144 return list;
145 }
146
147
148
149
150
151
152
153 public final Node getLiveSharedPortal(final SessionProvider sessionProvider,
154 final String repository) throws Exception {
155 Node portalsStorage = getLivePortalsStorage(sessionProvider);
156 String sharePortalName = wcmConfigService.getSharedPortalName();
157 try {
158 return portalsStorage.getNode(sharePortalName);
159 } catch (PathNotFoundException e) {
160 return null;
161 }
162 }
163
164 private Node getLivePortalsStorage(final SessionProvider sessionProvider) throws Exception {
165 NodeLocation locationEntry = wcmConfigService.getLivePortalsLocation();
166 String workspace = locationEntry.getWorkspace();
167 String portalsStoragePath = locationEntry.getPath();
168 ManageableRepository manageableRepository = repositoryService.getCurrentRepository();
169 Session session = sessionProvider.getSession(workspace, manageableRepository);
170 return (Node)session.getItem(portalsStoragePath);
171 }
172
173
174
175
176
177
178
179
180 public final void addLivePortal(final SessionProvider sessionProvider, final PortalConfig portalConfig)
181 throws Exception {
182 Node livePortalsStorage = getLivePortalsStorage(sessionProvider) ;
183 String portalName = portalConfig.getName();
184 if(livePortalsStorage.hasNode(portalName)) {
185 return;
186 }
187 ExtendedNode newPortal = (ExtendedNode)livePortalsStorage.addNode(portalName,PORTAL_FOLDER);
188 if (!newPortal.isNodeType("exo:owneable"))
189 newPortal.addMixin("exo:owneable");
190 if(newPortal.canAddMixin("metadata:siteMetadata")) {
191 newPortal.addMixin("metadata:siteMetadata");
192 newPortal.setProperty("siteTitle",portalName);
193 newPortal.setProperty("keywords",portalName);
194 newPortal.setProperty("robots","index,follow");
195 }
196 if(newPortal.canAddMixin("dc:elementSet")) {
197 newPortal.addMixin("dc:elementSet");
198 }
199
200 newPortal.getSession().save();
201
202 if(livePortalPaths.size() == 0) {
203 String sharedPortalName = wcmConfigService.getSharedPortalName();
204 NodeLocation nodeLocation = wcmConfigService.getLivePortalsLocation();
205 livePortalPaths.put(sharedPortalName,nodeLocation.getPath() + "/"+ sharedPortalName);
206 }
207 livePortalPaths.put(portalName,newPortal.getPath());
208 }
209
210
211
212
213
214
215
216
217 public void removeLivePortal(final SessionProvider sessionProvider, final PortalConfig portalConfig)
218 throws Exception {
219
220
221 Node node = getLivePortal(sessionProvider, portalConfig.getName());
222 Session session = node.getSession();
223 node.remove();
224 session.save();
225 livePortalPaths.remove(portalConfig.getName());
226 }
227
228
229
230
231 public Collection<String> getLivePortalsPath() throws Exception {
232 return livePortalPaths.values();
233 }
234
235 public String getPortalNameByPath(String portalPath) throws Exception {
236 Set<String> keys = livePortalPaths.keySet();
237 for(String portalName: keys.toArray(new String[keys.size()])) {
238 if(livePortalPaths.get(portalName).equalsIgnoreCase(portalPath)) {
239 return portalName;
240 }
241 }
242 return null;
243 }
244
245 public Node getLivePortalByChild(Node childNode) throws Exception {
246 for(String portalPath: livePortalPaths.values()) {
247 if(childNode.getPath().startsWith(portalPath)) {
248 return (Node)childNode.getSession().getItem(portalPath);
249 }
250 }
251 return null;
252 }
253
254 public void start() {
255 if (LOG.isInfoEnabled()) {
256 LOG.info("Start LivePortalManagementService....");
257 }
258 SessionProvider sessionProvider = null;
259 Session session = null;
260 try {
261 sessionProvider = SessionProvider.createSystemProvider();
262 ManageableRepository repository = repositoryService.getCurrentRepository();
263 NodeLocation nodeLocation = wcmConfigService.getLivePortalsLocation();
264 session = sessionProvider.getSession(nodeLocation.getWorkspace(),repository);
265 String statement = "select * from exo:portalFolder where jcr:path like '" + nodeLocation.getPath() + "/%'";
266 Query query = session.getWorkspace().getQueryManager().createQuery(statement,Query.SQL);
267 QueryResult result = query.execute();
268 for(NodeIterator iterator = result.getNodes(); iterator.hasNext();) {
269 Node portalNode = iterator.nextNode();
270 livePortalPaths.putIfAbsent(portalNode.getName(),portalNode.getPath());
271 }
272 } catch (Exception e) {
273 if (LOG.isErrorEnabled()) {
274 LOG.error("Error when starting LivePortalManagerService: ", e);
275 }
276 } finally {
277 sessionProvider.close();
278 }
279 }
280
281 public void stop() {
282 }
283
284 public String getPortalPathByName(String portalName) throws Exception {
285 return livePortalPaths.get(portalName);
286 }
287 }