001
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.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.util.FileUtil;
022 import com.liferay.portal.kernel.util.MimeTypesUtil;
023 import com.liferay.portal.model.Image;
024 import com.liferay.portal.security.permission.ActionKeys;
025 import com.liferay.portal.service.ServiceContext;
026 import com.liferay.portlet.imagegallery.model.IGFolder;
027 import com.liferay.portlet.imagegallery.model.IGImage;
028 import com.liferay.portlet.imagegallery.service.base.IGFolderServiceBaseImpl;
029 import com.liferay.portlet.imagegallery.service.permission.IGFolderPermission;
030
031 import java.io.File;
032
033 import java.util.List;
034
035
040 public class IGFolderServiceImpl extends IGFolderServiceBaseImpl {
041
042 public IGFolder addFolder(
043 long parentFolderId, String name, String description,
044 ServiceContext serviceContext)
045 throws PortalException, SystemException {
046
047 IGFolderPermission.check(
048 getPermissionChecker(), serviceContext.getScopeGroupId(),
049 parentFolderId, ActionKeys.ADD_FOLDER);
050
051 return igFolderLocalService.addFolder(
052 getUserId(), parentFolderId, name, description, serviceContext);
053 }
054
055 public IGFolder copyFolder(
056 long sourceFolderId, long parentFolderId, String name,
057 String description, ServiceContext serviceContext)
058 throws PortalException, SystemException {
059
060 IGFolder srcFolder = getFolder(sourceFolderId);
061
062 IGFolder destFolder = addFolder(
063 parentFolderId, name, description, serviceContext);
064
065 copyFolder(srcFolder, destFolder, serviceContext);
066
067 return destFolder;
068 }
069
070 public void deleteFolder(long folderId)
071 throws PortalException, SystemException {
072
073 IGFolder folder = igFolderLocalService.getFolder(folderId);
074
075 IGFolderPermission.check(
076 getPermissionChecker(), folder, ActionKeys.DELETE);
077
078 igFolderLocalService.deleteFolder(folderId);
079 }
080
081 public IGFolder getFolder(long folderId)
082 throws PortalException, SystemException {
083
084 IGFolder folder = igFolderLocalService.getFolder(folderId);
085
086 IGFolderPermission.check(
087 getPermissionChecker(), folder, ActionKeys.VIEW);
088
089 return folder;
090 }
091
092 public IGFolder getFolder(long groupId, long parentFolderId, String name)
093 throws PortalException, SystemException {
094
095 IGFolder folder = igFolderLocalService.getFolder(
096 groupId, parentFolderId, name);
097
098 IGFolderPermission.check(
099 getPermissionChecker(), folder, ActionKeys.VIEW);
100
101 return folder;
102 }
103
104 public List<IGFolder> getFolders(long groupId, long parentFolderId)
105 throws SystemException {
106
107 return igFolderPersistence.filterFindByG_P(groupId, parentFolderId);
108 }
109
110 public List<IGFolder> getFolders(
111 long groupId, long parentFolderId, int start, int end)
112 throws SystemException {
113
114 return igFolderPersistence.filterFindByG_P(
115 groupId, parentFolderId, start, end);
116 }
117
118 public int getFoldersCount(long groupId, long parentFolderId)
119 throws SystemException {
120
121 return igFolderPersistence.filterCountByG_P(groupId, parentFolderId);
122 }
123
124 public IGFolder updateFolder(
125 long folderId, long parentFolderId, String name, String description,
126 boolean mergeWithParentFolder, ServiceContext serviceContext)
127 throws PortalException, SystemException {
128
129 IGFolder folder = igFolderLocalService.getFolder(folderId);
130
131 IGFolderPermission.check(
132 getPermissionChecker(), folder, ActionKeys.UPDATE);
133
134 return igFolderLocalService.updateFolder(
135 folderId, parentFolderId, name, description, mergeWithParentFolder,
136 serviceContext);
137 }
138
139 protected void copyFolder(
140 IGFolder srcFolder, IGFolder destFolder,
141 ServiceContext serviceContext)
142 throws PortalException, SystemException {
143
144 List<IGImage> srcImages = igImageService.getImages(
145 srcFolder.getGroupId(), srcFolder.getFolderId());
146
147 for (IGImage srcImage : srcImages) {
148 String name = srcImage.getName();
149 String description = srcImage.getDescription();
150
151 File file = null;
152
153 try {
154 file = FileUtil.createTempFile(srcImage.getImageType());
155
156 Image image = imageLocalService.getImage(
157 srcImage.getLargeImageId());
158
159 byte[] bytes = image.getTextObj();
160
161 FileUtil.write(file, bytes);
162 }
163 catch (Exception e) {
164 _log.error(e, e);
165
166 continue;
167 }
168
169 String contentType = MimeTypesUtil.getContentType(
170 srcImage.getImageType());
171
172 igImageService.addImage(
173 destFolder.getGroupId(), destFolder.getFolderId(), name,
174 description, file, contentType, serviceContext);
175
176 file.delete();
177 }
178
179 List<IGFolder> srcSubfolders = getFolders(
180 srcFolder.getGroupId(), srcFolder.getFolderId());
181
182 for (IGFolder srcSubfolder : srcSubfolders) {
183 String name = srcSubfolder.getName();
184 String description = srcSubfolder.getDescription();
185
186 serviceContext.setScopeGroupId(srcFolder.getGroupId());
187
188 IGFolder destSubfolder = addFolder(
189 destFolder.getFolderId(), name, description, serviceContext);
190
191 copyFolder(srcSubfolder, destSubfolder, serviceContext);
192 }
193 }
194
195 private static Log _log = LogFactoryUtil.getLog(IGFolderServiceImpl.class);
196
197 }