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.portlet.imagegallery.model.IGImage;
22  import com.liferay.portlet.imagegallery.service.base.IGImageServiceBaseImpl;
23  import com.liferay.portlet.imagegallery.service.permission.IGFolderPermission;
24  import com.liferay.portlet.imagegallery.service.permission.IGImagePermission;
25  
26  import java.io.File;
27  
28  import java.util.Iterator;
29  import java.util.List;
30  
31  /**
32   * <a href="IGImageServiceImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class IGImageServiceImpl extends IGImageServiceBaseImpl {
37  
38      public IGImage addImage(
39              long folderId, String name, String description, File file,
40              String contentType, String[] tagsEntries,
41              boolean addCommunityPermissions, boolean addGuestPermissions)
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              tagsEntries, addCommunityPermissions, addGuestPermissions);
50      }
51  
52      public IGImage addImage(
53              long folderId, String name, String description, File file,
54              String contentType, String[] tagsEntries,
55              String[] communityPermissions, String[] guestPermissions)
56          throws PortalException, SystemException {
57  
58          IGFolderPermission.check(
59              getPermissionChecker(), folderId, ActionKeys.ADD_IMAGE);
60  
61          return igImageLocalService.addImage(
62              getUserId(), folderId, name, description, file, contentType,
63              tagsEntries, communityPermissions, guestPermissions);
64      }
65  
66      public void deleteImage(long imageId)
67          throws PortalException, SystemException {
68  
69          IGImagePermission.check(
70              getPermissionChecker(), imageId, ActionKeys.DELETE);
71  
72          igImageLocalService.deleteImage(imageId);
73      }
74  
75      public void deleteImageByFolderIdAndNameWithExtension(
76              long folderId, String nameWithExtension)
77          throws PortalException, SystemException {
78  
79          IGImage image =
80              igImageLocalService.getImageByFolderIdAndNameWithExtension(
81                  folderId, nameWithExtension);
82  
83          deleteImage(image.getImageId());
84      }
85  
86      public IGImage getImage(long imageId)
87          throws PortalException, SystemException {
88  
89          IGImagePermission.check(
90              getPermissionChecker(), imageId, ActionKeys.VIEW);
91  
92          return igImageLocalService.getImage(imageId);
93      }
94  
95      public IGImage getImageByFolderIdAndNameWithExtension(
96              long folderId, String nameWithExtension)
97          throws PortalException, SystemException {
98  
99          IGImage image =
100             igImageLocalService.getImageByFolderIdAndNameWithExtension(
101                 folderId, nameWithExtension);
102 
103         IGImagePermission.check(
104             getPermissionChecker(), image, ActionKeys.VIEW);
105 
106         return image;
107     }
108 
109     public IGImage getImageByLargeImageId(long largeImageId)
110         throws PortalException, SystemException {
111 
112         IGImage image = igImageLocalService.getImageByLargeImageId(
113             largeImageId);
114 
115         IGImagePermission.check(
116             getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
117 
118         return image;
119     }
120 
121     public IGImage getImageBySmallImageId(long smallImageId)
122         throws PortalException, SystemException {
123 
124         IGImage image = igImageLocalService.getImageBySmallImageId(
125             smallImageId);
126 
127         IGImagePermission.check(
128             getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
129 
130         return image;
131     }
132 
133     public List<IGImage> getImages(long folderId)
134         throws PortalException, SystemException {
135 
136         List<IGImage> images = igImageLocalService.getImages(folderId);
137 
138         images = ListUtil.copy(images);
139 
140         Iterator<IGImage> itr = images.iterator();
141 
142         while (itr.hasNext()) {
143             IGImage image = itr.next();
144 
145             if (!IGImagePermission.contains(
146                     getPermissionChecker(), image, ActionKeys.VIEW)) {
147 
148                 itr.remove();
149             }
150         }
151 
152         return images;
153     }
154 
155     public IGImage updateImage(
156             long imageId, long folderId, String name, String description,
157             File file, String contentType, String[] tagsEntries)
158         throws PortalException, SystemException {
159 
160         IGImagePermission.check(
161             getPermissionChecker(), imageId, ActionKeys.UPDATE);
162 
163         return igImageLocalService.updateImage(
164             getUserId(), imageId, folderId, name, description, file,
165             contentType, tagsEntries);
166     }
167 
168 }