001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.plugin.PluginPackage;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.ObjectValuePair;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.LayoutTemplate;
030: import com.liferay.portal.model.PluginSetting;
031: import com.liferay.portal.model.impl.LayoutTemplateImpl;
032: import com.liferay.portal.service.PluginSettingLocalServiceUtil;
033: import com.liferay.portal.util.PortalUtil;
034: import com.liferay.portal.util.PropsUtil;
035: import com.liferay.portal.util.PropsValues;
036: import com.liferay.portlet.layoutconfiguration.util.velocity.InitColumnProcessor;
037: import com.liferay.util.Http;
038: import com.liferay.util.ListUtil;
039:
040: import java.io.IOException;
041: import java.io.PrintWriter;
042: import java.io.StringWriter;
043:
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.HashSet;
047: import java.util.Iterator;
048: import java.util.LinkedHashMap;
049: import java.util.List;
050: import java.util.Map;
051: import java.util.Set;
052:
053: import javax.servlet.ServletContext;
054:
055: import org.apache.commons.logging.Log;
056: import org.apache.commons.logging.LogFactory;
057: import org.apache.velocity.VelocityContext;
058: import org.apache.velocity.app.Velocity;
059:
060: import org.dom4j.Document;
061: import org.dom4j.DocumentException;
062: import org.dom4j.Element;
063:
064: /**
065: * <a href="LayoutTemplateLocalUtil.java.html"><b><i>View Source</i></b></a>
066: *
067: * @author Ivica Cardic
068: * @author Jorge Ferrer
069: *
070: */
071: public class LayoutTemplateLocalUtil {
072:
073: public static String getContent(String layoutTemplateId,
074: boolean standard, String themeId) throws SystemException {
075:
076: LayoutTemplate layoutTemplate = getLayoutTemplate(
077: layoutTemplateId, standard, themeId);
078:
079: if (layoutTemplate == null) {
080: if (_log.isWarnEnabled()) {
081: _log.warn("Layout template " + layoutTemplateId
082: + " does not exist");
083: }
084:
085: String defaultUserLayoutTemplateId = PropsUtil
086: .get(PropsUtil.DEFAULT_USER_LAYOUT_TEMPLATE_ID);
087:
088: layoutTemplate = getLayoutTemplate(
089: defaultUserLayoutTemplateId, standard, themeId);
090:
091: if (layoutTemplate == null) {
092: _log.error("Layout template " + layoutTemplateId
093: + " and default user layout template "
094: + defaultUserLayoutTemplateId
095: + " does not exist");
096:
097: return StringPool.BLANK;
098: }
099: }
100:
101: if (PropsValues.LAYOUT_TEMPLATE_CACHE_ENABLED) {
102: return layoutTemplate.getContent();
103: } else {
104: try {
105: return layoutTemplate.getUncachedContent();
106: } catch (IOException ioe) {
107: throw new SystemException(ioe);
108: }
109: }
110: }
111:
112: public static LayoutTemplate getLayoutTemplate(
113: String layoutTemplateId, boolean standard, String themeId) {
114:
115: if (Validator.isNull(layoutTemplateId)) {
116: return null;
117: }
118:
119: LayoutTemplate layoutTemplate = null;
120:
121: if (themeId != null) {
122: if (standard) {
123: layoutTemplate = (LayoutTemplate) _getThemesStandard(
124: themeId).get(layoutTemplateId);
125: } else {
126: layoutTemplate = (LayoutTemplate) _getThemesCustom(
127: themeId).get(layoutTemplateId);
128: }
129:
130: if (layoutTemplate != null) {
131: return layoutTemplate;
132: }
133: }
134:
135: if (standard) {
136: layoutTemplate = (LayoutTemplate) _warStandard
137: .get(layoutTemplateId);
138:
139: if (layoutTemplate == null) {
140: layoutTemplate = (LayoutTemplate) _portalStandard
141: .get(layoutTemplateId);
142: }
143: } else {
144: layoutTemplate = (LayoutTemplate) _warCustom
145: .get(layoutTemplateId);
146:
147: if (layoutTemplate == null) {
148: layoutTemplate = (LayoutTemplate) _portalCustom
149: .get(layoutTemplateId);
150: }
151: }
152:
153: return layoutTemplate;
154: }
155:
156: public static List getLayoutTemplates() {
157: List customLayoutTemplates = new ArrayList(_portalCustom.size()
158: + _warCustom.size());
159:
160: customLayoutTemplates.addAll(ListUtil
161: .fromCollection(_portalCustom.values()));
162:
163: customLayoutTemplates.addAll(ListUtil.fromCollection(_warCustom
164: .values()));
165:
166: return customLayoutTemplates;
167: }
168:
169: public static List getLayoutTemplates(String themeId) {
170: Map _themesCustom = _getThemesCustom(themeId);
171:
172: List customLayoutTemplates = new ArrayList(_portalCustom.size()
173: + _warCustom.size() + _themesCustom.size());
174:
175: Iterator itr = _portalCustom.entrySet().iterator();
176:
177: while (itr.hasNext()) {
178: Map.Entry entry = (Map.Entry) itr.next();
179:
180: String layoutTemplateId = (String) entry.getKey();
181: LayoutTemplate layoutTemplate = (LayoutTemplate) entry
182: .getValue();
183:
184: if (_themesCustom.containsKey(layoutTemplateId)) {
185: customLayoutTemplates.add(_themesCustom
186: .get(layoutTemplateId));
187: } else if (_warCustom.containsKey(layoutTemplateId)) {
188: customLayoutTemplates.add(_warCustom
189: .get(layoutTemplateId));
190: } else {
191: customLayoutTemplates.add(layoutTemplate);
192: }
193: }
194:
195: itr = _warCustom.entrySet().iterator();
196:
197: while (itr.hasNext()) {
198: Map.Entry entry = (Map.Entry) itr.next();
199:
200: String layoutTemplateId = (String) entry.getKey();
201:
202: if (!_portalCustom.containsKey(layoutTemplateId)
203: && !_themesCustom.containsKey(layoutTemplateId)) {
204:
205: customLayoutTemplates.add(_warCustom
206: .get(layoutTemplateId));
207: }
208: }
209:
210: itr = _themesCustom.entrySet().iterator();
211:
212: while (itr.hasNext()) {
213: Map.Entry entry = (Map.Entry) itr.next();
214:
215: String layoutTemplateId = (String) entry.getKey();
216:
217: if (!_portalCustom.containsKey(layoutTemplateId)
218: && !_warCustom.containsKey(layoutTemplateId)) {
219:
220: customLayoutTemplates.add(_themesCustom
221: .get(layoutTemplateId));
222: }
223: }
224:
225: return customLayoutTemplates;
226: }
227:
228: public static String getWapContent(String layoutTemplateId,
229: boolean standard, String themeId) throws SystemException {
230:
231: LayoutTemplate layoutTemplate = getLayoutTemplate(
232: layoutTemplateId, standard, themeId);
233:
234: if (layoutTemplate == null) {
235: if (_log.isWarnEnabled()) {
236: _log.warn("Layout template " + layoutTemplateId
237: + " does not exist");
238: }
239:
240: String defaultUserLayoutTemplateId = PropsUtil
241: .get(PropsUtil.DEFAULT_USER_LAYOUT_TEMPLATE_ID);
242:
243: layoutTemplate = getLayoutTemplate(
244: defaultUserLayoutTemplateId, standard, themeId);
245:
246: if (layoutTemplate == null) {
247: _log.error("Layout template " + layoutTemplateId
248: + " and default user layout template "
249: + defaultUserLayoutTemplateId
250: + " does not exist");
251:
252: return StringPool.BLANK;
253: }
254: }
255:
256: if (GetterUtil.getBoolean(PropsUtil
257: .get(PropsUtil.LAYOUT_TEMPLATE_CACHE_ENABLED))) {
258:
259: return layoutTemplate.getWapContent();
260: } else {
261: try {
262: return layoutTemplate.getUncachedWapContent();
263: } catch (IOException ioe) {
264: throw new SystemException(ioe);
265: }
266: }
267: }
268:
269: public static List init(ServletContext ctx, String[] xmls,
270: PluginPackage pluginPackage) {
271:
272: return init(null, ctx, xmls, pluginPackage);
273: }
274:
275: public static List init(String servletContextName,
276: ServletContext ctx, String[] xmls,
277: PluginPackage pluginPackage) {
278:
279: List layoutTemplateIds = new ArrayList();
280:
281: try {
282: for (int i = 0; i < xmls.length; i++) {
283: Iterator itr = _readLayoutTemplates(servletContextName,
284: ctx, xmls[i], pluginPackage).iterator();
285:
286: while (itr.hasNext()) {
287: ObjectValuePair ovp = (ObjectValuePair) itr.next();
288:
289: if (!layoutTemplateIds.contains(ovp)) {
290: layoutTemplateIds.add(ovp);
291: }
292: }
293: }
294: } catch (Exception e) {
295: e.printStackTrace();
296: }
297:
298: return layoutTemplateIds;
299: }
300:
301: public static void readLayoutTemplate(String servletContextName,
302: ServletContext ctx, Set layoutTemplateIds, Element el,
303: boolean standard, String themeId,
304: PluginPackage pluginPackage) throws IOException {
305:
306: Map layoutTemplates = null;
307:
308: if (themeId != null) {
309: if (standard) {
310: layoutTemplates = _getThemesStandard(themeId);
311: } else {
312: layoutTemplates = _getThemesCustom(themeId);
313: }
314: } else if (servletContextName != null) {
315: if (standard) {
316: layoutTemplates = _warStandard;
317: } else {
318: layoutTemplates = _warCustom;
319: }
320: } else {
321: if (standard) {
322: layoutTemplates = _portalStandard;
323: } else {
324: layoutTemplates = _portalCustom;
325: }
326: }
327:
328: Iterator itr = el.elements("layout-template").iterator();
329:
330: while (itr.hasNext()) {
331: Element layoutTemplate = (Element) itr.next();
332:
333: String layoutTemplateId = layoutTemplate
334: .attributeValue("id");
335:
336: if (layoutTemplateIds != null) {
337: ObjectValuePair ovp = new ObjectValuePair(
338: layoutTemplateId, Boolean.valueOf(standard));
339:
340: layoutTemplateIds.add(ovp);
341: }
342:
343: LayoutTemplate layoutTemplateModel = (LayoutTemplate) layoutTemplates
344: .get(layoutTemplateId);
345:
346: if (layoutTemplateModel == null) {
347: layoutTemplateModel = new LayoutTemplateImpl(
348: layoutTemplateId);
349:
350: layoutTemplates.put(layoutTemplateId,
351: layoutTemplateModel);
352: }
353:
354: PluginSetting pluginSetting = PluginSettingLocalServiceUtil
355: .getDefaultPluginSetting();
356:
357: layoutTemplateModel.setPluginPackage(pluginPackage);
358: layoutTemplateModel.setServletContext(ctx);
359:
360: if (servletContextName != null) {
361: layoutTemplateModel
362: .setServletContextName(servletContextName);
363: }
364:
365: layoutTemplateModel.setStandard(standard);
366: layoutTemplateModel.setName(GetterUtil.getString(
367: layoutTemplate.attributeValue("name"),
368: layoutTemplateModel.getName()));
369: layoutTemplateModel.setTemplatePath(GetterUtil.getString(
370: layoutTemplate.elementText("template-path"),
371: layoutTemplateModel.getTemplatePath()));
372: layoutTemplateModel.setWapTemplatePath(GetterUtil
373: .getString(layoutTemplate
374: .elementText("wap-template-path"),
375: layoutTemplateModel.getWapTemplatePath()));
376: layoutTemplateModel.setThumbnailPath(GetterUtil.getString(
377: layoutTemplate.elementText("thumbnail-path"),
378: layoutTemplateModel.getThumbnailPath()));
379:
380: String content = null;
381:
382: try {
383: content = Http.URLtoString(ctx
384: .getResource(layoutTemplateModel
385: .getTemplatePath()));
386: } catch (Exception e) {
387: _log.error("Unable to get content at template path "
388: + layoutTemplateModel.getTemplatePath() + ": "
389: + e.getMessage());
390: }
391:
392: if (Validator.isNull(content)) {
393: _log.error("No content found at template path "
394: + layoutTemplateModel.getTemplatePath());
395: } else {
396: layoutTemplateModel.setContent(content);
397: layoutTemplateModel.setColumns(_getColumns(content));
398: }
399:
400: if (Validator.isNull(layoutTemplateModel
401: .getWapTemplatePath())) {
402: _log
403: .error("The element wap-template-path is not defined for "
404: + layoutTemplateId);
405: } else {
406: String wapContent = null;
407:
408: try {
409: wapContent = Http.URLtoString(ctx
410: .getResource(layoutTemplateModel
411: .getWapTemplatePath()));
412: } catch (Exception e) {
413: _log
414: .error("Unable to get content at WAP template path "
415: + layoutTemplateModel
416: .getWapTemplatePath()
417: + ": " + e.getMessage());
418: }
419:
420: if (Validator.isNull(wapContent)) {
421: _log.error("No content found at WAP template path "
422: + layoutTemplateModel.getWapTemplatePath());
423: } else {
424: layoutTemplateModel.setWapContent(wapContent);
425: }
426: }
427:
428: Element rolesEl = layoutTemplate.element("roles");
429:
430: if (rolesEl != null) {
431: Iterator itr2 = rolesEl.elements("role-name")
432: .iterator();
433:
434: while (itr2.hasNext()) {
435: Element roleNameEl = (Element) itr2.next();
436:
437: pluginSetting.addRole(roleNameEl.getText());
438: }
439: }
440:
441: layoutTemplateModel.setDefaultPluginSetting(pluginSetting);
442: }
443: }
444:
445: public static void uninstallLayoutTemplate(String layoutTemplateId,
446: boolean standard) {
447:
448: if (standard) {
449: _warStandard.remove(layoutTemplateId);
450: } else {
451: _warCustom.remove(layoutTemplateId);
452: }
453: }
454:
455: public static void uninstallLayoutTemplates(String themeId) {
456: _getThemesStandard(themeId).clear();
457: _getThemesCustom(themeId).clear();
458: }
459:
460: private static List _getColumns(String content) {
461: try {
462: InitColumnProcessor processor = new InitColumnProcessor();
463:
464: VelocityContext context = new VelocityContext();
465:
466: context.put("processor", processor);
467:
468: Velocity.evaluate(context, new PrintWriter(
469: new StringWriter()), LayoutTemplateLocalUtil.class
470: .getName(), content);
471:
472: List columns = processor.getColumns();
473:
474: Collections.sort(columns);
475:
476: return columns;
477: } catch (Exception e) {
478: _log.error(e);
479:
480: return new ArrayList();
481: }
482: }
483:
484: private static Set _readLayoutTemplates(String servletContextName,
485: ServletContext ctx, String xml, PluginPackage pluginPackage)
486: throws DocumentException, IOException {
487:
488: Set layoutTemplateIds = new HashSet();
489:
490: if (xml == null) {
491: return layoutTemplateIds;
492: }
493:
494: Document doc = PortalUtil.readDocumentFromXML(xml, true);
495:
496: Element root = doc.getRootElement();
497:
498: Element standardEl = root.element("standard");
499:
500: if (standardEl != null) {
501: readLayoutTemplate(servletContextName, ctx,
502: layoutTemplateIds, standardEl, true, null,
503: pluginPackage);
504: }
505:
506: Element customEl = root.element("custom");
507:
508: if (customEl != null) {
509: readLayoutTemplate(servletContextName, ctx,
510: layoutTemplateIds, customEl, false, null,
511: pluginPackage);
512: }
513:
514: return layoutTemplateIds;
515: }
516:
517: private static Map _getThemesCustom(String themeId) {
518: String key = themeId + _CUSTOM_SEPARATOR;
519:
520: Map layoutTemplates = (Map) _themes.get(key);
521:
522: if (layoutTemplates == null) {
523: layoutTemplates = new LinkedHashMap();
524:
525: _themes.put(key, layoutTemplates);
526: }
527:
528: return layoutTemplates;
529: }
530:
531: private static Map _getThemesStandard(String themeId) {
532: String key = themeId + _STANDARD_SEPARATOR;
533:
534: Map layoutTemplates = (Map) _themes.get(key);
535:
536: if (layoutTemplates == null) {
537: layoutTemplates = new LinkedHashMap();
538:
539: _themes.put(key, layoutTemplates);
540: }
541:
542: return layoutTemplates;
543: }
544:
545: private static final String _STANDARD_SEPARATOR = "_STANDARD_";
546:
547: private static final String _CUSTOM_SEPARATOR = "_CUSTOM_";
548:
549: private static Log _log = LogFactory
550: .getLog(LayoutTemplateLocalUtil.class);
551:
552: private static Map _portalStandard = new LinkedHashMap();
553: private static Map _portalCustom = new LinkedHashMap();
554:
555: private static Map _warStandard = new LinkedHashMap();
556: private static Map _warCustom = new LinkedHashMap();
557:
558: private static Map _themes = new LinkedHashMap();
559:
560: }
|