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.security.permission.ActionKeys;
28  import com.liferay.portal.kernel.util.StringMaker;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Group;
32  import com.liferay.portal.security.permission.PermissionThreadLocal;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
35  import com.liferay.portlet.documentlibrary.model.DLFolder;
36  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
37  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
38  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
39  import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
40  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
41  import com.liferay.util.HttpUtil;
42  
43  import java.io.File;
44  
45  import java.util.List;
46  import java.util.StringTokenizer;
47  
48  import org.w3c.dom.Document;
49  import org.w3c.dom.Element;
50  import org.w3c.dom.Node;
51  
52  /**
53   * <a href="DocumentCommandReceiver.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Ivica Cardic
56   *
57   */
58  public class DocumentCommandReceiver extends BaseCommandReceiver {
59  
60      protected String createFolder(CommandArgument arg) {
61          try {
62              Group group = arg.getCurrentGroup();
63  
64              DLFolder folder = _getFolder(
65                  group.getGroupId(), "/" + arg.getCurrentFolder());
66  
67              DLFolderServiceUtil.addFolder(
68                  arg.getPlid(), folder.getFolderId(), arg.getNewFolder(),
69                  StringPool.BLANK, true, true);
70          }
71          catch (Exception e) {
72              throw new FCKException(e);
73          }
74  
75          return "0";
76      }
77  
78      protected String fileUpload(
79          CommandArgument arg, String fileName, File file, String extension) {
80  
81          try {
82              Group group = arg.getCurrentGroup();
83  
84              DLFolder folder = _getFolder(
85                  group.getGroupId(), arg.getCurrentFolder());
86  
87              DLFolderPermission.check(
88                  PermissionThreadLocal.getPermissionChecker(), folder,
89                  ActionKeys.ADD_DOCUMENT);
90  
91              DLFileEntryLocalServiceUtil.addFileEntry(
92                  arg.getUserId(), folder.getFolderId(), fileName, fileName,
93                  StringPool.BLANK, new String[0], StringPool.BLANK, file, true,
94                  true);
95          }
96          catch (Exception e) {
97              throw new FCKException(e);
98          }
99  
100         return "0";
101     }
102 
103     protected void getFolders(CommandArgument arg, Document doc, Node root) {
104         try {
105             _getFolders(arg, doc, root);
106         }
107         catch (Exception e) {
108             throw new FCKException(e);
109         }
110     }
111 
112     protected void getFoldersAndFiles(
113         CommandArgument arg, Document doc, Node root) {
114 
115         try {
116             _getFolders(arg, doc, root);
117             _getFiles(arg, doc, root);
118         }
119         catch (Exception e) {
120             throw new FCKException(e);
121         }
122     }
123 
124     private void _getFiles(CommandArgument arg, Document doc, Node root)
125         throws Exception {
126 
127         Element filesEl = doc.createElement("Files");
128 
129         root.appendChild(filesEl);
130 
131         if (Validator.isNotNull(arg.getCurrentGroupName())) {
132             Group group = arg.getCurrentGroup();
133 
134             DLFolder folder = _getFolder(
135                 group.getGroupId(), arg.getCurrentFolder());
136 
137             List files = DLFileEntryLocalServiceUtil.getFileEntries(
138                 folder.getFolderId());
139 
140             for (int i = 0; i < files.size(); i++) {
141                 DLFileEntry fileEntry = (DLFileEntry)files.get(i);
142 
143                 Element fileEl = doc.createElement("File");
144 
145                 filesEl.appendChild(fileEl);
146 
147                 fileEl.setAttribute("name", fileEntry.getTitleWithExtension());
148                 fileEl.setAttribute("desc", fileEntry.getTitle());
149                 fileEl.setAttribute("size", getSize(fileEntry.getSize()));
150 
151                 StringMaker url = new StringMaker();
152 
153                 ThemeDisplay themeDisplay = arg.getThemeDisplay();
154 
155                 url.append(themeDisplay.getPathMain());
156                 url.append("/document_library/get_file?folderId=");
157                 url.append(fileEntry.getFolderId());
158                 url.append("&name=");
159                 url.append(HttpUtil.encodeURL(fileEntry.getName()));
160 
161                 fileEl.setAttribute("url", url.toString());
162             }
163         }
164     }
165 
166     private DLFolder _getFolder(long groupId, String folderName)
167         throws Exception {
168 
169         DLFolder folder = new DLFolderImpl();
170 
171         folder.setFolderId(DLFolderImpl.DEFAULT_PARENT_FOLDER_ID);
172 
173         if (!folderName.equals("/")) {
174             StringTokenizer st = new StringTokenizer(folderName, "/");
175 
176             while (st.hasMoreTokens()) {
177                 String curFolderName = (String)st.nextToken();
178 
179                 List folders = DLFolderLocalServiceUtil.getFolders(
180                     groupId, folder.getFolderId());
181 
182                 for (int i = 0; i < folders.size(); i++) {
183                     DLFolder curFolder = (DLFolder)folders.get(i);
184 
185                     if (curFolder.getName().equals(curFolderName)) {
186                         folder = curFolder;
187 
188                         break;
189                     }
190                 }
191             }
192         }
193 
194         return folder;
195     }
196 
197     private void _getFolders(CommandArgument arg, Document doc, Node root)
198         throws Exception {
199 
200         Element foldersEl = doc.createElement("Folders");
201 
202         root.appendChild(foldersEl);
203 
204         if (arg.getCurrentFolder().equals("/")) {
205             getRootFolders(arg, doc, foldersEl);
206         }
207         else {
208             Group group = arg.getCurrentGroup();
209 
210             DLFolder folder = _getFolder(
211                 group.getGroupId(), arg.getCurrentFolder());
212 
213             List folders = DLFolderLocalServiceUtil.getFolders(
214                 group.getGroupId(), folder.getFolderId());
215 
216             for (int i = 0; i < folders.size(); i++) {
217                 folder = (DLFolder) folders.get(i);
218 
219                 Element folderEl = doc.createElement("Folder");
220 
221                 foldersEl.appendChild(folderEl);
222 
223                 folderEl.setAttribute("name", folder.getName());
224             }
225         }
226     }
227 
228 }