1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.editor.fckeditor.receiver.impl;
24  
25  import com.liferay.portal.editor.fckeditor.command.CommandArgument;
26  import com.liferay.portal.editor.fckeditor.exception.FCKException;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.model.impl.LayoutImpl;
31  import com.liferay.portal.service.LayoutLocalServiceUtil;
32  import com.liferay.portal.util.PortalUtil;
33  
34  import java.io.File;
35  
36  import java.util.List;
37  
38  import org.w3c.dom.Document;
39  import org.w3c.dom.Element;
40  import org.w3c.dom.Node;
41  
42  /**
43   * <a href="PageCommandReceiver.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Ivica Cardic
46   *
47   */
48  public class PageCommandReceiver extends BaseCommandReceiver {
49  
50      protected String createFolder(CommandArgument arg) {
51          return "0";
52      }
53  
54      protected String fileUpload(
55          CommandArgument arg, String fileName, File file, String extension) {
56  
57          return "0";
58      }
59  
60      protected void getFolders(CommandArgument arg, Document doc, Node root) {
61          try {
62              _getFolders(arg, doc, root);
63          }
64          catch (Exception e) {
65              throw new FCKException(e);
66          }
67      }
68  
69      protected void getFoldersAndFiles(
70          CommandArgument arg, Document doc, Node root) {
71  
72          try {
73              _getFolders(arg, doc, root);
74              _getFiles(arg, doc, root);
75          }
76          catch (Exception e) {
77              throw new FCKException(e);
78          }
79      }
80  
81      private Layout _getLayout(String layoutName, Layout layout)
82          throws Exception {
83  
84          String friendlyURL = layout.getFriendlyURL();
85  
86          if (layoutName.equals(friendlyURL)) {
87              return layout;
88          }
89  
90          List<Layout> layoutChildren = layout.getChildren();
91  
92          if (layoutChildren.size() == 0) {
93              return null;
94          }
95          else {
96              for (Layout layoutChild : layoutChildren) {
97                  Layout currentLayout = _getLayout(layoutName, layoutChild);
98  
99                  if (currentLayout != null) {
100                     return currentLayout;
101                 }
102             }
103         }
104 
105         return null;
106     }
107 
108     private String _getLayoutName(Layout layout) {
109         return layout.getFriendlyURL();
110     }
111 
112     private String _getLayoutName(String folderName) {
113         String layoutName = folderName.substring(
114             folderName.lastIndexOf('~') + 1, folderName.length() - 1);
115 
116         layoutName = layoutName.replace('>', '/');
117 
118         return layoutName;
119     }
120 
121     private void _getFiles(CommandArgument arg, Document doc, Node root)
122         throws Exception {
123 
124         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
125             return;
126         }
127 
128         Element filesEl = doc.createElement("Files");
129 
130         root.appendChild(filesEl);
131 
132         Group group = arg.getCurrentGroup();
133 
134         List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
135             group.getGroupId(), false, LayoutImpl.DEFAULT_PARENT_LAYOUT_ID);
136 
137         if (("/" + arg.getCurrentGroupName() + "/").equals(
138                 arg.getCurrentFolder())) {
139 
140             for (Layout layout : layouts) {
141                 Element fileEl = doc.createElement("File");
142 
143                 filesEl.appendChild(fileEl);
144 
145                 fileEl.setAttribute("name", _getLayoutName(layout));
146                 fileEl.setAttribute("desc", _getLayoutName(layout));
147                 fileEl.setAttribute("size", StringPool.BLANK);
148                 fileEl.setAttribute(
149                     "url",
150                     PortalUtil.getLayoutURL(
151                         layout,arg.getThemeDisplay(), false));
152             }
153         }
154         else {
155             String layoutName = _getLayoutName(arg.getCurrentFolder());
156 
157             Layout layout = null;
158 
159             for (int i = 0; i < layouts.size(); i++) {
160                 layout = _getLayout(layoutName, layouts.get(i));
161 
162                 if (layout != null) {
163                     break;
164                 }
165             }
166 
167             if (layout == null) {
168                 return;
169             }
170 
171             List<Layout> layoutChildren = layout.getChildren();
172 
173             for (int i = 0; i < layoutChildren.size(); i++) {
174                 layout = layoutChildren.get(i);
175 
176                 Element fileEl = doc.createElement("File");
177 
178                 filesEl.appendChild(fileEl);
179 
180                 fileEl.setAttribute("name", _getLayoutName(layout));
181                 fileEl.setAttribute("desc", _getLayoutName(layout));
182                 fileEl.setAttribute("size", getSize());
183                 fileEl.setAttribute(
184                     "url",
185                     PortalUtil.getLayoutURL(
186                         layout, arg.getThemeDisplay(), false));
187             }
188         }
189     }
190 
191     private void _getFolders(CommandArgument arg, Document doc, Node root)
192         throws Exception {
193 
194         Element foldersEl = doc.createElement("Folders");
195 
196         root.appendChild(foldersEl);
197 
198         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
199             getRootFolders(arg, doc, foldersEl);
200         }
201         else {
202             Group group = arg.getCurrentGroup();
203 
204             List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
205                 group.getGroupId(), false, LayoutImpl.DEFAULT_PARENT_LAYOUT_ID);
206 
207             if (("/" + arg.getCurrentGroupName() + "/").equals(
208                     arg.getCurrentFolder())) {
209 
210                 for (Layout layout : layouts) {
211                     Element folderEl = doc.createElement("Folder");
212 
213                     foldersEl.appendChild(folderEl);
214 
215                     folderEl.setAttribute(
216                         "name", "~" + _getLayoutName(layout).replace('/', '>'));
217                 }
218             }
219             else {
220                 String layoutName = _getLayoutName(arg.getCurrentFolder());
221 
222                 Layout layout = null;
223 
224                 for (int i = 0; i < layouts.size(); i++) {
225                     layout = _getLayout(layoutName, layouts.get(i));
226 
227                     if (layout != null) {
228                         break;
229                     }
230                 }
231 
232                 if (layout != null) {
233                     List<Layout> layoutChildren = layout.getChildren();
234 
235                     for (int i = 0; i < layoutChildren.size(); i++) {
236                         layout = layoutChildren.get(i);
237 
238                         Element folderEl = doc.createElement("Folder");
239 
240                         foldersEl.appendChild(folderEl);
241 
242                         folderEl.setAttribute(
243                             "name",
244                             "~" + _getLayoutName(layout).replace('/', '>'));
245                     }
246                 }
247             }
248         }
249     }
250 
251 }