1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.cms.views.impl;
18
19 import java.io.ByteArrayInputStream;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import javax.jcr.AccessDeniedException;
27 import javax.jcr.Node;
28 import javax.jcr.NodeIterator;
29 import javax.jcr.Session;
30
31 import org.exoplatform.services.cms.BasePath;
32 import org.exoplatform.services.cms.impl.DMSConfiguration;
33 import org.exoplatform.services.cms.impl.DMSRepositoryConfiguration;
34 import org.exoplatform.services.cms.templates.TemplateService;
35 import org.exoplatform.services.cms.views.ManageViewService;
36 import org.exoplatform.services.cms.views.ViewConfig;
37 import org.exoplatform.services.cms.views.ViewConfig.Tab;
38 import org.exoplatform.services.jcr.RepositoryService;
39 import org.exoplatform.services.jcr.core.ManageableRepository;
40 import org.exoplatform.services.jcr.ext.common.SessionProvider;
41 import org.exoplatform.services.jcr.ext.hierarchy.NodeHierarchyCreator;
42 import org.exoplatform.services.jcr.util.Text;
43 import org.exoplatform.services.log.ExoLogger;
44 import org.exoplatform.services.log.Log;
45 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
46 import org.exoplatform.webui.ext.UIExtension;
47 import org.exoplatform.webui.ext.UIExtensionManager;
48 import org.picocontainer.Startable;
49
50 public class ManageViewServiceImpl implements ManageViewService, Startable {
51
52
53
54
55 private static final Log LOG = ExoLogger.getLogger(ManageViewServiceImpl.class.getName());
56
57 protected final static String EXO_TEMPLATE = "exo:template" ;
58 protected final static String ADMIN_VIEW = "admin" ;
59 protected final static String DEFAULT_VIEW = "default" ;
60 protected final static String EXO_PERMISSIONS = "exo:accessPermissions" ;
61 protected final static String EXO_HIDE_EXPLORER_PANEL = "exo:hideExplorerPanel";
62 protected final static String BUTTON_PROP = "exo:buttons" ;
63
64 private final List<ManageViewPlugin> plugins_ = new ArrayList<ManageViewPlugin> ();
65 private List<?> buttons_ ;
66 private final RepositoryService repositoryService_ ;
67 private String baseViewPath_ ;
68 private final NodeHierarchyCreator nodeHierarchyCreator_ ;
69 private final DMSConfiguration dmsConfiguration_;
70 private final UIExtensionManager extensionManager_;
71 private TemplateService templateService;
72 private Set<String> configuredTemplates_;
73 private Set<String> configuredViews_;
74
75
76
77
78
79
80
81
82
83 public ManageViewServiceImpl(RepositoryService jcrService,
84 NodeHierarchyCreator nodeHierarchyCreator, DMSConfiguration dmsConfiguration,
85 UIExtensionManager extensionManager) throws Exception{
86 repositoryService_ = jcrService ;
87 nodeHierarchyCreator_ = nodeHierarchyCreator ;
88 baseViewPath_ = nodeHierarchyCreator_.getJcrPath(BasePath.CMS_VIEWS_PATH) ;
89 dmsConfiguration_ = dmsConfiguration;
90 extensionManager_ = extensionManager;
91 templateService = WCMCoreUtils.getService(TemplateService.class);
92 }
93
94
95
96
97 private void initButtons() {
98 List<UIExtension> extensions = extensionManager_.getUIExtensions(EXTENSION_TYPE);
99 List<String> actions = new ArrayList<String>();
100 if (extensions != null) {
101 for (UIExtension extension : extensions) {
102 actions.add(extension.getName());
103 }
104 }
105
106 buttons_ = Collections.unmodifiableList(actions);
107 }
108
109
110 public void start() {
111 configuredTemplates_ = new HashSet<String>();
112 configuredViews_ = new HashSet<String>();
113 try {
114 initButtons();
115 for(ManageViewPlugin plugin : plugins_) {
116 plugin.init();
117 configuredTemplates_.addAll(plugin.getConfiguredTemplates());
118 configuredViews_.addAll(plugin.getConfiguredViews());
119 }
120 } catch(Exception e) {
121 if (LOG.isErrorEnabled()) {
122 LOG.error("an error occured while starting the component", e);
123 }
124 }
125 }
126
127
128
129
130 public void stop() { }
131
132
133
134
135 public void init() throws Exception {
136 configuredTemplates_ = new HashSet<String>();
137 configuredViews_ = new HashSet<String>();
138 for(ManageViewPlugin plugin : plugins_) {
139 plugin.init() ;
140 configuredTemplates_.addAll(plugin.getConfiguredTemplates());
141 configuredViews_.addAll(plugin.getConfiguredViews());
142 }
143 }
144
145
146
147
148 public void setManageViewPlugin(ManageViewPlugin viewPlugin) {
149 plugins_.add(viewPlugin) ;
150 }
151
152
153
154
155 public List<?> getButtons(){
156 return buttons_ ;
157 }
158
159
160
161
162 public Node getViewHome() throws Exception {
163 String viewsPath = nodeHierarchyCreator_.getJcrPath(BasePath.CMS_VIEWS_PATH);
164 return (Node) getSession().getItem(viewsPath);
165 }
166
167
168
169
170 public List<ViewConfig> getAllViews() throws Exception {
171 List<ViewConfig> viewList = new ArrayList<ViewConfig>() ;
172 ViewConfig view = null;
173 Node viewNode = null ;
174 String viewsPath = nodeHierarchyCreator_.getJcrPath(BasePath.CMS_VIEWS_PATH);
175 Session session = getSession();
176 try {
177 Node viewHome = (Node)session.getItem(viewsPath) ;
178 for(NodeIterator iter = viewHome.getNodes(); iter.hasNext();) {
179 view = new ViewConfig() ;
180 viewNode = iter.nextNode() ;
181 view.setName(viewNode.getName()) ;
182 view.setPermissions(viewNode.getProperty(EXO_PERMISSIONS).getString()) ;
183 view.setTemplate(viewNode.getProperty(EXO_TEMPLATE).getString()) ;
184 List<Tab> tabList = new ArrayList<Tab>() ;
185 for(NodeIterator tabsIterator = viewNode.getNodes(); tabsIterator.hasNext(); ) {
186 Tab tab = new Tab();
187 tab.setTabName(tabsIterator.nextNode().getName());
188 tabList.add(tab) ;
189 }
190 view.setTabList(tabList) ;
191 viewList.add(view) ;
192 }
193 } catch(AccessDeniedException ace) {
194 return new ArrayList<ViewConfig>() ;
195 } finally {
196 if(session != null) session.logout();
197 }
198 return viewList ;
199 }
200
201
202
203
204 public boolean hasView(String name) throws Exception {
205 Session session = getSession();
206 Node viewHome = (Node) session.getItem(baseViewPath_);
207 boolean b = viewHome.hasNode(name);
208 session.logout();
209 return b;
210 }
211
212
213
214
215 public Node getViewByName(String name, SessionProvider provider) throws Exception {
216 Session session = getSession(provider);
217 try {
218 return (Node) session.getItem(baseViewPath_ + "/" + name);
219 } catch (AccessDeniedException ace) {
220 return null;
221 }
222 }
223
224
225
226
227 public void addView(String name, String permissions, String template, List<?> tabs) throws Exception {
228 addView(name, permissions, false, template, tabs);
229 }
230
231
232
233
234 public void addView(String name, String permissions, boolean hideExplorerPanel, String template, List<?> tabs)
235 throws Exception {
236 Session session = getSession();
237 Node viewHome = (Node) session.getItem(baseViewPath_);
238 Node view;
239 if (viewHome.hasNode(name)) {
240 view = viewHome.getNode(name);
241 if (!view.isCheckedOut())
242 view.checkout();
243 view.setProperty(EXO_PERMISSIONS, permissions);
244 view.setProperty(EXO_TEMPLATE, template);
245 view.setProperty(EXO_HIDE_EXPLORER_PANEL, hideExplorerPanel);
246 } else {
247 view = addView(viewHome, name, hideExplorerPanel, permissions, template);
248 }
249 String tabName;
250 String buttons;
251 for (int i = 0; i < tabs.size(); i++) {
252 try {
253 Node tab = (Node) tabs.get(i);
254 tabName = tab.getName();
255 buttons = tab.getProperty(BUTTON_PROP).getString();
256 } catch (Exception e) {
257 Tab tab = (Tab) tabs.get(i);
258 tabName = Text.escapeIllegalJcrChars(tab.getTabName());
259 buttons = tab.getButtons();
260 }
261 addTab(view, tabName, buttons);
262 }
263 viewHome.save();
264 session.save();
265 session.logout();
266 }
267
268
269
270
271 public void removeView(String viewName) throws Exception {
272 Session session = getSession() ;
273 Node viewHome = (Node)session.getItem(baseViewPath_) ;
274 if(viewHome.hasNode(viewName)){
275 Node view = viewHome.getNode(viewName) ;
276 view.remove() ;
277 viewHome.save() ;
278 session.save();
279 }
280 session.logout();
281 }
282
283
284
285
286 public void addTab(Node view, String name, String buttons) throws Exception {
287 Node tab ;
288 if(view.hasNode(name)){
289 tab = view.getNode(name) ;
290 }else {
291 tab = view.addNode(name, "exo:tab");
292 }
293 tab.setProperty("exo:buttons", buttons);
294 view.save() ;
295 }
296
297
298
299
300 public Node getTemplateHome(String homeAlias, SessionProvider provider) throws Exception{
301 String homePath = getJCRPath(homeAlias) ;
302 Session session = getSession(provider) ;
303 try {
304 return (Node)session.getItem(homePath);
305 } catch(AccessDeniedException ace) {
306 return null ;
307 }
308 }
309
310
311
312
313
314
315
316 private String getJCRPath(String jcrAlias) throws Exception{
317 return nodeHierarchyCreator_.getJcrPath(jcrAlias) ;
318 }
319
320
321
322
323
324
325
326
327 private Session getSession() throws Exception {
328 ManageableRepository manageableRepository = repositoryService_.getCurrentRepository() ;
329 DMSRepositoryConfiguration dmsRepoConfig = dmsConfiguration_.getConfig();
330 return manageableRepository.getSystemSession(dmsRepoConfig.getSystemWorkspace()) ;
331 }
332
333
334
335
336
337
338
339 private Session getSession(SessionProvider sessionProvider) throws Exception{
340 ManageableRepository manageableRepository = repositoryService_.getCurrentRepository() ;
341 DMSRepositoryConfiguration dmsRepoConfig = dmsConfiguration_.getConfig();
342 return sessionProvider.getSession(dmsRepoConfig.getSystemWorkspace(), manageableRepository) ;
343 }
344
345
346
347
348 public List<Node> getAllTemplates(String homeAlias, SessionProvider provider) throws Exception {
349 Node templateHomNode = getTemplateHome(homeAlias, provider);
350 List<Node> list = new ArrayList<Node>();
351 if (templateHomNode == null)
352 return list;
353 for (NodeIterator iter = templateHomNode.getNodes(); iter.hasNext();) {
354 list.add(iter.nextNode());
355 }
356 return list;
357 }
358
359
360
361
362 public Node getTemplate(String path, SessionProvider provider) throws Exception{
363 return (Node)getSession(provider).getItem(path) ;
364 }
365
366
367
368
369 public String addTemplate(String name, String content, String homeTemplate) throws Exception {
370 Session session = getSession() ;
371 Node templateHome = (Node)session.getItem(homeTemplate) ;
372 String templatePath = templateService.createTemplate(templateHome,
373 name, name,
374 new ByteArrayInputStream(content.getBytes()),
375 new String[] { "*" });
376 session.save();
377 return templatePath;
378 }
379
380
381
382
383 public String addTemplate(String name,
384 String content,
385 String homeTemplate,
386 SessionProvider provider) throws Exception {
387 Session session = getSession(provider);
388 Node templateHome = (Node) session.getItem(homeTemplate);
389 String templatePath = templateService.createTemplate(templateHome,
390 name, name,
391 new ByteArrayInputStream(content.getBytes()),
392 new String[] { "*" });
393 session.save();
394 return templatePath;
395 }
396
397
398
399
400 public String updateTemplate(String name, String content, String homeTemplate) throws Exception {
401 Session session = getSession() ;
402 Node templateHome = (Node)session.getItem(homeTemplate) ;
403 String templatePath = templateService.updateTemplate(templateHome.getNode(name),
404 new ByteArrayInputStream(content.getBytes()),
405 new String[] { "*" });
406 session.save();
407 return templatePath;
408 }
409
410
411
412
413 public String updateTemplate(String name,
414 String content,
415 String homeTemplate,
416 SessionProvider provider) throws Exception {
417 Session session = getSession(provider);
418 Node templateHome = (Node) session.getItem(homeTemplate);
419 String templatePath = templateService.updateTemplate(templateHome.getNode(name),
420 new ByteArrayInputStream(content.getBytes()),
421 new String[] { "*" });
422 session.save();
423 return templatePath;
424 }
425
426
427
428
429 public void removeTemplate(String templatePath) throws Exception {
430 Node selectedTemplate = (Node) getSession().getItem(templatePath);
431 Node parent = selectedTemplate.getParent();
432 selectedTemplate.remove();
433 parent.save();
434 parent.getSession().save();
435 }
436
437
438
439
440 public void removeTemplate(String templatePath, SessionProvider provider) throws Exception {
441 Node selectedTemplate = (Node) getSession(provider).getItem(templatePath);
442 Node parent = selectedTemplate.getParent();
443 selectedTemplate.remove();
444 parent.save();
445 parent.getSession().save();
446 }
447
448
449
450
451
452
453
454
455
456
457 private Node addView(Node viewManager, String name, boolean hideExplorerPanel, String permissions, String template)
458 throws Exception {
459 Node contentNode = viewManager.addNode(name, "exo:view");
460 contentNode.setProperty("exo:accessPermissions", permissions);
461 contentNode.setProperty("exo:template", template);
462 contentNode.setProperty(EXO_HIDE_EXPLORER_PANEL, hideExplorerPanel);
463 viewManager.save();
464 return contentNode;
465 }
466
467 @Override
468 public Set<String> getConfiguredTemplates() {
469 return configuredTemplates_;
470 }
471
472 @Override
473 public Set<String> getConfiguredViews() {
474 return configuredViews_;
475 }
476
477 }