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