001    /**
002     * Copyright (c) 2000-2013 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.portal.editor.fckeditor.receiver.impl;
016    
017    import com.liferay.portal.NoSuchLayoutException;
018    import com.liferay.portal.editor.fckeditor.command.CommandArgument;
019    import com.liferay.portal.editor.fckeditor.exception.FCKException;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.model.LayoutConstants;
024    import com.liferay.portal.service.LayoutLocalServiceUtil;
025    import com.liferay.portal.service.LayoutServiceUtil;
026    import com.liferay.portal.util.PortalUtil;
027    
028    import java.io.InputStream;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    import org.w3c.dom.Document;
034    import org.w3c.dom.Element;
035    import org.w3c.dom.Node;
036    
037    /**
038     * @author Ivica Cardic
039     */
040    public class PageCommandReceiver extends BaseCommandReceiver {
041    
042            @Override
043            protected String createFolder(CommandArgument commandArgument) {
044                    return "0";
045            }
046    
047            @Override
048            protected String fileUpload(
049                    CommandArgument commandArgument, String fileName,
050                    InputStream inputStream, String extension, long size) {
051    
052                    return "0";
053            }
054    
055            @Override
056            protected void getFolders(
057                    CommandArgument commandArgument, Document document, Node rootNode) {
058    
059                    try {
060                            _getFolders(commandArgument, document, rootNode);
061                    }
062                    catch (Exception e) {
063                            throw new FCKException(e);
064                    }
065            }
066    
067            @Override
068            protected void getFoldersAndFiles(
069                    CommandArgument commandArgument, Document document, Node rootNode) {
070    
071                    try {
072                            _getFolders(commandArgument, document, rootNode);
073                            _getFiles(commandArgument, document, rootNode);
074                    }
075                    catch (Exception e) {
076                            throw new FCKException(e);
077                    }
078            }
079    
080            private void _getFiles(
081                            CommandArgument commandArgument, Document document, Node rootNode)
082                    throws Exception {
083    
084                    if (commandArgument.getCurrentFolder().equals(StringPool.SLASH)) {
085                            return;
086                    }
087    
088                    Element filesElement = document.createElement("Files");
089    
090                    rootNode.appendChild(filesElement);
091    
092                    Group group = commandArgument.getCurrentGroup();
093    
094                    List<Layout> layouts = new ArrayList<Layout>();
095    
096                    layouts.addAll(
097                            LayoutServiceUtil.getLayouts(
098                                    group.getGroupId(), false,
099                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID));
100    
101                    layouts.addAll(
102                            LayoutServiceUtil.getLayouts(
103                                    group.getGroupId(), true,
104                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID));
105    
106                    if (("/" + commandArgument.getCurrentGroupName() + "/").equals(
107                                    commandArgument.getCurrentFolder())) {
108    
109                            for (Layout layout : layouts) {
110                                    Element fileElement = document.createElement("File");
111    
112                                    filesElement.appendChild(fileElement);
113    
114                                    fileElement.setAttribute("name", _getLayoutName(layout));
115                                    fileElement.setAttribute("desc", _getLayoutName(layout));
116                                    fileElement.setAttribute("size", StringPool.BLANK);
117                                    fileElement.setAttribute(
118                                            "url",
119                                            PortalUtil.getLayoutURL(
120                                                    layout, commandArgument.getThemeDisplay(), false));
121                            }
122                    }
123                    else {
124                            String layoutName = _getLayoutName(
125                                    commandArgument.getCurrentFolder());
126    
127                            Layout layout = _getLayout(group.getGroupId(), layoutName);
128    
129                            if (layout == null) {
130                                    return;
131                            }
132    
133                            List<Layout> layoutChildren = layout.getChildren();
134    
135                            for (int i = 0; i < layoutChildren.size(); i++) {
136                                    layout = layoutChildren.get(i);
137    
138                                    Element fileElement = document.createElement("File");
139    
140                                    filesElement.appendChild(fileElement);
141    
142                                    fileElement.setAttribute("name", _getLayoutName(layout));
143                                    fileElement.setAttribute("desc", _getLayoutName(layout));
144                                    fileElement.setAttribute("size", getSize());
145                                    fileElement.setAttribute(
146                                            "url",
147                                            PortalUtil.getLayoutURL(
148                                                    layout, commandArgument.getThemeDisplay(), false));
149                            }
150                    }
151            }
152    
153            private void _getFolders(
154                            CommandArgument commandArgument, Document document, Node rootNode)
155                    throws Exception {
156    
157                    Element foldersElement = document.createElement("Folders");
158    
159                    rootNode.appendChild(foldersElement);
160    
161                    if (commandArgument.getCurrentFolder().equals(StringPool.SLASH)) {
162                            getRootFolders(commandArgument, document, foldersElement);
163                    }
164                    else {
165                            Group group = commandArgument.getCurrentGroup();
166    
167                            List<Layout> layouts = new ArrayList<Layout>();
168    
169                            layouts.addAll(
170                                    LayoutServiceUtil.getLayouts(
171                                            group.getGroupId(), false,
172                                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID));
173    
174                            layouts.addAll(
175                                    LayoutServiceUtil.getLayouts(
176                                            group.getGroupId(), true,
177                                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID));
178    
179                            if (("/" + commandArgument.getCurrentGroupName() + "/").equals(
180                                            commandArgument.getCurrentFolder())) {
181    
182                                    for (Layout layout : layouts) {
183                                            Element folderElement = document.createElement("Folder");
184    
185                                            foldersElement.appendChild(folderElement);
186    
187                                            folderElement.setAttribute(
188                                                    "name", "~" + _getLayoutName(layout).replace('/', '>'));
189                                    }
190                            }
191                            else {
192                                    String layoutName = _getLayoutName(
193                                            commandArgument.getCurrentFolder());
194    
195                                    Layout layout = _getLayout(group.getGroupId(), layoutName);
196    
197                                    if (layout != null) {
198                                            List<Layout> layoutChildren = layout.getChildren();
199    
200                                            for (int i = 0; i < layoutChildren.size(); i++) {
201                                                    layout = layoutChildren.get(i);
202    
203                                                    Element folderElement = document.createElement(
204                                                            "Folder");
205    
206                                                    foldersElement.appendChild(folderElement);
207    
208                                                    folderElement.setAttribute(
209                                                            "name",
210                                                            "~" + _getLayoutName(layout).replace('/', '>'));
211                                            }
212                                    }
213                            }
214                    }
215            }
216    
217            private Layout _getLayout(long groupId, String layoutName)
218                    throws Exception {
219    
220                    Layout layout = null;
221    
222                    try {
223                            layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
224                                    groupId, false, layoutName);
225    
226                            return layout;
227                    }
228                    catch (NoSuchLayoutException nsle) {
229                    }
230    
231                    try {
232                            layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
233                                    groupId, true, layoutName);
234    
235                            return layout;
236                    }
237                    catch (NoSuchLayoutException nsle) {
238                    }
239    
240                    return null;
241            }
242    
243            private String _getLayoutName(Layout layout) {
244                    return layout.getFriendlyURL();
245            }
246    
247            private String _getLayoutName(String folderName) {
248                    String layoutName = folderName.substring(
249                            folderName.lastIndexOf('~') + 1, folderName.length() - 1);
250    
251                    layoutName = layoutName.replace('>', '/');
252    
253                    return layoutName;
254            }
255    
256    }