001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.editor.fckeditor.receiver.impl;
016    
017    import com.liferay.portal.editor.fckeditor.command.CommandArgument;
018    import com.liferay.portal.editor.fckeditor.exception.FCKException;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
026    import com.liferay.portlet.documentlibrary.model.DLFolder;
027    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
028    import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
029    import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
030    import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
031    
032    import java.io.File;
033    
034    import java.util.List;
035    import java.util.StringTokenizer;
036    
037    import org.w3c.dom.Document;
038    import org.w3c.dom.Element;
039    import org.w3c.dom.Node;
040    
041    /**
042     * @author Ivica Cardic
043     */
044    public class DocumentCommandReceiver extends BaseCommandReceiver {
045    
046            protected String createFolder(CommandArgument arg) {
047                    try {
048                            Group group = arg.getCurrentGroup();
049    
050                            DLFolder folder = _getFolder(
051                                    group.getGroupId(), StringPool.SLASH + arg.getCurrentFolder());
052    
053                            long parentFolderId = folder.getFolderId();
054                            String name = arg.getNewFolder();
055                            String description = StringPool.BLANK;
056    
057                            ServiceContext serviceContext = new ServiceContext();
058    
059                            serviceContext.setAddCommunityPermissions(true);
060                            serviceContext.setAddGuestPermissions(true);
061    
062                            DLFolderServiceUtil.addFolder(
063                                    group.getGroupId(), parentFolderId, name, description,
064                                    serviceContext);
065                    }
066                    catch (Exception e) {
067                            throw new FCKException(e);
068                    }
069    
070                    return "0";
071            }
072    
073            protected String fileUpload(
074                    CommandArgument arg, String fileName, File file, String extension) {
075    
076                    try {
077                            Group group = arg.getCurrentGroup();
078    
079                            DLFolder folder = _getFolder(
080                                    group.getGroupId(), arg.getCurrentFolder());
081    
082                            long folderId = folder.getFolderId();
083                            String name = fileName;
084                            String title = fileName;
085                            String description = StringPool.BLANK;
086                            String changeLog = StringPool.BLANK;
087                            String extraSettings = StringPool.BLANK;
088    
089                            ServiceContext serviceContext = new ServiceContext();
090    
091                            serviceContext.setAddCommunityPermissions(true);
092                            serviceContext.setAddGuestPermissions(true);
093    
094                            DLFileEntryServiceUtil.addFileEntry(
095                                    group.getGroupId(), folderId, name, title, description,
096                                    changeLog, extraSettings, file, serviceContext);
097                    }
098                    catch (Exception e) {
099                            throw new FCKException(e);
100                    }
101    
102                    return "0";
103            }
104    
105            protected void getFolders(CommandArgument arg, Document doc, Node root) {
106                    try {
107                            _getFolders(arg, doc, root);
108                    }
109                    catch (Exception e) {
110                            throw new FCKException(e);
111                    }
112            }
113    
114            protected void getFoldersAndFiles(
115                    CommandArgument arg, Document doc, Node root) {
116    
117                    try {
118                            _getFolders(arg, doc, root);
119                            _getFiles(arg, doc, root);
120                    }
121                    catch (Exception e) {
122                            throw new FCKException(e);
123                    }
124            }
125    
126            private void _getFiles(CommandArgument arg, Document doc, Node root)
127                    throws Exception {
128    
129                    Element filesEl = doc.createElement("Files");
130    
131                    root.appendChild(filesEl);
132    
133                    if (Validator.isNull(arg.getCurrentGroupName())) {
134                            return;
135                    }
136    
137                    Group group = arg.getCurrentGroup();
138    
139                    DLFolder folder = _getFolder(
140                            group.getGroupId(), arg.getCurrentFolder());
141    
142                    List<DLFileEntry> fileEntries = DLFileEntryServiceUtil.getFileEntries(
143                            folder.getGroupId(), folder.getFolderId());
144    
145                    for (DLFileEntry fileEntry : fileEntries) {
146                            Element fileEl = doc.createElement("File");
147    
148                            filesEl.appendChild(fileEl);
149    
150                            fileEl.setAttribute("name", fileEntry.getTitle());
151                            fileEl.setAttribute("desc", fileEntry.getTitle());
152                            fileEl.setAttribute("size", getSize(fileEntry.getSize()));
153    
154                            StringBundler url = new StringBundler(5);
155    
156                            ThemeDisplay themeDisplay = arg.getThemeDisplay();
157    
158                            url.append(themeDisplay.getPathMain());
159                            url.append("/document_library/get_file?uuid=");
160                            url.append(fileEntry.getUuid());
161                            url.append("&groupId=");
162                            url.append(folder.getGroupId());
163    
164                            fileEl.setAttribute("url", url.toString());
165                    }
166            }
167    
168            private DLFolder _getFolder(long groupId, String folderName)
169                    throws Exception {
170    
171                    DLFolder folder = new DLFolderImpl();
172    
173                    folder.setFolderId(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
174                    folder.setGroupId(groupId);
175    
176                    if (folderName.equals(StringPool.SLASH)) {
177                            return folder;
178                    }
179    
180                    StringTokenizer st = new StringTokenizer(folderName, StringPool.SLASH);
181    
182                    while (st.hasMoreTokens()) {
183                            String curFolderName = st.nextToken();
184    
185                            List<DLFolder> folders = DLFolderServiceUtil.getFolders(
186                                    groupId, folder.getFolderId());
187    
188                            for (DLFolder curFolder : folders) {
189                                    if (curFolder.getName().equals(curFolderName)) {
190                                            folder = curFolder;
191    
192                                            break;
193                                    }
194                            }
195                    }
196    
197                    return folder;
198            }
199    
200            private void _getFolders(CommandArgument arg, Document doc, Node root)
201                    throws Exception {
202    
203                    Element foldersEl = doc.createElement("Folders");
204    
205                    root.appendChild(foldersEl);
206    
207                    if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
208                            getRootFolders(arg, doc, foldersEl);
209                    }
210                    else {
211                            Group group = arg.getCurrentGroup();
212    
213                            DLFolder folder = _getFolder(
214                                    group.getGroupId(), arg.getCurrentFolder());
215    
216                            List<DLFolder> folders = DLFolderServiceUtil.getFolders(
217                                    group.getGroupId(), folder.getFolderId());
218    
219                            for (DLFolder curFolder : folders) {
220                                    Element folderEl = doc.createElement("Folder");
221    
222                                    foldersEl.appendChild(folderEl);
223    
224                                    folderEl.setAttribute("name", curFolder.getName());
225                            }
226                    }
227            }
228    
229    }