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.search.Indexer;
020 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
021 import com.liferay.portal.kernel.util.FileUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.model.ResourceConstants;
024 import com.liferay.portal.model.User;
025 import com.liferay.portal.service.ServiceContext;
026 import com.liferay.portlet.asset.util.AssetUtil;
027 import com.liferay.portlet.imagegallery.DuplicateFolderNameException;
028 import com.liferay.portlet.imagegallery.FolderNameException;
029 import com.liferay.portlet.imagegallery.model.IGFolder;
030 import com.liferay.portlet.imagegallery.model.IGFolderConstants;
031 import com.liferay.portlet.imagegallery.model.IGImage;
032 import com.liferay.portlet.imagegallery.service.base.IGFolderLocalServiceBaseImpl;
033
034 import java.util.ArrayList;
035 import java.util.Date;
036 import java.util.List;
037
038
042 public class IGFolderLocalServiceImpl extends IGFolderLocalServiceBaseImpl {
043
044 public IGFolder addFolder(
045 long userId, long parentFolderId, String name, String description,
046 ServiceContext serviceContext)
047 throws PortalException, SystemException {
048
049 User user = userPersistence.findByPrimaryKey(userId);
050 long groupId = serviceContext.getScopeGroupId();
051 parentFolderId = getParentFolderId(groupId, parentFolderId);
052 Date now = new Date();
053
054 validate(groupId, parentFolderId, name);
055
056 long folderId = counterLocalService.increment();
057
058 IGFolder folder = igFolderPersistence.create(folderId);
059
060 folder.setUuid(serviceContext.getUuid());
061 folder.setGroupId(groupId);
062 folder.setCompanyId(user.getCompanyId());
063 folder.setUserId(user.getUserId());
064 folder.setCreateDate(serviceContext.getCreateDate(now));
065 folder.setModifiedDate(serviceContext.getModifiedDate(now));
066 folder.setParentFolderId(parentFolderId);
067 folder.setName(name);
068 folder.setDescription(description);
069 folder.setExpandoBridgeAttributes(serviceContext);
070
071 igFolderPersistence.update(folder, false);
072
073
074
075 if (serviceContext.getAddCommunityPermissions() ||
076 serviceContext.getAddGuestPermissions()) {
077
078 addFolderResources(
079 folder, serviceContext.getAddCommunityPermissions(),
080 serviceContext.getAddGuestPermissions());
081 }
082 else {
083 addFolderResources(
084 folder, serviceContext.getCommunityPermissions(),
085 serviceContext.getGuestPermissions());
086 }
087
088 return folder;
089 }
090
091 public void addFolderResources(
092 IGFolder folder, boolean addCommunityPermissions,
093 boolean addGuestPermissions)
094 throws PortalException, SystemException {
095
096 resourceLocalService.addResources(
097 folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
098 IGFolder.class.getName(), folder.getFolderId(), false,
099 addCommunityPermissions, addGuestPermissions);
100 }
101
102 public void addFolderResources(
103 IGFolder folder, String[] communityPermissions,
104 String[] guestPermissions)
105 throws PortalException, SystemException {
106
107 resourceLocalService.addModelResources(
108 folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
109 IGFolder.class.getName(), folder.getFolderId(),
110 communityPermissions, guestPermissions);
111 }
112
113 public void addFolderResources(
114 long folderId, boolean addCommunityPermissions,
115 boolean addGuestPermissions)
116 throws PortalException, SystemException {
117
118 IGFolder folder = igFolderPersistence.findByPrimaryKey(folderId);
119
120 addFolderResources(
121 folder, addCommunityPermissions, addGuestPermissions);
122 }
123
124 public void addFolderResources(
125 long folderId, String[] communityPermissions,
126 String[] guestPermissions)
127 throws PortalException, SystemException {
128
129 IGFolder folder = igFolderPersistence.findByPrimaryKey(folderId);
130
131 addFolderResources(folder, communityPermissions, guestPermissions);
132 }
133
134 public void deleteFolder(IGFolder folder)
135 throws PortalException, SystemException {
136
137
138
139 List<IGFolder> folders = igFolderPersistence.findByG_P(
140 folder.getGroupId(), folder.getFolderId());
141
142 for (IGFolder curFolder : folders) {
143 deleteFolder(curFolder);
144 }
145
146
147
148 igFolderPersistence.remove(folder);
149
150
151
152 resourceLocalService.deleteResource(
153 folder.getCompanyId(), IGFolder.class.getName(),
154 ResourceConstants.SCOPE_INDIVIDUAL, folder.getFolderId());
155
156
157
158 igImageLocalService.deleteImages(
159 folder.getGroupId(), folder.getFolderId());
160
161
162
163 expandoValueLocalService.deleteValues(
164 IGFolder.class.getName(), folder.getFolderId());
165 }
166
167 public void deleteFolder(long folderId)
168 throws PortalException, SystemException {
169
170 IGFolder folder = igFolderPersistence.findByPrimaryKey(folderId);
171
172 deleteFolder(folder);
173 }
174
175 public void deleteFolders(long groupId)
176 throws PortalException, SystemException {
177
178 List<IGFolder> folders = igFolderPersistence.findByG_P(
179 groupId, IGFolderConstants.DEFAULT_PARENT_FOLDER_ID);
180
181 for (IGFolder folder : folders) {
182 deleteFolder(folder);
183 }
184 }
185
186 public List<IGFolder> getCompanyFolders(long companyId, int start, int end)
187 throws SystemException {
188
189 return igFolderPersistence.findByCompanyId(companyId, start, end);
190 }
191
192 public int getCompanyFoldersCount(long companyId) throws SystemException {
193 return igFolderPersistence.countByCompanyId(companyId);
194 }
195
196 public IGFolder getFolder(long folderId)
197 throws PortalException, SystemException {
198
199 return igFolderPersistence.findByPrimaryKey(folderId);
200 }
201
202 public IGFolder getFolder(long groupId, long parentFolderId, String name)
203 throws PortalException, SystemException {
204
205 return igFolderPersistence.findByG_P_N(groupId, parentFolderId, name);
206 }
207
208 public List<IGFolder> getFolders(long groupId) throws SystemException {
209 return igFolderPersistence.findByGroupId(groupId);
210 }
211
212 public List<IGFolder> getFolders(long groupId, long parentFolderId)
213 throws SystemException {
214
215 return igFolderPersistence.findByG_P(groupId, parentFolderId);
216 }
217
218 public List<IGFolder> getFolders(
219 long groupId, long parentFolderId, int start, int end)
220 throws SystemException {
221
222 return igFolderPersistence.findByG_P(
223 groupId, parentFolderId, start, end);
224 }
225
226 public int getFoldersCount(long groupId, long parentFolderId)
227 throws SystemException {
228
229 return igFolderPersistence.countByG_P(groupId, parentFolderId);
230 }
231
232 public void getSubfolderIds(
233 List<Long> folderIds, long groupId, long folderId)
234 throws SystemException {
235
236 List<IGFolder> folders = igFolderPersistence.findByG_P(
237 groupId, folderId);
238
239 for (IGFolder folder : folders) {
240 folderIds.add(folder.getFolderId());
241
242 getSubfolderIds(
243 folderIds, folder.getGroupId(), folder.getFolderId());
244 }
245 }
246
247 public IGFolder updateFolder(
248 long folderId, long parentFolderId, String name, String description,
249 boolean mergeWithParentFolder, ServiceContext serviceContext)
250 throws PortalException, SystemException {
251
252
253
254 IGFolder folder = igFolderPersistence.findByPrimaryKey(folderId);
255
256 parentFolderId = getParentFolderId(folder, parentFolderId);
257
258 if (mergeWithParentFolder && (folderId != parentFolderId)) {
259 mergeFolders(folder, parentFolderId);
260
261 return folder;
262 }
263
264
265
266 validate(
267 folder.getFolderId(), folder.getGroupId(), parentFolderId, name);
268
269 folder.setModifiedDate(serviceContext.getModifiedDate(null));
270 folder.setParentFolderId(parentFolderId);
271 folder.setName(name);
272 folder.setDescription(description);
273 folder.setExpandoBridgeAttributes(serviceContext);
274
275 igFolderPersistence.update(folder, false);
276
277 return folder;
278 }
279
280 protected long getParentFolderId(IGFolder folder, long parentFolderId)
281 throws SystemException {
282
283 if (parentFolderId == IGFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
284 return parentFolderId;
285 }
286
287 if (folder.getFolderId() == parentFolderId) {
288 return folder.getParentFolderId();
289 }
290 else {
291 IGFolder parentFolder = igFolderPersistence.fetchByPrimaryKey(
292 parentFolderId);
293
294 if ((parentFolder == null) ||
295 (folder.getGroupId() != parentFolder.getGroupId())) {
296
297 return folder.getParentFolderId();
298 }
299
300 List<Long> subfolderIds = new ArrayList<Long>();
301
302 getSubfolderIds(
303 subfolderIds, folder.getGroupId(), folder.getFolderId());
304
305 if (subfolderIds.contains(parentFolderId)) {
306 return folder.getParentFolderId();
307 }
308
309 return parentFolderId;
310 }
311 }
312
313 protected long getParentFolderId(long groupId, long parentFolderId)
314 throws SystemException {
315
316 if (parentFolderId != IGFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
317 IGFolder parentFolder = igFolderPersistence.fetchByPrimaryKey(
318 parentFolderId);
319
320 if ((parentFolder == null) ||
321 (groupId != parentFolder.getGroupId())) {
322
323 parentFolderId = IGFolderConstants.DEFAULT_PARENT_FOLDER_ID;
324 }
325 }
326
327 return parentFolderId;
328 }
329
330 protected void mergeFolders(IGFolder fromFolder, long toFolderId)
331 throws PortalException, SystemException {
332
333 List<IGFolder> folders = igFolderPersistence.findByG_P(
334 fromFolder.getGroupId(), fromFolder.getFolderId());
335
336 for (IGFolder folder : folders) {
337 mergeFolders(folder, toFolderId);
338 }
339
340 List<IGImage> images = igImagePersistence.findByG_F(
341 fromFolder.getGroupId(), fromFolder.getFolderId());
342
343 for (IGImage image : images) {
344 image.setFolderId(toFolderId);
345
346 igImagePersistence.update(image, false);
347
348 Indexer indexer = IndexerRegistryUtil.getIndexer(IGImage.class);
349
350 indexer.reindex(image);
351 }
352
353 deleteFolder(fromFolder);
354 }
355
356 protected void validate(
357 long folderId, long groupId, long parentFolderId, String name)
358 throws PortalException, SystemException {
359
360 if (!AssetUtil.isValidWord(name)) {
361 throw new FolderNameException();
362 }
363
364 IGFolder folder = igFolderPersistence.fetchByG_P_N(
365 groupId, parentFolderId, name);
366
367 if ((folder != null) && (folder.getFolderId() != folderId)) {
368 throw new DuplicateFolderNameException();
369 }
370
371 if (name.indexOf(StringPool.PERIOD) != -1) {
372 String nameWithExtension = name;
373
374 name = FileUtil.stripExtension(nameWithExtension);
375
376 List<IGImage> images = igImagePersistence.findByG_F_N(
377 groupId, parentFolderId, name);
378
379 for (IGImage image : images) {
380 if (nameWithExtension.equals(image.getNameWithExtension())) {
381 throw new DuplicateFolderNameException();
382 }
383 }
384 }
385 }
386
387 protected void validate(long groupId, long parentFolderId, String name)
388 throws PortalException, SystemException {
389
390 long folderId = 0;
391
392 validate(folderId, groupId, parentFolderId, name);
393 }
394
395 }