001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.imagegallery.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.OrderByComparator;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portlet.imagegallery.model.IGImage;
023    import com.liferay.portlet.imagegallery.service.base.IGImageServiceBaseImpl;
024    import com.liferay.portlet.imagegallery.service.permission.IGFolderPermission;
025    import com.liferay.portlet.imagegallery.service.permission.IGImagePermission;
026    import com.liferay.portlet.imagegallery.util.comparator.ImageModifiedDateComparator;
027    
028    import java.io.File;
029    
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Alexander Chow
035     */
036    public class IGImageServiceImpl extends IGImageServiceBaseImpl {
037    
038            public IGImage addImage(
039                            long groupId, long folderId, String name, String description,
040                            File file, String contentType, ServiceContext serviceContext)
041                    throws PortalException, SystemException {
042    
043                    IGFolderPermission.check(
044                            getPermissionChecker(), groupId, folderId, ActionKeys.ADD_IMAGE);
045    
046                    return igImageLocalService.addImage(
047                            getUserId(), groupId, folderId, name, description, file,
048                            contentType, serviceContext);
049            }
050    
051            public void deleteImage(long imageId)
052                    throws PortalException, SystemException {
053    
054                    IGImagePermission.check(
055                            getPermissionChecker(), imageId, ActionKeys.DELETE);
056    
057                    igImageLocalService.deleteImage(imageId);
058            }
059    
060            public void deleteImageByFolderIdAndNameWithExtension(
061                            long groupId, long folderId, String nameWithExtension)
062                    throws PortalException, SystemException {
063    
064                    IGImage image =
065                            igImageLocalService.getImageByFolderIdAndNameWithExtension(
066                                    groupId, folderId, nameWithExtension);
067    
068                    deleteImage(image.getImageId());
069            }
070    
071            public List<IGImage> getGroupImages(
072                            long groupId, long userId, int start, int end)
073                    throws SystemException {
074    
075                    OrderByComparator orderByComparator = new ImageModifiedDateComparator();
076    
077                    if (userId <= 0) {
078                            return igImagePersistence.filterFindByGroupId(
079                                    groupId, start, end, orderByComparator);
080                    }
081                    else {
082                            return igImagePersistence.filterFindByG_U(
083                                    groupId, userId, start, end, orderByComparator);
084                    }
085            }
086    
087            public int getGroupImagesCount(long groupId, long userId)
088                    throws SystemException {
089    
090                    if (userId <= 0) {
091                            return igImagePersistence.filterCountByGroupId(groupId);
092                    }
093                    else {
094                            return igImagePersistence.filterCountByG_U(groupId, userId);
095                    }
096            }
097    
098            public IGImage getImage(long imageId)
099                    throws PortalException, SystemException {
100    
101                    IGImagePermission.check(
102                            getPermissionChecker(), imageId, ActionKeys.VIEW);
103    
104                    return igImageLocalService.getImage(imageId);
105            }
106    
107            public IGImage getImageByFolderIdAndNameWithExtension(
108                            long groupId, long folderId, String nameWithExtension)
109                    throws PortalException, SystemException {
110    
111                    IGImage image =
112                            igImageLocalService.getImageByFolderIdAndNameWithExtension(
113                                    groupId, folderId, nameWithExtension);
114    
115                    IGImagePermission.check(
116                            getPermissionChecker(), image, ActionKeys.VIEW);
117    
118                    return image;
119            }
120    
121            public IGImage getImageByLargeImageId(long largeImageId)
122                    throws PortalException, SystemException {
123    
124                    IGImage image = igImageLocalService.getImageByLargeImageId(
125                            largeImageId);
126    
127                    IGImagePermission.check(
128                            getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
129    
130                    return image;
131            }
132    
133            public IGImage getImageBySmallImageId(long smallImageId)
134                    throws PortalException, SystemException {
135    
136                    IGImage image = igImageLocalService.getImageBySmallImageId(
137                            smallImageId);
138    
139                    IGImagePermission.check(
140                            getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
141    
142                    return image;
143            }
144    
145            public List<IGImage> getImages(long groupId, long folderId)
146                    throws SystemException {
147    
148                    return igImagePersistence.filterFindByG_F(groupId, folderId);
149            }
150    
151            public List<IGImage> getImages(
152                            long groupId, long folderId, int start, int end)
153                    throws SystemException {
154    
155                    return igImagePersistence.filterFindByG_F(
156                            groupId, folderId, start, end);
157            }
158    
159            public int getImagesCount(long groupId, long folderId)
160                    throws SystemException {
161    
162                    return igImagePersistence.filterCountByG_F(groupId, folderId);
163            }
164    
165            public IGImage updateImage(
166                            long imageId, long groupId, long folderId, String name,
167                            String description, File file, String contentType,
168                            ServiceContext serviceContext)
169                    throws PortalException, SystemException {
170    
171                    IGImagePermission.check(
172                            getPermissionChecker(), imageId, ActionKeys.UPDATE);
173    
174                    return igImageLocalService.updateImage(
175                            getUserId(), imageId, groupId, folderId, name, description, file,
176                            contentType, serviceContext);
177            }
178    
179    }