001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.imagegallery.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.search.Hits;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.lucene.LuceneFields;
029: import com.liferay.portal.lucene.LuceneUtil;
030: import com.liferay.portal.model.User;
031: import com.liferay.portal.model.impl.ResourceImpl;
032: import com.liferay.portal.util.PortalUtil;
033: import com.liferay.portlet.imagegallery.FolderNameException;
034: import com.liferay.portlet.imagegallery.model.IGFolder;
035: import com.liferay.portlet.imagegallery.model.IGImage;
036: import com.liferay.portlet.imagegallery.model.impl.IGFolderImpl;
037: import com.liferay.portlet.imagegallery.service.base.IGFolderLocalServiceBaseImpl;
038: import com.liferay.portlet.imagegallery.util.Indexer;
039: import com.liferay.util.lucene.HitsImpl;
040:
041: import java.util.ArrayList;
042: import java.util.Date;
043: import java.util.Iterator;
044: import java.util.List;
045:
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048: import org.apache.lucene.document.Document;
049: import org.apache.lucene.index.IndexWriter;
050: import org.apache.lucene.index.Term;
051: import org.apache.lucene.search.BooleanClause;
052: import org.apache.lucene.search.BooleanQuery;
053: import org.apache.lucene.search.Searcher;
054: import org.apache.lucene.search.TermQuery;
055:
056: /**
057: * <a href="IGFolderLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
058: *
059: * @author Brian Wing Shun Chan
060: *
061: */
062: public class IGFolderLocalServiceImpl extends
063: IGFolderLocalServiceBaseImpl {
064:
065: public IGFolder addFolder(long userId, long plid,
066: long parentFolderId, String name, String description,
067: boolean addCommunityPermissions, boolean addGuestPermissions)
068: throws PortalException, SystemException {
069:
070: return addFolder(null, userId, plid, parentFolderId, name,
071: description, Boolean.valueOf(addCommunityPermissions),
072: Boolean.valueOf(addGuestPermissions), null, null);
073: }
074:
075: public IGFolder addFolder(String uuid, long userId, long plid,
076: long parentFolderId, String name, String description,
077: boolean addCommunityPermissions, boolean addGuestPermissions)
078: throws PortalException, SystemException {
079:
080: return addFolder(uuid, userId, plid, parentFolderId, name,
081: description, Boolean.valueOf(addCommunityPermissions),
082: Boolean.valueOf(addGuestPermissions), null, null);
083: }
084:
085: public IGFolder addFolder(long userId, long plid,
086: long parentFolderId, String name, String description,
087: String[] communityPermissions, String[] guestPermissions)
088: throws PortalException, SystemException {
089:
090: return addFolder(null, userId, plid, parentFolderId, name,
091: description, null, null, communityPermissions,
092: guestPermissions);
093: }
094:
095: public IGFolder addFolder(String uuid, long userId, long plid,
096: long parentFolderId, String name, String description,
097: Boolean addCommunityPermissions,
098: Boolean addGuestPermissions, String[] communityPermissions,
099: String[] guestPermissions) throws PortalException,
100: SystemException {
101:
102: long groupId = PortalUtil.getPortletGroupId(plid);
103:
104: return addFolderToGroup(uuid, userId, groupId, parentFolderId,
105: name, description, addCommunityPermissions,
106: addGuestPermissions, communityPermissions,
107: guestPermissions);
108: }
109:
110: public IGFolder addFolderToGroup(String uuid, long userId,
111: long groupId, long parentFolderId, String name,
112: String description, Boolean addCommunityPermissions,
113: Boolean addGuestPermissions, String[] communityPermissions,
114: String[] guestPermissions) throws PortalException,
115: SystemException {
116:
117: // Folder
118:
119: User user = userPersistence.findByPrimaryKey(userId);
120: parentFolderId = getParentFolderId(groupId, parentFolderId);
121: Date now = new Date();
122:
123: validate(name);
124:
125: long folderId = counterLocalService.increment();
126:
127: IGFolder folder = igFolderPersistence.create(folderId);
128:
129: folder.setUuid(uuid);
130: folder.setGroupId(groupId);
131: folder.setCompanyId(user.getCompanyId());
132: folder.setUserId(user.getUserId());
133: folder.setCreateDate(now);
134: folder.setModifiedDate(now);
135: folder.setParentFolderId(parentFolderId);
136: folder.setName(name);
137: folder.setDescription(description);
138:
139: igFolderPersistence.update(folder);
140:
141: // Resources
142:
143: if ((addCommunityPermissions != null)
144: && (addGuestPermissions != null)) {
145:
146: addFolderResources(folder, addCommunityPermissions
147: .booleanValue(), addGuestPermissions.booleanValue());
148: } else {
149: addFolderResources(folder, communityPermissions,
150: guestPermissions);
151: }
152:
153: return folder;
154: }
155:
156: public void addFolderResources(long folderId,
157: boolean addCommunityPermissions, boolean addGuestPermissions)
158: throws PortalException, SystemException {
159:
160: IGFolder folder = igFolderPersistence
161: .findByPrimaryKey(folderId);
162:
163: addFolderResources(folder, addCommunityPermissions,
164: addGuestPermissions);
165: }
166:
167: public void addFolderResources(IGFolder folder,
168: boolean addCommunityPermissions, boolean addGuestPermissions)
169: throws PortalException, SystemException {
170:
171: resourceLocalService.addResources(folder.getCompanyId(), folder
172: .getGroupId(), folder.getUserId(), IGFolder.class
173: .getName(), folder.getFolderId(), false,
174: addCommunityPermissions, addGuestPermissions);
175: }
176:
177: public void addFolderResources(long folderId,
178: String[] communityPermissions, String[] guestPermissions)
179: throws PortalException, SystemException {
180:
181: IGFolder folder = igFolderPersistence
182: .findByPrimaryKey(folderId);
183:
184: addFolderResources(folder, communityPermissions,
185: guestPermissions);
186: }
187:
188: public void addFolderResources(IGFolder folder,
189: String[] communityPermissions, String[] guestPermissions)
190: throws PortalException, SystemException {
191:
192: resourceLocalService.addModelResources(folder.getCompanyId(),
193: folder.getGroupId(), folder.getUserId(), IGFolder.class
194: .getName(), folder.getFolderId(),
195: communityPermissions, guestPermissions);
196: }
197:
198: public void deleteFolder(long folderId) throws PortalException,
199: SystemException {
200:
201: IGFolder folder = igFolderPersistence
202: .findByPrimaryKey(folderId);
203:
204: deleteFolder(folder);
205: }
206:
207: public void deleteFolder(IGFolder folder) throws PortalException,
208: SystemException {
209:
210: // Folders
211:
212: Iterator itr = igFolderPersistence.findByG_P(
213: folder.getGroupId(), folder.getFolderId()).iterator();
214:
215: while (itr.hasNext()) {
216: IGFolder curFolder = (IGFolder) itr.next();
217:
218: deleteFolder(curFolder);
219: }
220:
221: // Images
222:
223: igImageLocalService.deleteImages(folder.getFolderId());
224:
225: // Resources
226:
227: resourceLocalService.deleteResource(folder.getCompanyId(),
228: IGFolder.class.getName(),
229: ResourceImpl.SCOPE_INDIVIDUAL, folder.getFolderId());
230:
231: // Folder
232:
233: igFolderPersistence.remove(folder.getFolderId());
234: }
235:
236: public void deleteFolders(long groupId) throws PortalException,
237: SystemException {
238:
239: Iterator itr = igFolderPersistence.findByG_P(groupId,
240: IGFolderImpl.DEFAULT_PARENT_FOLDER_ID).iterator();
241:
242: while (itr.hasNext()) {
243: IGFolder folder = (IGFolder) itr.next();
244:
245: deleteFolder(folder);
246: }
247: }
248:
249: public IGFolder getFolder(long folderId) throws PortalException,
250: SystemException {
251:
252: return igFolderPersistence.findByPrimaryKey(folderId);
253: }
254:
255: public List getFolders(long groupId) throws SystemException {
256: return igFolderPersistence.findByGroupId(groupId);
257: }
258:
259: public List getFolders(long groupId, long parentFolderId)
260: throws SystemException {
261:
262: return igFolderPersistence.findByG_P(groupId, parentFolderId);
263: }
264:
265: public List getFolders(long groupId, long parentFolderId,
266: int begin, int end) throws SystemException {
267:
268: return igFolderPersistence.findByG_P(groupId, parentFolderId,
269: begin, end);
270: }
271:
272: public int getFoldersCount(long groupId, long parentFolderId)
273: throws SystemException {
274:
275: return igFolderPersistence.countByG_P(groupId, parentFolderId);
276: }
277:
278: public void getSubfolderIds(List folderIds, long groupId,
279: long folderId) throws SystemException {
280:
281: Iterator itr = igFolderPersistence.findByG_P(groupId, folderId)
282: .iterator();
283:
284: while (itr.hasNext()) {
285: IGFolder folder = (IGFolder) itr.next();
286:
287: folderIds.add(new Long(folder.getFolderId()));
288:
289: getSubfolderIds(folderIds, folder.getGroupId(), folder
290: .getFolderId());
291: }
292: }
293:
294: public void reIndex(String[] ids) throws SystemException {
295: if (LuceneUtil.INDEX_READ_ONLY) {
296: return;
297: }
298:
299: long companyId = GetterUtil.getLong(ids[0]);
300:
301: IndexWriter writer = null;
302:
303: try {
304: writer = LuceneUtil.getWriter(companyId);
305:
306: Iterator itr1 = igFolderPersistence.findByCompanyId(
307: companyId).iterator();
308:
309: while (itr1.hasNext()) {
310: IGFolder folder = (IGFolder) itr1.next();
311:
312: long folderId = folder.getFolderId();
313:
314: Iterator itr2 = igImagePersistence.findByFolderId(
315: folderId).iterator();
316:
317: while (itr2.hasNext()) {
318: IGImage image = (IGImage) itr2.next();
319:
320: long groupId = folder.getGroupId();
321: long imageId = image.getImageId();
322: String description = image.getDescription();
323:
324: String[] tagsEntries = tagsEntryLocalService
325: .getEntryNames(IGImage.class.getName(),
326: imageId);
327:
328: try {
329: Document doc = Indexer.getAddImageDocument(
330: companyId, groupId, folderId, imageId,
331: description, tagsEntries);
332:
333: writer.addDocument(doc);
334: } catch (Exception e1) {
335: _log.error("Reindexing " + imageId, e1);
336: }
337: }
338: }
339: } catch (SystemException se) {
340: throw se;
341: } catch (Exception e2) {
342: throw new SystemException(e2);
343: } finally {
344: try {
345: if (writer != null) {
346: LuceneUtil.write(companyId);
347: }
348: } catch (Exception e) {
349: _log.error(e);
350: }
351: }
352: }
353:
354: public Hits search(long companyId, long groupId, long[] folderIds,
355: String keywords) throws SystemException {
356:
357: Searcher searcher = null;
358:
359: try {
360: HitsImpl hits = new HitsImpl();
361:
362: BooleanQuery contextQuery = new BooleanQuery();
363:
364: LuceneUtil.addRequiredTerm(contextQuery,
365: LuceneFields.PORTLET_ID, Indexer.PORTLET_ID);
366:
367: if (groupId > 0) {
368: LuceneUtil.addRequiredTerm(contextQuery,
369: LuceneFields.GROUP_ID, groupId);
370: }
371:
372: if ((folderIds != null) && (folderIds.length > 0)) {
373: BooleanQuery folderIdsQuery = new BooleanQuery();
374:
375: for (int i = 0; i < folderIds.length; i++) {
376: Term term = new Term("folderId", String
377: .valueOf(folderIds[i]));
378: TermQuery termQuery = new TermQuery(term);
379:
380: folderIdsQuery.add(termQuery,
381: BooleanClause.Occur.SHOULD);
382: }
383:
384: contextQuery.add(folderIdsQuery,
385: BooleanClause.Occur.MUST);
386: }
387:
388: BooleanQuery searchQuery = new BooleanQuery();
389:
390: if (Validator.isNotNull(keywords)) {
391: LuceneUtil.addTerm(searchQuery,
392: LuceneFields.DESCRIPTION, keywords);
393: LuceneUtil.addTerm(searchQuery, LuceneFields.TAG_ENTRY,
394: keywords);
395: }
396:
397: BooleanQuery fullQuery = new BooleanQuery();
398:
399: fullQuery.add(contextQuery, BooleanClause.Occur.MUST);
400:
401: if (searchQuery.clauses().size() > 0) {
402: fullQuery.add(searchQuery, BooleanClause.Occur.MUST);
403: }
404:
405: searcher = LuceneUtil.getSearcher(companyId);
406:
407: hits.recordHits(searcher.search(fullQuery), searcher);
408:
409: return hits;
410: } catch (Exception e) {
411: return LuceneUtil.closeSearcher(searcher, keywords, e);
412: }
413: }
414:
415: public IGFolder updateFolder(long folderId, long parentFolderId,
416: String name, String description,
417: boolean mergeWithParentFolder) throws PortalException,
418: SystemException {
419:
420: // Folder
421:
422: IGFolder folder = igFolderPersistence
423: .findByPrimaryKey(folderId);
424:
425: parentFolderId = getParentFolderId(folder, parentFolderId);
426:
427: validate(name);
428:
429: folder.setModifiedDate(new Date());
430: folder.setParentFolderId(parentFolderId);
431: folder.setName(name);
432: folder.setDescription(description);
433:
434: igFolderPersistence.update(folder);
435:
436: // Merge folders
437:
438: if (mergeWithParentFolder
439: && (folderId != parentFolderId)
440: && (parentFolderId != IGFolderImpl.DEFAULT_PARENT_FOLDER_ID)) {
441:
442: mergeFolders(folder, parentFolderId);
443: }
444:
445: return folder;
446: }
447:
448: protected long getParentFolderId(long groupId, long parentFolderId)
449: throws SystemException {
450:
451: if (parentFolderId != IGFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
452: IGFolder parentFolder = igFolderPersistence
453: .fetchByPrimaryKey(parentFolderId);
454:
455: if ((parentFolder == null)
456: || (groupId != parentFolder.getGroupId())) {
457:
458: parentFolderId = IGFolderImpl.DEFAULT_PARENT_FOLDER_ID;
459: }
460: }
461:
462: return parentFolderId;
463: }
464:
465: protected long getParentFolderId(IGFolder folder,
466: long parentFolderId) throws SystemException {
467:
468: if (parentFolderId == IGFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
469: return parentFolderId;
470: }
471:
472: if (folder.getFolderId() == parentFolderId) {
473: return folder.getParentFolderId();
474: } else {
475: IGFolder parentFolder = igFolderPersistence
476: .fetchByPrimaryKey(parentFolderId);
477:
478: if ((parentFolder == null)
479: || (folder.getGroupId() != parentFolder
480: .getGroupId())) {
481:
482: return folder.getParentFolderId();
483: }
484:
485: List subfolderIds = new ArrayList();
486:
487: getSubfolderIds(subfolderIds, folder.getGroupId(), folder
488: .getFolderId());
489:
490: if (subfolderIds.contains(new Long(parentFolderId))) {
491: return folder.getParentFolderId();
492: }
493:
494: return parentFolderId;
495: }
496: }
497:
498: protected void mergeFolders(IGFolder fromFolder, long toFolderId)
499: throws PortalException, SystemException {
500:
501: Iterator itr = igFolderPersistence.findByG_P(
502: fromFolder.getGroupId(), fromFolder.getFolderId())
503: .iterator();
504:
505: while (itr.hasNext()) {
506: IGFolder folder = (IGFolder) itr.next();
507:
508: mergeFolders(folder, toFolderId);
509: }
510:
511: itr = igImagePersistence.findByFolderId(
512: fromFolder.getFolderId()).iterator();
513:
514: while (itr.hasNext()) {
515:
516: // Image
517:
518: IGImage image = (IGImage) itr.next();
519:
520: image.setFolderId(toFolderId);
521:
522: igImagePersistence.update(image);
523: }
524:
525: igFolderPersistence.remove(fromFolder.getFolderId());
526: }
527:
528: protected void validate(String name) throws PortalException {
529: if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1)
530: || (name.indexOf("//") != -1)) {
531:
532: throw new FolderNameException();
533: }
534: }
535:
536: private static Log _log = LogFactory
537: .getLog(IGFolderLocalServiceImpl.class);
538:
539: }
|