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.util.HttpUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Group;
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.impl.DLFolderImpl;
35  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
36  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
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 plid = arg.getPlid();
64              long parentFolderId = folder.getFolderId();
65              String name = arg.getNewFolder();
66              String description = StringPool.BLANK;
67              boolean addCommunityPermissions = true;
68              boolean addGuestPermissions = true;
69  
70              DLFolderServiceUtil.addFolder(
71                  plid, parentFolderId, name, description,
72                  addCommunityPermissions, addGuestPermissions);
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 = arg.getCurrentGroup();
86  
87              DLFolder folder = _getFolder(
88                  group.getGroupId(), arg.getCurrentFolder());
89  
90              long folderId = folder.getFolderId();
91              String name = fileName;
92              String title = fileName;
93              String description = StringPool.BLANK;
94              String[] tagsEntries = null;
95              String extraSettings = StringPool.BLANK;
96              boolean addCommunityPermissions = true;
97              boolean addGuestPermissions = true;
98  
99              DLFileEntryServiceUtil.addFileEntry(
100                 folderId, name, title, description, tagsEntries, extraSettings,
101                 file, addCommunityPermissions, addGuestPermissions);
102         }
103         catch (Exception e) {
104             throw new FCKException(e);
105         }
106 
107         return "0";
108     }
109 
110     protected void getFolders(CommandArgument arg, Document doc, Node root) {
111         try {
112             _getFolders(arg, doc, root);
113         }
114         catch (Exception e) {
115             throw new FCKException(e);
116         }
117     }
118 
119     protected void getFoldersAndFiles(
120         CommandArgument arg, Document doc, Node root) {
121 
122         try {
123             _getFolders(arg, doc, root);
124             _getFiles(arg, doc, root);
125         }
126         catch (Exception e) {
127             throw new FCKException(e);
128         }
129     }
130 
131     private void _getFiles(CommandArgument arg, Document doc, Node root)
132         throws Exception {
133 
134         Element filesEl = doc.createElement("Files");
135 
136         root.appendChild(filesEl);
137 
138         if (Validator.isNull(arg.getCurrentGroupName())) {
139             return;
140         }
141 
142         Group group = arg.getCurrentGroup();
143 
144         DLFolder folder = _getFolder(
145             group.getGroupId(), arg.getCurrentFolder());
146 
147         List<DLFileEntry> fileEntries = DLFileEntryServiceUtil.getFileEntries(
148             folder.getFolderId());
149 
150         for (DLFileEntry fileEntry : fileEntries) {
151             Element fileEl = doc.createElement("File");
152 
153             filesEl.appendChild(fileEl);
154 
155             fileEl.setAttribute("name", fileEntry.getTitleWithExtension());
156             fileEl.setAttribute("desc", fileEntry.getTitleWithExtension());
157             fileEl.setAttribute("size", getSize(fileEntry.getSize()));
158 
159             StringBuilder url = new StringBuilder();
160 
161             ThemeDisplay themeDisplay = arg.getThemeDisplay();
162 
163             url.append(themeDisplay.getPathMain());
164             url.append("/document_library/get_file?folderId=");
165             url.append(fileEntry.getFolderId());
166             url.append("&name=");
167             url.append(HttpUtil.encodeURL(fileEntry.getName()));
168 
169             fileEl.setAttribute("url", url.toString());
170         }
171     }
172 
173     private DLFolder _getFolder(long groupId, String folderName)
174         throws Exception {
175 
176         DLFolder folder = new DLFolderImpl();
177 
178         folder.setFolderId(DLFolderImpl.DEFAULT_PARENT_FOLDER_ID);
179 
180         if (folderName.equals(StringPool.SLASH)) {
181             return folder;
182         }
183 
184         StringTokenizer st = new StringTokenizer(folderName, StringPool.SLASH);
185 
186         while (st.hasMoreTokens()) {
187             String curFolderName = st.nextToken();
188 
189             List<DLFolder> folders = DLFolderLocalServiceUtil.getFolders(
190                 groupId, folder.getFolderId());
191 
192             for (DLFolder curFolder : folders) {
193                 if (curFolder.getName().equals(curFolderName)) {
194                     folder = curFolder;
195 
196                     break;
197                 }
198             }
199         }
200 
201         return folder;
202     }
203 
204     private void _getFolders(CommandArgument arg, Document doc, Node root)
205         throws Exception {
206 
207         Element foldersEl = doc.createElement("Folders");
208 
209         root.appendChild(foldersEl);
210 
211         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
212             getRootFolders(arg, doc, foldersEl);
213         }
214         else {
215             Group group = arg.getCurrentGroup();
216 
217             DLFolder folder = _getFolder(
218                 group.getGroupId(), arg.getCurrentFolder());
219 
220             List<DLFolder> folders = DLFolderLocalServiceUtil.getFolders(
221                 group.getGroupId(), folder.getFolderId());
222 
223             for (DLFolder curFolder : folders) {
224                 Element folderEl = doc.createElement("Folder");
225 
226                 foldersEl.appendChild(folderEl);
227 
228                 folderEl.setAttribute("name", curFolder.getName());
229             }
230         }
231     }
232 
233 }