1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class DocumentCommandReceiver extends BaseCommandReceiver {
54  
55      protected String createFolder(CommandArgument arg) {
56          try {
57              Group group = arg.getCurrentGroup();
58  
59              DLFolder folder = _getFolder(
60                  group.getGroupId(), StringPool.SLASH + arg.getCurrentFolder());
61  
62              long parentFolderId = folder.getFolderId();
63              String name = arg.getNewFolder();
64              String description = StringPool.BLANK;
65  
66              ServiceContext serviceContext = new ServiceContext();
67  
68              serviceContext.setAddCommunityPermissions(true);
69              serviceContext.setAddGuestPermissions(true);
70  
71              DLFolderServiceUtil.addFolder(
72                  group.getGroupId(), parentFolderId, name, description,
73                  serviceContext);
74          }
75          catch (Exception e) {
76              throw new FCKException(e);
77          }
78  
79          return "0";
80      }
81  
82      protected String fileUpload(
83          CommandArgument arg, String fileName, File file, String extension) {
84  
85          try {
86              Group group = arg.getCurrentGroup();
87  
88              DLFolder folder = _getFolder(
89                  group.getGroupId(), arg.getCurrentFolder());
90  
91              long folderId = folder.getFolderId();
92              String name = fileName;
93              String title = fileName;
94              String description = StringPool.BLANK;
95              String extraSettings = StringPool.BLANK;
96  
97              ServiceContext serviceContext = new ServiceContext();
98  
99              serviceContext.setAddCommunityPermissions(true);
100             serviceContext.setAddGuestPermissions(true);
101 
102             DLFileEntryServiceUtil.addFileEntry(
103                 folderId, name, title, description, extraSettings, file,
104                 serviceContext);
105         }
106         catch (Exception e) {
107             throw new FCKException(e);
108         }
109 
110         return "0";
111     }
112 
113     protected void getFolders(CommandArgument arg, Document doc, Node root) {
114         try {
115             _getFolders(arg, doc, root);
116         }
117         catch (Exception e) {
118             throw new FCKException(e);
119         }
120     }
121 
122     protected void getFoldersAndFiles(
123         CommandArgument arg, Document doc, Node root) {
124 
125         try {
126             _getFolders(arg, doc, root);
127             _getFiles(arg, doc, root);
128         }
129         catch (Exception e) {
130             throw new FCKException(e);
131         }
132     }
133 
134     private void _getFiles(CommandArgument arg, Document doc, Node root)
135         throws Exception {
136 
137         Element filesEl = doc.createElement("Files");
138 
139         root.appendChild(filesEl);
140 
141         if (Validator.isNull(arg.getCurrentGroupName())) {
142             return;
143         }
144 
145         Group group = arg.getCurrentGroup();
146 
147         DLFolder folder = _getFolder(
148             group.getGroupId(), arg.getCurrentFolder());
149 
150         List<DLFileEntry> fileEntries = DLFileEntryServiceUtil.getFileEntries(
151             folder.getFolderId());
152 
153         for (DLFileEntry fileEntry : fileEntries) {
154             Element fileEl = doc.createElement("File");
155 
156             filesEl.appendChild(fileEl);
157 
158             fileEl.setAttribute("name", fileEntry.getTitleWithExtension());
159             fileEl.setAttribute("desc", fileEntry.getTitleWithExtension());
160             fileEl.setAttribute("size", getSize(fileEntry.getSize()));
161 
162             StringBuilder url = new StringBuilder();
163 
164             ThemeDisplay themeDisplay = arg.getThemeDisplay();
165 
166             url.append(themeDisplay.getPathMain());
167             url.append("/document_library/get_file?uuid=");
168             url.append(fileEntry.getUuid());
169             url.append("&groupId=");
170             url.append(folder.getGroupId());
171 
172             fileEl.setAttribute("url", url.toString());
173         }
174     }
175 
176     private DLFolder _getFolder(long groupId, String folderName)
177         throws Exception {
178 
179         DLFolder folder = new DLFolderImpl();
180 
181         folder.setFolderId(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
182 
183         if (folderName.equals(StringPool.SLASH)) {
184             return folder;
185         }
186 
187         StringTokenizer st = new StringTokenizer(folderName, StringPool.SLASH);
188 
189         while (st.hasMoreTokens()) {
190             String curFolderName = st.nextToken();
191 
192             List<DLFolder> folders = DLFolderServiceUtil.getFolders(
193                 groupId, folder.getFolderId());
194 
195             for (DLFolder curFolder : folders) {
196                 if (curFolder.getName().equals(curFolderName)) {
197                     folder = curFolder;
198 
199                     break;
200                 }
201             }
202         }
203 
204         return folder;
205     }
206 
207     private void _getFolders(CommandArgument arg, Document doc, Node root)
208         throws Exception {
209 
210         Element foldersEl = doc.createElement("Folders");
211 
212         root.appendChild(foldersEl);
213 
214         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
215             getRootFolders(arg, doc, foldersEl);
216         }
217         else {
218             Group group = arg.getCurrentGroup();
219 
220             DLFolder folder = _getFolder(
221                 group.getGroupId(), arg.getCurrentFolder());
222 
223             List<DLFolder> folders = DLFolderServiceUtil.getFolders(
224                 group.getGroupId(), folder.getFolderId());
225 
226             for (DLFolder curFolder : folders) {
227                 Element folderEl = doc.createElement("Folder");
228 
229                 foldersEl.appendChild(folderEl);
230 
231                 folderEl.setAttribute("name", curFolder.getName());
232             }
233         }
234     }
235 
236 }