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