001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.layoutconfiguration.util.velocity;
016    
017    import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.MethodHandler;
020    import com.liferay.portal.kernel.util.MethodKey;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.UnicodeProperties;
023    import com.liferay.portal.model.CustomizedPages;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portlet.sites.util.SitesUtil;
026    
027    import java.io.Writer;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.jsp.PageContext;
031    import javax.servlet.jsp.tagext.Tag;
032    
033    /**
034     * @author Raymond Augé
035     */
036    public class CustomizationSettingsProcessor implements ColumnProcessor {
037    
038            public CustomizationSettingsProcessor(
039                    HttpServletRequest request, PageContext pageContext, Writer writer) {
040    
041                    _pageContext = pageContext;
042                    _request = request;
043                    _writer = writer;
044    
045                    Layout selLayout = (Layout)_request.getAttribute(
046                            "edit_pages.jsp-selLayout");
047    
048                    _layoutTypeSettings = selLayout.getTypeSettingsProperties();
049    
050                    _customizationEnabled = true;
051    
052                    if (!SitesUtil.isLayoutUpdateable(selLayout)) {
053                            _customizationEnabled = false;
054                    }
055    
056                    if (selLayout.isLayoutPrototypeLinkActive()) {
057                            _customizationEnabled = false;
058                    }
059            }
060    
061            public String processColumn(String columnId) throws Exception {
062                    return processColumn(columnId, StringPool.BLANK);
063            }
064    
065            public String processColumn(String columnId, String classNames)
066                    throws Exception {
067    
068                    String customizableKey = CustomizedPages.namespaceColumnId(columnId);
069    
070                    boolean customizable = false;
071    
072                    if (_customizationEnabled) {
073                            customizable = GetterUtil.getBoolean(
074                                    _layoutTypeSettings.getProperty(
075                                            customizableKey, String.valueOf(false)));
076                    }
077    
078                    _writer.append("<div class=\"");
079                    _writer.append(classNames);
080                    _writer.append("\">");
081    
082                    _writer.append("<h1>");
083                    _writer.append(columnId);
084                    _writer.append("</h1>");
085    
086                    Object inputTag = _inputTagClass.newInstance();
087    
088                    BeanPropertiesUtil.setProperty(
089                            inputTag, "disabled", !_customizationEnabled);
090                    BeanPropertiesUtil.setProperty(inputTag, "label", "customizable");
091                    BeanPropertiesUtil.setProperty(
092                            inputTag, "name",
093                            "TypeSettingsProperties--".concat(customizableKey).concat("--"));
094                    BeanPropertiesUtil.setProperty(inputTag, "pageContext", _pageContext);
095                    BeanPropertiesUtil.setProperty(inputTag, "type", "checkbox");
096                    BeanPropertiesUtil.setProperty(inputTag, "value", customizable);
097    
098                    MethodHandler doEndMethodHandler = new MethodHandler(
099                            _doEndTagMethodKey);
100                    MethodHandler doStartMethodHandler = new MethodHandler(
101                            _doStartTagMethodKey);
102    
103                    int result = (Integer)doStartMethodHandler.invoke(inputTag);
104    
105                    if (result == Tag.EVAL_BODY_INCLUDE) {
106                            doEndMethodHandler.invoke(inputTag);
107                    }
108    
109                    _writer.append("</div>");
110    
111                    return StringPool.BLANK;
112            }
113    
114            public String processMax() throws Exception {
115                    return processMax(StringPool.BLANK);
116            }
117    
118            public String processMax(String classNames) throws Exception {
119                    return StringPool.BLANK;
120            }
121    
122            public String processPortlet(String portletId) throws Exception {
123                    _writer.append("<div class=\"portlet\">");
124                    _writer.append(portletId);
125                    _writer.append("</div>");
126    
127                    return StringPool.BLANK;
128            }
129    
130            private static MethodKey _doEndTagMethodKey = new MethodKey(
131                    "com.liferay.taglib.aui.InputTag", "doEndTag");
132            private static MethodKey _doStartTagMethodKey = new MethodKey(
133                    "com.liferay.taglib.aui.InputTag", "doStartTag");
134            private static Class<?> _inputTagClass;
135    
136            private boolean _customizationEnabled;
137            private UnicodeProperties _layoutTypeSettings;
138            private PageContext _pageContext;
139            private HttpServletRequest _request;
140            private Writer _writer;
141    
142            static {
143                    try {
144                            _inputTagClass = Class.forName("com.liferay.taglib.aui.InputTag");
145                    }
146                    catch (ClassNotFoundException e) {
147                    }
148            }
149    
150    }