1
22
23 package com.liferay.portal.service.impl;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.log.Log;
27 import com.liferay.portal.kernel.log.LogFactoryUtil;
28 import com.liferay.portal.kernel.plugin.PluginPackage;
29 import com.liferay.portal.kernel.util.GetterUtil;
30 import com.liferay.portal.kernel.util.HttpUtil;
31 import com.liferay.portal.kernel.util.ListUtil;
32 import com.liferay.portal.kernel.util.ObjectValuePair;
33 import com.liferay.portal.kernel.util.StringPool;
34 import com.liferay.portal.kernel.util.Validator;
35 import com.liferay.portal.kernel.velocity.VelocityContext;
36 import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
37 import com.liferay.portal.kernel.xml.Document;
38 import com.liferay.portal.kernel.xml.Element;
39 import com.liferay.portal.kernel.xml.SAXReaderUtil;
40 import com.liferay.portal.model.LayoutTemplate;
41 import com.liferay.portal.model.LayoutTemplateConstants;
42 import com.liferay.portal.model.PluginSetting;
43 import com.liferay.portal.model.impl.LayoutTemplateImpl;
44 import com.liferay.portal.service.base.LayoutTemplateLocalServiceBaseImpl;
45 import com.liferay.portal.util.PropsValues;
46 import com.liferay.portlet.layoutconfiguration.util.velocity.InitColumnProcessor;
47
48 import java.io.IOException;
49 import java.io.PrintWriter;
50 import java.io.StringWriter;
51
52 import java.util.ArrayList;
53 import java.util.HashSet;
54 import java.util.Iterator;
55 import java.util.LinkedHashMap;
56 import java.util.List;
57 import java.util.Map;
58 import java.util.Set;
59
60 import javax.servlet.ServletContext;
61
62
72 public class LayoutTemplateLocalServiceImpl
73 extends LayoutTemplateLocalServiceBaseImpl {
74
75 public String getContent(
76 String layoutTemplateId, boolean standard, String themeId)
77 throws SystemException {
78
79 LayoutTemplate layoutTemplate = getLayoutTemplate(
80 layoutTemplateId, standard, themeId);
81
82 if (layoutTemplate == null) {
83 if (_log.isWarnEnabled()) {
84 _log.warn(
85 "Layout template " + layoutTemplateId + " does not exist");
86 }
87
88 layoutTemplate = getLayoutTemplate(
89 PropsValues.DEFAULT_LAYOUT_TEMPLATE_ID, standard, themeId);
90
91 if (layoutTemplate == null) {
92 _log.error(
93 "Layout template " + layoutTemplateId +
94 " and default layout template " +
95 PropsValues.DEFAULT_LAYOUT_TEMPLATE_ID +
96 " do not exist");
97
98 return StringPool.BLANK;
99 }
100 }
101
102 if (PropsValues.LAYOUT_TEMPLATE_CACHE_ENABLED) {
103 return layoutTemplate.getContent();
104 }
105 else {
106 try {
107 return layoutTemplate.getUncachedContent();
108 }
109 catch (IOException ioe) {
110 throw new SystemException(ioe);
111 }
112 }
113 }
114
115 public LayoutTemplate getLayoutTemplate(
116 String layoutTemplateId, boolean standard, String themeId) {
117
118 if (Validator.isNull(layoutTemplateId)) {
119 return null;
120 }
121
122 LayoutTemplate layoutTemplate = null;
123
124 if (themeId != null) {
125 if (standard) {
126 layoutTemplate = _getThemesStandard(themeId).get(
127 layoutTemplateId);
128 }
129 else {
130 layoutTemplate = _getThemesCustom(themeId).get(
131 layoutTemplateId);
132 }
133
134 if (layoutTemplate != null) {
135 return layoutTemplate;
136 }
137 }
138
139 if (standard) {
140 layoutTemplate = _warStandard.get(layoutTemplateId);
141
142 if (layoutTemplate == null) {
143 layoutTemplate = _portalStandard.get(layoutTemplateId);
144 }
145 }
146 else {
147 layoutTemplate = _warCustom.get(layoutTemplateId);
148
149 if (layoutTemplate == null) {
150 layoutTemplate = _portalCustom.get(layoutTemplateId);
151 }
152 }
153
154 return layoutTemplate;
155 }
156
157 public List<LayoutTemplate> getLayoutTemplates() {
158 List<LayoutTemplate> customLayoutTemplates =
159 new ArrayList<LayoutTemplate>(
160 _portalCustom.size() + _warCustom.size());
161
162 customLayoutTemplates.addAll(
163 ListUtil.fromCollection(_portalCustom.values()));
164
165 customLayoutTemplates.addAll(
166 ListUtil.fromCollection(_warCustom.values()));
167
168 return customLayoutTemplates;
169 }
170
171 public List<LayoutTemplate> getLayoutTemplates(String themeId) {
172 Map<String, LayoutTemplate> _themesCustom = _getThemesCustom(themeId);
173
174 List<LayoutTemplate> customLayoutTemplates =
175 new ArrayList<LayoutTemplate>(
176 _portalCustom.size() + _warCustom.size() +
177 _themesCustom.size());
178
179 Iterator<Map.Entry<String, LayoutTemplate>> itr =
180 _portalCustom.entrySet().iterator();
181
182 while (itr.hasNext()) {
183 Map.Entry<String, LayoutTemplate> entry = itr.next();
184
185 String layoutTemplateId = entry.getKey();
186 LayoutTemplate layoutTemplate = entry.getValue();
187
188 if (_themesCustom.containsKey(layoutTemplateId)) {
189 customLayoutTemplates.add(_themesCustom.get(layoutTemplateId));
190 }
191 else if (_warCustom.containsKey(layoutTemplateId)) {
192 customLayoutTemplates.add(_warCustom.get(layoutTemplateId));
193 }
194 else {
195 customLayoutTemplates.add(layoutTemplate);
196 }
197 }
198
199 itr = _warCustom.entrySet().iterator();
200
201 while (itr.hasNext()) {
202 Map.Entry<String, LayoutTemplate> entry = itr.next();
203
204 String layoutTemplateId = entry.getKey();
205
206 if (!_portalCustom.containsKey(layoutTemplateId) &&
207 !_themesCustom.containsKey(layoutTemplateId)) {
208
209 customLayoutTemplates.add(_warCustom.get(layoutTemplateId));
210 }
211 }
212
213 itr = _themesCustom.entrySet().iterator();
214
215 while (itr.hasNext()) {
216 Map.Entry<String, LayoutTemplate> entry = itr.next();
217
218 String layoutTemplateId = entry.getKey();
219
220 if (!_portalCustom.containsKey(layoutTemplateId) &&
221 !_warCustom.containsKey(layoutTemplateId)) {
222
223 customLayoutTemplates.add(_themesCustom.get(layoutTemplateId));
224 }
225 }
226
227 return customLayoutTemplates;
228 }
229
230 public String getWapContent(
231 String layoutTemplateId, boolean standard, String themeId)
232 throws SystemException {
233
234 LayoutTemplate layoutTemplate = getLayoutTemplate(
235 layoutTemplateId, standard, themeId);
236
237 if (layoutTemplate == null) {
238 if (_log.isWarnEnabled()) {
239 _log.warn(
240 "Layout template " + layoutTemplateId + " does not exist");
241 }
242
243 layoutTemplate = getLayoutTemplate(
244 PropsValues.DEFAULT_LAYOUT_TEMPLATE_ID, standard, themeId);
245
246 if (layoutTemplate == null) {
247 _log.error(
248 "Layout template " + layoutTemplateId +
249 " and default layout template " +
250 PropsValues.DEFAULT_LAYOUT_TEMPLATE_ID +
251 " do not exist");
252
253 return StringPool.BLANK;
254 }
255 }
256
257 if (PropsValues.LAYOUT_TEMPLATE_CACHE_ENABLED) {
258 return layoutTemplate.getWapContent();
259 }
260 else {
261 try {
262 return layoutTemplate.getUncachedWapContent();
263 }
264 catch (IOException ioe) {
265 throw new SystemException(ioe);
266 }
267 }
268 }
269
270 public List<ObjectValuePair<String, Boolean>> init(
271 ServletContext servletContext, String[] xmls,
272 PluginPackage pluginPackage) {
273
274 return init(null, servletContext, xmls, pluginPackage);
275 }
276
277 public List<ObjectValuePair<String, Boolean>> init(
278 String servletContextName, ServletContext servletContext, String[] xmls,
279 PluginPackage pluginPackage) {
280
281 List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
282 new ArrayList<ObjectValuePair<String, Boolean>>();
283
284 try {
285 for (int i = 0; i < xmls.length; i++) {
286 Set<ObjectValuePair<String, Boolean>> curLayoutTemplateIds =
287 _readLayoutTemplates(
288 servletContextName, servletContext, xmls[i],
289 pluginPackage);
290
291 Iterator<ObjectValuePair<String, Boolean>> itr =
292 curLayoutTemplateIds.iterator();
293
294 while (itr.hasNext()) {
295 ObjectValuePair<String, Boolean> ovp = itr.next();
296
297 if (!layoutTemplateIds.contains(ovp)) {
298 layoutTemplateIds.add(ovp);
299 }
300 }
301 }
302 }
303 catch (Exception e) {
304 _log.error(e, e);
305 }
306
307 return layoutTemplateIds;
308 }
309
310 public void readLayoutTemplate(
311 String servletContextName, ServletContext servletContext,
312 Set<ObjectValuePair<String, Boolean>> layoutTemplateIds,
313 com.liferay.portal.kernel.xml.Element el, boolean standard,
314 String themeId, PluginPackage pluginPackage) {
315
316 Map<String, LayoutTemplate> layoutTemplates = null;
317
318 if (themeId != null) {
319 if (standard) {
320 layoutTemplates = _getThemesStandard(themeId);
321 }
322 else {
323 layoutTemplates = _getThemesCustom(themeId);
324 }
325 }
326 else if (servletContextName != null) {
327 if (standard) {
328 layoutTemplates = _warStandard;
329 }
330 else {
331 layoutTemplates = _warCustom;
332 }
333 }
334 else {
335 if (standard) {
336 layoutTemplates = _portalStandard;
337 }
338 else {
339 layoutTemplates = _portalCustom;
340 }
341 }
342
343 Iterator<com.liferay.portal.kernel.xml.Element> itr = el.elements(
344 "layout-template").iterator();
345
346 while (itr.hasNext()) {
347 com.liferay.portal.kernel.xml.Element layoutTemplate = itr.next();
348
349 String layoutTemplateId = layoutTemplate.attributeValue("id");
350
351 if (layoutTemplateIds != null) {
352 ObjectValuePair<String, Boolean> ovp =
353 new ObjectValuePair<String, Boolean>(
354 layoutTemplateId, standard);
355
356 layoutTemplateIds.add(ovp);
357 }
358
359 LayoutTemplate layoutTemplateModel = layoutTemplates.get(
360 layoutTemplateId);
361
362 if (layoutTemplateModel == null) {
363 layoutTemplateModel = new LayoutTemplateImpl(layoutTemplateId);
364
365 layoutTemplates.put(layoutTemplateId, layoutTemplateModel);
366 }
367
368 PluginSetting pluginSetting =
369 pluginSettingLocalService.getDefaultPluginSetting();
370
371 layoutTemplateModel.setPluginPackage(pluginPackage);
372 layoutTemplateModel.setServletContext(servletContext);
373
374 if (servletContextName != null) {
375 layoutTemplateModel.setServletContextName(servletContextName);
376 }
377
378 layoutTemplateModel.setStandard(standard);
379 layoutTemplateModel.setThemeId(themeId);
380 layoutTemplateModel.setName(GetterUtil.getString(
381 layoutTemplate.attributeValue("name"),
382 layoutTemplateModel.getName()));
383 layoutTemplateModel.setTemplatePath(GetterUtil.getString(
384 layoutTemplate.elementText("template-path"),
385 layoutTemplateModel.getTemplatePath()));
386 layoutTemplateModel.setWapTemplatePath(GetterUtil.getString(
387 layoutTemplate.elementText("wap-template-path"),
388 layoutTemplateModel.getWapTemplatePath()));
389 layoutTemplateModel.setThumbnailPath(GetterUtil.getString(
390 layoutTemplate.elementText("thumbnail-path"),
391 layoutTemplateModel.getThumbnailPath()));
392
393 String content = null;
394
395 try {
396 content = HttpUtil.URLtoString(servletContext.getResource(
397 layoutTemplateModel.getTemplatePath()));
398 }
399 catch (Exception e) {
400 _log.error(
401 "Unable to get content at template path " +
402 layoutTemplateModel.getTemplatePath() + ": " +
403 e.getMessage());
404 }
405
406 if (Validator.isNull(content)) {
407 _log.error(
408 "No content found at template path " +
409 layoutTemplateModel.getTemplatePath());
410 }
411 else {
412 StringBuilder sb = new StringBuilder();
413
414 sb.append(themeId);
415
416 if (standard) {
417 sb.append(LayoutTemplateConstants.STANDARD_SEPARATOR);
418 }
419 else {
420 sb.append(LayoutTemplateConstants.CUSTOM_SEPARATOR);
421 }
422
423 sb.append(layoutTemplateId);
424
425 String velocityTemplateId = sb.toString();
426
427 layoutTemplateModel.setContent(content);
428 layoutTemplateModel.setColumns(
429 _getColumns(velocityTemplateId, content));
430 }
431
432 if (Validator.isNull(layoutTemplateModel.getWapTemplatePath())) {
433 _log.error(
434 "The element wap-template-path is not defined for " +
435 layoutTemplateId);
436 }
437 else {
438 String wapContent = null;
439
440 try {
441 wapContent = HttpUtil.URLtoString(
442 servletContext.getResource(
443 layoutTemplateModel.getWapTemplatePath()));
444 }
445 catch (Exception e) {
446 _log.error(
447 "Unable to get content at WAP template path " +
448 layoutTemplateModel.getWapTemplatePath() + ": " +
449 e.getMessage());
450 }
451
452 if (Validator.isNull(wapContent)) {
453 _log.error(
454 "No content found at WAP template path " +
455 layoutTemplateModel.getWapTemplatePath());
456 }
457 else {
458 layoutTemplateModel.setWapContent(wapContent);
459 }
460 }
461
462 com.liferay.portal.kernel.xml.Element rolesEl =
463 layoutTemplate.element("roles");
464
465 if (rolesEl != null) {
466 Iterator<com.liferay.portal.kernel.xml.Element> itr2 =
467 rolesEl.elements("role-name").iterator();
468
469 while (itr2.hasNext()) {
470 com.liferay.portal.kernel.xml.Element roleNameEl =
471 itr2.next();
472
473 pluginSetting.addRole(roleNameEl.getText());
474 }
475 }
476
477 layoutTemplateModel.setDefaultPluginSetting(pluginSetting);
478 }
479 }
480
481 public void uninstallLayoutTemplate(
482 String layoutTemplateId, boolean standard) {
483
484 if (standard) {
485 VelocityEngineUtil.flushTemplate(
486 "null" + LayoutTemplateConstants.STANDARD_SEPARATOR +
487 layoutTemplateId);
488
489 _warStandard.remove(layoutTemplateId);
490 }
491 else {
492 VelocityEngineUtil.flushTemplate(
493 "null" + LayoutTemplateConstants.CUSTOM_SEPARATOR +
494 layoutTemplateId);
495
496 _warCustom.remove(layoutTemplateId);
497 }
498 }
499
500 public void uninstallLayoutTemplates(String themeId) {
501 Map<String, LayoutTemplate> _themesStandard =
502 _getThemesStandard(themeId);
503
504 for (Map.Entry<String, LayoutTemplate> entry :
505 _themesStandard.entrySet()) {
506
507 LayoutTemplate layoutTemplate = entry.getValue();
508
509 VelocityEngineUtil.flushTemplate(
510 themeId + LayoutTemplateConstants.STANDARD_SEPARATOR +
511 layoutTemplate.getLayoutTemplateId());
512 }
513
514 _themesStandard.clear();
515
516 Map<String, LayoutTemplate> _themesCustom = _getThemesCustom(themeId);
517
518 for (Map.Entry<String, LayoutTemplate> entry :
519 _themesCustom.entrySet()) {
520
521 LayoutTemplate layoutTemplate = entry.getValue();
522
523 VelocityEngineUtil.flushTemplate(
524 themeId + LayoutTemplateConstants.CUSTOM_SEPARATOR +
525 layoutTemplate.getLayoutTemplateId());
526 }
527
528 _themesCustom.clear();
529 }
530
531 private List<String> _getColumns(
532 String velocityTemplateId, String velocityTemplateContent) {
533
534 try {
535 InitColumnProcessor processor = new InitColumnProcessor();
536
537 VelocityContext velocityContext =
538 VelocityEngineUtil.getStandardToolsContext();
539
540 velocityContext.put("processor", processor);
541
542 VelocityEngineUtil.mergeTemplate(
543 velocityTemplateId, velocityTemplateContent, velocityContext,
544 new PrintWriter(new StringWriter()));
545
546 return ListUtil.sort(processor.getColumns());
547 }
548 catch (Exception e) {
549 _log.error(e);
550
551 return new ArrayList<String>();
552 }
553 }
554
555 private Set<ObjectValuePair<String, Boolean>> _readLayoutTemplates(
556 String servletContextName, ServletContext servletContext,
557 String xml, PluginPackage pluginPackage)
558 throws Exception {
559
560 Set<ObjectValuePair<String, Boolean>> layoutTemplateIds =
561 new HashSet<ObjectValuePair<String, Boolean>>();
562
563 if (xml == null) {
564 return layoutTemplateIds;
565 }
566
567 Document doc = SAXReaderUtil.read(xml, true);
568
569 Element root = doc.getRootElement();
570
571 Element standardEl = root.element("standard");
572
573 if (standardEl != null) {
574 readLayoutTemplate(
575 servletContextName, servletContext, layoutTemplateIds,
576 standardEl, true, null, pluginPackage);
577 }
578
579 Element customEl = root.element("custom");
580
581 if (customEl != null) {
582 readLayoutTemplate(
583 servletContextName, servletContext, layoutTemplateIds,
584 customEl, false, null, pluginPackage);
585 }
586
587 return layoutTemplateIds;
588 }
589
590 private Map<String, LayoutTemplate> _getThemesCustom(String themeId) {
591 String key = themeId + LayoutTemplateConstants.CUSTOM_SEPARATOR;
592
593 Map<String, LayoutTemplate> layoutTemplates = _themes.get(key);
594
595 if (layoutTemplates == null) {
596 layoutTemplates = new LinkedHashMap<String, LayoutTemplate>();
597
598 _themes.put(key, layoutTemplates);
599 }
600
601 return layoutTemplates;
602 }
603
604 private Map<String, LayoutTemplate> _getThemesStandard(String themeId) {
605 String key = themeId + LayoutTemplateConstants.STANDARD_SEPARATOR;
606
607 Map<String, LayoutTemplate> layoutTemplates = _themes.get(key);
608
609 if (layoutTemplates == null) {
610 layoutTemplates = new LinkedHashMap<String, LayoutTemplate>();
611
612 _themes.put(key, layoutTemplates);
613 }
614
615 return layoutTemplates;
616 }
617
618 private static Log _log =
619 LogFactoryUtil.getLog(LayoutTemplateLocalServiceImpl.class);
620
621 private static Map<String, LayoutTemplate> _portalStandard =
622 new LinkedHashMap<String, LayoutTemplate>();
623 private static Map<String, LayoutTemplate> _portalCustom =
624 new LinkedHashMap<String, LayoutTemplate>();
625
626 private static Map<String, LayoutTemplate> _warStandard =
627 new LinkedHashMap<String, LayoutTemplate>();
628 private static Map<String, LayoutTemplate> _warCustom =
629 new LinkedHashMap<String, LayoutTemplate>();
630
631 private static Map<String, Map<String, LayoutTemplate>> _themes =
632 new LinkedHashMap<String, Map<String, LayoutTemplate>>();
633
634 }