1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.tags.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
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.Document;
31  import com.liferay.portal.kernel.search.Field;
32  import com.liferay.portal.kernel.search.Hits;
33  import com.liferay.portal.kernel.search.SearchEngineUtil;
34  import com.liferay.portal.kernel.search.TermQuery;
35  import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
36  import com.liferay.portal.kernel.util.GetterUtil;
37  import com.liferay.portal.kernel.util.InstancePool;
38  import com.liferay.portal.kernel.util.ListUtil;
39  import com.liferay.portal.kernel.util.StringPool;
40  import com.liferay.portal.kernel.util.StringUtil;
41  import com.liferay.portal.kernel.util.Validator;
42  import com.liferay.portal.model.User;
43  import com.liferay.portal.service.ServiceContext;
44  import com.liferay.portal.util.PortalInstances;
45  import com.liferay.portal.util.PortalUtil;
46  import com.liferay.portal.util.PortletKeys;
47  import com.liferay.portal.util.PropsValues;
48  import com.liferay.portlet.blogs.model.BlogsEntry;
49  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
50  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
51  import com.liferay.portlet.imagegallery.model.IGImage;
52  import com.liferay.portlet.journal.model.JournalArticle;
53  import com.liferay.portlet.messageboards.model.MBMessage;
54  import com.liferay.portlet.tags.NoSuchEntryException;
55  import com.liferay.portlet.tags.model.TagsAsset;
56  import com.liferay.portlet.tags.model.TagsAssetDisplay;
57  import com.liferay.portlet.tags.model.TagsAssetType;
58  import com.liferay.portlet.tags.model.TagsEntry;
59  import com.liferay.portlet.tags.model.TagsEntryConstants;
60  import com.liferay.portlet.tags.service.base.TagsAssetLocalServiceBaseImpl;
61  import com.liferay.portlet.tags.util.TagsAssetValidator;
62  import com.liferay.portlet.tags.util.TagsUtil;
63  import com.liferay.portlet.wiki.model.WikiPage;
64  
65  import java.util.ArrayList;
66  import java.util.Date;
67  import java.util.List;
68  
69  /**
70   * <a href="TagsAssetLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
71   *
72   * @author Brian Wing Shun Chan
73   * @author Bruno Farache
74   *
75   */
76  public class TagsAssetLocalServiceImpl extends TagsAssetLocalServiceBaseImpl {
77  
78      public void deleteAsset(long assetId)
79          throws PortalException, SystemException {
80  
81          TagsAsset asset = tagsAssetPersistence.findByPrimaryKey(assetId);
82  
83          deleteAsset(asset);
84      }
85  
86      public void deleteAsset(String className, long classPK)
87          throws SystemException {
88  
89          long classNameId = PortalUtil.getClassNameId(className);
90  
91          TagsAsset asset = tagsAssetPersistence.fetchByC_C(classNameId, classPK);
92  
93          if (asset != null) {
94              deleteAsset(asset);
95          }
96      }
97  
98      public void deleteAsset(TagsAsset asset) throws SystemException {
99          tagsAssetPersistence.remove(asset);
100     }
101 
102     public TagsAsset getAsset(long assetId)
103         throws PortalException, SystemException {
104 
105         return tagsAssetPersistence.findByPrimaryKey(assetId);
106     }
107 
108     public TagsAsset getAsset(String className, long classPK)
109         throws PortalException, SystemException {
110 
111         long classNameId = PortalUtil.getClassNameId(className);
112 
113         return tagsAssetPersistence.findByC_C(classNameId, classPK);
114     }
115 
116     public TagsAssetType[] getAssetTypes(String languageId) {
117         TagsAssetType[] assetTypes =
118             new TagsAssetType[TagsUtil.ASSET_TYPE_CLASS_NAMES.length];
119 
120         for (int i = 0; i < TagsUtil.ASSET_TYPE_CLASS_NAMES.length; i++) {
121             assetTypes[i] = getAssetType(
122                 TagsUtil.ASSET_TYPE_CLASS_NAMES[i], languageId);
123         }
124 
125         return assetTypes;
126     }
127 
128     public List<TagsAsset> getAssets(
129             long[] entryIds, long[] notEntryIds, boolean andOperator,
130             boolean excludeZeroViewCount, int start, int end)
131         throws SystemException {
132 
133         return getAssets(
134             0, new long[0], entryIds, notEntryIds, andOperator,
135             excludeZeroViewCount, null, null, start, end);
136     }
137 
138     public List<TagsAsset> getAssets(
139             long groupId, long[] classNameIds, long[] entryIds,
140             long[] notEntryIds, boolean andOperator,
141             boolean excludeZeroViewCount, int start, int end)
142         throws SystemException {
143 
144         return getAssets(
145             groupId, classNameIds, entryIds, notEntryIds, andOperator,
146             excludeZeroViewCount, null, null, start, end);
147     }
148 
149     public List<TagsAsset> getAssets(
150             long[] entryIds, long[] notEntryIds, boolean andOperator,
151             boolean excludeZeroViewCount, Date publishDate, Date expirationDate,
152             int start, int end)
153         throws SystemException {
154 
155         return getAssets(
156             0, new long[0], entryIds, notEntryIds, andOperator,
157             excludeZeroViewCount, publishDate, expirationDate, start, end);
158     }
159 
160     public List<TagsAsset> getAssets(
161             long groupId, long[] classNameIds, long[] entryIds,
162             long[] notEntryIds, boolean andOperator,
163             boolean excludeZeroViewCount, Date publishDate, Date expirationDate,
164             int start, int end)
165         throws SystemException {
166 
167         if ((entryIds.length == 0) && (notEntryIds.length == 0)) {
168             return tagsAssetFinder.findAssets(
169                 groupId, classNameIds, null, null, null, null,
170                 excludeZeroViewCount, publishDate, expirationDate, start, end);
171         }
172         else if (andOperator) {
173             return tagsAssetFinder.findByAndEntryIds(
174                 groupId, classNameIds, entryIds, notEntryIds, null, null, null,
175                 null, excludeZeroViewCount, publishDate, expirationDate, start,
176                 end);
177         }
178         else {
179             return tagsAssetFinder.findByOrEntryIds(
180                 groupId, classNameIds, entryIds, notEntryIds, null, null, null,
181                 null, excludeZeroViewCount, publishDate, expirationDate, start,
182                 end);
183         }
184     }
185 
186     public List<TagsAsset> getAssets(
187             long[] entryIds, long[] notEntryIds, boolean andOperator,
188             String orderByCol1, String orderByCol2, String orderByType1,
189             String orderByType2, boolean excludeZeroViewCount, Date publishDate,
190             Date expirationDate, int start, int end)
191         throws SystemException {
192 
193         return getAssets(
194             0, new long[0], entryIds, notEntryIds, andOperator,
195             excludeZeroViewCount, publishDate, expirationDate, start, end);
196     }
197 
198     public List<TagsAsset> getAssets(
199             long groupId, long[] classNameIds, long[] entryIds,
200             long[] notEntryIds, boolean andOperator, String orderByCol1,
201             String orderByCol2, String orderByType1, String orderByType2,
202             boolean excludeZeroViewCount, Date publishDate, Date expirationDate,
203             int start, int end)
204         throws SystemException {
205 
206         if ((entryIds.length == 0) && (notEntryIds.length == 0)) {
207             return tagsAssetFinder.findAssets(
208                 groupId, classNameIds, orderByCol1, orderByCol2, orderByType1,
209                 orderByType2, excludeZeroViewCount, publishDate, expirationDate,
210                 start, end);
211         }
212         else if (andOperator) {
213             return tagsAssetFinder.findByAndEntryIds(
214                 groupId, classNameIds, entryIds, notEntryIds, orderByCol1,
215                 orderByCol2, orderByType1, orderByType2, excludeZeroViewCount,
216                 publishDate, expirationDate, start, end);
217         }
218         else {
219             return tagsAssetFinder.findByOrEntryIds(
220                 groupId, classNameIds, entryIds, notEntryIds, orderByCol1,
221                 orderByCol2, orderByType1, orderByType2, excludeZeroViewCount,
222                 publishDate, expirationDate, start, end);
223         }
224     }
225 
226     public int getAssetsCount(
227             long[] entryIds, long[] notEntryIds, boolean andOperator,
228             boolean excludeZeroViewCount)
229         throws SystemException {
230 
231         return getAssetsCount(
232             0, new long[0], entryIds, notEntryIds, andOperator,
233             excludeZeroViewCount, null, null);
234     }
235 
236     public int getAssetsCount(
237             long groupId, long[] entryIds, long[] notEntryIds,
238             boolean andOperator, boolean excludeZeroViewCount)
239         throws SystemException {
240 
241         return getAssetsCount(
242             groupId, new long[0], entryIds, notEntryIds, andOperator,
243             excludeZeroViewCount, null, null);
244     }
245 
246     public int getAssetsCount(
247             long[] entryIds, long[] notEntryIds, boolean andOperator,
248             boolean excludeZeroViewCount, Date publishDate, Date expirationDate)
249         throws SystemException {
250 
251         return getAssetsCount(
252             0, new long[0], entryIds, notEntryIds, andOperator,
253             excludeZeroViewCount, publishDate, expirationDate);
254     }
255 
256     public int getAssetsCount(
257             long groupId, long[] classNameIds, long[] entryIds,
258             long[] notEntryIds, boolean andOperator,
259             boolean excludeZeroViewCount, Date publishDate, Date expirationDate)
260         throws SystemException {
261 
262         if ((entryIds.length == 0) && (notEntryIds.length == 0)) {
263             return tagsAssetFinder.countAssets(
264                 groupId, classNameIds, excludeZeroViewCount, publishDate,
265                 expirationDate);
266         }
267         else if (andOperator) {
268             return tagsAssetFinder.countByAndEntryIds(
269                 groupId, classNameIds, entryIds, notEntryIds,
270                 excludeZeroViewCount, publishDate, expirationDate);
271         }
272         else {
273             return tagsAssetFinder.countByOrEntryIds(
274                 groupId, classNameIds, entryIds, notEntryIds,
275                 excludeZeroViewCount, publishDate, expirationDate);
276         }
277     }
278 
279     public TagsAssetDisplay[] getCompanyAssetDisplays(
280             long companyId, int start, int end, String languageId)
281         throws SystemException {
282 
283         return getAssetDisplays(
284             getCompanyAssets(companyId, start, end), languageId);
285     }
286 
287     public List<TagsAsset> getCompanyAssets(long companyId, int start, int end)
288         throws SystemException {
289 
290         return tagsAssetPersistence.findByCompanyId(companyId, start, end);
291     }
292 
293     public int getCompanyAssetsCount(long companyId) throws SystemException {
294         return tagsAssetPersistence.countByCompanyId(companyId);
295     }
296 
297     public List<TagsAsset> getTopViewedAssets(
298             String className, boolean asc, int start, int end)
299         throws SystemException {
300 
301         return getTopViewedAssets(new String[] {className}, asc, start, end);
302     }
303 
304     public List<TagsAsset> getTopViewedAssets(
305             String[] className, boolean asc, int start, int end)
306         throws SystemException {
307 
308         long[] classNameIds = new long[className.length];
309 
310         for (int i = 0; i < className.length; i++) {
311             classNameIds[i] = PortalUtil.getClassNameId(className[i]);
312         }
313 
314         return tagsAssetFinder.findByViewCount(classNameIds, asc, start, end);
315     }
316 
317     public TagsAsset incrementViewCounter(String className, long classPK)
318         throws SystemException {
319 
320         if (classPK <= 0) {
321             return null;
322         }
323 
324         long classNameId = PortalUtil.getClassNameId(className);
325 
326         TagsAsset asset = tagsAssetPersistence.fetchByC_C(classNameId, classPK);
327 
328         if (asset != null) {
329             asset.setViewCount(asset.getViewCount() + 1);
330 
331             tagsAssetPersistence.update(asset, false);
332         }
333 
334         return asset;
335     }
336 
337     public Hits search(
338             long companyId, String portletId, String keywords, int start,
339             int end)
340         throws SystemException {
341 
342         try {
343             BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
344 
345             if (Validator.isNotNull(portletId)) {
346                 contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
347             }
348             else {
349                 BooleanQuery portletIdsQuery = BooleanQueryFactoryUtil.create();
350 
351                 for (String assetTypePortletId :
352                         TagsUtil.ASSET_TYPE_PORTLET_IDS) {
353 
354                     TermQuery termQuery = TermQueryFactoryUtil.create(
355                         Field.PORTLET_ID, assetTypePortletId);
356 
357                     portletIdsQuery.add(termQuery, BooleanClauseOccur.SHOULD);
358                 }
359 
360                 contextQuery.add(portletIdsQuery, BooleanClauseOccur.MUST);
361             }
362 
363             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
364 
365             if (Validator.isNotNull(keywords)) {
366                 searchQuery.addTerm(Field.TITLE, keywords);
367                 searchQuery.addTerm(Field.CONTENT, keywords);
368                 searchQuery.addTerm(Field.DESCRIPTION, keywords);
369                 searchQuery.addTerm(Field.PROPERTIES, keywords);
370                 searchQuery.addTerm(Field.TAGS_ENTRIES, keywords);
371             }
372 
373             BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
374 
375             fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
376 
377             if (searchQuery.clauses().size() > 0) {
378                 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
379             }
380 
381             return SearchEngineUtil.search(companyId, fullQuery, start, end);
382         }
383         catch (Exception e) {
384             throw new SystemException(e);
385         }
386     }
387 
388     public TagsAssetDisplay[] searchAssetDisplays(
389             long companyId, String portletId, String keywords,
390             String languageId, int start, int end)
391         throws SystemException {
392 
393         List<TagsAsset> assets = new ArrayList<TagsAsset>();
394 
395         Hits hits = search(companyId, portletId, keywords, start, end);
396 
397         List<Document> hitsList = hits.toList();
398 
399         for (Document doc : hitsList) {
400             try {
401                 TagsAsset asset = getAsset(doc);
402 
403                 if (asset != null) {
404                     assets.add(asset);
405                 }
406             }
407             catch (Exception e) {
408                 if (_log.isWarnEnabled()) {
409                     _log.warn(e);
410                 }
411             }
412         }
413 
414         return getAssetDisplays(assets, languageId);
415     }
416 
417     public int searchAssetDisplaysCount(
418             long companyId, String portletId, String keywords,
419             String languageId)
420         throws SystemException {
421 
422         Hits hits = search(
423             companyId, portletId, keywords, QueryUtil.ALL_POS,
424             QueryUtil.ALL_POS);
425 
426         return hits.getLength();
427     }
428 
429     public TagsAsset updateAsset(
430             long userId, long groupId, String className, long classPK,
431             String[] categoryNames, String[] entryNames)
432         throws PortalException, SystemException {
433 
434         return updateAsset(
435             userId, groupId, className, classPK, categoryNames, entryNames,
436             true, null, null, null, null, null, null, null, null, null, 0, 0,
437             null);
438     }
439 
440     public TagsAsset updateAsset(
441             long userId, long groupId, String className, long classPK,
442             String[] categoryNames, String[] entryNames, boolean visible,
443             Date startDate, Date endDate, Date publishDate, Date expirationDate,
444             String mimeType, String title, String description, String summary,
445             String url, int height, int width, Integer priority)
446         throws PortalException, SystemException {
447 
448         return updateAsset(
449             userId, groupId, className, classPK, categoryNames, entryNames,
450             visible, startDate, endDate, publishDate, expirationDate, mimeType,
451             title, description, summary, url, height, width, priority, true);
452     }
453 
454     public TagsAsset updateAsset(
455             long userId, long groupId, String className, long classPK,
456             String[] categoryNames, String[] entryNames, boolean visible,
457             Date startDate, Date endDate, Date publishDate, Date expirationDate,
458             String mimeType, String title, String description, String summary,
459             String url, int height, int width, Integer priority, boolean sync)
460         throws PortalException, SystemException {
461 
462         // Asset
463 
464         User user = userPersistence.findByPrimaryKey(userId);
465         long classNameId = PortalUtil.getClassNameId(className);
466 
467         if (entryNames == null) {
468             entryNames = new String[0];
469         }
470 
471         if (categoryNames == null) {
472             categoryNames = new String[0];
473         }
474 
475         title = StringUtil.shorten(title, 300, StringPool.BLANK);
476         Date now = new Date();
477 
478         validate(className, entryNames);
479 
480         TagsAsset asset = tagsAssetPersistence.fetchByC_C(classNameId, classPK);
481 
482         if (asset == null) {
483             long assetId = counterLocalService.increment();
484 
485             asset = tagsAssetPersistence.create(assetId);
486 
487             asset.setCompanyId(user.getCompanyId());
488             asset.setUserId(user.getUserId());
489             asset.setUserName(user.getFullName());
490             asset.setCreateDate(now);
491             asset.setClassNameId(classNameId);
492             asset.setClassPK(classPK);
493             asset.setVisible(visible);
494             asset.setPublishDate(publishDate);
495             asset.setExpirationDate(expirationDate);
496 
497             if (priority == null) {
498                 asset.setPriority(0);
499             }
500 
501             asset.setViewCount(0);
502         }
503 
504         asset.setGroupId(groupId);
505         asset.setModifiedDate(now);
506         asset.setVisible(visible);
507         asset.setStartDate(startDate);
508         asset.setEndDate(endDate);
509         asset.setPublishDate(publishDate);
510         asset.setExpirationDate(expirationDate);
511         asset.setMimeType(mimeType);
512         asset.setTitle(title);
513         asset.setDescription(description);
514         asset.setSummary(summary);
515         asset.setUrl(url);
516         asset.setHeight(height);
517         asset.setWidth(width);
518 
519         if (priority != null) {
520             asset.setPriority(priority.intValue());
521         }
522 
523         // Entries
524 
525         List<TagsEntry> entries = new ArrayList<TagsEntry>(entryNames.length);
526 
527         for (int i = 0; i < entryNames.length; i++) {
528             TagsEntry entry = null;
529 
530             try {
531                 entry = tagsEntryLocalService.getEntry(
532                     groupId, entryNames[i], TagsEntryConstants.FOLKSONOMY_TAG);
533             }
534             catch (NoSuchEntryException nsee) {
535                 ServiceContext serviceContext = new ServiceContext();
536 
537                 serviceContext.setAddCommunityPermissions(true);
538                 serviceContext.setAddGuestPermissions(true);
539                 serviceContext.setScopeGroupId(groupId);
540 
541                 entry = tagsEntryLocalService.addEntry(
542                     user.getUserId(), null, entryNames[i], null,
543                     PropsValues.TAGS_PROPERTIES_DEFAULT, serviceContext);
544             }
545 
546             if (entry != null) {
547                 entries.add(entry);
548             }
549         }
550 
551         // Categories
552 
553         for (int i = 0; i < categoryNames.length; i++) {
554             try {
555                 TagsEntry entry = tagsEntryLocalService.getEntry(
556                     groupId, categoryNames[i],
557                     TagsEntryConstants.FOLKSONOMY_CATEGORY);
558 
559                 entries.add(entry);
560             }
561             catch (NoSuchEntryException nsee) {
562             }
563         }
564 
565         tagsAssetPersistence.setTagsEntries(asset.getAssetId(), entries);
566 
567         // Update asset after entries so that asset listeners have access the
568         // saved entries
569 
570         tagsAssetPersistence.update(asset, false);
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 }