1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.editor.fckeditor.receiver.impl;
16  
17  import com.liferay.portal.editor.fckeditor.command.CommandArgument;
18  import com.liferay.portal.editor.fckeditor.exception.FCKException;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.model.Group;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.model.LayoutConstants;
23  import com.liferay.portal.service.LayoutLocalServiceUtil;
24  import com.liferay.portal.util.PortalUtil;
25  
26  import java.io.File;
27  
28  import java.util.List;
29  
30  import org.w3c.dom.Document;
31  import org.w3c.dom.Element;
32  import org.w3c.dom.Node;
33  
34  /**
35   * <a href="PageCommandReceiver.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Ivica Cardic
38   */
39  public class PageCommandReceiver extends BaseCommandReceiver {
40  
41      protected String createFolder(CommandArgument arg) {
42          return "0";
43      }
44  
45      protected String fileUpload(
46          CommandArgument arg, String fileName, File file, String extension) {
47  
48          return "0";
49      }
50  
51      protected void getFolders(CommandArgument arg, Document doc, Node root) {
52          try {
53              _getFolders(arg, doc, root);
54          }
55          catch (Exception e) {
56              throw new FCKException(e);
57          }
58      }
59  
60      protected void getFoldersAndFiles(
61          CommandArgument arg, Document doc, Node root) {
62  
63          try {
64              _getFolders(arg, doc, root);
65              _getFiles(arg, doc, root);
66          }
67          catch (Exception e) {
68              throw new FCKException(e);
69          }
70      }
71  
72      private Layout _getLayout(String layoutName, Layout layout)
73          throws Exception {
74  
75          String friendlyURL = layout.getFriendlyURL();
76  
77          if (layoutName.equals(friendlyURL)) {
78              return layout;
79          }
80  
81          List<Layout> layoutChildren = layout.getChildren();
82  
83          if (layoutChildren.size() == 0) {
84              return null;
85          }
86          else {
87              for (Layout layoutChild : layoutChildren) {
88                  Layout currentLayout = _getLayout(layoutName, layoutChild);
89  
90                  if (currentLayout != null) {
91                      return currentLayout;
92                  }
93              }
94          }
95  
96          return null;
97      }
98  
99      private String _getLayoutName(Layout layout) {
100         return layout.getFriendlyURL();
101     }
102 
103     private String _getLayoutName(String folderName) {
104         String layoutName = folderName.substring(
105             folderName.lastIndexOf('~') + 1, folderName.length() - 1);
106 
107         layoutName = layoutName.replace('>', '/');
108 
109         return layoutName;
110     }
111 
112     private void _getFiles(CommandArgument arg, Document doc, Node root)
113         throws Exception {
114 
115         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
116             return;
117         }
118 
119         Element filesEl = doc.createElement("Files");
120 
121         root.appendChild(filesEl);
122 
123         Group group = arg.getCurrentGroup();
124 
125         List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
126             group.getGroupId(), false,
127             LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
128 
129         if (("/" + arg.getCurrentGroupName() + "/").equals(
130                 arg.getCurrentFolder())) {
131 
132             for (Layout layout : layouts) {
133                 Element fileEl = doc.createElement("File");
134 
135                 filesEl.appendChild(fileEl);
136 
137                 fileEl.setAttribute("name", _getLayoutName(layout));
138                 fileEl.setAttribute("desc", _getLayoutName(layout));
139                 fileEl.setAttribute("size", StringPool.BLANK);
140                 fileEl.setAttribute(
141                     "url",
142                     PortalUtil.getLayoutURL(
143                         layout,arg.getThemeDisplay(), false));
144             }
145         }
146         else {
147             String layoutName = _getLayoutName(arg.getCurrentFolder());
148 
149             Layout layout = null;
150 
151             for (int i = 0; i < layouts.size(); i++) {
152                 layout = _getLayout(layoutName, layouts.get(i));
153 
154                 if (layout != null) {
155                     break;
156                 }
157             }
158 
159             if (layout == null) {
160                 return;
161             }
162 
163             List<Layout> layoutChildren = layout.getChildren();
164 
165             for (int i = 0; i < layoutChildren.size(); i++) {
166                 layout = layoutChildren.get(i);
167 
168                 Element fileEl = doc.createElement("File");
169 
170                 filesEl.appendChild(fileEl);
171 
172                 fileEl.setAttribute("name", _getLayoutName(layout));
173                 fileEl.setAttribute("desc", _getLayoutName(layout));
174                 fileEl.setAttribute("size", getSize());
175                 fileEl.setAttribute(
176                     "url",
177                     PortalUtil.getLayoutURL(
178                         layout, arg.getThemeDisplay(), false));
179             }
180         }
181     }
182 
183     private void _getFolders(CommandArgument arg, Document doc, Node root)
184         throws Exception {
185 
186         Element foldersEl = doc.createElement("Folders");
187 
188         root.appendChild(foldersEl);
189 
190         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
191             getRootFolders(arg, doc, foldersEl);
192         }
193         else {
194             Group group = arg.getCurrentGroup();
195 
196             List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
197                 group.getGroupId(), false,
198                 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
199 
200             if (("/" + arg.getCurrentGroupName() + "/").equals(
201                     arg.getCurrentFolder())) {
202 
203                 for (Layout layout : layouts) {
204                     Element folderEl = doc.createElement("Folder");
205 
206                     foldersEl.appendChild(folderEl);
207 
208                     folderEl.setAttribute(
209                         "name", "~" + _getLayoutName(layout).replace('/', '>'));
210                 }
211             }
212             else {
213                 String layoutName = _getLayoutName(arg.getCurrentFolder());
214 
215                 Layout layout = null;
216 
217                 for (int i = 0; i < layouts.size(); i++) {
218                     layout = _getLayout(layoutName, layouts.get(i));
219 
220                     if (layout != null) {
221                         break;
222                     }
223                 }
224 
225                 if (layout != null) {
226                     List<Layout> layoutChildren = layout.getChildren();
227 
228                     for (int i = 0; i < layoutChildren.size(); i++) {
229                         layout = layoutChildren.get(i);
230 
231                         Element folderEl = doc.createElement("Folder");
232 
233                         foldersEl.appendChild(folderEl);
234 
235                         folderEl.setAttribute(
236                             "name",
237                             "~" + _getLayoutName(layout).replace('/', '>'));
238                     }
239                 }
240             }
241         }
242     }
243 
244 }