1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.portlet.imagegallery.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.FileUtil;
30  import com.liferay.portal.kernel.util.ListUtil;
31  import com.liferay.portal.model.Image;
32  import com.liferay.portal.security.permission.ActionKeys;
33  import com.liferay.portal.service.ServiceContext;
34  import com.liferay.portal.util.ContentTypeUtil;
35  import com.liferay.portlet.imagegallery.model.IGFolder;
36  import com.liferay.portlet.imagegallery.model.IGImage;
37  import com.liferay.portlet.imagegallery.service.base.IGFolderServiceBaseImpl;
38  import com.liferay.portlet.imagegallery.service.permission.IGFolderPermission;
39  
40  import java.io.File;
41  
42  import java.util.Iterator;
43  import java.util.List;
44  
45  /**
46   * <a href="IGFolderServiceImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Jorge Ferrer
50   *
51   */
52  public class IGFolderServiceImpl extends IGFolderServiceBaseImpl {
53  
54      public IGFolder addFolder(
55              long parentFolderId, String name, String description,
56              ServiceContext serviceContext)
57          throws PortalException, SystemException {
58  
59          IGFolderPermission.check(
60              getPermissionChecker(), serviceContext.getScopeGroupId(),
61              parentFolderId, ActionKeys.ADD_FOLDER);
62  
63          return igFolderLocalService.addFolder(
64              getUserId(), parentFolderId, name, description, serviceContext);
65      }
66  
67      public IGFolder copyFolder(
68              long sourceFolderId, long parentFolderId, String name,
69              String description, ServiceContext serviceContext)
70          throws PortalException, SystemException {
71  
72          IGFolder srcFolder = getFolder(sourceFolderId);
73  
74          IGFolder destFolder = addFolder(
75              parentFolderId, name, description, serviceContext);
76  
77          copyFolder(srcFolder, destFolder, serviceContext);
78  
79          return destFolder;
80      }
81  
82      public void deleteFolder(long folderId)
83          throws PortalException, SystemException {
84  
85          IGFolderPermission.check(
86              getPermissionChecker(), folderId, ActionKeys.DELETE);
87  
88          igFolderLocalService.deleteFolder(folderId);
89      }
90  
91      public IGFolder getFolder(long folderId)
92          throws PortalException, SystemException {
93  
94          IGFolderPermission.check(
95              getPermissionChecker(), folderId, ActionKeys.VIEW);
96  
97          return igFolderLocalService.getFolder(folderId);
98      }
99  
100     public IGFolder getFolder(long groupId, long parentFolderId, String name)
101         throws PortalException, SystemException {
102 
103         IGFolder folder = igFolderLocalService.getFolder(
104             groupId, parentFolderId, name);
105 
106         IGFolderPermission.check(
107             getPermissionChecker(), folder.getFolderId(), ActionKeys.VIEW);
108 
109         return folder;
110     }
111 
112     public List<IGFolder> getFolders(long groupId, long parentFolderId)
113         throws PortalException, SystemException {
114 
115         List<IGFolder> folders = igFolderLocalService.getFolders(
116             groupId, parentFolderId);
117 
118         folders = ListUtil.copy(folders);
119 
120         Iterator<IGFolder> itr = folders.iterator();
121 
122         while (itr.hasNext()) {
123             IGFolder folder = itr.next();
124 
125             if (!IGFolderPermission.contains(
126                     getPermissionChecker(), folder.getFolderId(),
127                     ActionKeys.VIEW)) {
128 
129                 itr.remove();
130             }
131         }
132 
133         return folders;
134     }
135 
136     public IGFolder updateFolder(
137             long folderId, long parentFolderId, String name, String description,
138             boolean mergeWithParentFolder)
139         throws PortalException, SystemException {
140 
141         IGFolderPermission.check(
142             getPermissionChecker(), folderId, ActionKeys.UPDATE);
143 
144         return igFolderLocalService.updateFolder(
145             folderId, parentFolderId, name, description, mergeWithParentFolder);
146     }
147 
148     protected void copyFolder(
149             IGFolder srcFolder, IGFolder destFolder,
150             ServiceContext serviceContext)
151         throws PortalException, SystemException {
152 
153         List<IGImage> srcImages = igImageService.getImages(
154             srcFolder.getFolderId());
155 
156         for (IGImage srcImage : srcImages) {
157             String name = srcImage.getName();
158             String description = srcImage.getDescription();
159 
160             File file = null;
161 
162             try {
163                 file = FileUtil.createTempFile(srcImage.getImageType());
164 
165                 Image image = imageLocalService.getImage(
166                     srcImage.getLargeImageId());
167 
168                 byte[] bytes = image.getTextObj();
169 
170                 FileUtil.write(file, bytes);
171             }
172             catch (Exception e) {
173                 _log.error(e, e);
174 
175                 continue;
176             }
177 
178             String contentType = ContentTypeUtil.getContentType(
179                 srcImage.getImageType());
180 
181             igImageService.addImage(
182                 destFolder.getFolderId(), name, description, file, contentType,
183                 serviceContext);
184 
185             file.delete();
186         }
187 
188         List<IGFolder> srcSubfolders = getFolders(
189             srcFolder.getGroupId(), srcFolder.getFolderId());
190 
191         for (IGFolder srcSubfolder : srcSubfolders) {
192             String name = srcSubfolder.getName();
193             String description = srcSubfolder.getDescription();
194 
195             serviceContext.setScopeGroupId(srcFolder.getGroupId());
196 
197             IGFolder destSubfolder = addFolder(
198                 destFolder.getFolderId(), name, description, serviceContext);
199 
200             copyFolder(srcSubfolder, destSubfolder, serviceContext);
201         }
202     }
203 
204     private static Log _log = LogFactoryUtil.getLog(IGFolderServiceImpl.class);
205 
206 }