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.servlet.ImageServletTokenUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.model.Image;
025    import com.liferay.portal.service.ImageLocalServiceUtil;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portlet.imagegallery.model.IGFolder;
029    import com.liferay.portlet.imagegallery.model.IGFolderConstants;
030    import com.liferay.portlet.imagegallery.model.IGImage;
031    import com.liferay.portlet.imagegallery.model.impl.IGFolderImpl;
032    import com.liferay.portlet.imagegallery.service.IGFolderServiceUtil;
033    import com.liferay.portlet.imagegallery.service.IGImageServiceUtil;
034    
035    import java.io.File;
036    
037    import java.util.List;
038    import java.util.StringTokenizer;
039    
040    import org.w3c.dom.Document;
041    import org.w3c.dom.Element;
042    import org.w3c.dom.Node;
043    
044    /**
045     * @author Ivica Cardic
046     */
047    public class ImageCommandReceiver extends BaseCommandReceiver {
048    
049            protected String createFolder(CommandArgument arg) {
050                    try {
051                            Group group = arg.getCurrentGroup();
052    
053                            IGFolder folder = _getFolder(
054                                    group.getGroupId(), StringPool.SLASH + arg.getCurrentFolder());
055    
056                            long parentFolderId = folder.getFolderId();
057                            String name = arg.getNewFolder();
058                            String description = StringPool.BLANK;
059    
060                            ServiceContext serviceContext = new ServiceContext();
061    
062                            serviceContext.setAddCommunityPermissions(true);
063                            serviceContext.setAddGuestPermissions(true);
064                            serviceContext.setPlid(arg.getPlid());
065                            serviceContext.setScopeGroupId(group.getGroupId());
066    
067                            IGFolderServiceUtil.addFolder(
068                                    parentFolderId, name, description, serviceContext);
069                    }
070                    catch (Exception e) {
071                            throw new FCKException(e);
072                    }
073    
074                    return "0";
075            }
076    
077            protected String fileUpload(
078                    CommandArgument arg, String fileName, File file, String extension) {
079    
080                    try {
081                            Group group = arg.getCurrentGroup();
082    
083                            long groupId = group.getGroupId();
084    
085                            IGFolder folder = _getFolder(groupId, arg.getCurrentFolder());
086    
087                            long folderId = folder.getFolderId();
088                            String name = fileName;
089                            String description = StringPool.BLANK;
090                            String contentType = extension.toLowerCase();
091    
092                            ServiceContext serviceContext = new ServiceContext();
093    
094                            serviceContext.setAddCommunityPermissions(true);
095                            serviceContext.setAddGuestPermissions(true);
096    
097                            IGImageServiceUtil.addImage(
098                                    groupId, folderId, name, description, file, contentType,
099                                    serviceContext);
100                    }
101                    catch (Exception e) {
102                            throw new FCKException(e);
103                    }
104    
105                    return "0";
106            }
107    
108            protected void getFolders(CommandArgument arg, Document doc, Node root) {
109                    try {
110                            _getFolders(arg, doc, root);
111                    }
112                    catch (Exception e) {
113                            throw new FCKException(e);
114                    }
115            }
116    
117            protected void getFoldersAndFiles(
118                    CommandArgument arg, Document doc, Node root) {
119    
120                    try {
121                            _getFolders(arg, doc, root);
122                            _getFiles(arg, doc, root);
123                    }
124                    catch (Exception e) {
125                            throw new FCKException(e);
126                    }
127            }
128    
129            private void _getFiles(CommandArgument arg, Document doc, Node root)
130                    throws Exception {
131    
132                    Element filesEl = doc.createElement("Files");
133    
134                    root.appendChild(filesEl);
135    
136                    if (Validator.isNull(arg.getCurrentGroupName())) {
137                            return;
138                    }
139    
140                    Group group = arg.getCurrentGroup();
141    
142                    IGFolder folder = _getFolder(
143                            group.getGroupId(), arg.getCurrentFolder());
144    
145                    List<IGImage> images = IGImageServiceUtil.getImages(
146                            folder.getGroupId(), folder.getFolderId());
147    
148                    for (IGImage image : images) {
149                            long largeImageId = image.getLargeImageId();
150    
151                            Image portalImage = ImageLocalServiceUtil.getImageOrDefault(
152                                    largeImageId);
153    
154                            Element fileEl = doc.createElement("File");
155    
156                            filesEl.appendChild(fileEl);
157    
158                            fileEl.setAttribute("name", image.getNameWithExtension());
159                            fileEl.setAttribute("desc", image.getNameWithExtension());
160                            fileEl.setAttribute("size", getSize(portalImage.getSize()));
161    
162                            StringBundler url = new StringBundler(7);
163    
164                            ThemeDisplay themeDisplay = arg.getThemeDisplay();
165    
166                            url.append(themeDisplay.getPathImage());
167                            url.append("/image_gallery?uuid=");
168                            url.append(image.getUuid());
169                            url.append("&groupId=");
170                            url.append(folder.getGroupId());
171                            url.append("&t=");
172                            url.append(ImageServletTokenUtil.getToken(largeImageId));
173    
174                            fileEl.setAttribute("url", url.toString());
175                    }
176            }
177    
178            private IGFolder _getFolder(long groupId, String folderName)
179                    throws Exception {
180    
181                    IGFolder folder = new IGFolderImpl();
182    
183                    folder.setFolderId(IGFolderConstants.DEFAULT_PARENT_FOLDER_ID);
184                    folder.setGroupId(groupId);
185    
186                    if (folderName.equals(StringPool.SLASH)) {
187                            return folder;
188                    }
189    
190                    StringTokenizer st = new StringTokenizer(folderName, StringPool.SLASH);
191    
192                    while (st.hasMoreTokens()) {
193                            String curFolderName = st.nextToken();
194    
195                            List<IGFolder> folders = IGFolderServiceUtil.getFolders(
196                                    groupId, folder.getFolderId());
197    
198                            for (IGFolder curFolder : folders) {
199                                    if (curFolder.getName().equals(curFolderName)) {
200                                            folder = curFolder;
201    
202                                            break;
203                                    }
204                            }
205                    }
206    
207                    return folder;
208            }
209    
210            private void _getFolders(CommandArgument arg, Document doc, Node root)
211                    throws Exception {
212    
213                    Element foldersEl = doc.createElement("Folders");
214    
215                    root.appendChild(foldersEl);
216    
217                    if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
218                            getRootFolders(arg, doc, foldersEl);
219                    }
220                    else {
221                            Group group = arg.getCurrentGroup();
222    
223                            IGFolder folder = _getFolder(
224                                    group.getGroupId(), arg.getCurrentFolder());
225    
226                            List<IGFolder> folders = IGFolderServiceUtil.getFolders(
227                                    group.getGroupId(), folder.getFolderId());
228    
229                            for (IGFolder curFolder : folders) {
230                                    Element folderEl = doc.createElement("Folder");
231    
232                                    foldersEl.appendChild(folderEl);
233    
234                                    folderEl.setAttribute("name", curFolder.getName());
235                            }
236                    }
237            }
238    
239    }