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.servlet.ImageServletTokenUtil;
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.model.Image;
32  import com.liferay.portal.service.ImageLocalServiceUtil;
33  import com.liferay.portal.service.ServiceContext;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.portlet.imagegallery.model.IGFolder;
36  import com.liferay.portlet.imagegallery.model.IGImage;
37  import com.liferay.portlet.imagegallery.model.impl.IGFolderImpl;
38  import com.liferay.portlet.imagegallery.service.IGFolderServiceUtil;
39  import com.liferay.portlet.imagegallery.service.IGImageServiceUtil;
40  
41  import java.io.File;
42  
43  import java.util.List;
44  import java.util.StringTokenizer;
45  
46  import org.w3c.dom.Document;
47  import org.w3c.dom.Element;
48  import org.w3c.dom.Node;
49  
50  /**
51   * <a href="ImageCommandReceiver.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Ivica Cardic
54   */
55  public class ImageCommandReceiver extends BaseCommandReceiver {
56  
57      protected String createFolder(CommandArgument arg) {
58          try {
59              Group group = arg.getCurrentGroup();
60  
61              IGFolder folder = _getFolder(
62                  group.getGroupId(), StringPool.SLASH + arg.getCurrentFolder());
63  
64              long parentFolderId = folder.getFolderId();
65              String name = arg.getNewFolder();
66              String description = StringPool.BLANK;
67  
68              ServiceContext serviceContext = new ServiceContext();
69  
70              serviceContext.setAddCommunityPermissions(true);
71              serviceContext.setAddGuestPermissions(true);
72              serviceContext.setPlid(arg.getPlid());
73              serviceContext.setScopeGroupId(group.getGroupId());
74  
75              IGFolderServiceUtil.addFolder(
76                  parentFolderId, name, description, serviceContext);
77          }
78          catch (Exception e) {
79              throw new FCKException(e);
80          }
81  
82          return "0";
83      }
84  
85      protected String fileUpload(
86          CommandArgument arg, String fileName, File file, String extension) {
87  
88          try {
89              Group group = arg.getCurrentGroup();
90  
91              IGFolder folder = _getFolder(
92                  group.getGroupId(), arg.getCurrentFolder());
93  
94              long folderId = folder.getFolderId();
95              String name = fileName;
96              String description = StringPool.BLANK;
97              String contentType = extension.toLowerCase();
98  
99              ServiceContext serviceContext = new ServiceContext();
100 
101             serviceContext.setAddCommunityPermissions(true);
102             serviceContext.setAddGuestPermissions(true);
103 
104             IGImageServiceUtil.addImage(
105                 folderId, name, description, file, contentType, serviceContext);
106         }
107         catch (Exception e) {
108             throw new FCKException(e);
109         }
110 
111         return "0";
112     }
113 
114     protected void getFolders(CommandArgument arg, Document doc, Node root) {
115         try {
116             _getFolders(arg, doc, root);
117         }
118         catch (Exception e) {
119             throw new FCKException(e);
120         }
121     }
122 
123     protected void getFoldersAndFiles(
124         CommandArgument arg, Document doc, Node root) {
125 
126         try {
127             _getFolders(arg, doc, root);
128             _getFiles(arg, doc, root);
129         }
130         catch (Exception e) {
131             throw new FCKException(e);
132         }
133     }
134 
135     private void _getFiles(CommandArgument arg, Document doc, Node root)
136         throws Exception {
137 
138         Element filesEl = doc.createElement("Files");
139 
140         root.appendChild(filesEl);
141 
142         if (Validator.isNull(arg.getCurrentGroupName())) {
143             return;
144         }
145 
146         Group group = arg.getCurrentGroup();
147 
148         IGFolder folder = _getFolder(
149             group.getGroupId(), arg.getCurrentFolder());
150 
151         List<IGImage> images = IGImageServiceUtil.getImages(
152             folder.getFolderId());
153 
154         for (IGImage image : images) {
155             long largeImageId = image.getLargeImageId();
156 
157             Image portalImage = ImageLocalServiceUtil.getImageOrDefault(
158                 largeImageId);
159 
160             Element fileEl = doc.createElement("File");
161 
162             filesEl.appendChild(fileEl);
163 
164             fileEl.setAttribute("name", image.getNameWithExtension());
165             fileEl.setAttribute("desc", image.getNameWithExtension());
166             fileEl.setAttribute("size", getSize(portalImage.getSize()));
167 
168             StringBuilder url = new StringBuilder();
169 
170             ThemeDisplay themeDisplay = arg.getThemeDisplay();
171 
172             url.append(themeDisplay.getPathImage());
173             url.append("/image_gallery?uuid=");
174             url.append(image.getUuid());
175             url.append("&groupId=");
176             url.append(folder.getGroupId());
177             url.append("&t=");
178             url.append(ImageServletTokenUtil.getToken(largeImageId));
179 
180             fileEl.setAttribute("url", url.toString());
181         }
182     }
183 
184     private IGFolder _getFolder(long groupId, String folderName)
185         throws Exception {
186 
187         IGFolder folder = new IGFolderImpl();
188 
189         folder.setFolderId(IGFolderImpl.DEFAULT_PARENT_FOLDER_ID);
190 
191         if (folderName.equals(StringPool.SLASH)) {
192             return folder;
193         }
194 
195         StringTokenizer st = new StringTokenizer(folderName, StringPool.SLASH);
196 
197         while (st.hasMoreTokens()) {
198             String curFolderName = st.nextToken();
199 
200             List<IGFolder> folders = IGFolderServiceUtil.getFolders(
201                 groupId, folder.getFolderId());
202 
203             for (IGFolder curFolder : folders) {
204                 if (curFolder.getName().equals(curFolderName)) {
205                     folder = curFolder;
206 
207                     break;
208                 }
209             }
210         }
211 
212         return folder;
213     }
214 
215     private void _getFolders(CommandArgument arg, Document doc, Node root)
216         throws Exception {
217 
218         Element foldersEl = doc.createElement("Folders");
219 
220         root.appendChild(foldersEl);
221 
222         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
223             getRootFolders(arg, doc, foldersEl);
224         }
225         else {
226             Group group = arg.getCurrentGroup();
227 
228             IGFolder folder = _getFolder(
229                 group.getGroupId(), arg.getCurrentFolder());
230 
231             List<IGFolder> folders = IGFolderServiceUtil.getFolders(
232                 group.getGroupId(), folder.getFolderId());
233 
234             for (IGFolder curFolder : folders) {
235                 Element folderEl = doc.createElement("Folder");
236 
237                 foldersEl.appendChild(folderEl);
238 
239                 folderEl.setAttribute("name", curFolder.getName());
240             }
241         }
242     }
243 
244 }