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