1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.imagegallery.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.FileUtil;
27  import com.liferay.portal.kernel.util.ListUtil;
28  import com.liferay.portal.model.Image;
29  import com.liferay.portal.security.permission.ActionKeys;
30  import com.liferay.portal.service.ServiceContext;
31  import com.liferay.portal.util.ContentTypeUtil;
32  import com.liferay.portlet.imagegallery.model.IGFolder;
33  import com.liferay.portlet.imagegallery.model.IGImage;
34  import com.liferay.portlet.imagegallery.service.base.IGFolderServiceBaseImpl;
35  import com.liferay.portlet.imagegallery.service.permission.IGFolderPermission;
36  
37  import java.io.File;
38  
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * <a href="IGFolderServiceImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Jorge Ferrer
47   *
48   */
49  public class IGFolderServiceImpl extends IGFolderServiceBaseImpl {
50  
51      public IGFolder addFolder(
52              long parentFolderId, String name, String description,
53              ServiceContext serviceContext)
54          throws PortalException, SystemException {
55  
56          IGFolderPermission.check(
57              getPermissionChecker(), serviceContext.getScopeGroupId(),
58              parentFolderId, ActionKeys.ADD_FOLDER);
59  
60          return igFolderLocalService.addFolder(
61              getUserId(), parentFolderId, name, description, serviceContext);
62      }
63  
64      public IGFolder copyFolder(
65              long sourceFolderId, long parentFolderId, String name,
66              String description, ServiceContext serviceContext)
67          throws PortalException, SystemException {
68  
69          IGFolder srcFolder = getFolder(sourceFolderId);
70  
71          IGFolder destFolder = addFolder(
72              parentFolderId, name, description, serviceContext);
73  
74          copyFolder(srcFolder, destFolder, serviceContext);
75  
76          return destFolder;
77      }
78  
79      public void deleteFolder(long folderId)
80          throws PortalException, SystemException {
81  
82          IGFolderPermission.check(
83              getPermissionChecker(), folderId, ActionKeys.DELETE);
84  
85          igFolderLocalService.deleteFolder(folderId);
86      }
87  
88      public IGFolder getFolder(long folderId)
89          throws PortalException, SystemException {
90  
91          IGFolderPermission.check(
92              getPermissionChecker(), folderId, ActionKeys.VIEW);
93  
94          return igFolderLocalService.getFolder(folderId);
95      }
96  
97      public IGFolder getFolder(long groupId, long parentFolderId, String name)
98          throws PortalException, SystemException {
99  
100         IGFolder folder = igFolderLocalService.getFolder(
101             groupId, parentFolderId, name);
102 
103         IGFolderPermission.check(
104             getPermissionChecker(), folder.getFolderId(), ActionKeys.VIEW);
105 
106         return folder;
107     }
108 
109     public List<IGFolder> getFolders(long groupId, long parentFolderId)
110         throws PortalException, SystemException {
111 
112         List<IGFolder> folders = igFolderLocalService.getFolders(
113             groupId, parentFolderId);
114 
115         folders = ListUtil.copy(folders);
116 
117         Iterator<IGFolder> itr = folders.iterator();
118 
119         while (itr.hasNext()) {
120             IGFolder folder = itr.next();
121 
122             if (!IGFolderPermission.contains(
123                     getPermissionChecker(), folder.getFolderId(),
124                     ActionKeys.VIEW)) {
125 
126                 itr.remove();
127             }
128         }
129 
130         return folders;
131     }
132 
133     public IGFolder updateFolder(
134             long folderId, long parentFolderId, String name, String description,
135             boolean mergeWithParentFolder, ServiceContext serviceContext)
136         throws PortalException, SystemException {
137 
138         IGFolderPermission.check(
139             getPermissionChecker(), folderId, ActionKeys.UPDATE);
140 
141         return igFolderLocalService.updateFolder(
142             folderId, parentFolderId, name, description, mergeWithParentFolder,
143             serviceContext);
144     }
145 
146     protected void copyFolder(
147             IGFolder srcFolder, IGFolder destFolder,
148             ServiceContext serviceContext)
149         throws PortalException, SystemException {
150 
151         List<IGImage> srcImages = igImageService.getImages(
152             srcFolder.getFolderId());
153 
154         for (IGImage srcImage : srcImages) {
155             String name = srcImage.getName();
156             String description = srcImage.getDescription();
157 
158             File file = null;
159 
160             try {
161                 file = FileUtil.createTempFile(srcImage.getImageType());
162 
163                 Image image = imageLocalService.getImage(
164                     srcImage.getLargeImageId());
165 
166                 byte[] bytes = image.getTextObj();
167 
168                 FileUtil.write(file, bytes);
169             }
170             catch (Exception e) {
171                 _log.error(e, e);
172 
173                 continue;
174             }
175 
176             String contentType = ContentTypeUtil.getContentType(
177                 srcImage.getImageType());
178 
179             igImageService.addImage(
180                 destFolder.getFolderId(), name, description, file, contentType,
181                 serviceContext);
182 
183             file.delete();
184         }
185 
186         List<IGFolder> srcSubfolders = getFolders(
187             srcFolder.getGroupId(), srcFolder.getFolderId());
188 
189         for (IGFolder srcSubfolder : srcSubfolders) {
190             String name = srcSubfolder.getName();
191             String description = srcSubfolder.getDescription();
192 
193             serviceContext.setScopeGroupId(srcFolder.getGroupId());
194 
195             IGFolder destSubfolder = addFolder(
196                 destFolder.getFolderId(), name, description, serviceContext);
197 
198             copyFolder(srcSubfolder, destSubfolder, serviceContext);
199         }
200     }
201 
202     private static Log _log = LogFactoryUtil.getLog(IGFolderServiceImpl.class);
203 
204 }