001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.asset.service.impl;
016    
017    import com.liferay.portal.NoSuchGroupException;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.increment.BufferedIncrement;
022    import com.liferay.portal.kernel.increment.NumberIncrement;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.search.Document;
026    import com.liferay.portal.kernel.search.Field;
027    import com.liferay.portal.kernel.search.Hits;
028    import com.liferay.portal.kernel.search.Indexer;
029    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
030    import com.liferay.portal.kernel.search.SearchContext;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.InstancePool;
033    import com.liferay.portal.kernel.util.ListUtil;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.model.User;
038    import com.liferay.portal.security.permission.ActionKeys;
039    import com.liferay.portal.service.ServiceContext;
040    import com.liferay.portal.util.PortalUtil;
041    import com.liferay.portal.util.PortletKeys;
042    import com.liferay.portal.util.PropsValues;
043    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
044    import com.liferay.portlet.asset.NoSuchEntryException;
045    import com.liferay.portlet.asset.NoSuchTagException;
046    import com.liferay.portlet.asset.model.AssetCategory;
047    import com.liferay.portlet.asset.model.AssetEntry;
048    import com.liferay.portlet.asset.model.AssetEntryDisplay;
049    import com.liferay.portlet.asset.model.AssetLink;
050    import com.liferay.portlet.asset.model.AssetLinkConstants;
051    import com.liferay.portlet.asset.model.AssetRendererFactory;
052    import com.liferay.portlet.asset.model.AssetTag;
053    import com.liferay.portlet.asset.service.base.AssetEntryLocalServiceBaseImpl;
054    import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
055    import com.liferay.portlet.asset.util.AssetEntryValidator;
056    import com.liferay.portlet.blogs.model.BlogsEntry;
057    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
058    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
059    import com.liferay.portlet.documentlibrary.model.DLFolder;
060    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
061    import com.liferay.portlet.imagegallery.model.IGImage;
062    import com.liferay.portlet.journal.model.JournalArticle;
063    import com.liferay.portlet.messageboards.model.MBMessage;
064    import com.liferay.portlet.wiki.model.WikiPage;
065    
066    import java.io.Serializable;
067    
068    import java.util.ArrayList;
069    import java.util.Date;
070    import java.util.HashMap;
071    import java.util.List;
072    import java.util.Map;
073    
074    /**
075     * @author Brian Wing Shun Chan
076     * @author Bruno Farache
077     * @author Zsolt Berentey
078     */
079    public class AssetEntryLocalServiceImpl extends AssetEntryLocalServiceBaseImpl {
080    
081            public void deleteEntry(AssetEntry entry)
082                    throws PortalException, SystemException {
083    
084                    // Entry
085    
086                    assetEntryPersistence.remove(entry);
087    
088                    // Links
089    
090                    assetLinkLocalService.deleteLinks(entry.getEntryId());
091    
092                    // Social
093    
094                    socialEquityLogLocalService.deactivateEquityLogs(entry.getEntryId());
095            }
096    
097            public void deleteEntry(long entryId)
098                    throws PortalException, SystemException {
099    
100                    AssetEntry entry = assetEntryPersistence.findByPrimaryKey(entryId);
101    
102                    deleteEntry(entry);
103            }
104    
105            public void deleteEntry(String className, long classPK)
106                    throws PortalException, SystemException {
107    
108                    long classNameId = PortalUtil.getClassNameId(className);
109    
110                    AssetEntry entry = assetEntryPersistence.fetchByC_C(
111                            classNameId, classPK);
112    
113                    if (entry != null) {
114                            deleteEntry(entry);
115                    }
116            }
117    
118            public List<AssetEntry> getAncestorEntries(long entryId)
119                    throws PortalException, SystemException {
120    
121                    List<AssetEntry> entries = new ArrayList<AssetEntry>();
122    
123                    AssetEntry parentEntry = getParentEntry(entryId);
124    
125                    while (parentEntry != null) {
126                            entries.add(parentEntry);
127    
128                            parentEntry = getParentEntry(parentEntry.getEntryId());
129                    }
130    
131                    return entries;
132            }
133    
134            public List<AssetEntry> getChildEntries(long entryId)
135                    throws PortalException, SystemException {
136    
137                    List<AssetEntry> entries = new ArrayList<AssetEntry>();
138    
139                    List<AssetLink> links = assetLinkLocalService.getLinks(
140                            entryId, AssetLinkConstants.TYPE_CHILD);
141    
142                    for (AssetLink link : links) {
143                            AssetEntry curAsset = getEntry(link.getEntryId2());
144    
145                            entries.add(curAsset);
146                    }
147    
148                    return entries;
149            }
150    
151            public List<AssetEntry> getCompanyEntries(
152                            long companyId, int start, int end)
153                    throws SystemException {
154    
155                    return assetEntryPersistence.findByCompanyId(companyId, start, end);
156            }
157    
158            public int getCompanyEntriesCount(long companyId) throws SystemException {
159                    return assetEntryPersistence.countByCompanyId(companyId);
160            }
161    
162            public AssetEntryDisplay[] getCompanyEntryDisplays(
163                            long companyId, int start, int end, String languageId)
164                    throws SystemException {
165    
166                    return getEntryDisplays(
167                            getCompanyEntries(companyId, start, end), languageId);
168            }
169    
170            public List<AssetEntry> getEntries(AssetEntryQuery entryQuery)
171                    throws SystemException {
172    
173                    return assetEntryFinder.findEntries(entryQuery);
174            }
175    
176            public int getEntriesCount(AssetEntryQuery entryQuery)
177                    throws SystemException {
178    
179                    return assetEntryFinder.countEntries(entryQuery);
180            }
181    
182            public AssetEntry getEntry(long entryId)
183                    throws PortalException, SystemException {
184    
185                    return assetEntryPersistence.findByPrimaryKey(entryId);
186            }
187    
188            public AssetEntry getEntry(long groupId, String classUuid)
189                    throws PortalException, SystemException {
190    
191                    return assetEntryPersistence.findByG_CU(groupId, classUuid);
192            }
193    
194            public AssetEntry getEntry(String className, long classPK)
195                    throws PortalException, SystemException {
196    
197                    long classNameId = PortalUtil.getClassNameId(className);
198    
199                    return assetEntryPersistence.findByC_C(classNameId, classPK);
200            }
201    
202            public AssetEntry getNextEntry(long entryId)
203                    throws PortalException, SystemException {
204    
205                    try {
206                            getParentEntry(entryId);
207                    }
208                    catch (NoSuchEntryException nsee) {
209                            List<AssetEntry> childEntries = getChildEntries(entryId);
210    
211                            if (childEntries.isEmpty()) {
212                                    throw new NoSuchEntryException();
213                            }
214    
215                            return childEntries.get(0);
216                    }
217    
218                    List<AssetLink> links = assetLinkLocalService.getLinks(
219                            entryId, AssetLinkConstants.TYPE_CHILD);
220    
221                    for (int i = 0; i < links.size(); i++) {
222                            AssetLink link = links.get(i);
223    
224                            if (link.getEntryId2() == entryId) {
225                                    if ((i + 1) >= links.size()) {
226                                            throw new NoSuchEntryException();
227                                    }
228                                    else {
229                                            AssetLink nextLink = links.get(i + 1);
230    
231                                            return getEntry(nextLink.getEntryId2());
232                                    }
233                            }
234                    }
235    
236                    throw new NoSuchEntryException();
237            }
238    
239            public AssetEntry getParentEntry(long entryId)
240                    throws PortalException, SystemException {
241    
242                    List<AssetLink> links = assetLinkLocalService.getReverseLinks(
243                            entryId, AssetLinkConstants.TYPE_CHILD);
244    
245                    if (links.isEmpty()) {
246                            throw new NoSuchEntryException();
247                    }
248    
249                    AssetLink link = links.get(0);
250    
251                    return getEntry(link.getEntryId1());
252            }
253    
254            public AssetEntry getPreviousEntry(long entryId)
255                    throws PortalException, SystemException {
256    
257                    getParentEntry(entryId);
258    
259                    List<AssetLink> links = assetLinkLocalService.getLinks(
260                            entryId, AssetLinkConstants.TYPE_CHILD);
261    
262                    for (int i = 0; i < links.size(); i++) {
263                            AssetLink link = links.get(i);
264    
265                            if (link.getEntryId2() == entryId) {
266                                    if (i == 0) {
267                                            throw new NoSuchEntryException();
268                                    }
269                                    else {
270                                            AssetLink nextAssetLink = links.get(i - 1);
271    
272                                            return getEntry(nextAssetLink.getEntryId2());
273                                    }
274                            }
275                    }
276    
277                    throw new NoSuchEntryException();
278            }
279    
280            public List<AssetEntry> getTopViewedEntries(
281                            String className, boolean asc, int start, int end)
282                    throws SystemException {
283    
284                    return getTopViewedEntries(new String[] {className}, asc, start, end);
285            }
286    
287            public List<AssetEntry> getTopViewedEntries(
288                            String[] className, boolean asc, int start, int end)
289                    throws SystemException {
290    
291                    long[] classNameIds = new long[className.length];
292    
293                    for (int i = 0; i < className.length; i++) {
294                            classNameIds[i] = PortalUtil.getClassNameId(className[i]);
295                    }
296    
297                    AssetEntryQuery entryQuery = new AssetEntryQuery();
298    
299                    entryQuery.setClassNameIds(classNameIds);
300                    entryQuery.setEnd(end);
301                    entryQuery.setExcludeZeroViewCount(true);
302                    entryQuery.setOrderByCol1("viewCount");
303                    entryQuery.setOrderByType1(asc ? "ASC" : "DESC");
304                    entryQuery.setStart(start);
305    
306                    return assetEntryFinder.findEntries(entryQuery);
307            }
308    
309            public void incrementViewCounter(
310                            long userId, String className, long classPK)
311                    throws PortalException, SystemException {
312    
313                    assetEntryLocalService.incrementViewCounter(
314                            userId, className, classPK, 1);
315            }
316    
317            @BufferedIncrement(incrementClass = NumberIncrement.class)
318            public void incrementViewCounter(
319                            long userId, String className, long classPK, int increment)
320                    throws PortalException, SystemException {
321    
322                    if (!PropsValues.ASSET_ENTRY_INCREMENT_VIEW_COUNTER_ENABLED) {
323                            return;
324                    }
325    
326                    if (classPK <= 0) {
327                            return;
328                    }
329    
330                    long classNameId = PortalUtil.getClassNameId(className);
331    
332                    AssetEntry entry = assetEntryPersistence.fetchByC_C(
333                            classNameId, classPK);
334    
335                    if (entry != null) {
336                            entry.setViewCount(entry.getViewCount() + increment);
337    
338                            assetEntryPersistence.update(entry, false);
339    
340                            // Social
341    
342                            if ((userId > 0) && (entry.getUserId() != userId)) {
343                                    socialEquityLogLocalService.addEquityLogs(
344                                            userId, entry.getEntryId(), ActionKeys.VIEW);
345                            }
346                    }
347            }
348    
349            public Hits search(
350                            long companyId, String portletId, String keywords, int start,
351                            int end)
352                    throws SystemException {
353    
354                    try {
355                            SearchContext searchContext = new SearchContext();
356    
357                            searchContext.setCompanyId(companyId);
358                            searchContext.setEnd(end);
359                            searchContext.setKeywords(keywords);
360                            searchContext.setPortletIds(getPortletIds(portletId));
361                            searchContext.setStart(start);
362    
363                            Indexer indexer = IndexerRegistryUtil.getIndexer(AssetEntry.class);
364    
365                            return indexer.search(searchContext);
366                    }
367                    catch (Exception e) {
368                            throw new SystemException(e);
369                    }
370            }
371    
372            public Hits search(
373                            long companyId, long[] groupIds, String portletId, String userName,
374                            String title, String description, String assetCategoryIds,
375                            String assetTagNames, boolean andSearch, int start, int end)
376                    throws SystemException {
377    
378                    try {
379                            Map<String, Serializable> attributes =
380                                    new HashMap<String, Serializable>();
381    
382                            attributes.put(Field.DESCRIPTION, description);
383                            attributes.put(Field.TITLE, title);
384                            attributes.put(Field.USER_NAME, userName);
385    
386                            SearchContext searchContext = new SearchContext();
387    
388                            searchContext.setAndSearch(andSearch);
389                            searchContext.setAttributes(attributes);
390                            searchContext.setCompanyId(companyId);
391                            searchContext.setEnd(end);
392                            searchContext.setGroupIds(groupIds);
393                            searchContext.setPortletIds(getPortletIds(portletId));
394                            searchContext.setStart(start);
395    
396                            Indexer indexer = IndexerRegistryUtil.getIndexer(AssetEntry.class);
397    
398                            return indexer.search(searchContext);
399                    }
400                    catch (Exception e) {
401                            throw new SystemException(e);
402                    }
403            }
404    
405            public AssetEntryDisplay[] searchEntryDisplays(
406                            long companyId, String portletId, String keywords,
407                            String languageId, int start, int end)
408                    throws SystemException {
409    
410                    List<AssetEntry> entries = new ArrayList<AssetEntry>();
411    
412                    Hits hits = search(companyId, portletId, keywords, start, end);
413    
414                    List<Document> hitsList = hits.toList();
415    
416                    for (Document doc : hitsList) {
417                            try {
418                                    AssetEntry entry = getEntry(doc);
419    
420                                    if (entry != null) {
421                                            entries.add(entry);
422                                    }
423                            }
424                            catch (Exception e) {
425                                    if (_log.isWarnEnabled()) {
426                                            _log.warn(e);
427                                    }
428                            }
429                    }
430    
431                    return getEntryDisplays(entries, languageId);
432            }
433    
434            public int searchEntryDisplaysCount(
435                            long companyId, String portletId, String keywords,
436                            String languageId)
437                    throws SystemException {
438    
439                    Hits hits = search(
440                            companyId, portletId, keywords, QueryUtil.ALL_POS,
441                            QueryUtil.ALL_POS);
442    
443                    return hits.getLength();
444            }
445    
446            public AssetEntry updateEntry(
447                            long userId, long groupId, String className, long classPK,
448                            long[] categoryIds, String[] tagNames)
449                    throws PortalException, SystemException {
450    
451                    return updateEntry(
452                            userId, groupId, className, classPK, null, categoryIds, tagNames,
453                            true, null, null, null, null, null, null, null, null, null, 0, 0,
454                            null, false);
455            }
456    
457            public AssetEntry updateEntry(
458                            long userId, long groupId, String className, long classPK,
459                            String classUuid, long[] categoryIds, String[] tagNames,
460                            boolean visible, Date startDate, Date endDate, Date publishDate,
461                            Date expirationDate, String mimeType, String title,
462                            String description, String summary, String url, int height,
463                            int width, Integer priority, boolean sync)
464                    throws PortalException, SystemException {
465    
466                    // Entry
467    
468                    User user = userPersistence.findByPrimaryKey(userId);
469                    long classNameId = PortalUtil.getClassNameId(className);
470    
471                    title = StringUtil.shorten(title, 300, StringPool.BLANK);
472                    Date now = new Date();
473    
474                    validate(className, categoryIds, tagNames);
475    
476                    AssetEntry entry = assetEntryPersistence.fetchByC_C(
477                            classNameId, classPK);
478    
479                    if (entry == null) {
480                            long entryId = counterLocalService.increment();
481    
482                            entry = assetEntryPersistence.create(entryId);
483    
484                            entry.setCompanyId(user.getCompanyId());
485                            entry.setUserId(user.getUserId());
486                            entry.setUserName(user.getFullName());
487                            entry.setCreateDate(now);
488                            entry.setClassNameId(classNameId);
489                            entry.setClassPK(classPK);
490                            entry.setClassUuid(classUuid);
491                            entry.setVisible(visible);
492                            entry.setPublishDate(publishDate);
493                            entry.setExpirationDate(expirationDate);
494    
495                            if (priority == null) {
496                                    entry.setPriority(0);
497                            }
498    
499                            entry.setViewCount(0);
500                    }
501    
502                    entry.setGroupId(groupId);
503                    entry.setModifiedDate(now);
504                    entry.setVisible(visible);
505                    entry.setStartDate(startDate);
506                    entry.setEndDate(endDate);
507                    entry.setPublishDate(publishDate);
508                    entry.setExpirationDate(expirationDate);
509                    entry.setMimeType(mimeType);
510                    entry.setTitle(title);
511                    entry.setDescription(description);
512                    entry.setSummary(summary);
513                    entry.setUrl(url);
514                    entry.setHeight(height);
515                    entry.setWidth(width);
516    
517                    if (priority != null) {
518                            entry.setPriority(priority.intValue());
519                    }
520    
521                    // Categories
522    
523                    if (categoryIds != null) {
524                            assetEntryPersistence.setAssetCategories(
525                                    entry.getEntryId(), categoryIds);
526                    }
527    
528                    // Tags
529    
530                    if (tagNames != null) {
531                            long parentGroupId = PortalUtil.getParentGroupId(groupId);
532    
533                            List<AssetTag> tags = new ArrayList<AssetTag>(tagNames.length);
534    
535                            for (String tagName : tagNames) {
536                                    AssetTag tag = null;
537    
538                                    try {
539                                            tag = assetTagLocalService.getTag(parentGroupId, tagName);
540                                    }
541                                    catch (NoSuchTagException nste) {
542                                            ServiceContext serviceContext = new ServiceContext();
543    
544                                            serviceContext.setAddCommunityPermissions(true);
545                                            serviceContext.setAddGuestPermissions(true);
546                                            serviceContext.setScopeGroupId(parentGroupId);
547    
548                                            tag = assetTagLocalService.addTag(
549                                                    user.getUserId(), tagName,
550                                                    PropsValues.ASSET_TAG_PROPERTIES_DEFAULT,
551                                                    serviceContext);
552                                    }
553    
554                                    if (tag != null) {
555                                            tags.add(tag);
556                                    }
557                            }
558    
559                            List<AssetTag> oldTags = assetEntryPersistence.getAssetTags(
560                                    entry.getEntryId());
561    
562                            assetEntryPersistence.setAssetTags(entry.getEntryId(), tags);
563    
564                            if (entry.isNew()) {
565                                    for (AssetTag tag : tags) {
566                                            assetTagLocalService.incrementAssetCount(
567                                                    tag.getTagId(), classNameId);
568                                    }
569                            }
570                            else {
571                                    for (AssetTag oldTag : oldTags) {
572                                            if (!tags.contains(oldTag)) {
573                                                    assetTagLocalService.decrementAssetCount(
574                                                            oldTag.getTagId(), classNameId);
575                                            }
576                                    }
577    
578                                    for (AssetTag tag : tags) {
579                                            if (!oldTags.contains(tag)) {
580                                                    assetTagLocalService.incrementAssetCount(
581                                                            tag.getTagId(), classNameId);
582                                            }
583                                    }
584                            }
585                    }
586    
587                    // Update entry after tags so that entry listeners have access to the
588                    // saved categories and tags
589    
590                    assetEntryPersistence.update(entry, false);
591    
592                    // Synchronize
593    
594                    if (!sync) {
595                            return entry;
596                    }
597    
598                    if (className.equals(BlogsEntry.class.getName())) {
599                            BlogsEntry blogsEntry = blogsEntryPersistence.findByPrimaryKey(
600                                    classPK);
601    
602                            blogsEntry.setTitle(title);
603    
604                            blogsEntryPersistence.update(blogsEntry, false);
605                    }
606                    else if (className.equals(BookmarksEntry.class.getName())) {
607                            BookmarksEntry bookmarksEntry =
608                                    bookmarksEntryPersistence.findByPrimaryKey(classPK);
609    
610                            bookmarksEntry.setName(title);
611                            bookmarksEntry.setComments(description);
612                            bookmarksEntry.setUrl(url);
613    
614                            bookmarksEntryPersistence.update(bookmarksEntry, false);
615                    }
616                    else if (className.equals(DLFileEntry.class.getName())) {
617                            DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
618                                    classPK);
619    
620                            dlFileEntry.setTitle(title);
621                            dlFileEntry.setDescription(description);
622    
623                            dlFileEntryPersistence.update(dlFileEntry, false);
624                    }
625                    else if (className.equals(JournalArticle.class.getName())) {
626                            JournalArticle journalArticle =
627                                    journalArticlePersistence.findByPrimaryKey(classPK);
628    
629                            journalArticle.setTitle(title);
630                            journalArticle.setDescription(description);
631    
632                            journalArticlePersistence.update(journalArticle, false);
633                    }
634                    else if (className.equals(MBMessage.class.getName())) {
635                            MBMessage mbMessage = mbMessagePersistence.findByPrimaryKey(
636                                    classPK);
637    
638                            mbMessage.setSubject(title);
639    
640                            mbMessagePersistence.update(mbMessage, false);
641                    }
642                    else if (className.equals(WikiPage.class.getName())) {
643                            WikiPage wikiPage = wikiPagePersistence.findByPrimaryKey(classPK);
644    
645                            wikiPage.setTitle(title);
646    
647                            wikiPagePersistence.update(wikiPage, false);
648                    }
649    
650                    return entry;
651            }
652    
653            public AssetEntry updateVisible(
654                            String className, long classPK, boolean visible)
655                    throws PortalException, SystemException {
656    
657                    long classNameId = PortalUtil.getClassNameId(className);
658    
659                    AssetEntry entry = assetEntryPersistence.findByC_C(
660                            classNameId, classPK);
661    
662                    entry.setVisible(visible);
663    
664                    assetEntryPersistence.update(entry, false);
665    
666                    return entry;
667            }
668    
669            public void validate(
670                            String className, long[] categoryIds, String[] tagNames)
671                    throws PortalException {
672    
673                    AssetEntryValidator validator = (AssetEntryValidator)InstancePool.get(
674                            PropsValues.ASSET_ENTRY_VALIDATOR);
675    
676                    validator.validate(className, categoryIds, tagNames);
677            }
678    
679            protected AssetEntry getEntry(Document doc)
680                    throws PortalException, SystemException {
681    
682                    String portletId = GetterUtil.getString(doc.get(Field.PORTLET_ID));
683    
684                    if (portletId.equals(PortletKeys.BLOGS)) {
685                            long entryId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
686    
687                            long classNameId = PortalUtil.getClassNameId(
688                                    BlogsEntry.class.getName());
689                            long classPK = entryId;
690    
691                            return assetEntryPersistence.findByC_C(classNameId, classPK);
692                    }
693                    else if (portletId.equals(PortletKeys.BOOKMARKS)) {
694                            long entryId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
695    
696                            long classNameId = PortalUtil.getClassNameId(
697                                    BookmarksEntry.class.getName());
698                            long classPK = entryId;
699    
700                            return assetEntryPersistence.findByC_C(classNameId, classPK);
701                    }
702                    else if (portletId.equals(PortletKeys.DOCUMENT_LIBRARY)) {
703                            long repositoryId = GetterUtil.getLong(doc.get("repositoryId"));
704                            String name = doc.get("path");
705    
706                            long groupId = 0;
707                            long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
708    
709                            try {
710                                    groupPersistence.findByPrimaryKey(repositoryId);
711    
712                                    groupId = repositoryId;
713                            }
714                            catch (NoSuchGroupException nsge) {
715                                    DLFolder folder = dlFolderPersistence.findByPrimaryKey(
716                                            repositoryId);
717    
718                                    groupId = folder.getGroupId();
719                                    folderId = folder.getFolderId();
720                            }
721    
722                            DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
723                                    groupId, folderId, name);
724    
725                            long classNameId = PortalUtil.getClassNameId(
726                                    DLFileEntry.class.getName());
727                            long classPK = fileEntry.getFileEntryId();
728    
729                            return assetEntryPersistence.findByC_C(classNameId, classPK);
730                    }
731                    else if (portletId.equals(PortletKeys.IMAGE_GALLERY)) {
732                            long imageId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
733    
734                            long classNameId = PortalUtil.getClassNameId(
735                                    IGImage.class.getName());
736                            long classPK = imageId;
737    
738                            return assetEntryPersistence.findByC_C(classNameId, classPK);
739                    }
740                    else if (portletId.equals(PortletKeys.JOURNAL)) {
741                            long groupId = GetterUtil.getLong(doc.get(Field.GROUP_ID));
742                            String articleId = doc.get(Field.ENTRY_CLASS_PK);
743                            //double version = GetterUtil.getDouble(doc.get("version"));
744    
745                            long articleResourcePrimKey =
746                                    journalArticleResourceLocalService.getArticleResourcePrimKey(
747                                            groupId, articleId);
748    
749                            long classNameId = PortalUtil.getClassNameId(
750                                    JournalArticle.class.getName());
751                            long classPK = articleResourcePrimKey;
752    
753                            return assetEntryPersistence.findByC_C(classNameId, classPK);
754                    }
755                    else if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
756                            long messageId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
757    
758                            long classNameId = PortalUtil.getClassNameId(
759                                    MBMessage.class.getName());
760                            long classPK = messageId;
761    
762                            return assetEntryPersistence.findByC_C(classNameId, classPK);
763                    }
764                    else if (portletId.equals(PortletKeys.WIKI)) {
765                            long nodeId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
766                            String title = doc.get(Field.TITLE);
767    
768                            long pageResourcePrimKey =
769                                    wikiPageResourceLocalService.getPageResourcePrimKey(
770                                            nodeId, title);
771    
772                            long classNameId = PortalUtil.getClassNameId(
773                                    WikiPage.class.getName());
774                            long classPK = pageResourcePrimKey;
775    
776                            return assetEntryPersistence.findByC_C(classNameId, classPK);
777                    }
778    
779                    return null;
780            }
781    
782            protected AssetEntryDisplay[] getEntryDisplays(
783                            List<AssetEntry> entries, String languageId)
784                    throws SystemException {
785    
786                    AssetEntryDisplay[] entryDisplays =
787                            new AssetEntryDisplay[entries.size()];
788    
789                    for (int i = 0; i < entries.size(); i++) {
790                            AssetEntry entry = entries.get(i);
791    
792                            String className = PortalUtil.getClassName(entry.getClassNameId());
793                            String portletId = PortalUtil.getClassNamePortletId(className);
794                            String portletTitle = PortalUtil.getPortletTitle(
795                                    portletId, languageId);
796    
797                            List<AssetCategory> categories =
798                                    assetEntryPersistence.getAssetCategories(entry.getEntryId());
799    
800                            String categoryIdsString = ListUtil.toString(
801                                    categories, "assetCategoryId", StringPool.COMMA);
802                            long[] categoryIds = StringUtil.split(
803                                    categoryIdsString, StringPool.COMMA, 0L);
804    
805                            List<AssetTag> tags = assetEntryPersistence.getAssetTags(
806                                    entry.getEntryId());
807    
808                            String tagNames = ListUtil.toString(tags, "name", ", ");
809    
810                            AssetEntryDisplay entryDisplay = new AssetEntryDisplay();
811    
812                            entryDisplay.setEntryId(entry.getEntryId());
813                            entryDisplay.setCompanyId(entry.getCompanyId());
814                            entryDisplay.setUserId(entry.getUserId());
815                            entryDisplay.setUserName(entry.getUserName());
816                            entryDisplay.setCreateDate(entry.getCreateDate());
817                            entryDisplay.setModifiedDate(entry.getModifiedDate());
818                            entryDisplay.setClassNameId(entry.getClassNameId());
819                            entryDisplay.setClassName(className);
820                            entryDisplay.setClassPK(entry.getClassPK());
821                            entryDisplay.setPortletId(portletId);
822                            entryDisplay.setPortletTitle(portletTitle);
823                            entryDisplay.setStartDate(entry.getStartDate());
824                            entryDisplay.setEndDate(entry.getEndDate());
825                            entryDisplay.setPublishDate(entry.getPublishDate());
826                            entryDisplay.setExpirationDate(entry.getExpirationDate());
827                            entryDisplay.setMimeType(entry.getMimeType());
828                            entryDisplay.setTitle(entry.getTitle());
829                            entryDisplay.setDescription(entry.getDescription());
830                            entryDisplay.setSummary(entry.getSummary());
831                            entryDisplay.setUrl(entry.getUrl());
832                            entryDisplay.setHeight(entry.getHeight());
833                            entryDisplay.setWidth(entry.getWidth());
834                            entryDisplay.setPriority(entry.getPriority());
835                            entryDisplay.setViewCount(entry.getViewCount());
836                            entryDisplay.setCategoryIds(categoryIds);
837                            entryDisplay.setTagNames(tagNames);
838    
839                            entryDisplays[i] = entryDisplay;
840                    }
841    
842                    return entryDisplays;
843            }
844    
845            private String[] getPortletIds(String portletId) {
846                    if (Validator.isNotNull(portletId)) {
847                            return new String[] {portletId};
848                    }
849                    else {
850                            List<AssetRendererFactory> rendererFactories =
851                                    AssetRendererFactoryRegistryUtil.getAssetRendererFactories();
852    
853                            String[] portletIds = new String[rendererFactories.size()];
854    
855                            for (int i = 0; i < rendererFactories.size(); i++) {
856                                    AssetRendererFactory rendererFactory = rendererFactories.get(i);
857    
858                                    portletIds[i] = rendererFactory.getPortletId();
859                            }
860    
861                            return portletIds;
862                    }
863            }
864    
865            private static Log _log = LogFactoryUtil.getLog(
866                    AssetEntryLocalServiceImpl.class);
867    
868    }