1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.imagegallery.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.ListUtil;
28  import com.liferay.portal.security.permission.ActionKeys;
29  import com.liferay.portal.service.ServiceContext;
30  import com.liferay.portlet.imagegallery.model.IGImage;
31  import com.liferay.portlet.imagegallery.service.base.IGImageServiceBaseImpl;
32  import com.liferay.portlet.imagegallery.service.permission.IGFolderPermission;
33  import com.liferay.portlet.imagegallery.service.permission.IGImagePermission;
34  
35  import java.io.File;
36  
37  import java.util.Iterator;
38  import java.util.List;
39  
40  /**
41   * <a href="IGImageServiceImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class IGImageServiceImpl extends IGImageServiceBaseImpl {
47  
48      public IGImage addImage(
49              long folderId, String name, String description, File file,
50              String contentType, ServiceContext serviceContext)
51          throws PortalException, SystemException {
52  
53          IGFolderPermission.check(
54              getPermissionChecker(), folderId, ActionKeys.ADD_IMAGE);
55  
56          return igImageLocalService.addImage(
57              getUserId(), folderId, name, description, file, contentType,
58              serviceContext);
59      }
60  
61      public void deleteImage(long imageId)
62          throws PortalException, SystemException {
63  
64          IGImagePermission.check(
65              getPermissionChecker(), imageId, ActionKeys.DELETE);
66  
67          igImageLocalService.deleteImage(imageId);
68      }
69  
70      public void deleteImageByFolderIdAndNameWithExtension(
71              long folderId, String nameWithExtension)
72          throws PortalException, SystemException {
73  
74          IGImage image =
75              igImageLocalService.getImageByFolderIdAndNameWithExtension(
76                  folderId, nameWithExtension);
77  
78          deleteImage(image.getImageId());
79      }
80  
81      public IGImage getImage(long imageId)
82          throws PortalException, SystemException {
83  
84          IGImagePermission.check(
85              getPermissionChecker(), imageId, ActionKeys.VIEW);
86  
87          return igImageLocalService.getImage(imageId);
88      }
89  
90      public IGImage getImageByFolderIdAndNameWithExtension(
91              long folderId, String nameWithExtension)
92          throws PortalException, SystemException {
93  
94          IGImage image =
95              igImageLocalService.getImageByFolderIdAndNameWithExtension(
96                  folderId, nameWithExtension);
97  
98          IGImagePermission.check(
99              getPermissionChecker(), image, ActionKeys.VIEW);
100 
101         return image;
102     }
103 
104     public IGImage getImageByLargeImageId(long largeImageId)
105         throws PortalException, SystemException {
106 
107         IGImage image = igImageLocalService.getImageByLargeImageId(
108             largeImageId);
109 
110         IGImagePermission.check(
111             getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
112 
113         return image;
114     }
115 
116     public IGImage getImageBySmallImageId(long smallImageId)
117         throws PortalException, SystemException {
118 
119         IGImage image = igImageLocalService.getImageBySmallImageId(
120             smallImageId);
121 
122         IGImagePermission.check(
123             getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
124 
125         return image;
126     }
127 
128     public List<IGImage> getImages(long folderId)
129         throws PortalException, SystemException {
130 
131         List<IGImage> images = igImageLocalService.getImages(folderId);
132 
133         images = ListUtil.copy(images);
134 
135         Iterator<IGImage> itr = images.iterator();
136 
137         while (itr.hasNext()) {
138             IGImage image = itr.next();
139 
140             if (!IGImagePermission.contains(
141                     getPermissionChecker(), image, ActionKeys.VIEW)) {
142 
143                 itr.remove();
144             }
145         }
146 
147         return images;
148     }
149 
150     public IGImage updateImage(
151             long imageId, long folderId, String name, String description,
152             File file, String contentType, ServiceContext serviceContext)
153         throws PortalException, SystemException {
154 
155         IGImagePermission.check(
156             getPermissionChecker(), imageId, ActionKeys.UPDATE);
157 
158         return igImageLocalService.updateImage(
159             getUserId(), imageId, folderId, name, description, file,
160             contentType, serviceContext);
161     }
162 
163 }