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