001    /**
002     * Copyright (c) 2000-present 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.taglib.ui;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.MapUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.service.LayoutLocalServiceUtil;
027    import com.liferay.taglib.ui.util.SessionTreeJSClicks;
028    import com.liferay.taglib.util.IncludeTag;
029    
030    import java.util.ArrayList;
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    import javax.portlet.PortletURL;
036    
037    import javax.servlet.http.HttpServletRequest;
038    
039    /**
040     * @author Eudaldo Alonso
041     */
042    public class LayoutsTreeTag extends IncludeTag {
043    
044            @Override
045            public int doStartTag() {
046                    return EVAL_BODY_INCLUDE;
047            }
048    
049            public void setCheckContentDisplayPage(boolean checkContentDisplayPage) {
050                    _checkContentDisplayPage = checkContentDisplayPage;
051            }
052    
053            public void setDefaultStateChecked(boolean defaultStateChecked) {
054                    _defaultStateChecked = defaultStateChecked;
055            }
056    
057            public void setDraggableTree(boolean draggableTree) {
058                    _draggableTree = draggableTree;
059            }
060    
061            public void setExpandFirstNode(boolean expandFirstNode) {
062                    _expandFirstNode = expandFirstNode;
063            }
064    
065            public void setGroupId(long groupId) {
066                    _groupId = groupId;
067            }
068    
069            public void setIncomplete(boolean incomplete) {
070                    _incomplete = incomplete;
071            }
072    
073            public void setLinkTemplate(String linkTemplate) {
074                    _linkTemplate = linkTemplate;
075            }
076    
077            public void setPortletURL(PortletURL portletURL) {
078                    _portletURL = portletURL;
079            }
080    
081            public void setPortletURLs(Map<String, PortletURL> portletURLs) {
082                    _portletURLs = portletURLs;
083            }
084    
085            public void setPrivateLayout(boolean privateLayout) {
086                    _privateLayout = privateLayout;
087            }
088    
089            public void setRootLinkTemplate(String rootLinkTemplate) {
090                    _rootLinkTemplate = rootLinkTemplate;
091            }
092    
093            public void setRootNodeName(String rootNodeName) {
094                    _rootNodeName = rootNodeName;
095            }
096    
097            public void setSaveState(boolean saveState) {
098                    _saveState = saveState;
099            }
100    
101            public void setSelectableTree(boolean selectableTree) {
102                    _selectableTree = selectableTree;
103            }
104    
105            public void setSelectedLayoutIds(String selectedLayoutIds) {
106                    _selectedLayoutIds = selectedLayoutIds;
107            }
108    
109            public void setSelPlid(Long selPlid) {
110                    _selPlid = selPlid;
111            }
112    
113            public void setTreeId(String treeId) {
114                    _treeId = treeId;
115            }
116    
117            @Override
118            protected void cleanUp() {
119                    _checkContentDisplayPage = false;
120                    _defaultStateChecked = false;
121                    _draggableTree = true;
122                    _expandFirstNode = true;
123                    _groupId = 0;
124                    _incomplete = true;
125                    _linkTemplate = null;
126                    _portletURL = null;
127                    _portletURLs = null;
128                    _privateLayout = false;
129                    _rootLinkTemplate = null;
130                    _rootNodeName = null;
131                    _saveState = true;
132                    _selectableTree = false;
133                    _selectedLayoutIds = null;
134                    _selPlid = null;
135                    _treeId = null;
136            }
137    
138            protected String getCheckedNodes() {
139                    JSONArray checkedNodesJSONArray = JSONFactoryUtil.createJSONArray();
140    
141                    String checkedLayoutIds = GetterUtil.getString(
142                            _selectedLayoutIds,
143                            SessionTreeJSClicks.getOpenNodes(
144                                    request, _treeId + "SelectedNode"));
145    
146                    if (Validator.isNull(checkedLayoutIds)) {
147                            return checkedNodesJSONArray.toString();
148                    }
149    
150                    for (long checkedLayoutId : StringUtil.split(checkedLayoutIds, 0L)) {
151                            Layout checkedLayout = LayoutLocalServiceUtil.fetchLayout(
152                                    _groupId, _privateLayout, checkedLayoutId);
153    
154                            if (checkedLayout != null) {
155                                    checkedNodesJSONArray.put(
156                                            String.valueOf(checkedLayout.getPlid()));
157                            }
158                    }
159    
160                    return checkedNodesJSONArray.toString();
161            }
162    
163            protected String getModules() {
164                    List<String> modules = new ArrayList<>();
165    
166                    modules.add("liferay-layouts-tree");
167    
168                    if (_selectableTree) {
169                            modules.add("liferay-layouts-tree-selectable");
170                    }
171    
172                    if (_checkContentDisplayPage) {
173                            modules.add("liferay-layouts-tree-check-content-display-page");
174                    }
175    
176                    if (_saveState) {
177                            modules.add("liferay-layouts-tree-state");
178                    }
179    
180                    return StringUtil.merge(modules);
181            }
182    
183            @Override
184            protected String getPage() {
185                    return _PAGE;
186            }
187    
188            protected JSONArray getPortletURLsJSONArray(
189                    Map<String, PortletURL> portletURLs) {
190    
191                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
192    
193                    if (MapUtil.isEmpty(portletURLs)) {
194                            return jsonArray;
195                    }
196    
197                    for (String name : portletURLs.keySet()) {
198                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
199    
200                            jsonObject.put("name", name);
201    
202                            PortletURL portletURL = portletURLs.get(name);
203    
204                            portletURL.setParameter("selPlid", "{selPlid}");
205    
206                            jsonObject.put(
207                                    "value",
208                                    StringUtil.replace(
209                                            portletURL.toString(), HttpUtil.encodePath("{selPlid}"),
210                                            "{selPlid}"));
211    
212                            jsonArray.put(jsonObject);
213                    }
214    
215                    return jsonArray;
216            }
217    
218            @Override
219            protected boolean isCleanUpSetAttributes() {
220                    return _CLEAN_UP_SET_ATTRIBUTES;
221            }
222    
223            @Override
224            protected void setAttributes(HttpServletRequest request) {
225                    request.setAttribute(
226                            "liferay-ui:layouts-tree:checkedNodes", getCheckedNodes());
227                    request.setAttribute(
228                            "liferay-ui:layouts-tree:checkContentDisplayPage",
229                            String.valueOf(_checkContentDisplayPage));
230                    request.setAttribute(
231                            "liferay-ui:layouts-tree:defaultStateChecked",
232                            String.valueOf(_defaultStateChecked));
233                    request.setAttribute(
234                            "liferay-ui:layouts-tree:draggableTree",
235                            String.valueOf(_draggableTree));
236                    request.setAttribute(
237                            "liferay-ui:layouts-tree:expandFirstNode",
238                            String.valueOf(_expandFirstNode));
239                    request.setAttribute(
240                            "liferay-ui:layouts-tree:groupId", String.valueOf(_groupId));
241                    request.setAttribute(
242                            "liferay-ui:layouts-tree:incomplete", String.valueOf(_incomplete));
243                    request.setAttribute(
244                            "liferay-ui:layouts-tree:linkTemplate",
245                            String.valueOf(_linkTemplate));
246                    request.setAttribute("liferay-ui:layouts-tree:modules", getModules());
247    
248                    Map<String, PortletURL> portletURLs = _portletURLs;
249    
250                    if (_portletURL != null) {
251                            if (portletURLs == null) {
252                                    portletURLs = new HashMap<>();
253                            }
254    
255                            portletURLs.put("layoutURL", _portletURL);
256                    }
257    
258                    request.setAttribute(
259                            "liferay-ui:layouts-tree:portletURLs", portletURLs);
260                    request.setAttribute(
261                            "liferay-ui:layouts-tree:portletURLsJSONArray",
262                            getPortletURLsJSONArray(portletURLs));
263    
264                    request.setAttribute(
265                            "liferay-ui:layouts-tree:privateLayout",
266                            String.valueOf(_privateLayout));
267                    request.setAttribute(
268                            "liferay-ui:layouts-tree:rootLinkTemplate", _rootLinkTemplate);
269                    request.setAttribute(
270                            "liferay-ui:layouts-tree:rootNodeName", _rootNodeName);
271                    request.setAttribute(
272                            "liferay-ui:layouts-tree:saveState", String.valueOf(_saveState));
273                    request.setAttribute(
274                            "liferay-ui:layouts-tree:selectableTree",
275                            String.valueOf(_selectableTree));
276                    request.setAttribute(
277                            "liferay-ui:layouts-tree:selectedLayoutIds", _selectedLayoutIds);
278                    request.setAttribute("liferay-ui:layouts-tree:selPlid", _selPlid);
279                    request.setAttribute("liferay-ui:layouts-tree:treeId", _treeId);
280            }
281    
282            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
283    
284            private static final String _PAGE = "/html/taglib/ui/layouts_tree/page.jsp";
285    
286            private boolean _checkContentDisplayPage;
287            private boolean _defaultStateChecked;
288            private boolean _draggableTree = true;
289            private boolean _expandFirstNode = true;
290            private long _groupId;
291            private boolean _incomplete = true;
292            private String _linkTemplate;
293            private PortletURL _portletURL;
294            private Map<String, PortletURL> _portletURLs;
295            private boolean _privateLayout;
296            private String _rootLinkTemplate;
297            private String _rootNodeName;
298            private boolean _saveState = true;
299            private boolean _selectableTree;
300            private String _selectedLayoutIds;
301            private Long _selPlid;
302            private String _treeId;
303    
304    }