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.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.service.LayoutLocalServiceUtil;
024    import com.liferay.taglib.ui.util.SessionTreeJSClicks;
025    import com.liferay.taglib.util.IncludeTag;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    import javax.portlet.PortletURL;
031    
032    import javax.servlet.http.HttpServletRequest;
033    
034    /**
035     * @author Eudaldo Alonso
036     */
037    public class LayoutsTreeTag extends IncludeTag {
038    
039            @Override
040            public int doStartTag() {
041                    return EVAL_BODY_INCLUDE;
042            }
043    
044            public void setCheckContentDisplayPage(boolean checkContentDisplayPage) {
045                    _checkContentDisplayPage = checkContentDisplayPage;
046            }
047    
048            public void setDefaultStateChecked(boolean defaultStateChecked) {
049                    _defaultStateChecked = defaultStateChecked;
050            }
051    
052            public void setDraggableTree(boolean draggableTree) {
053                    _draggableTree = draggableTree;
054            }
055    
056            public void setExpandFirstNode(boolean expandFirstNode) {
057                    _expandFirstNode = expandFirstNode;
058            }
059    
060            public void setGroupId(long groupId) {
061                    _groupId = groupId;
062            }
063    
064            public void setIncomplete(boolean incomplete) {
065                    _incomplete = incomplete;
066            }
067    
068            public void setPortletURL(PortletURL portletURL) {
069                    _portletURL = portletURL;
070            }
071    
072            public void setPrivateLayout(boolean privateLayout) {
073                    _privateLayout = privateLayout;
074            }
075    
076            public void setRootNodeName(String rootNodeName) {
077                    _rootNodeName = rootNodeName;
078            }
079    
080            public void setSaveState(boolean saveState) {
081                    _saveState = saveState;
082            }
083    
084            public void setSelectableTree(boolean selectableTree) {
085                    _selectableTree = selectableTree;
086            }
087    
088            public void setSelectedLayoutIds(String selectedLayoutIds) {
089                    _selectedLayoutIds = selectedLayoutIds;
090            }
091    
092            public void setSelPlid(long selPlid) {
093                    _selPlid = selPlid;
094            }
095    
096            public void setTreeId(String treeId) {
097                    _treeId = treeId;
098            }
099    
100            @Override
101            protected void cleanUp() {
102                    _checkContentDisplayPage = false;
103                    _defaultStateChecked = false;
104                    _draggableTree = true;
105                    _expandFirstNode = true;
106                    _groupId = 0;
107                    _incomplete = true;
108                    _portletURL = null;
109                    _privateLayout = false;
110                    _rootNodeName = null;
111                    _saveState = true;
112                    _selectableTree = false;
113                    _selectedLayoutIds = null;
114                    _selPlid = 0;
115                    _treeId = null;
116            }
117    
118            protected String getCheckedNodes() {
119                    JSONArray checkedNodesJSONArray = JSONFactoryUtil.createJSONArray();
120    
121                    String checkedLayoutIds = GetterUtil.getString(
122                            _selectedLayoutIds,
123                            SessionTreeJSClicks.getOpenNodes(
124                                    request, _treeId + "SelectedNode"));
125    
126                    if (Validator.isNull(checkedLayoutIds)) {
127                            return checkedNodesJSONArray.toString();
128                    }
129    
130                    for (long checkedLayoutId : StringUtil.split(checkedLayoutIds, 0L)) {
131                            Layout checkedLayout = LayoutLocalServiceUtil.fetchLayout(
132                                    _groupId, _privateLayout, checkedLayoutId);
133    
134                            if (checkedLayout != null) {
135                                    checkedNodesJSONArray.put(
136                                            String.valueOf(checkedLayout.getPlid()));
137                            }
138                    }
139    
140                    return checkedNodesJSONArray.toString();
141            }
142    
143            protected String getModules() {
144                    List<String> modules = new ArrayList<>();
145    
146                    modules.add("liferay-layouts-tree");
147    
148                    if (_selectableTree) {
149                            modules.add("liferay-layouts-tree-selectable");
150                    }
151    
152                    if (_checkContentDisplayPage) {
153                            modules.add("liferay-layouts-tree-check-content-display-page");
154                    }
155    
156                    if (_saveState) {
157                            modules.add("liferay-layouts-tree-state");
158                    }
159    
160                    return StringUtil.merge(modules);
161            }
162    
163            @Override
164            protected String getPage() {
165                    return _PAGE;
166            }
167    
168            @Override
169            protected boolean isCleanUpSetAttributes() {
170                    return _CLEAN_UP_SET_ATTRIBUTES;
171            }
172    
173            @Override
174            protected void setAttributes(HttpServletRequest request) {
175                    request.setAttribute(
176                            "liferay-ui:layouts-tree:checkedNodes", getCheckedNodes());
177                    request.setAttribute(
178                            "liferay-ui:layouts-tree:checkContentDisplayPage",
179                            String.valueOf(_checkContentDisplayPage));
180                    request.setAttribute(
181                            "liferay-ui:layouts-tree:defaultStateChecked",
182                            String.valueOf(_defaultStateChecked));
183                    request.setAttribute(
184                            "liferay-ui:layouts-tree:draggableTree",
185                            String.valueOf(_draggableTree));
186                    request.setAttribute(
187                            "liferay-ui:layouts-tree:expandFirstNode",
188                            String.valueOf(_expandFirstNode));
189                    request.setAttribute(
190                            "liferay-ui:layouts-tree:groupId", String.valueOf(_groupId));
191                    request.setAttribute(
192                            "liferay-ui:layouts-tree:incomplete", String.valueOf(_incomplete));
193                    request.setAttribute("liferay-ui:layouts-tree:modules", getModules());
194                    request.setAttribute("liferay-ui:layouts-tree:portletURL", _portletURL);
195                    request.setAttribute(
196                            "liferay-ui:layouts-tree:privateLayout",
197                            String.valueOf(_privateLayout));
198                    request.setAttribute(
199                            "liferay-ui:layouts-tree:rootNodeName", _rootNodeName);
200                    request.setAttribute(
201                            "liferay-ui:layouts-tree:saveState", String.valueOf(_saveState));
202                    request.setAttribute(
203                            "liferay-ui:layouts-tree:selectableTree",
204                            String.valueOf(_selectableTree));
205                    request.setAttribute(
206                            "liferay-ui:layouts-tree:selectedLayoutIds", _selectedLayoutIds);
207                    request.setAttribute(
208                            "liferay-ui:layouts-tree:selPlid", String.valueOf(_selPlid));
209                    request.setAttribute("liferay-ui:layouts-tree:treeId", _treeId);
210            }
211    
212            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
213    
214            private static final String _PAGE = "/html/taglib/ui/layouts_tree/page.jsp";
215    
216            private boolean _checkContentDisplayPage;
217            private boolean _defaultStateChecked;
218            private boolean _draggableTree = true;
219            private boolean _expandFirstNode = true;
220            private long _groupId;
221            private boolean _incomplete = true;
222            private PortletURL _portletURL;
223            private boolean _privateLayout;
224            private String _rootNodeName;
225            private boolean _saveState = true;
226            private boolean _selectableTree;
227            private String _selectedLayoutIds;
228            private long _selPlid;
229            private String _treeId;
230    
231    }