001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.asset.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.increment.BufferedIncrement;
020    import com.liferay.portal.kernel.increment.NumberIncrement;
021    import com.liferay.portal.kernel.lar.ExportImportThreadLocal;
022    import com.liferay.portal.kernel.search.Document;
023    import com.liferay.portal.kernel.search.Field;
024    import com.liferay.portal.kernel.search.Hits;
025    import com.liferay.portal.kernel.search.Indexer;
026    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
027    import com.liferay.portal.kernel.search.QueryConfig;
028    import com.liferay.portal.kernel.search.SearchContext;
029    import com.liferay.portal.kernel.util.ArrayUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstancePool;
032    import com.liferay.portal.kernel.util.StringPool;
033    import com.liferay.portal.kernel.util.StringUtil;
034    import com.liferay.portal.kernel.util.Validator;
035    import com.liferay.portal.kernel.workflow.WorkflowConstants;
036    import com.liferay.portal.model.Group;
037    import com.liferay.portal.model.SystemEventConstants;
038    import com.liferay.portal.model.User;
039    import com.liferay.portal.security.permission.ActionKeys;
040    import com.liferay.portal.security.permission.PermissionChecker;
041    import com.liferay.portal.security.permission.PermissionThreadLocal;
042    import com.liferay.portal.util.PortalUtil;
043    import com.liferay.portal.util.PortletKeys;
044    import com.liferay.portal.util.PropsValues;
045    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
046    import com.liferay.portlet.asset.NoSuchEntryException;
047    import com.liferay.portlet.asset.model.AssetCategory;
048    import com.liferay.portlet.asset.model.AssetEntry;
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.permission.AssetCategoryPermission;
055    import com.liferay.portlet.asset.service.permission.AssetTagPermission;
056    import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
057    import com.liferay.portlet.asset.util.AssetEntryValidator;
058    import com.liferay.portlet.assetpublisher.util.AssetSearcher;
059    import com.liferay.portlet.blogs.model.BlogsEntry;
060    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
061    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
062    import com.liferay.portlet.journal.model.JournalArticle;
063    import com.liferay.portlet.messageboards.model.MBMessage;
064    import com.liferay.portlet.social.model.SocialActivityConstants;
065    import com.liferay.portlet.wiki.model.WikiPage;
066    
067    import java.util.ArrayList;
068    import java.util.Date;
069    import java.util.List;
070    
071    /**
072     * Provides the local service for accessing, deleting, updating, and validating
073     * asset entries.
074     *
075     * @author Brian Wing Shun Chan
076     * @author Bruno Farache
077     * @author Zsolt Berentey
078     */
079    public class AssetEntryLocalServiceImpl extends AssetEntryLocalServiceBaseImpl {
080    
081            @Override
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                    // System event
093    
094                    systemEventLocalService.addSystemEvent(
095                            0, entry.getGroupId(), entry.getClassName(), entry.getClassPK(),
096                            entry.getClassUuid(), null, SystemEventConstants.TYPE_DELETE, null);
097    
098                    // Links
099    
100                    assetLinkLocalService.deleteLinks(entry.getEntryId());
101    
102                    // Tags
103    
104                    for (AssetTag tag : tags) {
105                            if (entry.isVisible()) {
106                                    assetTagLocalService.decrementAssetCount(
107                                            tag.getTagId(), entry.getClassNameId());
108                            }
109                    }
110    
111                    // Social
112    
113                    socialActivityLocalService.deleteActivities(entry);
114            }
115    
116            @Override
117            public void deleteEntry(long entryId)
118                    throws PortalException, SystemException {
119    
120                    AssetEntry entry = assetEntryPersistence.findByPrimaryKey(entryId);
121    
122                    deleteEntry(entry);
123            }
124    
125            @Override
126            public void deleteEntry(String className, long classPK)
127                    throws PortalException, SystemException {
128    
129                    long classNameId = PortalUtil.getClassNameId(className);
130    
131                    AssetEntry entry = assetEntryPersistence.fetchByC_C(
132                            classNameId, classPK);
133    
134                    if (entry != null) {
135                            deleteEntry(entry);
136                    }
137            }
138    
139            @Override
140            public AssetEntry fetchEntry(long entryId) throws SystemException {
141                    return assetEntryPersistence.fetchByPrimaryKey(entryId);
142            }
143    
144            @Override
145            public AssetEntry fetchEntry(long groupId, String classUuid)
146                    throws SystemException {
147    
148                    return assetEntryPersistence.fetchByG_CU(groupId, classUuid);
149            }
150    
151            @Override
152            public AssetEntry fetchEntry(String className, long classPK)
153                    throws SystemException {
154    
155                    long classNameId = PortalUtil.getClassNameId(className);
156    
157                    return assetEntryPersistence.fetchByC_C(classNameId, classPK);
158            }
159    
160            @Override
161            public List<AssetEntry> getAncestorEntries(long entryId)
162                    throws PortalException, SystemException {
163    
164                    List<AssetEntry> entries = new ArrayList<AssetEntry>();
165    
166                    AssetEntry parentEntry = getParentEntry(entryId);
167    
168                    while (parentEntry != null) {
169                            entries.add(parentEntry);
170    
171                            parentEntry = getParentEntry(parentEntry.getEntryId());
172                    }
173    
174                    return entries;
175            }
176    
177            @Override
178            public List<AssetEntry> getChildEntries(long entryId)
179                    throws PortalException, SystemException {
180    
181                    List<AssetEntry> entries = new ArrayList<AssetEntry>();
182    
183                    List<AssetLink> links = assetLinkLocalService.getDirectLinks(
184                            entryId, AssetLinkConstants.TYPE_CHILD);
185    
186                    for (AssetLink link : links) {
187                            AssetEntry curAsset = getEntry(link.getEntryId2());
188    
189                            entries.add(curAsset);
190                    }
191    
192                    return entries;
193            }
194    
195            @Override
196            public List<AssetEntry> getCompanyEntries(
197                            long companyId, int start, int end)
198                    throws SystemException {
199    
200                    return assetEntryPersistence.findByCompanyId(companyId, start, end);
201            }
202    
203            @Override
204            public int getCompanyEntriesCount(long companyId) throws SystemException {
205                    return assetEntryPersistence.countByCompanyId(companyId);
206            }
207    
208            @Override
209            public List<AssetEntry> getEntries(AssetEntryQuery entryQuery)
210                    throws SystemException {
211    
212                    return assetEntryFinder.findEntries(entryQuery);
213            }
214    
215            @Override
216            public int getEntriesCount(AssetEntryQuery entryQuery)
217                    throws SystemException {
218    
219                    return assetEntryFinder.countEntries(entryQuery);
220            }
221    
222            @Override
223            public AssetEntry getEntry(long entryId)
224                    throws PortalException, SystemException {
225    
226                    return assetEntryPersistence.findByPrimaryKey(entryId);
227            }
228    
229            @Override
230            public AssetEntry getEntry(long groupId, String classUuid)
231                    throws PortalException, SystemException {
232    
233                    return assetEntryPersistence.findByG_CU(groupId, classUuid);
234            }
235    
236            @Override
237            public AssetEntry getEntry(String className, long classPK)
238                    throws PortalException, SystemException {
239    
240                    long classNameId = PortalUtil.getClassNameId(className);
241    
242                    return assetEntryPersistence.findByC_C(classNameId, classPK);
243            }
244    
245            @Override
246            public AssetEntry getNextEntry(long entryId)
247                    throws PortalException, SystemException {
248    
249                    try {
250                            getParentEntry(entryId);
251                    }
252                    catch (NoSuchEntryException nsee) {
253                            List<AssetEntry> childEntries = getChildEntries(entryId);
254    
255                            if (childEntries.isEmpty()) {
256                                    throw new NoSuchEntryException("{entryId=" + entryId + "}");
257                            }
258    
259                            return childEntries.get(0);
260                    }
261    
262                    List<AssetLink> links = assetLinkLocalService.getDirectLinks(
263                            entryId, AssetLinkConstants.TYPE_CHILD);
264    
265                    for (int i = 0; i < links.size(); i++) {
266                            AssetLink link = links.get(i);
267    
268                            if (link.getEntryId2() == entryId) {
269                                    if ((i + 1) >= links.size()) {
270                                            throw new NoSuchEntryException("{entryId=" + entryId + "}");
271                                    }
272                                    else {
273                                            AssetLink nextLink = links.get(i + 1);
274    
275                                            return getEntry(nextLink.getEntryId2());
276                                    }
277                            }
278                    }
279    
280                    throw new NoSuchEntryException("{entryId=" + entryId + "}");
281            }
282    
283            @Override
284            public AssetEntry getParentEntry(long entryId)
285                    throws PortalException, SystemException {
286    
287                    List<AssetLink> links = assetLinkLocalService.getReverseLinks(
288                            entryId, AssetLinkConstants.TYPE_CHILD);
289    
290                    if (links.isEmpty()) {
291                            throw new NoSuchEntryException("{entryId=" + entryId + "}");
292                    }
293    
294                    AssetLink link = links.get(0);
295    
296                    return getEntry(link.getEntryId1());
297            }
298    
299            @Override
300            public AssetEntry getPreviousEntry(long entryId)
301                    throws PortalException, SystemException {
302    
303                    getParentEntry(entryId);
304    
305                    List<AssetLink> links = assetLinkLocalService.getDirectLinks(
306                            entryId, AssetLinkConstants.TYPE_CHILD);
307    
308                    for (int i = 0; i < links.size(); i++) {
309                            AssetLink link = links.get(i);
310    
311                            if (link.getEntryId2() == entryId) {
312                                    if (i == 0) {
313                                            throw new NoSuchEntryException("{entryId=" + entryId + "}");
314                                    }
315                                    else {
316                                            AssetLink nextAssetLink = links.get(i - 1);
317    
318                                            return getEntry(nextAssetLink.getEntryId2());
319                                    }
320                            }
321                    }
322    
323                    throw new NoSuchEntryException("{entryId=" + entryId + "}");
324            }
325    
326            @Override
327            public List<AssetEntry> getTopViewedEntries(
328                            String className, boolean asc, int start, int end)
329                    throws SystemException {
330    
331                    return getTopViewedEntries(new String[] {className}, asc, start, end);
332            }
333    
334            @Override
335            public List<AssetEntry> getTopViewedEntries(
336                            String[] className, boolean asc, int start, int end)
337                    throws SystemException {
338    
339                    long[] classNameIds = new long[className.length];
340    
341                    for (int i = 0; i < className.length; i++) {
342                            classNameIds[i] = PortalUtil.getClassNameId(className[i]);
343                    }
344    
345                    AssetEntryQuery entryQuery = new AssetEntryQuery();
346    
347                    entryQuery.setClassNameIds(classNameIds);
348                    entryQuery.setEnd(end);
349                    entryQuery.setExcludeZeroViewCount(true);
350                    entryQuery.setOrderByCol1("viewCount");
351                    entryQuery.setOrderByType1(asc ? "ASC" : "DESC");
352                    entryQuery.setStart(start);
353    
354                    return assetEntryFinder.findEntries(entryQuery);
355            }
356    
357            @Override
358            public AssetEntry incrementViewCounter(
359                            long userId, String className, long classPK)
360                    throws PortalException, SystemException {
361    
362                    User user = userPersistence.findByPrimaryKey(userId);
363    
364                    assetEntryLocalService.incrementViewCounter(
365                            user.getUserId(), className, classPK, 1);
366    
367                    AssetEntry assetEntry = getEntry(className, classPK);
368    
369                    if (!user.isDefaultUser()) {
370                            socialActivityLocalService.addActivity(
371                                    user.getUserId(), assetEntry.getGroupId(), className, classPK,
372                                    SocialActivityConstants.TYPE_VIEW, StringPool.BLANK, 0);
373                    }
374    
375                    return assetEntry;
376            }
377    
378            @BufferedIncrement(
379                    configuration = "AssetEntry", incrementClass = NumberIncrement.class)
380            @Override
381            public AssetEntry incrementViewCounter(
382                            long userId, String className, long classPK, int increment)
383                    throws SystemException {
384    
385                    if (ExportImportThreadLocal.isImportInProcess() || (classPK <= 0)) {
386                            return null;
387                    }
388    
389                    long classNameId = PortalUtil.getClassNameId(className);
390    
391                    AssetEntry entry = assetEntryPersistence.fetchByC_C(
392                            classNameId, classPK);
393    
394                    if (entry == null) {
395                            return null;
396                    }
397    
398                    entry.setViewCount(entry.getViewCount() + increment);
399    
400                    assetEntryPersistence.update(entry);
401    
402                    try {
403                            reindex(entry);
404                    }
405                    catch (PortalException pe) {
406                            throw new SystemException(pe);
407                    }
408    
409                    return entry;
410            }
411    
412            @Override
413            public void reindex(List<AssetEntry> entries) throws PortalException {
414                    for (AssetEntry entry : entries) {
415                            reindex(entry);
416                    }
417            }
418    
419            /**
420             * @deprecated As of 6.2.0, replaced by {@link #search(long, long[], long,
421             *             String, String, int, int, int)}
422             */
423            @Override
424            public Hits search(
425                            long companyId, long[] groupIds, long userId, String className,
426                            String keywords, int start, int end)
427                    throws SystemException {
428    
429                    return search(
430                            companyId, groupIds, userId, className, keywords,
431                            WorkflowConstants.STATUS_ANY, start, end);
432            }
433    
434            @Override
435            public Hits search(
436                            long companyId, long[] groupIds, long userId, String className,
437                            String keywords, int status, int start, int end)
438                    throws SystemException {
439    
440                    try {
441                            SearchContext searchContext = new SearchContext();
442    
443                            searchContext.setAttribute("paginationType", "regular");
444                            searchContext.setAttribute("status", status);
445                            searchContext.setCompanyId(companyId);
446                            searchContext.setEnd(end);
447                            searchContext.setGroupIds(groupIds);
448                            searchContext.setKeywords(keywords);
449                            searchContext.setStart(start);
450                            searchContext.setUserId(userId);
451    
452                            return doSearch(companyId, className, searchContext);
453                    }
454                    catch (Exception e) {
455                            throw new SystemException(e);
456                    }
457            }
458    
459            /**
460             * @deprecated As of 6.2.0, replaced by {@link #search(long, long[], long,
461             *             String, String, String, String, String, String, int, boolean,
462             *             int, int)}
463             */
464            @Override
465            public Hits search(
466                            long companyId, long[] groupIds, long userId, String className,
467                            String userName, String title, String description,
468                            String assetCategoryIds, String assetTagNames, boolean andSearch,
469                            int start, int end)
470                    throws SystemException {
471    
472                    return search(
473                            companyId, groupIds, userId, className, userName, title,
474                            description, assetCategoryIds, assetTagNames,
475                            WorkflowConstants.STATUS_ANY, andSearch, start, end);
476            }
477    
478            @Override
479            public Hits search(
480                            long companyId, long[] groupIds, long userId, String className,
481                            String userName, String title, String description,
482                            String assetCategoryIds, String assetTagNames, int status,
483                            boolean andSearch, int start, int end)
484                    throws SystemException {
485    
486                    try {
487                            SearchContext searchContext = new SearchContext();
488    
489                            searchContext.setAndSearch(andSearch);
490                            searchContext.setAssetCategoryIds(
491                                    StringUtil.split(assetCategoryIds, 0L));
492                            searchContext.setAssetTagNames(StringUtil.split(assetTagNames));
493                            searchContext.setAttribute(Field.DESCRIPTION, description);
494                            searchContext.setAttribute(Field.TITLE, title);
495                            searchContext.setAttribute(Field.USER_NAME, userName);
496                            searchContext.setAttribute("paginationType", "regular");
497                            searchContext.setAttribute("status", status);
498                            searchContext.setCompanyId(companyId);
499                            searchContext.setEnd(end);
500                            searchContext.setGroupIds(groupIds);
501                            searchContext.setStart(start);
502                            searchContext.setUserId(userId);
503    
504                            return doSearch(companyId, className, searchContext);
505                    }
506                    catch (Exception e) {
507                            throw new SystemException(e);
508                    }
509            }
510    
511            /**
512             * @deprecated As of 6.2.0, replaced by {@link #search(long, long[], long,
513             *             String, String, int, int, int)}
514             */
515            @Override
516            public Hits search(
517                            long companyId, long[] groupIds, String className, String keywords,
518                            int start, int end)
519                    throws SystemException {
520    
521                    return search(
522                            companyId, groupIds, 0, className, keywords,
523                            WorkflowConstants.STATUS_ANY, start, end);
524            }
525    
526            @Override
527            public AssetEntry updateEntry(
528                            long userId, long groupId, Date createDate, Date modifiedDate,
529                            String className, long classPK, String classUuid, long classTypeId,
530                            long[] categoryIds, String[] tagNames, boolean visible,
531                            Date startDate, Date endDate, Date expirationDate, String mimeType,
532                            String title, String description, String summary, String url,
533                            String layoutUuid, int height, int width, Integer priority,
534                            boolean sync)
535                    throws PortalException, SystemException {
536    
537                    return updateEntry(
538                            userId, groupId, createDate, modifiedDate, className, classPK,
539                            classUuid, classTypeId, categoryIds, tagNames, visible, startDate,
540                            endDate, expirationDate, null, mimeType, title, description,
541                            summary, url, layoutUuid, height, width, priority, sync);
542            }
543    
544            @Override
545            public AssetEntry updateEntry(
546                            long userId, long groupId, Date createDate, Date modifiedDate,
547                            String className, long classPK, String classUuid, long classTypeId,
548                            long[] categoryIds, String[] tagNames, boolean visible,
549                            Date startDate, Date endDate, Date expirationDate, Date publishDate,
550                            String mimeType, String title, String description, String summary,
551                            String url, String layoutUuid, int height, int width,
552                            Integer priority, boolean sync)
553                    throws PortalException, SystemException {
554    
555                    // Entry
556    
557                    long classNameId = PortalUtil.getClassNameId(className);
558    
559                    validate(groupId, className, classPK, categoryIds, tagNames);
560    
561                    AssetEntry entry = assetEntryPersistence.fetchByC_C(
562                            classNameId, classPK);
563    
564                    boolean oldVisible = false;
565    
566                    if (entry != null) {
567                            oldVisible = entry.isVisible();
568                    }
569    
570                    if (modifiedDate == null) {
571                            modifiedDate = new Date();
572                    }
573    
574                    if (entry == null) {
575                            long entryId = counterLocalService.increment();
576    
577                            entry = assetEntryPersistence.create(entryId);
578    
579                            Group group = groupLocalService.getGroup(groupId);
580    
581                            entry.setCompanyId(group.getCompanyId());
582    
583                            entry.setUserId(userId);
584    
585                            User user = userPersistence.fetchByPrimaryKey(userId);
586    
587                            if (user != null) {
588                                    entry.setUserName(user.getFullName());
589                            }
590                            else {
591                                    entry.setUserName(StringPool.BLANK);
592                            }
593    
594                            if (createDate == null) {
595                                    createDate = new Date();
596                            }
597    
598                            entry.setCreateDate(createDate);
599    
600                            entry.setModifiedDate(modifiedDate);
601                            entry.setClassNameId(classNameId);
602                            entry.setClassPK(classPK);
603                            entry.setClassUuid(classUuid);
604                            entry.setVisible(visible);
605                            entry.setExpirationDate(expirationDate);
606    
607                            if (priority == null) {
608                                    entry.setPriority(0);
609                            }
610    
611                            entry.setViewCount(0);
612                    }
613    
614                    entry.setGroupId(groupId);
615                    entry.setModifiedDate(modifiedDate);
616                    entry.setClassTypeId(classTypeId);
617                    entry.setVisible(visible);
618                    entry.setStartDate(startDate);
619                    entry.setEndDate(endDate);
620    
621                    if (publishDate != null) {
622                            entry.setPublishDate(publishDate);
623                    }
624    
625                    entry.setExpirationDate(expirationDate);
626                    entry.setMimeType(mimeType);
627                    entry.setTitle(title);
628                    entry.setDescription(description);
629                    entry.setSummary(summary);
630                    entry.setUrl(url);
631                    entry.setLayoutUuid(layoutUuid);
632                    entry.setHeight(height);
633                    entry.setWidth(width);
634    
635                    if (priority != null) {
636                            entry.setPriority(priority.intValue());
637                    }
638    
639                    // Categories
640    
641                    if (categoryIds != null) {
642                            categoryIds = checkCategories(className, classPK, categoryIds);
643    
644                            assetEntryPersistence.setAssetCategories(
645                                    entry.getEntryId(), categoryIds);
646                    }
647    
648                    // Tags
649    
650                    if (tagNames != null) {
651                            tagNames = checkTags(className, classPK, tagNames);
652    
653                            long siteGroupId = PortalUtil.getSiteGroupId(groupId);
654    
655                            Group siteGroup = groupLocalService.getGroup(siteGroupId);
656    
657                            List<AssetTag> tags = assetTagLocalService.checkTags(
658                                    userId, siteGroup, tagNames);
659    
660                            List<AssetTag> oldTags = assetEntryPersistence.getAssetTags(
661                                    entry.getEntryId());
662    
663                            assetEntryPersistence.setAssetTags(entry.getEntryId(), tags);
664    
665                            if (entry.isVisible()) {
666                                    boolean isNew = entry.isNew();
667    
668                                    assetEntryPersistence.updateImpl(entry);
669    
670                                    if (isNew) {
671                                            for (AssetTag tag : tags) {
672                                                    assetTagLocalService.incrementAssetCount(
673                                                            tag.getTagId(), classNameId);
674                                            }
675                                    }
676                                    else {
677                                            for (AssetTag oldTag : oldTags) {
678                                                    if (!tags.contains(oldTag)) {
679                                                            assetTagLocalService.decrementAssetCount(
680                                                                    oldTag.getTagId(), classNameId);
681                                                    }
682                                            }
683    
684                                            for (AssetTag tag : tags) {
685                                                    if (!oldTags.contains(tag)) {
686                                                            assetTagLocalService.incrementAssetCount(
687                                                                    tag.getTagId(), classNameId);
688                                                    }
689                                            }
690                                    }
691                            }
692                            else if (oldVisible) {
693                                    for (AssetTag oldTag : oldTags) {
694                                            assetTagLocalService.decrementAssetCount(
695                                                    oldTag.getTagId(), classNameId);
696                                    }
697                            }
698                    }
699    
700                    // Update entry after tags so that entry listeners have access to the
701                    // saved categories and tags
702    
703                    assetEntryPersistence.update(entry);
704    
705                    // Synchronize
706    
707                    if (sync) {
708                            if (className.equals(BlogsEntry.class.getName())) {
709                                    BlogsEntry blogsEntry = blogsEntryPersistence.findByPrimaryKey(
710                                            classPK);
711    
712                                    blogsEntry.setTitle(title);
713    
714                                    blogsEntryPersistence.update(blogsEntry);
715                            }
716                            else if (className.equals(BookmarksEntry.class.getName())) {
717                                    BookmarksEntry bookmarksEntry =
718                                            bookmarksEntryPersistence.findByPrimaryKey(classPK);
719    
720                                    bookmarksEntry.setName(title);
721                                    bookmarksEntry.setDescription(description);
722                                    bookmarksEntry.setUrl(url);
723    
724                                    bookmarksEntryPersistence.update(bookmarksEntry);
725                            }
726                            else if (className.equals(DLFileEntry.class.getName())) {
727                                    DLFileEntry dlFileEntry =
728                                            dlFileEntryPersistence.findByPrimaryKey(classPK);
729    
730                                    dlFileEntry.setTitle(title);
731                                    dlFileEntry.setDescription(description);
732    
733                                    dlFileEntryPersistence.update(dlFileEntry);
734                            }
735                            else if (className.equals(JournalArticle.class.getName())) {
736                                    JournalArticle journalArticle =
737                                            journalArticlePersistence.findByPrimaryKey(classPK);
738    
739                                    journalArticle.setTitle(title);
740                                    journalArticle.setDescription(description);
741    
742                                    journalArticlePersistence.update(journalArticle);
743                            }
744                            else if (className.equals(MBMessage.class.getName())) {
745                                    MBMessage mbMessage = mbMessagePersistence.findByPrimaryKey(
746                                            classPK);
747    
748                                    mbMessage.setSubject(title);
749    
750                                    mbMessagePersistence.update(mbMessage);
751                            }
752                            else if (className.equals(WikiPage.class.getName())) {
753                                    WikiPage wikiPage = wikiPagePersistence.findByPrimaryKey(
754                                            classPK);
755    
756                                    wikiPage.setTitle(title);
757    
758                                    wikiPagePersistence.update(wikiPage);
759                            }
760                    }
761    
762                    // Indexer
763    
764                    reindex(entry);
765    
766                    return entry;
767            }
768    
769            @Override
770            public AssetEntry updateEntry(
771                            long userId, long groupId, String className, long classPK,
772                            long[] categoryIds, String[] tagNames)
773                    throws PortalException, SystemException {
774    
775                    long classNameId = PortalUtil.getClassNameId(className);
776    
777                    AssetEntry entry = assetEntryPersistence.fetchByC_C(
778                            classNameId, classPK);
779    
780                    if (entry != null) {
781                            return updateEntry(
782                                    userId, groupId, entry.getCreateDate(), entry.getModifiedDate(),
783                                    className, classPK, entry.getClassUuid(),
784                                    entry.getClassTypeId(), categoryIds, tagNames,
785                                    entry.isVisible(), entry.getStartDate(), entry.getEndDate(),
786                                    entry.getExpirationDate(), entry.getMimeType(),
787                                    entry.getTitle(), entry.getDescription(), entry.getSummary(),
788                                    entry.getUrl(), entry.getLayoutUuid(), entry.getHeight(),
789                                    entry.getWidth(), GetterUtil.getInteger(entry.getPriority()),
790                                    false);
791                    }
792    
793                    return updateEntry(
794                            userId, groupId, null, null, className, classPK, null, 0,
795                            categoryIds, tagNames, true, null, null, null, null, null, null,
796                            null, null, null, 0, 0, null, false);
797            }
798    
799            /**
800             * @deprecated As of 6.2.0, replaced by {@link #updateEntry(long, long,
801             *             String, long, String, long, long[], String[], boolean, Date,
802             *             Date, Date, String, String, String, String, String, String,
803             *             int, int, Integer, boolean)}
804             */
805            @Override
806            public AssetEntry updateEntry(
807                            long userId, long groupId, String className, long classPK,
808                            String classUuid, long classTypeId, long[] categoryIds,
809                            String[] tagNames, boolean visible, Date startDate, Date endDate,
810                            Date publishDate, Date expirationDate, String mimeType,
811                            String title, String description, String summary, String url,
812                            String layoutUuid, int height, int width, Integer priority,
813                            boolean sync)
814                    throws PortalException, SystemException {
815    
816                    return updateEntry(
817                            userId, groupId, className, classPK, classUuid, classTypeId,
818                            categoryIds, tagNames, visible, startDate, endDate, expirationDate,
819                            mimeType, title, description, summary, url, layoutUuid, height,
820                            width, priority, sync);
821            }
822    
823            /**
824             * @deprecated As of 6.2.0, replaced by {@link #updateEntry(long, long,
825             *             Date, Date, String, long, String, long, long[], String[],
826             *             boolean, Date, Date, Date, String, String, String, String,
827             *             String, String, int, int, Integer, boolean)}
828             */
829            @Override
830            public AssetEntry updateEntry(
831                            long userId, long groupId, String className, long classPK,
832                            String classUuid, long classTypeId, long[] categoryIds,
833                            String[] tagNames, boolean visible, Date startDate, Date endDate,
834                            Date expirationDate, String mimeType, String title,
835                            String description, String summary, String url, String layoutUuid,
836                            int height, int width, Integer priority, boolean sync)
837                    throws PortalException, SystemException {
838    
839                    return updateEntry(
840                            userId, groupId, null, null, className, classPK, classUuid,
841                            classTypeId, categoryIds, tagNames, visible, startDate, endDate,
842                            expirationDate, mimeType, title, description, summary, url,
843                            layoutUuid, height, width, priority, sync);
844            }
845    
846            @Override
847            public AssetEntry updateEntry(
848                            String className, long classPK, Date publishDate, boolean visible)
849                    throws PortalException, SystemException {
850    
851                    long classNameId = PortalUtil.getClassNameId(className);
852    
853                    AssetEntry entry = assetEntryPersistence.findByC_C(
854                            classNameId, classPK);
855    
856                    entry.setPublishDate(publishDate);
857    
858                    return updateVisible(entry, visible);
859            }
860    
861            @Override
862            public AssetEntry updateEntry(
863                            String className, long classPK, Date publishDate,
864                            Date expirationDate, boolean visible)
865                    throws PortalException, SystemException {
866    
867                    long classNameId = PortalUtil.getClassNameId(className);
868    
869                    AssetEntry entry = assetEntryPersistence.findByC_C(
870                            classNameId, classPK);
871    
872                    entry.setExpirationDate(expirationDate);
873                    entry.setPublishDate(publishDate);
874    
875                    return updateVisible(entry, visible);
876            }
877    
878            @Override
879            public AssetEntry updateVisible(
880                            String className, long classPK, boolean visible)
881                    throws PortalException, SystemException {
882    
883                    long classNameId = PortalUtil.getClassNameId(className);
884    
885                    AssetEntry entry = assetEntryPersistence.findByC_C(
886                            classNameId, classPK);
887    
888                    return updateVisible(entry, visible);
889            }
890    
891            @Override
892            public void validate(
893                            long groupId, String className, long classPK, long[] categoryIds,
894                            String[] tagNames)
895                    throws PortalException, SystemException {
896    
897                    if (ExportImportThreadLocal.isImportInProcess()) {
898                            return;
899                    }
900    
901                    AssetEntryValidator validator = (AssetEntryValidator)InstancePool.get(
902                            PropsValues.ASSET_ENTRY_VALIDATOR);
903    
904                    validator.validate(groupId, className, classPK, categoryIds, tagNames);
905            }
906    
907            @Override
908            public void validate(
909                            long groupId, String className, long[] categoryIds,
910                            String[] tagNames)
911                    throws PortalException, SystemException {
912    
913                    if (ExportImportThreadLocal.isImportInProcess()) {
914                            return;
915                    }
916    
917                    AssetEntryValidator validator = (AssetEntryValidator)InstancePool.get(
918                            PropsValues.ASSET_ENTRY_VALIDATOR);
919    
920                    validator.validate(groupId, className, categoryIds, tagNames);
921            }
922    
923            protected long[] checkCategories(
924                            String className, long classPK, long[] categoryIds)
925                    throws PortalException, SystemException {
926    
927                    PermissionChecker permissionChecker =
928                            PermissionThreadLocal.getPermissionChecker();
929    
930                    if (permissionChecker == null) {
931                            return categoryIds;
932                    }
933    
934                    List<AssetCategory> oldCategories =
935                            assetCategoryLocalService.getCategories(className, classPK);
936    
937                    for (AssetCategory category : oldCategories) {
938                            if (!ArrayUtil.contains(
939                                            categoryIds, category.getCategoryId()) &&
940                                    !AssetCategoryPermission.contains(
941                                            permissionChecker, category, ActionKeys.VIEW)) {
942    
943                                    categoryIds = ArrayUtil.append(
944                                            categoryIds, category.getCategoryId());
945                            }
946                    }
947    
948                    return categoryIds;
949            }
950    
951            protected String[] checkTags(
952                            String className, long classPK, String[] tagNames)
953                    throws PortalException, SystemException {
954    
955                    PermissionChecker permissionChecker =
956                            PermissionThreadLocal.getPermissionChecker();
957    
958                    if (permissionChecker == null) {
959                            return tagNames;
960                    }
961    
962                    List<AssetTag> oldTags = assetTagLocalService.getTags(
963                            className, classPK);
964    
965                    for (AssetTag tag : oldTags) {
966                            if (!ArrayUtil.contains(tagNames, tag.getName()) &&
967                                    !AssetTagPermission.contains(
968                                            permissionChecker, tag, ActionKeys.VIEW)) {
969    
970                                    tagNames = ArrayUtil.append(tagNames, tag.getName());
971                            }
972                    }
973    
974                    return tagNames;
975            }
976    
977            protected Hits doSearch(
978                            long companyId, String className, SearchContext searchContext)
979                    throws Exception {
980    
981                    Indexer indexer = AssetSearcher.getInstance();
982    
983                    AssetSearcher assetSearcher = (AssetSearcher)indexer;
984    
985                    AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
986    
987                    assetEntryQuery.setClassNameIds(getClassNameIds(companyId, className));
988    
989                    QueryConfig queryConfig = searchContext.getQueryConfig();
990    
991                    queryConfig.setHighlightEnabled(false);
992                    queryConfig.setScoreEnabled(false);
993    
994                    assetSearcher.setAssetEntryQuery(assetEntryQuery);
995    
996                    return assetSearcher.search(searchContext);
997            }
998    
999            protected long[] getClassNameIds(long companyId, String className) {
1000                    if (Validator.isNotNull(className)) {
1001                            return new long[] {PortalUtil.getClassNameId(className)};
1002                    }
1003    
1004                    List<AssetRendererFactory> rendererFactories =
1005                            AssetRendererFactoryRegistryUtil.getAssetRendererFactories(
1006                                    companyId);
1007    
1008                    long[] classNameIds = new long[rendererFactories.size()];
1009    
1010                    for (int i = 0; i < rendererFactories.size(); i++) {
1011                            AssetRendererFactory rendererFactory = rendererFactories.get(i);
1012    
1013                            classNameIds[i] = PortalUtil.getClassNameId(
1014                                    rendererFactory.getClassName());
1015                    }
1016    
1017                    return classNameIds;
1018            }
1019    
1020            protected AssetEntry getEntry(Document document)
1021                    throws PortalException, SystemException {
1022    
1023                    String portletId = GetterUtil.getString(document.get(Field.PORTLET_ID));
1024    
1025                    if (portletId.equals(PortletKeys.BLOGS)) {
1026                            long entryId = GetterUtil.getLong(
1027                                    document.get(Field.ENTRY_CLASS_PK));
1028    
1029                            long classNameId = PortalUtil.getClassNameId(
1030                                    BlogsEntry.class.getName());
1031                            long classPK = entryId;
1032    
1033                            return assetEntryPersistence.findByC_C(classNameId, classPK);
1034                    }
1035                    else if (portletId.equals(PortletKeys.BOOKMARKS)) {
1036                            long entryId = GetterUtil.getLong(
1037                                    document.get(Field.ENTRY_CLASS_PK));
1038    
1039                            long classNameId = PortalUtil.getClassNameId(
1040                                    BookmarksEntry.class.getName());
1041                            long classPK = entryId;
1042    
1043                            return assetEntryPersistence.findByC_C(classNameId, classPK);
1044                    }
1045                    else if (portletId.equals(PortletKeys.DOCUMENT_LIBRARY)) {
1046                            long fileEntryId = GetterUtil.getLong(
1047                                    document.get(Field.ENTRY_CLASS_PK));
1048    
1049                            long classNameId = PortalUtil.getClassNameId(
1050                                    DLFileEntry.class.getName());
1051                            long classPK = fileEntryId;
1052    
1053                            return assetEntryPersistence.findByC_C(classNameId, classPK);
1054                    }
1055                    else if (portletId.equals(PortletKeys.JOURNAL)) {
1056                            long groupId = GetterUtil.getLong(document.get(Field.GROUP_ID));
1057                            String articleId = document.get("articleId");
1058                            //double version = GetterUtil.getDouble(document.get("version"));
1059    
1060                            long articleResourcePrimKey =
1061                                    journalArticleResourceLocalService.getArticleResourcePrimKey(
1062                                            groupId, articleId);
1063    
1064                            long classNameId = PortalUtil.getClassNameId(
1065                                    JournalArticle.class.getName());
1066                            long classPK = articleResourcePrimKey;
1067    
1068                            return assetEntryPersistence.findByC_C(classNameId, classPK);
1069                    }
1070                    else if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
1071                            long messageId = GetterUtil.getLong(
1072                                    document.get(Field.ENTRY_CLASS_PK));
1073    
1074                            long classNameId = PortalUtil.getClassNameId(
1075                                    MBMessage.class.getName());
1076                            long classPK = messageId;
1077    
1078                            return assetEntryPersistence.findByC_C(classNameId, classPK);
1079                    }
1080                    else if (portletId.equals(PortletKeys.WIKI)) {
1081                            long nodeId = GetterUtil.getLong(
1082                                    document.get(Field.ENTRY_CLASS_PK));
1083                            String title = document.get(Field.TITLE);
1084    
1085                            long pageResourcePrimKey =
1086                                    wikiPageResourceLocalService.getPageResourcePrimKey(
1087                                            nodeId, title);
1088    
1089                            long classNameId = PortalUtil.getClassNameId(
1090                                    WikiPage.class.getName());
1091                            long classPK = pageResourcePrimKey;
1092    
1093                            return assetEntryPersistence.findByC_C(classNameId, classPK);
1094                    }
1095    
1096                    return null;
1097            }
1098    
1099            protected void reindex(AssetEntry entry) throws PortalException {
1100                    String className = PortalUtil.getClassName(entry.getClassNameId());
1101    
1102                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(className);
1103    
1104                    indexer.reindex(className, entry.getClassPK());
1105            }
1106    
1107            protected AssetEntry updateVisible(AssetEntry entry, boolean visible)
1108                    throws PortalException, SystemException {
1109    
1110                    if (visible == entry.isVisible()) {
1111                            return assetEntryPersistence.update(entry);
1112                    }
1113    
1114                    entry.setVisible(visible);
1115    
1116                    assetEntryPersistence.update(entry);
1117    
1118                    List<AssetTag> tags = assetEntryPersistence.getAssetTags(
1119                            entry.getEntryId());
1120    
1121                    if (visible) {
1122                            for (AssetTag tag : tags) {
1123                                    assetTagLocalService.incrementAssetCount(
1124                                            tag.getTagId(), entry.getClassNameId());
1125                            }
1126    
1127                            socialActivityCounterLocalService.enableActivityCounters(
1128                                    entry.getClassNameId(), entry.getClassPK());
1129                    }
1130                    else {
1131                            for (AssetTag tag : tags) {
1132                                    assetTagLocalService.decrementAssetCount(
1133                                            tag.getTagId(), entry.getClassNameId());
1134                            }
1135    
1136                            socialActivityCounterLocalService.disableActivityCounters(
1137                                    entry.getClassNameId(), entry.getClassPK());
1138                    }
1139    
1140                    reindex(entry);
1141    
1142                    return entry;
1143            }
1144    
1145    }