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.search.BooleanClauseOccur;
28  import com.liferay.portal.kernel.search.BooleanQuery;
29  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
30  import com.liferay.portal.kernel.search.Field;
31  import com.liferay.portal.kernel.search.Hits;
32  import com.liferay.portal.kernel.search.SearchEngineUtil;
33  import com.liferay.portal.kernel.search.TermQuery;
34  import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
35  import com.liferay.portal.kernel.util.FileUtil;
36  import com.liferay.portal.kernel.util.GetterUtil;
37  import com.liferay.portal.kernel.util.StringPool;
38  import com.liferay.portal.kernel.util.Validator;
39  import com.liferay.portal.model.ResourceConstants;
40  import com.liferay.portal.model.User;
41  import com.liferay.portal.service.ServiceContext;
42  import com.liferay.portlet.expando.model.ExpandoBridge;
43  import com.liferay.portlet.imagegallery.DuplicateFolderNameException;
44  import com.liferay.portlet.imagegallery.FolderNameException;
45  import com.liferay.portlet.imagegallery.model.IGFolder;
46  import com.liferay.portlet.imagegallery.model.IGImage;
47  import com.liferay.portlet.imagegallery.model.impl.IGFolderImpl;
48  import com.liferay.portlet.imagegallery.service.base.IGFolderLocalServiceBaseImpl;
49  import com.liferay.portlet.imagegallery.util.Indexer;
50  import com.liferay.portlet.tags.util.TagsUtil;
51  
52  import java.util.ArrayList;
53  import java.util.Date;
54  import java.util.List;
55  
56  /**
57   * <a href="IGFolderLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   *
61   */
62  public class IGFolderLocalServiceImpl extends IGFolderLocalServiceBaseImpl {
63  
64      public IGFolder addFolder(
65              long userId, long parentFolderId, String name, String description,
66              ServiceContext serviceContext)
67          throws PortalException, SystemException {
68  
69          return addFolder(
70              null, userId, parentFolderId, name, description, serviceContext);
71      }
72  
73      public IGFolder addFolder(
74              String uuid, long userId, long parentFolderId, String name,
75              String description, ServiceContext serviceContext)
76          throws PortalException, SystemException {
77  
78          User user = userPersistence.findByPrimaryKey(userId);
79          long groupId = serviceContext.getScopeGroupId();
80          parentFolderId = getParentFolderId(groupId, parentFolderId);
81          Date now = new Date();
82  
83          validate(groupId, parentFolderId, name);
84  
85          long folderId = counterLocalService.increment();
86  
87          IGFolder folder = igFolderPersistence.create(folderId);
88  
89          folder.setUuid(uuid);
90          folder.setGroupId(groupId);
91          folder.setCompanyId(user.getCompanyId());
92          folder.setUserId(user.getUserId());
93          folder.setCreateDate(now);
94          folder.setModifiedDate(now);
95          folder.setParentFolderId(parentFolderId);
96          folder.setName(name);
97          folder.setDescription(description);
98  
99          igFolderPersistence.update(folder, false);
100 
101         // Resources
102 
103         if (serviceContext.getAddCommunityPermissions() ||
104             serviceContext.getAddGuestPermissions()) {
105 
106             addFolderResources(
107                 folder, serviceContext.getAddCommunityPermissions(),
108                 serviceContext.getAddGuestPermissions());
109         }
110         else {
111             addFolderResources(
112                 folder, serviceContext.getCommunityPermissions(),
113                 serviceContext.getGuestPermissions());
114         }
115 
116         // Expando
117 
118         ExpandoBridge expandoBridge = folder.getExpandoBridge();
119 
120         expandoBridge.setAttributes(serviceContext);
121 
122         return folder;
123     }
124 
125     public void addFolderResources(
126             long folderId, boolean addCommunityPermissions,
127             boolean addGuestPermissions)
128         throws PortalException, SystemException {
129 
130         IGFolder folder = igFolderPersistence.findByPrimaryKey(folderId);
131 
132         addFolderResources(
133             folder, addCommunityPermissions, addGuestPermissions);
134     }
135 
136     public void addFolderResources(
137             IGFolder folder, boolean addCommunityPermissions,
138             boolean addGuestPermissions)
139         throws PortalException, SystemException {
140 
141         resourceLocalService.addResources(
142             folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
143             IGFolder.class.getName(), folder.getFolderId(), false,
144             addCommunityPermissions, addGuestPermissions);
145     }
146 
147     public void addFolderResources(
148             long folderId, String[] communityPermissions,
149             String[] guestPermissions)
150         throws PortalException, SystemException {
151 
152         IGFolder folder = igFolderPersistence.findByPrimaryKey(folderId);
153 
154         addFolderResources(folder, communityPermissions, guestPermissions);
155     }
156 
157     public void addFolderResources(
158             IGFolder folder, String[] communityPermissions,
159             String[] guestPermissions)
160         throws PortalException, SystemException {
161 
162         resourceLocalService.addModelResources(
163             folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
164             IGFolder.class.getName(), folder.getFolderId(),
165             communityPermissions, guestPermissions);
166     }
167 
168     public void deleteFolder(long folderId)
169         throws PortalException, SystemException {
170 
171         IGFolder folder = igFolderPersistence.findByPrimaryKey(folderId);
172 
173         deleteFolder(folder);
174     }
175 
176     public void deleteFolder(IGFolder folder)
177         throws PortalException, SystemException {
178 
179         // Folders
180 
181         List<IGFolder> folders = igFolderPersistence.findByG_P(
182             folder.getGroupId(), folder.getFolderId());
183 
184         for (IGFolder curFolder : folders) {
185             deleteFolder(curFolder);
186         }
187 
188         // Images
189 
190         igImageLocalService.deleteImages(folder.getFolderId());
191 
192         // Expando
193 
194         expandoValueLocalService.deleteValues(
195             IGFolder.class.getName(), folder.getFolderId());
196 
197         // Resources
198 
199         resourceLocalService.deleteResource(
200             folder.getCompanyId(), IGFolder.class.getName(),
201             ResourceConstants.SCOPE_INDIVIDUAL, folder.getFolderId());
202 
203         // Folder
204 
205         igFolderPersistence.remove(folder);
206     }
207 
208     public void deleteFolders(long groupId)
209         throws PortalException, SystemException {
210 
211         List<IGFolder> folders = igFolderPersistence.findByG_P(
212             groupId, IGFolderImpl.DEFAULT_PARENT_FOLDER_ID);
213 
214         for (IGFolder folder : folders) {
215             deleteFolder(folder);
216         }
217     }
218 
219     public IGFolder getFolder(long folderId)
220         throws PortalException, SystemException {
221 
222         return igFolderPersistence.findByPrimaryKey(folderId);
223     }
224 
225     public IGFolder getFolder(long groupId, long parentFolderId, String name)
226         throws PortalException, SystemException {
227 
228         return igFolderPersistence.findByG_P_N(groupId, parentFolderId, name);
229     }
230 
231     public List<IGFolder> getFolders(long groupId) throws SystemException {
232         return igFolderPersistence.findByGroupId(groupId);
233     }
234 
235     public List<IGFolder> getFolders(long groupId, long parentFolderId)
236         throws SystemException {
237 
238         return igFolderPersistence.findByG_P(groupId, parentFolderId);
239     }
240 
241     public List<IGFolder> getFolders(
242             long groupId, long parentFolderId, int start, int end)
243         throws SystemException {
244 
245         return igFolderPersistence.findByG_P(
246             groupId, parentFolderId, start, end);
247     }
248 
249     public int getFoldersCount(long groupId, long parentFolderId)
250         throws SystemException {
251 
252         return igFolderPersistence.countByG_P(groupId, parentFolderId);
253     }
254 
255     public void getSubfolderIds(
256             List<Long> folderIds, long groupId, long folderId)
257         throws SystemException {
258 
259         List<IGFolder> folders = igFolderPersistence.findByG_P(
260             groupId, folderId);
261 
262         for (IGFolder folder : folders) {
263             folderIds.add(folder.getFolderId());
264 
265             getSubfolderIds(
266                 folderIds, folder.getGroupId(), folder.getFolderId());
267         }
268     }
269 
270     public void reIndex(String[] ids) throws SystemException {
271         if (SearchEngineUtil.isIndexReadOnly()) {
272             return;
273         }
274 
275         long companyId = GetterUtil.getLong(ids[0]);
276 
277         try {
278             reIndexFolders(companyId);
279         }
280         catch (SystemException se) {
281             throw se;
282         }
283         catch (Exception e) {
284             throw new SystemException(e);
285         }
286     }
287 
288     public Hits search(
289             long companyId, long groupId, long[] folderIds, String keywords,
290             int start, int end)
291         throws SystemException {
292 
293         try {
294             BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
295 
296             contextQuery.addRequiredTerm(Field.PORTLET_ID, Indexer.PORTLET_ID);
297 
298             if (groupId > 0) {
299                 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
300             }
301 
302             if ((folderIds != null) && (folderIds.length > 0)) {
303                 BooleanQuery folderIdsQuery = BooleanQueryFactoryUtil.create();
304 
305                 for (long folderId : folderIds) {
306                     TermQuery termQuery = TermQueryFactoryUtil.create(
307                         "folderId", folderId);
308 
309                     folderIdsQuery.add(termQuery, BooleanClauseOccur.SHOULD);
310                 }
311 
312                 contextQuery.add(folderIdsQuery, BooleanClauseOccur.MUST);
313             }
314 
315             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
316 
317             if (Validator.isNotNull(keywords)) {
318                 searchQuery.addTerm(Field.TITLE, keywords);
319                 searchQuery.addTerm(Field.DESCRIPTION, keywords);
320                 searchQuery.addTerm(Field.TAGS_ENTRIES, keywords);
321             }
322 
323             BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
324 
325             fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
326 
327             if (searchQuery.clauses().size() > 0) {
328                 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
329             }
330 
331             return SearchEngineUtil.search(companyId, fullQuery, start, end);
332         }
333         catch (Exception e) {
334             throw new SystemException(e);
335         }
336     }
337 
338     public IGFolder updateFolder(
339             long folderId, long parentFolderId, String name, String description,
340             boolean mergeWithParentFolder, ServiceContext serviceContext)
341         throws PortalException, SystemException {
342 
343         // Folder
344 
345         IGFolder folder = igFolderPersistence.findByPrimaryKey(folderId);
346 
347         parentFolderId = getParentFolderId(folder, parentFolderId);
348 
349         validate(
350             folder.getFolderId(), folder.getGroupId(), parentFolderId, name);
351 
352         folder.setModifiedDate(new Date());
353         folder.setParentFolderId(parentFolderId);
354         folder.setName(name);
355         folder.setDescription(description);
356 
357         igFolderPersistence.update(folder, false);
358 
359         // Expando
360 
361         ExpandoBridge expandoBridge = folder.getExpandoBridge();
362 
363         expandoBridge.setAttributes(serviceContext);
364 
365         // Merge folders
366 
367         if (mergeWithParentFolder && (folderId != parentFolderId) &&
368             (parentFolderId != IGFolderImpl.DEFAULT_PARENT_FOLDER_ID)) {
369 
370             mergeFolders(folder, parentFolderId);
371         }
372 
373         return folder;
374     }
375 
376     protected long getParentFolderId(long groupId, long parentFolderId)
377         throws SystemException {
378 
379         if (parentFolderId != IGFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
380             IGFolder parentFolder = igFolderPersistence.fetchByPrimaryKey(
381                 parentFolderId);
382 
383             if ((parentFolder == null) ||
384                 (groupId != parentFolder.getGroupId())) {
385 
386                 parentFolderId = IGFolderImpl.DEFAULT_PARENT_FOLDER_ID;
387             }
388         }
389 
390         return parentFolderId;
391     }
392 
393     protected long getParentFolderId(IGFolder folder, long parentFolderId)
394         throws SystemException {
395 
396         if (parentFolderId == IGFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
397             return parentFolderId;
398         }
399 
400         if (folder.getFolderId() == parentFolderId) {
401             return folder.getParentFolderId();
402         }
403         else {
404             IGFolder parentFolder = igFolderPersistence.fetchByPrimaryKey(
405                 parentFolderId);
406 
407             if ((parentFolder == null) ||
408                 (folder.getGroupId() != parentFolder.getGroupId())) {
409 
410                 return folder.getParentFolderId();
411             }
412 
413             List<Long> subfolderIds = new ArrayList<Long>();
414 
415             getSubfolderIds(
416                 subfolderIds, folder.getGroupId(), folder.getFolderId());
417 
418             if (subfolderIds.contains(parentFolderId)) {
419                 return folder.getParentFolderId();
420             }
421 
422             return parentFolderId;
423         }
424     }
425 
426     protected void mergeFolders(IGFolder fromFolder, long toFolderId)
427         throws PortalException, SystemException {
428 
429         List<IGFolder> folders = igFolderPersistence.findByG_P(
430             fromFolder.getGroupId(), fromFolder.getFolderId());
431 
432         for (IGFolder folder : folders) {
433             mergeFolders(folder, toFolderId);
434         }
435 
436         List<IGImage> images = igImagePersistence.findByFolderId(
437             fromFolder.getFolderId());
438 
439         for (IGImage image : images) {
440             image.setFolderId(toFolderId);
441 
442             igImagePersistence.update(image, false);
443         }
444 
445         deleteFolder(fromFolder);
446     }
447 
448     protected void reIndexFolders(long companyId) throws SystemException {
449         int folderCount = igFolderPersistence.countByCompanyId(companyId);
450 
451         int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
452 
453         for (int i = 0; i <= folderPages; i++) {
454             int folderStart = (i * Indexer.DEFAULT_INTERVAL);
455             int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
456 
457             reIndexFolders(companyId, folderStart, folderEnd);
458         }
459     }
460 
461     protected void reIndexFolders(
462             long companyId, int folderStart, int folderEnd)
463         throws SystemException {
464 
465         List<IGFolder> folders = igFolderPersistence.findByCompanyId(
466             companyId, folderStart, folderEnd);
467 
468         for (IGFolder folder : folders) {
469             long folderId = folder.getFolderId();
470 
471             int entryCount = igImagePersistence.countByFolderId(folderId);
472 
473             int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
474 
475             for (int i = 0; i <= entryPages; i++) {
476                 int entryStart = (i * Indexer.DEFAULT_INTERVAL);
477                 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
478 
479                 reIndexImages(folderId, entryStart, entryEnd);
480             }
481         }
482     }
483 
484     protected void reIndexImages(long folderId, int entryStart, int entryEnd)
485         throws SystemException {
486 
487         List<IGImage> images = igImagePersistence.findByFolderId(
488             folderId, entryStart, entryEnd);
489 
490         for (IGImage image : images) {
491             igImageLocalService.reIndex(image);
492         }
493     }
494 
495     protected void validate(long groupId, long parentFolderId, String name)
496         throws PortalException, SystemException {
497 
498         long folderId = 0;
499 
500         validate(folderId, groupId, parentFolderId, name);
501     }
502 
503     protected void validate(
504             long folderId, long groupId, long parentFolderId, String name)
505         throws PortalException, SystemException {
506 
507         if (!TagsUtil.isValidWord(name)) {
508             throw new FolderNameException();
509         }
510 
511         IGFolder folder = igFolderPersistence.fetchByG_P_N(
512             groupId, parentFolderId, name);
513 
514         if ((folder != null) && (folder.getFolderId() != folderId)) {
515             throw new DuplicateFolderNameException();
516         }
517 
518         if (name.indexOf(StringPool.PERIOD) != -1) {
519             String nameWithExtension = name;
520 
521             name = FileUtil.stripExtension(nameWithExtension);
522 
523             List<IGImage> images = igImagePersistence.findByF_N(
524                 parentFolderId, name);
525 
526             for (IGImage image : images) {
527                 if (nameWithExtension.equals(image.getNameWithExtension())) {
528                     throw new DuplicateFolderNameException();
529                 }
530             }
531         }
532     }
533 
534 }