1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.editor.fckeditor.receiver.impl;
21  
22  import com.liferay.portal.editor.fckeditor.command.CommandArgument;
23  import com.liferay.portal.editor.fckeditor.exception.FCKException;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.Group;
27  import com.liferay.portal.service.ServiceContext;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
30  import com.liferay.portlet.documentlibrary.model.DLFolder;
31  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
32  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
33  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
34  import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
35  
36  import java.io.File;
37  
38  import java.util.List;
39  import java.util.StringTokenizer;
40  
41  import org.w3c.dom.Document;
42  import org.w3c.dom.Element;
43  import org.w3c.dom.Node;
44  
45  /**
46   * <a href="DocumentCommandReceiver.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Ivica Cardic
49   *
50   */
51  public class DocumentCommandReceiver extends BaseCommandReceiver {
52  
53      protected String createFolder(CommandArgument arg) {
54          try {
55              Group group = arg.getCurrentGroup();
56  
57              DLFolder folder = _getFolder(
58                  group.getGroupId(), StringPool.SLASH + arg.getCurrentFolder());
59  
60              long parentFolderId = folder.getFolderId();
61              String name = arg.getNewFolder();
62              String description = StringPool.BLANK;
63  
64              ServiceContext serviceContext = new ServiceContext();
65  
66              serviceContext.setAddCommunityPermissions(true);
67              serviceContext.setAddGuestPermissions(true);
68  
69              DLFolderServiceUtil.addFolder(
70                  group.getGroupId(), parentFolderId, name, description,
71                  serviceContext);
72          }
73          catch (Exception e) {
74              throw new FCKException(e);
75          }
76  
77          return "0";
78      }
79  
80      protected String fileUpload(
81          CommandArgument arg, String fileName, File file, String extension) {
82  
83          try {
84              Group group = arg.getCurrentGroup();
85  
86              DLFolder folder = _getFolder(
87                  group.getGroupId(), arg.getCurrentFolder());
88  
89              long folderId = folder.getFolderId();
90              String name = fileName;
91              String title = fileName;
92              String description = StringPool.BLANK;
93              String extraSettings = StringPool.BLANK;
94  
95              ServiceContext serviceContext = new ServiceContext();
96  
97              serviceContext.setAddCommunityPermissions(true);
98              serviceContext.setAddGuestPermissions(true);
99  
100             DLFileEntryServiceUtil.addFileEntry(
101                 folderId, name, title, description, extraSettings, file,
102                 serviceContext);
103         }
104         catch (Exception e) {
105             throw new FCKException(e);
106         }
107 
108         return "0";
109     }
110 
111     protected void getFolders(CommandArgument arg, Document doc, Node root) {
112         try {
113             _getFolders(arg, doc, root);
114         }
115         catch (Exception e) {
116             throw new FCKException(e);
117         }
118     }
119 
120     protected void getFoldersAndFiles(
121         CommandArgument arg, Document doc, Node root) {
122 
123         try {
124             _getFolders(arg, doc, root);
125             _getFiles(arg, doc, root);
126         }
127         catch (Exception e) {
128             throw new FCKException(e);
129         }
130     }
131 
132     private void _getFiles(CommandArgument arg, Document doc, Node root)
133         throws Exception {
134 
135         Element filesEl = doc.createElement("Files");
136 
137         root.appendChild(filesEl);
138 
139         if (Validator.isNull(arg.getCurrentGroupName())) {
140             return;
141         }
142 
143         Group group = arg.getCurrentGroup();
144 
145         DLFolder folder = _getFolder(
146             group.getGroupId(), arg.getCurrentFolder());
147 
148         List<DLFileEntry> fileEntries = DLFileEntryServiceUtil.getFileEntries(
149             folder.getFolderId());
150 
151         for (DLFileEntry fileEntry : fileEntries) {
152             Element fileEl = doc.createElement("File");
153 
154             filesEl.appendChild(fileEl);
155 
156             fileEl.setAttribute("name", fileEntry.getTitleWithExtension());
157             fileEl.setAttribute("desc", fileEntry.getTitleWithExtension());
158             fileEl.setAttribute("size", getSize(fileEntry.getSize()));
159 
160             StringBuilder url = new StringBuilder();
161 
162             ThemeDisplay themeDisplay = arg.getThemeDisplay();
163 
164             url.append(themeDisplay.getPathMain());
165             url.append("/document_library/get_file?uuid=");
166             url.append(fileEntry.getUuid());
167             url.append("&groupId=");
168             url.append(folder.getGroupId());
169 
170             fileEl.setAttribute("url", url.toString());
171         }
172     }
173 
174     private DLFolder _getFolder(long groupId, String folderName)
175         throws Exception {
176 
177         DLFolder folder = new DLFolderImpl();
178 
179         folder.setFolderId(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
180 
181         if (folderName.equals(StringPool.SLASH)) {
182             return folder;
183         }
184 
185         StringTokenizer st = new StringTokenizer(folderName, StringPool.SLASH);
186 
187         while (st.hasMoreTokens()) {
188             String curFolderName = st.nextToken();
189 
190             List<DLFolder> folders = DLFolderServiceUtil.getFolders(
191                 groupId, folder.getFolderId());
192 
193             for (DLFolder curFolder : folders) {
194                 if (curFolder.getName().equals(curFolderName)) {
195                     folder = curFolder;
196 
197                     break;
198                 }
199             }
200         }
201 
202         return folder;
203     }
204 
205     private void _getFolders(CommandArgument arg, Document doc, Node root)
206         throws Exception {
207 
208         Element foldersEl = doc.createElement("Folders");
209 
210         root.appendChild(foldersEl);
211 
212         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
213             getRootFolders(arg, doc, foldersEl);
214         }
215         else {
216             Group group = arg.getCurrentGroup();
217 
218             DLFolder folder = _getFolder(
219                 group.getGroupId(), arg.getCurrentFolder());
220 
221             List<DLFolder> folders = DLFolderServiceUtil.getFolders(
222                 group.getGroupId(), folder.getFolderId());
223 
224             for (DLFolder curFolder : folders) {
225                 Element folderEl = doc.createElement("Folder");
226 
227                 foldersEl.appendChild(folderEl);
228 
229                 folderEl.setAttribute("name", curFolder.getName());
230             }
231         }
232     }
233 
234 }