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