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