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