1   /**
2    * Copyright (c) 2000-2009 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.LayoutConstants;
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,
136             LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
137 
138         if (("/" + arg.getCurrentGroupName() + "/").equals(
139                 arg.getCurrentFolder())) {
140 
141             for (Layout layout : layouts) {
142                 Element fileEl = doc.createElement("File");
143 
144                 filesEl.appendChild(fileEl);
145 
146                 fileEl.setAttribute("name", _getLayoutName(layout));
147                 fileEl.setAttribute("desc", _getLayoutName(layout));
148                 fileEl.setAttribute("size", StringPool.BLANK);
149                 fileEl.setAttribute(
150                     "url",
151                     PortalUtil.getLayoutURL(
152                         layout,arg.getThemeDisplay(), false));
153             }
154         }
155         else {
156             String layoutName = _getLayoutName(arg.getCurrentFolder());
157 
158             Layout layout = null;
159 
160             for (int i = 0; i < layouts.size(); i++) {
161                 layout = _getLayout(layoutName, layouts.get(i));
162 
163                 if (layout != null) {
164                     break;
165                 }
166             }
167 
168             if (layout == null) {
169                 return;
170             }
171 
172             List<Layout> layoutChildren = layout.getChildren();
173 
174             for (int i = 0; i < layoutChildren.size(); i++) {
175                 layout = layoutChildren.get(i);
176 
177                 Element fileEl = doc.createElement("File");
178 
179                 filesEl.appendChild(fileEl);
180 
181                 fileEl.setAttribute("name", _getLayoutName(layout));
182                 fileEl.setAttribute("desc", _getLayoutName(layout));
183                 fileEl.setAttribute("size", getSize());
184                 fileEl.setAttribute(
185                     "url",
186                     PortalUtil.getLayoutURL(
187                         layout, arg.getThemeDisplay(), false));
188             }
189         }
190     }
191 
192     private void _getFolders(CommandArgument arg, Document doc, Node root)
193         throws Exception {
194 
195         Element foldersEl = doc.createElement("Folders");
196 
197         root.appendChild(foldersEl);
198 
199         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
200             getRootFolders(arg, doc, foldersEl);
201         }
202         else {
203             Group group = arg.getCurrentGroup();
204 
205             List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
206                 group.getGroupId(), false,
207                 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
208 
209             if (("/" + arg.getCurrentGroupName() + "/").equals(
210                     arg.getCurrentFolder())) {
211 
212                 for (Layout layout : layouts) {
213                     Element folderEl = doc.createElement("Folder");
214 
215                     foldersEl.appendChild(folderEl);
216 
217                     folderEl.setAttribute(
218                         "name", "~" + _getLayoutName(layout).replace('/', '>'));
219                 }
220             }
221             else {
222                 String layoutName = _getLayoutName(arg.getCurrentFolder());
223 
224                 Layout layout = null;
225 
226                 for (int i = 0; i < layouts.size(); i++) {
227                     layout = _getLayout(layoutName, layouts.get(i));
228 
229                     if (layout != null) {
230                         break;
231                     }
232                 }
233 
234                 if (layout != null) {
235                     List<Layout> layoutChildren = layout.getChildren();
236 
237                     for (int i = 0; i < layoutChildren.size(); i++) {
238                         layout = layoutChildren.get(i);
239 
240                         Element folderEl = doc.createElement("Folder");
241 
242                         foldersEl.appendChild(folderEl);
243 
244                         folderEl.setAttribute(
245                             "name",
246                             "~" + _getLayoutName(layout).replace('/', '>'));
247                     }
248                 }
249             }
250         }
251     }
252 
253 }