1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.imagegallery.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.ListUtil;
20  import com.liferay.portal.security.permission.ActionKeys;
21  import com.liferay.portal.service.ServiceContext;
22  import com.liferay.portlet.imagegallery.model.IGImage;
23  import com.liferay.portlet.imagegallery.service.base.IGImageServiceBaseImpl;
24  import com.liferay.portlet.imagegallery.service.permission.IGFolderPermission;
25  import com.liferay.portlet.imagegallery.service.permission.IGImagePermission;
26  
27  import java.io.File;
28  
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * <a href="IGImageServiceImpl.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   * @author Alexander Chow
37   */
38  public class IGImageServiceImpl extends IGImageServiceBaseImpl {
39  
40      public IGImage addImage(
41              long groupId, long folderId, String name, String description,
42              File file, String contentType, ServiceContext serviceContext)
43          throws PortalException, SystemException {
44  
45          IGFolderPermission.check(
46              getPermissionChecker(), groupId, folderId, ActionKeys.ADD_IMAGE);
47  
48          return igImageLocalService.addImage(
49              null, getUserId(), groupId, folderId, name, description, file,
50              contentType, serviceContext);
51      }
52  
53      public void deleteImage(long imageId)
54          throws PortalException, SystemException {
55  
56          IGImagePermission.check(
57              getPermissionChecker(), imageId, ActionKeys.DELETE);
58  
59          igImageLocalService.deleteImage(imageId);
60      }
61  
62      public void deleteImageByFolderIdAndNameWithExtension(
63              long groupId, long folderId, String nameWithExtension)
64          throws PortalException, SystemException {
65  
66          IGImage image =
67              igImageLocalService.getImageByFolderIdAndNameWithExtension(
68                  groupId, folderId, nameWithExtension);
69  
70          deleteImage(image.getImageId());
71      }
72  
73      public IGImage getImage(long imageId)
74          throws PortalException, SystemException {
75  
76          IGImagePermission.check(
77              getPermissionChecker(), imageId, ActionKeys.VIEW);
78  
79          return igImageLocalService.getImage(imageId);
80      }
81  
82      public IGImage getImageByFolderIdAndNameWithExtension(
83              long groupId, long folderId, String nameWithExtension)
84          throws PortalException, SystemException {
85  
86          IGImage image =
87              igImageLocalService.getImageByFolderIdAndNameWithExtension(
88                  groupId, folderId, nameWithExtension);
89  
90          IGImagePermission.check(
91              getPermissionChecker(), image, ActionKeys.VIEW);
92  
93          return image;
94      }
95  
96      public IGImage getImageByLargeImageId(long largeImageId)
97          throws PortalException, SystemException {
98  
99          IGImage image = igImageLocalService.getImageByLargeImageId(
100             largeImageId);
101 
102         IGImagePermission.check(
103             getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
104 
105         return image;
106     }
107 
108     public IGImage getImageBySmallImageId(long smallImageId)
109         throws PortalException, SystemException {
110 
111         IGImage image = igImageLocalService.getImageBySmallImageId(
112             smallImageId);
113 
114         IGImagePermission.check(
115             getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
116 
117         return image;
118     }
119 
120     public List<IGImage> getImages(long groupId, long folderId)
121         throws PortalException, SystemException {
122 
123         List<IGImage> images = igImageLocalService.getImages(groupId, folderId);
124 
125         images = ListUtil.copy(images);
126 
127         Iterator<IGImage> itr = images.iterator();
128 
129         while (itr.hasNext()) {
130             IGImage image = itr.next();
131 
132             if (!IGImagePermission.contains(
133                     getPermissionChecker(), image, ActionKeys.VIEW)) {
134 
135                 itr.remove();
136             }
137         }
138 
139         return images;
140     }
141 
142     public IGImage updateImage(
143             long imageId, long groupId, long folderId, String name,
144             String description, File file, String contentType,
145             ServiceContext serviceContext)
146         throws PortalException, SystemException {
147 
148         IGImagePermission.check(
149             getPermissionChecker(), imageId, ActionKeys.UPDATE);
150 
151         return igImageLocalService.updateImage(
152             getUserId(), imageId, groupId, folderId, name, description, file,
153             contentType, serviceContext);
154     }
155 
156 }