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