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