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.tags.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.search.BooleanClauseOccur;
31  import com.liferay.portal.kernel.search.BooleanQuery;
32  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
33  import com.liferay.portal.kernel.search.Document;
34  import com.liferay.portal.kernel.search.Field;
35  import com.liferay.portal.kernel.search.Hits;
36  import com.liferay.portal.kernel.search.SearchEngineUtil;
37  import com.liferay.portal.kernel.search.TermQuery;
38  import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
39  import com.liferay.portal.kernel.util.GetterUtil;
40  import com.liferay.portal.kernel.util.InstancePool;
41  import com.liferay.portal.kernel.util.ListUtil;
42  import com.liferay.portal.kernel.util.StringPool;
43  import com.liferay.portal.kernel.util.StringUtil;
44  import com.liferay.portal.kernel.util.Validator;
45  import com.liferay.portal.model.User;
46  import com.liferay.portal.service.ServiceContext;
47  import com.liferay.portal.util.PortalInstances;
48  import com.liferay.portal.util.PortalUtil;
49  import com.liferay.portal.util.PortletKeys;
50  import com.liferay.portal.util.PropsValues;
51  import com.liferay.portlet.blogs.model.BlogsEntry;
52  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
53  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
54  import com.liferay.portlet.imagegallery.model.IGImage;
55  import com.liferay.portlet.journal.model.JournalArticle;
56  import com.liferay.portlet.messageboards.model.MBMessage;
57  import com.liferay.portlet.tags.NoSuchEntryException;
58  import com.liferay.portlet.tags.model.TagsAsset;
59  import com.liferay.portlet.tags.model.TagsAssetDisplay;
60  import com.liferay.portlet.tags.model.TagsAssetType;
61  import com.liferay.portlet.tags.model.TagsEntry;
62  import com.liferay.portlet.tags.model.TagsEntryConstants;
63  import com.liferay.portlet.tags.service.base.TagsAssetLocalServiceBaseImpl;
64  import com.liferay.portlet.tags.util.TagsAssetValidator;
65  import com.liferay.portlet.tags.util.TagsUtil;
66  import com.liferay.portlet.wiki.model.WikiPage;
67  
68  import java.util.ArrayList;
69  import java.util.Date;
70  import java.util.List;
71  
72  /**
73   * <a href="TagsAssetLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
74   *
75   * @author Brian Wing Shun Chan
76   * @author Bruno Farache
77   *
78   */
79  public class TagsAssetLocalServiceImpl extends TagsAssetLocalServiceBaseImpl {
80  
81      public void deleteAsset(long assetId)
82          throws PortalException, SystemException {
83  
84          TagsAsset asset = tagsAssetPersistence.findByPrimaryKey(assetId);
85  
86          deleteAsset(asset);
87      }
88  
89      public void deleteAsset(String className, long classPK)
90          throws SystemException {
91  
92          long classNameId = PortalUtil.getClassNameId(className);
93  
94          TagsAsset asset = tagsAssetPersistence.fetchByC_C(classNameId, classPK);
95  
96          if (asset != null) {
97              deleteAsset(asset);
98          }
99      }
100 
101     public void deleteAsset(TagsAsset asset) throws SystemException {
102         tagsAssetPersistence.remove(asset);
103     }
104 
105     public TagsAsset getAsset(long assetId)
106         throws PortalException, SystemException {
107 
108         return tagsAssetPersistence.findByPrimaryKey(assetId);
109     }
110 
111     public TagsAsset getAsset(String className, long classPK)
112         throws PortalException, SystemException {
113 
114         long classNameId = PortalUtil.getClassNameId(className);
115 
116         return tagsAssetPersistence.findByC_C(classNameId, classPK);
117     }
118 
119     public TagsAssetType[] getAssetTypes(String languageId) {
120         TagsAssetType[] assetTypes =
121             new TagsAssetType[TagsUtil.ASSET_TYPE_CLASS_NAMES.length];
122 
123         for (int i = 0; i < TagsUtil.ASSET_TYPE_CLASS_NAMES.length; i++) {
124             assetTypes[i] = getAssetType(
125                 TagsUtil.ASSET_TYPE_CLASS_NAMES[i], languageId);
126         }
127 
128         return assetTypes;
129     }
130 
131     public List<TagsAsset> getAssets(
132             long[] entryIds, long[] notEntryIds, boolean andOperator,
133             boolean excludeZeroViewCount, int start, int end)
134         throws SystemException {
135 
136         return getAssets(
137             0, new long[0], entryIds, notEntryIds, andOperator,
138             excludeZeroViewCount, null, null, start, end);
139     }
140 
141     public List<TagsAsset> getAssets(
142             long groupId, long[] classNameIds, long[] entryIds,
143             long[] notEntryIds, boolean andOperator,
144             boolean excludeZeroViewCount, int start, int end)
145         throws SystemException {
146 
147         return getAssets(
148             groupId, classNameIds, entryIds, notEntryIds, andOperator,
149             excludeZeroViewCount, null, null, start, end);
150     }
151 
152     public List<TagsAsset> getAssets(
153             long[] entryIds, long[] notEntryIds, boolean andOperator,
154             boolean excludeZeroViewCount, Date publishDate, Date expirationDate,
155             int start, int end)
156         throws SystemException {
157 
158         return getAssets(
159             0, new long[0], entryIds, notEntryIds, andOperator,
160             excludeZeroViewCount, publishDate, expirationDate, start, end);
161     }
162 
163     public List<TagsAsset> getAssets(
164             long groupId, long[] classNameIds, long[] entryIds,
165             long[] notEntryIds, boolean andOperator,
166             boolean excludeZeroViewCount, Date publishDate, Date expirationDate,
167             int start, int end)
168         throws SystemException {
169 
170         if ((entryIds.length == 0) && (notEntryIds.length == 0)) {
171             return tagsAssetFinder.findAssets(
172                 groupId, classNameIds, null, null, null, null,
173                 excludeZeroViewCount, publishDate, expirationDate, start, end);
174         }
175         else if (andOperator) {
176             return tagsAssetFinder.findByAndEntryIds(
177                 groupId, classNameIds, entryIds, notEntryIds, null, null, null,
178                 null, excludeZeroViewCount, publishDate, expirationDate, start,
179                 end);
180         }
181         else {
182             return tagsAssetFinder.findByOrEntryIds(
183                 groupId, classNameIds, entryIds, notEntryIds, null, null, null,
184                 null, excludeZeroViewCount, publishDate, expirationDate, start,
185                 end);
186         }
187     }
188 
189     public List<TagsAsset> getAssets(
190             long[] entryIds, long[] notEntryIds, boolean andOperator,
191             String orderByCol1, String orderByCol2, String orderByType1,
192             String orderByType2, boolean excludeZeroViewCount, Date publishDate,
193             Date expirationDate, int start, int end)
194         throws SystemException {
195 
196         return getAssets(
197             0, new long[0], entryIds, notEntryIds, andOperator,
198             excludeZeroViewCount, publishDate, expirationDate, start, end);
199     }
200 
201     public List<TagsAsset> getAssets(
202             long groupId, long[] classNameIds, long[] entryIds,
203             long[] notEntryIds, boolean andOperator, String orderByCol1,
204             String orderByCol2, String orderByType1, String orderByType2,
205             boolean excludeZeroViewCount, Date publishDate, Date expirationDate,
206             int start, int end)
207         throws SystemException {
208 
209         if ((entryIds.length == 0) && (notEntryIds.length == 0)) {
210             return tagsAssetFinder.findAssets(
211                 groupId, classNameIds, orderByCol1, orderByCol2, orderByType1,
212                 orderByType2, excludeZeroViewCount, publishDate, expirationDate,
213                 start, end);
214         }
215         else if (andOperator) {
216             return tagsAssetFinder.findByAndEntryIds(
217                 groupId, classNameIds, entryIds, notEntryIds, orderByCol1,
218                 orderByCol2, orderByType1, orderByType2, excludeZeroViewCount,
219                 publishDate, expirationDate, start, end);
220         }
221         else {
222             return tagsAssetFinder.findByOrEntryIds(
223                 groupId, classNameIds, entryIds, notEntryIds, orderByCol1,
224                 orderByCol2, orderByType1, orderByType2, excludeZeroViewCount,
225                 publishDate, expirationDate, start, end);
226         }
227     }
228 
229     public int getAssetsCount(
230             long[] entryIds, long[] notEntryIds, boolean andOperator,
231             boolean excludeZeroViewCount)
232         throws SystemException {
233 
234         return getAssetsCount(
235             0, new long[0], entryIds, notEntryIds, andOperator,
236             excludeZeroViewCount, null, null);
237     }
238 
239     public int getAssetsCount(
240             long groupId, long[] entryIds, long[] notEntryIds,
241             boolean andOperator, boolean excludeZeroViewCount)
242         throws SystemException {
243 
244         return getAssetsCount(
245             groupId, new long[0], entryIds, notEntryIds, andOperator,
246             excludeZeroViewCount, null, null);
247     }
248 
249     public int getAssetsCount(
250             long[] entryIds, long[] notEntryIds, boolean andOperator,
251             boolean excludeZeroViewCount, Date publishDate, Date expirationDate)
252         throws SystemException {
253 
254         return getAssetsCount(
255             0, new long[0], entryIds, notEntryIds, andOperator,
256             excludeZeroViewCount, publishDate, expirationDate);
257     }
258 
259     public int getAssetsCount(
260             long groupId, long[] classNameIds, long[] entryIds,
261             long[] notEntryIds, boolean andOperator,
262             boolean excludeZeroViewCount, Date publishDate, Date expirationDate)
263         throws SystemException {
264 
265         if ((entryIds.length == 0) && (notEntryIds.length == 0)) {
266             return tagsAssetFinder.countAssets(
267                 groupId, classNameIds, excludeZeroViewCount, publishDate,
268                 expirationDate);
269         }
270         else if (andOperator) {
271             return tagsAssetFinder.countByAndEntryIds(
272                 groupId, classNameIds, entryIds, notEntryIds,
273                 excludeZeroViewCount, publishDate, expirationDate);
274         }
275         else {
276             return tagsAssetFinder.countByOrEntryIds(
277                 groupId, classNameIds, entryIds, notEntryIds,
278                 excludeZeroViewCount, publishDate, expirationDate);
279         }
280     }
281 
282     public TagsAssetDisplay[] getCompanyAssetDisplays(
283             long companyId, int start, int end, String languageId)
284         throws SystemException {
285 
286         return getAssetDisplays(
287             getCompanyAssets(companyId, start, end), languageId);
288     }
289 
290     public List<TagsAsset> getCompanyAssets(long companyId, int start, int end)
291         throws SystemException {
292 
293         return tagsAssetPersistence.findByCompanyId(companyId, start, end);
294     }
295 
296     public int getCompanyAssetsCount(long companyId) throws SystemException {
297         return tagsAssetPersistence.countByCompanyId(companyId);
298     }
299 
300     public List<TagsAsset> getTopViewedAssets(
301             String className, boolean asc, int start, int end)
302         throws SystemException {
303 
304         return getTopViewedAssets(new String[] {className}, asc, start, end);
305     }
306 
307     public List<TagsAsset> getTopViewedAssets(
308             String[] className, boolean asc, int start, int end)
309         throws SystemException {
310 
311         long[] classNameIds = new long[className.length];
312 
313         for (int i = 0; i < className.length; i++) {
314             classNameIds[i] = PortalUtil.getClassNameId(className[i]);
315         }
316 
317         return tagsAssetFinder.findByViewCount(classNameIds, asc, start, end);
318     }
319 
320     public TagsAsset incrementViewCounter(String className, long classPK)
321         throws SystemException {
322 
323         if (classPK <= 0) {
324             return null;
325         }
326 
327         long classNameId = PortalUtil.getClassNameId(className);
328 
329         TagsAsset asset = tagsAssetPersistence.fetchByC_C(classNameId, classPK);
330 
331         if (asset != null) {
332             asset.setViewCount(asset.getViewCount() + 1);
333 
334             tagsAssetPersistence.update(asset, false);
335         }
336 
337         return asset;
338     }
339 
340     public Hits search(
341             long companyId, String portletId, String keywords, int start,
342             int end)
343         throws SystemException {
344 
345         try {
346             BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
347 
348             if (Validator.isNotNull(portletId)) {
349                 contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
350             }
351             else {
352                 BooleanQuery portletIdsQuery = BooleanQueryFactoryUtil.create();
353 
354                 for (String assetTypePortletId :
355                         TagsUtil.ASSET_TYPE_PORTLET_IDS) {
356 
357                     TermQuery termQuery = TermQueryFactoryUtil.create(
358                         Field.PORTLET_ID, assetTypePortletId);
359 
360                     portletIdsQuery.add(termQuery, BooleanClauseOccur.SHOULD);
361                 }
362 
363                 contextQuery.add(portletIdsQuery, BooleanClauseOccur.MUST);
364             }
365 
366             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
367 
368             if (Validator.isNotNull(keywords)) {
369                 searchQuery.addTerm(Field.TITLE, keywords);
370                 searchQuery.addTerm(Field.CONTENT, keywords);
371                 searchQuery.addTerm(Field.DESCRIPTION, keywords);
372                 searchQuery.addTerm(Field.PROPERTIES, keywords);
373                 searchQuery.addTerm(Field.TAGS_ENTRIES, keywords);
374             }
375 
376             BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
377 
378             fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
379 
380             if (searchQuery.clauses().size() > 0) {
381                 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
382             }
383 
384             return SearchEngineUtil.search(companyId, fullQuery, start, end);
385         }
386         catch (Exception e) {
387             throw new SystemException(e);
388         }
389     }
390 
391     public TagsAssetDisplay[] searchAssetDisplays(
392             long companyId, String portletId, String keywords,
393             String languageId, int start, int end)
394         throws SystemException {
395 
396         List<TagsAsset> assets = new ArrayList<TagsAsset>();
397 
398         Hits hits = search(companyId, portletId, keywords, start, end);
399 
400         List<Document> hitsList = hits.toList();
401 
402         for (Document doc : hitsList) {
403             try {
404                 TagsAsset asset = getAsset(doc);
405 
406                 if (asset != null) {
407                     assets.add(asset);
408                 }
409             }
410             catch (Exception e) {
411                 if (_log.isWarnEnabled()) {
412                     _log.warn(e);
413                 }
414             }
415         }
416 
417         return getAssetDisplays(assets, languageId);
418     }
419 
420     public int searchAssetDisplaysCount(
421             long companyId, String portletId, String keywords,
422             String languageId)
423         throws SystemException {
424 
425         Hits hits = search(
426             companyId, portletId, keywords, QueryUtil.ALL_POS,
427             QueryUtil.ALL_POS);
428 
429         return hits.getLength();
430     }
431 
432     public TagsAsset updateAsset(
433             long userId, long groupId, String className, long classPK,
434             String[] categoryNames, String[] entryNames)
435         throws PortalException, SystemException {
436 
437         return updateAsset(
438             userId, groupId, className, classPK, categoryNames, entryNames,
439             true, null, null, null, null, null, null, null, null, null, 0, 0,
440             null);
441     }
442 
443     public TagsAsset updateAsset(
444             long userId, long groupId, String className, long classPK,
445             String[] categoryNames, String[] entryNames, boolean visible,
446             Date startDate, Date endDate, Date publishDate, Date expirationDate,
447             String mimeType, String title, String description, String summary,
448             String url, int height, int width, Integer priority)
449         throws PortalException, SystemException {
450 
451         return updateAsset(
452             userId, groupId, className, classPK, categoryNames, entryNames,
453             visible, startDate, endDate, publishDate, expirationDate, mimeType,
454             title, description, summary, url, height, width, priority, true);
455     }
456 
457     public TagsAsset updateAsset(
458             long userId, long groupId, String className, long classPK,
459             String[] categoryNames, String[] entryNames, boolean visible,
460             Date startDate, Date endDate, Date publishDate, Date expirationDate,
461             String mimeType, String title, String description, String summary,
462             String url, int height, int width, Integer priority, boolean sync)
463         throws PortalException, SystemException {
464 
465         // Asset
466 
467         User user = userPersistence.findByPrimaryKey(userId);
468         long classNameId = PortalUtil.getClassNameId(className);
469 
470         if (entryNames == null) {
471             entryNames = new String[0];
472         }
473 
474         if (categoryNames == null) {
475             categoryNames = new String[0];
476         }
477 
478         title = StringUtil.shorten(title, 300, StringPool.BLANK);
479         Date now = new Date();
480 
481         validate(className, entryNames);
482 
483         TagsAsset asset = tagsAssetPersistence.fetchByC_C(classNameId, classPK);
484 
485         if (asset == null) {
486             long assetId = counterLocalService.increment();
487 
488             asset = tagsAssetPersistence.create(assetId);
489 
490             asset.setCompanyId(user.getCompanyId());
491             asset.setUserId(user.getUserId());
492             asset.setUserName(user.getFullName());
493             asset.setCreateDate(now);
494             asset.setClassNameId(classNameId);
495             asset.setClassPK(classPK);
496             asset.setVisible(visible);
497             asset.setPublishDate(publishDate);
498             asset.setExpirationDate(expirationDate);
499 
500             if (priority == null) {
501                 asset.setPriority(0);
502             }
503 
504             asset.setViewCount(0);
505         }
506 
507         asset.setGroupId(groupId);
508         asset.setModifiedDate(now);
509         asset.setVisible(visible);
510         asset.setStartDate(startDate);
511         asset.setEndDate(endDate);
512         asset.setPublishDate(publishDate);
513         asset.setExpirationDate(expirationDate);
514         asset.setMimeType(mimeType);
515         asset.setTitle(title);
516         asset.setDescription(description);
517         asset.setSummary(summary);
518         asset.setUrl(url);
519         asset.setHeight(height);
520         asset.setWidth(width);
521 
522         if (priority != null) {
523             asset.setPriority(priority.intValue());
524         }
525 
526         tagsAssetPersistence.update(asset, false);
527 
528         // Entries
529 
530         List<TagsEntry> entries = new ArrayList<TagsEntry>(entryNames.length);
531 
532         for (int i = 0; i < entryNames.length; i++) {
533             TagsEntry entry = null;
534 
535             try {
536                 entry = tagsEntryLocalService.getEntry(
537                     groupId, entryNames[i], TagsEntryConstants.FOLKSONOMY_TAG);
538             }
539             catch (NoSuchEntryException nsee) {
540                 ServiceContext serviceContext = new ServiceContext();
541 
542                 serviceContext.setAddCommunityPermissions(true);
543                 serviceContext.setAddGuestPermissions(true);
544                 serviceContext.setScopeGroupId(groupId);
545 
546                 entry = tagsEntryLocalService.addEntry(
547                     user.getUserId(), null, entryNames[i], null,
548                     PropsValues.TAGS_PROPERTIES_DEFAULT, serviceContext);
549             }
550 
551             if (entry != null) {
552                 entries.add(entry);
553             }
554         }
555 
556         // Categories
557 
558         for (int i = 0; i < categoryNames.length; i++) {
559             try {
560                 TagsEntry entry = tagsEntryLocalService.getEntry(
561                     groupId, categoryNames[i],
562                     TagsEntryConstants.FOLKSONOMY_CATEGORY);
563 
564                 entries.add(entry);
565             }
566             catch (NoSuchEntryException nsee) {
567             }
568         }
569 
570         tagsAssetPersistence.setTagsEntries(asset.getAssetId(), entries);
571 
572         // Synchronize
573 
574         if (!sync) {
575             return asset;
576         }
577 
578         if (className.equals(BlogsEntry.class.getName())) {
579             BlogsEntry entry = blogsEntryPersistence.findByPrimaryKey(classPK);
580 
581             entry.setTitle(title);
582 
583             blogsEntryPersistence.update(entry, false);
584         }
585         else if (className.equals(BookmarksEntry.class.getName())) {
586             BookmarksEntry entry = bookmarksEntryPersistence.findByPrimaryKey(
587                 classPK);
588 
589             entry.setName(title);
590             entry.setComments(description);
591             entry.setUrl(url);
592 
593             bookmarksEntryPersistence.update(entry, false);
594         }
595         else if (className.equals(DLFileEntry.class.getName())) {
596             DLFileEntry fileEntry = dlFileEntryPersistence.findByPrimaryKey(
597                 classPK);
598 
599             fileEntry.setTitle(title);
600             fileEntry.setDescription(description);
601 
602             dlFileEntryPersistence.update(fileEntry, false);
603         }
604         else if (className.equals(JournalArticle.class.getName())) {
605             JournalArticle article = journalArticlePersistence.findByPrimaryKey(
606                 classPK);
607 
608             article.setTitle(title);
609             article.setDescription(description);
610 
611             journalArticlePersistence.update(article, false);
612         }
613         else if (className.equals(MBMessage.class.getName())) {
614             MBMessage message = mbMessagePersistence.findByPrimaryKey(classPK);
615 
616             message.setSubject(title);
617 
618             mbMessagePersistence.update(message, false);
619         }
620         else if (className.equals(WikiPage.class.getName())) {
621             WikiPage page = wikiPagePersistence.findByPrimaryKey(classPK);
622 
623             page.setTitle(title);
624 
625             wikiPagePersistence.update(page, false);
626         }
627 
628         return asset;
629     }
630 
631     public TagsAsset updateVisible(
632             String className, long classPK, boolean visible)
633         throws PortalException, SystemException {
634 
635         long classNameId = PortalUtil.getClassNameId(className);
636 
637         TagsAsset asset = tagsAssetPersistence.findByC_C(classNameId, classPK);
638 
639         asset.setVisible(visible);
640 
641         tagsAssetPersistence.update(asset, false);
642 
643         return asset;
644     }
645 
646     public void validate(String className, String[] entryNames)
647         throws PortalException {
648 
649         TagsAssetValidator validator = (TagsAssetValidator)InstancePool.get(
650             PropsValues.TAGS_ASSET_VALIDATOR);
651 
652         validator.validate(className, entryNames);
653     }
654 
655     protected TagsAsset getAsset(Document doc)
656         throws PortalException, SystemException {
657 
658         String portletId = GetterUtil.getString(doc.get(Field.PORTLET_ID));
659 
660         if (portletId.equals(PortletKeys.BLOGS)) {
661             long entryId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
662 
663             long classNameId = PortalUtil.getClassNameId(
664                 BlogsEntry.class.getName());
665             long classPK = entryId;
666 
667             return tagsAssetPersistence.findByC_C(classNameId, classPK);
668         }
669         else if (portletId.equals(PortletKeys.BOOKMARKS)) {
670             long entryId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
671 
672             long classNameId = PortalUtil.getClassNameId(
673                 BookmarksEntry.class.getName());
674             long classPK = entryId;
675 
676             return tagsAssetPersistence.findByC_C(classNameId, classPK);
677         }
678         else if (portletId.equals(PortletKeys.DOCUMENT_LIBRARY)) {
679             long folderId = GetterUtil.getLong(doc.get("repositoryId"));
680             String name = doc.get("path");
681 
682             DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
683                 folderId, name);
684 
685             long classNameId = PortalUtil.getClassNameId(
686                 DLFileEntry.class.getName());
687             long classPK = fileEntry.getFileEntryId();
688 
689             return tagsAssetPersistence.findByC_C(classNameId, classPK);
690         }
691         else if (portletId.equals(PortletKeys.IMAGE_GALLERY)) {
692             long imageId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
693 
694             long classNameId = PortalUtil.getClassNameId(
695                 IGImage.class.getName());
696             long classPK = imageId;
697 
698             return tagsAssetPersistence.findByC_C(classNameId, classPK);
699         }
700         else if (portletId.equals(PortletKeys.JOURNAL)) {
701             long groupId = GetterUtil.getLong(doc.get(Field.GROUP_ID));
702             String articleId = doc.get(Field.ENTRY_CLASS_PK);
703             //double version = GetterUtil.getDouble(doc.get("version"));
704 
705             long articleResourcePrimKey =
706                 journalArticleResourceLocalService.getArticleResourcePrimKey(
707                     groupId, articleId);
708 
709             long classNameId = PortalUtil.getClassNameId(
710                 JournalArticle.class.getName());
711             long classPK = articleResourcePrimKey;
712 
713             return tagsAssetPersistence.findByC_C(classNameId, classPK);
714         }
715         else if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
716             long messageId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
717 
718             long classNameId = PortalUtil.getClassNameId(
719                 MBMessage.class.getName());
720             long classPK = messageId;
721 
722             return tagsAssetPersistence.findByC_C(classNameId, classPK);
723         }
724         else if (portletId.equals(PortletKeys.WIKI)) {
725             long nodeId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
726             String title = doc.get(Field.TITLE);
727 
728             long pageResourcePrimKey =
729                 wikiPageResourceLocalService.getPageResourcePrimKey(
730                     nodeId, title);
731 
732             long classNameId = PortalUtil.getClassNameId(
733                 WikiPage.class.getName());
734             long classPK = pageResourcePrimKey;
735 
736             return tagsAssetPersistence.findByC_C(classNameId, classPK);
737         }
738 
739         return null;
740     }
741 
742     protected TagsAssetDisplay[] getAssetDisplays(
743             List<TagsAsset> assets, String languageId)
744         throws SystemException {
745 
746         TagsAssetDisplay[] assetDisplays = new TagsAssetDisplay[assets.size()];
747 
748         for (int i = 0; i < assets.size(); i++) {
749             TagsAsset asset = assets.get(i);
750 
751             String className = PortalUtil.getClassName(asset.getClassNameId());
752             String portletId = PortalUtil.getClassNamePortletId(className);
753             String portletTitle = PortalUtil.getPortletTitle(
754                 portletId, asset.getCompanyId(), languageId);
755 
756             List<TagsEntry> tagsEntriesList =
757                 tagsAssetPersistence.getTagsEntries(asset.getAssetId());
758 
759             String tagsEntries = ListUtil.toString(
760                 tagsEntriesList, "name", ", ");
761 
762             TagsAssetDisplay assetDisplay = new TagsAssetDisplay();
763 
764             assetDisplay.setAssetId(asset.getAssetId());
765             assetDisplay.setCompanyId(asset.getCompanyId());
766             assetDisplay.setUserId(asset.getUserId());
767             assetDisplay.setUserName(asset.getUserName());
768             assetDisplay.setCreateDate(asset.getCreateDate());
769             assetDisplay.setModifiedDate(asset.getModifiedDate());
770             assetDisplay.setClassNameId(asset.getClassNameId());
771             assetDisplay.setClassName(className);
772             assetDisplay.setClassPK(asset.getClassPK());
773             assetDisplay.setPortletId(portletId);
774             assetDisplay.setPortletTitle(portletTitle);
775             assetDisplay.setStartDate(asset.getStartDate());
776             assetDisplay.setEndDate(asset.getEndDate());
777             assetDisplay.setPublishDate(asset.getPublishDate());
778             assetDisplay.setExpirationDate(asset.getExpirationDate());
779             assetDisplay.setMimeType(asset.getMimeType());
780             assetDisplay.setTitle(asset.getTitle());
781             assetDisplay.setDescription(asset.getDescription());
782             assetDisplay.setSummary(asset.getSummary());
783             assetDisplay.setUrl(asset.getUrl());
784             assetDisplay.setHeight(asset.getHeight());
785             assetDisplay.setWidth(asset.getWidth());
786             assetDisplay.setPriority(asset.getPriority());
787             assetDisplay.setViewCount(asset.getViewCount());
788             assetDisplay.setTagsEntries(tagsEntries);
789 
790             assetDisplays[i] = assetDisplay;
791         }
792 
793         return assetDisplays;
794     }
795 
796     protected TagsAssetType getAssetType(String className, String languageId) {
797         long companyId = PortalInstances.getDefaultCompanyId();
798 
799         long classNameId = PortalUtil.getClassNameId(className);
800 
801         String portletId = PortalUtil.getClassNamePortletId(className);
802         String portletTitle = PortalUtil.getPortletTitle(
803             portletId, companyId, languageId);
804 
805         TagsAssetType assetType = new TagsAssetType();
806 
807         assetType.setClassNameId(classNameId);
808         assetType.setClassName(className);
809         assetType.setPortletId(portletId);
810         assetType.setPortletTitle(portletTitle);
811 
812         return assetType;
813     }
814 
815     private static Log _log =
816         LogFactoryUtil.getLog(TagsAssetLocalServiceImpl.class);
817 
818 }