1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.imagegallery.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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   */
37  public class IGImageServiceImpl extends IGImageServiceBaseImpl {
38  
39      public IGImage addImage(
40              long folderId, String name, String description, File file,
41              String contentType, ServiceContext serviceContext)
42          throws PortalException, SystemException {
43  
44          IGFolderPermission.check(
45              getPermissionChecker(), folderId, ActionKeys.ADD_IMAGE);
46  
47          return igImageLocalService.addImage(
48              getUserId(), folderId, name, description, file, contentType,
49              serviceContext);
50      }
51  
52      public void deleteImage(long imageId)
53          throws PortalException, SystemException {
54  
55          IGImagePermission.check(
56              getPermissionChecker(), imageId, ActionKeys.DELETE);
57  
58          igImageLocalService.deleteImage(imageId);
59      }
60  
61      public void deleteImageByFolderIdAndNameWithExtension(
62              long folderId, String nameWithExtension)
63          throws PortalException, SystemException {
64  
65          IGImage image =
66              igImageLocalService.getImageByFolderIdAndNameWithExtension(
67                  folderId, nameWithExtension);
68  
69          deleteImage(image.getImageId());
70      }
71  
72      public IGImage getImage(long imageId)
73          throws PortalException, SystemException {
74  
75          IGImagePermission.check(
76              getPermissionChecker(), imageId, ActionKeys.VIEW);
77  
78          return igImageLocalService.getImage(imageId);
79      }
80  
81      public IGImage getImageByFolderIdAndNameWithExtension(
82              long folderId, String nameWithExtension)
83          throws PortalException, SystemException {
84  
85          IGImage image =
86              igImageLocalService.getImageByFolderIdAndNameWithExtension(
87                  folderId, nameWithExtension);
88  
89          IGImagePermission.check(
90              getPermissionChecker(), image, ActionKeys.VIEW);
91  
92          return image;
93      }
94  
95      public IGImage getImageByLargeImageId(long largeImageId)
96          throws PortalException, SystemException {
97  
98          IGImage image = igImageLocalService.getImageByLargeImageId(
99              largeImageId);
100 
101         IGImagePermission.check(
102             getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
103 
104         return image;
105     }
106 
107     public IGImage getImageBySmallImageId(long smallImageId)
108         throws PortalException, SystemException {
109 
110         IGImage image = igImageLocalService.getImageBySmallImageId(
111             smallImageId);
112 
113         IGImagePermission.check(
114             getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
115 
116         return image;
117     }
118 
119     public List<IGImage> getImages(long folderId)
120         throws PortalException, SystemException {
121 
122         List<IGImage> images = igImageLocalService.getImages(folderId);
123 
124         images = ListUtil.copy(images);
125 
126         Iterator<IGImage> itr = images.iterator();
127 
128         while (itr.hasNext()) {
129             IGImage image = itr.next();
130 
131             if (!IGImagePermission.contains(
132                     getPermissionChecker(), image, ActionKeys.VIEW)) {
133 
134                 itr.remove();
135             }
136         }
137 
138         return images;
139     }
140 
141     public IGImage updateImage(
142             long imageId, long folderId, String name, String description,
143             File file, String contentType, ServiceContext serviceContext)
144         throws PortalException, SystemException {
145 
146         IGImagePermission.check(
147             getPermissionChecker(), imageId, ActionKeys.UPDATE);
148 
149         return igImageLocalService.updateImage(
150             getUserId(), imageId, folderId, name, description, file,
151             contentType, serviceContext);
152     }
153 
154 }