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