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                    // Entry
538    
539                    long classNameId = PortalUtil.getClassNameId(className);
540    
541                    validate(groupId, className, categoryIds, tagNames);
542    
543                    AssetEntry entry = assetEntryPersistence.fetchByC_C(
544                            classNameId, classPK);
545    
546                    boolean oldVisible = false;
547    
548                    if (entry != null) {
549                            oldVisible = entry.isVisible();
550                    }
551    
552                    if (modifiedDate == null) {
553                            modifiedDate = new Date();
554                    }
555    
556                    if (entry == null) {
557                            long entryId = counterLocalService.increment();
558    
559                            entry = assetEntryPersistence.create(entryId);
560    
561                            Group group = groupLocalService.getGroup(groupId);
562    
563                            entry.setCompanyId(group.getCompanyId());
564    
565                            entry.setUserId(userId);
566    
567                            User user = userPersistence.fetchByPrimaryKey(userId);
568    
569                            if (user != null) {
570                                    entry.setUserName(user.getFullName());
571                            }
572                            else {
573                                    entry.setUserName(StringPool.BLANK);
574                            }
575    
576                            if (createDate == null) {
577                                    createDate = new Date();
578                            }
579    
580                            entry.setCreateDate(createDate);
581    
582                            entry.setModifiedDate(modifiedDate);
583                            entry.setClassNameId(classNameId);
584                            entry.setClassPK(classPK);
585                            entry.setClassUuid(classUuid);
586                            entry.setVisible(visible);
587                            entry.setExpirationDate(expirationDate);
588    
589                            if (priority == null) {
590                                    entry.setPriority(0);
591                            }
592    
593                            entry.setViewCount(0);
594                    }
595    
596                    entry.setGroupId(groupId);
597                    entry.setModifiedDate(modifiedDate);
598                    entry.setClassTypeId(classTypeId);
599                    entry.setVisible(visible);
600                    entry.setStartDate(startDate);
601                    entry.setEndDate(endDate);
602                    entry.setExpirationDate(expirationDate);
603                    entry.setMimeType(mimeType);
604                    entry.setTitle(title);
605                    entry.setDescription(description);
606                    entry.setSummary(summary);
607                    entry.setUrl(url);
608                    entry.setLayoutUuid(layoutUuid);
609                    entry.setHeight(height);
610                    entry.setWidth(width);
611    
612                    if (priority != null) {
613                            entry.setPriority(priority.intValue());
614                    }
615    
616                    // Categories
617    
618                    if (categoryIds != null) {
619                            categoryIds = checkCategories(className, classPK, categoryIds);
620    
621                            assetEntryPersistence.setAssetCategories(
622                                    entry.getEntryId(), categoryIds);
623                    }
624    
625                    // Tags
626    
627                    if (tagNames != null) {
628                            tagNames = checkTags(className, classPK, tagNames);
629    
630                            long siteGroupId = PortalUtil.getSiteGroupId(groupId);
631    
632                            Group siteGroup = groupLocalService.getGroup(siteGroupId);
633    
634                            List<AssetTag> tags = assetTagLocalService.checkTags(
635                                    userId, siteGroup, tagNames);
636    
637                            List<AssetTag> oldTags = assetEntryPersistence.getAssetTags(
638                                    entry.getEntryId());
639    
640                            assetEntryPersistence.setAssetTags(entry.getEntryId(), tags);
641    
642                            if (entry.isVisible()) {
643                                    boolean isNew = entry.isNew();
644    
645                                    assetEntryPersistence.updateImpl(entry);
646    
647                                    if (isNew) {
648                                            for (AssetTag tag : tags) {
649                                                    assetTagLocalService.incrementAssetCount(
650                                                            tag.getTagId(), classNameId);
651                                            }
652                                    }
653                                    else {
654                                            for (AssetTag oldTag : oldTags) {
655                                                    if (!tags.contains(oldTag)) {
656                                                            assetTagLocalService.decrementAssetCount(
657                                                                    oldTag.getTagId(), classNameId);
658                                                    }
659                                            }
660    
661                                            for (AssetTag tag : tags) {
662                                                    if (!oldTags.contains(tag)) {
663                                                            assetTagLocalService.incrementAssetCount(
664                                                                    tag.getTagId(), classNameId);
665                                                    }
666                                            }
667                                    }
668                            }
669                            else if (oldVisible) {
670                                    for (AssetTag oldTag : oldTags) {
671                                            assetTagLocalService.decrementAssetCount(
672                                                    oldTag.getTagId(), classNameId);
673                                    }
674                            }
675                    }
676    
677                    // Update entry after tags so that entry listeners have access to the
678                    // saved categories and tags
679    
680                    assetEntryPersistence.update(entry);
681    
682                    // Synchronize
683    
684                    if (sync) {
685                            if (className.equals(BlogsEntry.class.getName())) {
686                                    BlogsEntry blogsEntry = blogsEntryPersistence.findByPrimaryKey(
687                                            classPK);
688    
689                                    blogsEntry.setTitle(title);
690    
691                                    blogsEntryPersistence.update(blogsEntry);
692                            }
693                            else if (className.equals(BookmarksEntry.class.getName())) {
694                                    BookmarksEntry bookmarksEntry =
695                                            bookmarksEntryPersistence.findByPrimaryKey(classPK);
696    
697                                    bookmarksEntry.setName(title);
698                                    bookmarksEntry.setDescription(description);
699                                    bookmarksEntry.setUrl(url);
700    
701                                    bookmarksEntryPersistence.update(bookmarksEntry);
702                            }
703                            else if (className.equals(DLFileEntry.class.getName())) {
704                                    DLFileEntry dlFileEntry =
705                                            dlFileEntryPersistence.findByPrimaryKey(classPK);
706    
707                                    dlFileEntry.setTitle(title);
708                                    dlFileEntry.setDescription(description);
709    
710                                    dlFileEntryPersistence.update(dlFileEntry);
711                            }
712                            else if (className.equals(JournalArticle.class.getName())) {
713                                    JournalArticle journalArticle =
714                                            journalArticlePersistence.findByPrimaryKey(classPK);
715    
716                                    journalArticle.setTitle(title);
717                                    journalArticle.setDescription(description);
718    
719                                    journalArticlePersistence.update(journalArticle);
720                            }
721                            else if (className.equals(MBMessage.class.getName())) {
722                                    MBMessage mbMessage = mbMessagePersistence.findByPrimaryKey(
723                                            classPK);
724    
725                                    mbMessage.setSubject(title);
726    
727                                    mbMessagePersistence.update(mbMessage);
728                            }
729                            else if (className.equals(WikiPage.class.getName())) {
730                                    WikiPage wikiPage = wikiPagePersistence.findByPrimaryKey(
731                                            classPK);
732    
733                                    wikiPage.setTitle(title);
734    
735                                    wikiPagePersistence.update(wikiPage);
736                            }
737                    }
738    
739                    // Indexer
740    
741                    reindex(entry);
742    
743                    return entry;
744            }
745    
746            @Override
747            public AssetEntry updateEntry(
748                            long userId, long groupId, String className, long classPK,
749                            long[] categoryIds, String[] tagNames)
750                    throws PortalException, SystemException {
751    
752                    long classNameId = PortalUtil.getClassNameId(className);
753    
754                    AssetEntry entry = assetEntryPersistence.fetchByC_C(
755                            classNameId, classPK);
756    
757                    if (entry != null) {
758                            return updateEntry(
759                                    userId, groupId, entry.getCreateDate(), entry.getModifiedDate(),
760                                    className, classPK, entry.getClassUuid(),
761                                    entry.getClassTypeId(), categoryIds, tagNames,
762                                    entry.isVisible(), entry.getStartDate(), entry.getEndDate(),
763                                    entry.getExpirationDate(), entry.getMimeType(),
764                                    entry.getTitle(), entry.getDescription(), entry.getSummary(),
765                                    entry.getUrl(), entry.getLayoutUuid(), entry.getHeight(),
766                                    entry.getWidth(), GetterUtil.getInteger(entry.getPriority()),
767                                    false);
768                    }
769    
770                    return updateEntry(
771                            userId, groupId, null, null, className, classPK, null, 0,
772                            categoryIds, tagNames, true, null, null, null, null, null, null,
773                            null, null, null, 0, 0, null, false);
774            }
775    
776            /**
777             * @deprecated As of 6.2.0, replaced by {@link #updateEntry(long, long,
778             *             String, long, String, long, long[], String[], boolean, Date,
779             *             Date, Date, String, String, String, String, String, String,
780             *             int, int, Integer, boolean)}
781             */
782            @Override
783            public AssetEntry updateEntry(
784                            long userId, long groupId, String className, long classPK,
785                            String classUuid, long classTypeId, long[] categoryIds,
786                            String[] tagNames, boolean visible, Date startDate, Date endDate,
787                            Date publishDate, Date expirationDate, String mimeType,
788                            String title, String description, String summary, String url,
789                            String layoutUuid, int height, int width, Integer priority,
790                            boolean sync)
791                    throws PortalException, SystemException {
792    
793                    return updateEntry(
794                            userId, groupId, className, classPK, classUuid, classTypeId,
795                            categoryIds, tagNames, visible, startDate, endDate, expirationDate,
796                            mimeType, title, description, summary, url, layoutUuid, height,
797                            width, priority, sync);
798            }
799    
800            /**
801             * @deprecated As of 6.2.0, replaced by {@link #updateEntry(long, long,
802             *             Date, Date, String, long, String, long, long[], String[],
803             *             boolean, Date, Date, Date, String, String, String, String,
804             *             String, String, int, int, Integer, boolean)}
805             */
806            @Override
807            public AssetEntry updateEntry(
808                            long userId, long groupId, String className, long classPK,
809                            String classUuid, long classTypeId, long[] categoryIds,
810                            String[] tagNames, boolean visible, Date startDate, Date endDate,
811                            Date expirationDate, String mimeType, String title,
812                            String description, String summary, String url, String layoutUuid,
813                            int height, int width, Integer priority, boolean sync)
814                    throws PortalException, SystemException {
815    
816                    return updateEntry(
817                            userId, groupId, null, null, className, classPK, classUuid,
818                            classTypeId, categoryIds, tagNames, visible, startDate, endDate,
819                            expirationDate, mimeType, title, description, summary, url,
820                            layoutUuid, height, width, priority, sync);
821            }
822    
823            @Override
824            public AssetEntry updateEntry(
825                            String className, long classPK, Date publishDate, boolean visible)
826                    throws PortalException, SystemException {
827    
828                    long classNameId = PortalUtil.getClassNameId(className);
829    
830                    AssetEntry entry = assetEntryPersistence.findByC_C(
831                            classNameId, classPK);
832    
833                    entry.setPublishDate(publishDate);
834    
835                    return updateVisible(entry, visible);
836            }
837    
838            @Override
839            public AssetEntry updateEntry(
840                            String className, long classPK, Date publishDate,
841                            Date expirationDate, boolean visible)
842                    throws PortalException, SystemException {
843    
844                    long classNameId = PortalUtil.getClassNameId(className);
845    
846                    AssetEntry entry = assetEntryPersistence.findByC_C(
847                            classNameId, classPK);
848    
849                    entry.setExpirationDate(expirationDate);
850                    entry.setPublishDate(publishDate);
851    
852                    return updateVisible(entry, visible);
853            }
854    
855            @Override
856            public AssetEntry updateVisible(
857                            String className, long classPK, boolean visible)
858                    throws PortalException, SystemException {
859    
860                    long classNameId = PortalUtil.getClassNameId(className);
861    
862                    AssetEntry entry = assetEntryPersistence.findByC_C(
863                            classNameId, classPK);
864    
865                    return updateVisible(entry, visible);
866            }
867    
868            @Override
869            public void validate(
870                            long groupId, String className, long[] categoryIds,
871                            String[] tagNames)
872                    throws PortalException, SystemException {
873    
874                    if (ExportImportThreadLocal.isImportInProcess()) {
875                            return;
876                    }
877    
878                    AssetEntryValidator validator = (AssetEntryValidator)InstancePool.get(
879                            PropsValues.ASSET_ENTRY_VALIDATOR);
880    
881                    validator.validate(groupId, className, categoryIds, tagNames);
882            }
883    
884            protected long[] checkCategories(
885                            String className, long classPK, long[] categoryIds)
886                    throws PortalException, SystemException {
887    
888                    PermissionChecker permissionChecker =
889                            PermissionThreadLocal.getPermissionChecker();
890    
891                    if (permissionChecker == null) {
892                            return categoryIds;
893                    }
894    
895                    List<AssetCategory> oldCategories =
896                            assetCategoryLocalService.getCategories(className, classPK);
897    
898                    for (AssetCategory category : oldCategories) {
899                            if (!ArrayUtil.contains(
900                                            categoryIds, category.getCategoryId()) &&
901                                    !AssetCategoryPermission.contains(
902                                            permissionChecker, category, ActionKeys.VIEW)) {
903    
904                                    categoryIds = ArrayUtil.append(
905                                            categoryIds, category.getCategoryId());
906                            }
907                    }
908    
909                    return categoryIds;
910            }
911    
912            protected String[] checkTags(
913                            String className, long classPK, String[] tagNames)
914                    throws PortalException, SystemException {
915    
916                    PermissionChecker permissionChecker =
917                            PermissionThreadLocal.getPermissionChecker();
918    
919                    if (permissionChecker == null) {
920                            return tagNames;
921                    }
922    
923                    List<AssetTag> oldTags = assetTagLocalService.getTags(
924                            className, classPK);
925    
926                    for (AssetTag tag : oldTags) {
927                            if (!ArrayUtil.contains(tagNames, tag.getName()) &&
928                                    !AssetTagPermission.contains(
929                                            permissionChecker, tag, ActionKeys.VIEW)) {
930    
931                                    tagNames = ArrayUtil.append(tagNames, tag.getName());
932                            }
933                    }
934    
935                    return tagNames;
936            }
937    
938            protected Hits doSearch(
939                            long companyId, String className, SearchContext searchContext)
940                    throws Exception {
941    
942                    Indexer indexer = AssetSearcher.getInstance();
943    
944                    AssetSearcher assetSearcher = (AssetSearcher)indexer;
945    
946                    AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
947    
948                    assetEntryQuery.setClassNameIds(getClassNameIds(companyId, className));
949    
950                    QueryConfig queryConfig = searchContext.getQueryConfig();
951    
952                    queryConfig.setHighlightEnabled(false);
953                    queryConfig.setScoreEnabled(false);
954    
955                    assetSearcher.setAssetEntryQuery(assetEntryQuery);
956    
957                    return assetSearcher.search(searchContext);
958            }
959    
960            protected long[] getClassNameIds(long companyId, String className) {
961                    if (Validator.isNotNull(className)) {
962                            return new long[] {PortalUtil.getClassNameId(className)};
963                    }
964    
965                    List<AssetRendererFactory> rendererFactories =
966                            AssetRendererFactoryRegistryUtil.getAssetRendererFactories(
967                                    companyId);
968    
969                    long[] classNameIds = new long[rendererFactories.size()];
970    
971                    for (int i = 0; i < rendererFactories.size(); i++) {
972                            AssetRendererFactory rendererFactory = rendererFactories.get(i);
973    
974                            classNameIds[i] = PortalUtil.getClassNameId(
975                                    rendererFactory.getClassName());
976                    }
977    
978                    return classNameIds;
979            }
980    
981            protected AssetEntry getEntry(Document document)
982                    throws PortalException, SystemException {
983    
984                    String portletId = GetterUtil.getString(document.get(Field.PORTLET_ID));
985    
986                    if (portletId.equals(PortletKeys.BLOGS)) {
987                            long entryId = GetterUtil.getLong(
988                                    document.get(Field.ENTRY_CLASS_PK));
989    
990                            long classNameId = PortalUtil.getClassNameId(
991                                    BlogsEntry.class.getName());
992                            long classPK = entryId;
993    
994                            return assetEntryPersistence.findByC_C(classNameId, classPK);
995                    }
996                    else if (portletId.equals(PortletKeys.BOOKMARKS)) {
997                            long entryId = GetterUtil.getLong(
998                                    document.get(Field.ENTRY_CLASS_PK));
999    
1000                            long classNameId = PortalUtil.getClassNameId(
1001                                    BookmarksEntry.class.getName());
1002                            long classPK = entryId;
1003    
1004                            return assetEntryPersistence.findByC_C(classNameId, classPK);
1005                    }
1006                    else if (portletId.equals(PortletKeys.DOCUMENT_LIBRARY)) {
1007                            long fileEntryId = GetterUtil.getLong(
1008                                    document.get(Field.ENTRY_CLASS_PK));
1009    
1010                            long classNameId = PortalUtil.getClassNameId(
1011                                    DLFileEntry.class.getName());
1012                            long classPK = fileEntryId;
1013    
1014                            return assetEntryPersistence.findByC_C(classNameId, classPK);
1015                    }
1016                    else if (portletId.equals(PortletKeys.JOURNAL)) {
1017                            long groupId = GetterUtil.getLong(document.get(Field.GROUP_ID));
1018                            String articleId = document.get("articleId");
1019                            //double version = GetterUtil.getDouble(document.get("version"));
1020    
1021                            long articleResourcePrimKey =
1022                                    journalArticleResourceLocalService.getArticleResourcePrimKey(
1023                                            groupId, articleId);
1024    
1025                            long classNameId = PortalUtil.getClassNameId(
1026                                    JournalArticle.class.getName());
1027                            long classPK = articleResourcePrimKey;
1028    
1029                            return assetEntryPersistence.findByC_C(classNameId, classPK);
1030                    }
1031                    else if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
1032                            long messageId = GetterUtil.getLong(
1033                                    document.get(Field.ENTRY_CLASS_PK));
1034    
1035                            long classNameId = PortalUtil.getClassNameId(
1036                                    MBMessage.class.getName());
1037                            long classPK = messageId;
1038    
1039                            return assetEntryPersistence.findByC_C(classNameId, classPK);
1040                    }
1041                    else if (portletId.equals(PortletKeys.WIKI)) {
1042                            long nodeId = GetterUtil.getLong(
1043                                    document.get(Field.ENTRY_CLASS_PK));
1044                            String title = document.get(Field.TITLE);
1045    
1046                            long pageResourcePrimKey =
1047                                    wikiPageResourceLocalService.getPageResourcePrimKey(
1048                                            nodeId, title);
1049    
1050                            long classNameId = PortalUtil.getClassNameId(
1051                                    WikiPage.class.getName());
1052                            long classPK = pageResourcePrimKey;
1053    
1054                            return assetEntryPersistence.findByC_C(classNameId, classPK);
1055                    }
1056    
1057                    return null;
1058            }
1059    
1060            protected void reindex(AssetEntry entry) throws PortalException {
1061                    String className = PortalUtil.getClassName(entry.getClassNameId());
1062    
1063                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(className);
1064    
1065                    indexer.reindex(className, entry.getClassPK());
1066            }
1067    
1068            protected AssetEntry updateVisible(AssetEntry entry, boolean visible)
1069                    throws PortalException, SystemException {
1070    
1071                    if (visible == entry.isVisible()) {
1072                            return assetEntryPersistence.update(entry);
1073                    }
1074    
1075                    entry.setVisible(visible);
1076    
1077                    assetEntryPersistence.update(entry);
1078    
1079                    List<AssetTag> tags = assetEntryPersistence.getAssetTags(
1080                            entry.getEntryId());
1081    
1082                    if (visible) {
1083                            for (AssetTag tag : tags) {
1084                                    assetTagLocalService.incrementAssetCount(
1085                                            tag.getTagId(), entry.getClassNameId());
1086                            }
1087    
1088                            socialActivityCounterLocalService.enableActivityCounters(
1089                                    entry.getClassNameId(), entry.getClassPK());
1090                    }
1091                    else {
1092                            for (AssetTag tag : tags) {
1093                                    assetTagLocalService.decrementAssetCount(
1094                                            tag.getTagId(), entry.getClassNameId());
1095                            }
1096    
1097                            socialActivityCounterLocalService.disableActivityCounters(
1098                                    entry.getClassNameId(), entry.getClassPK());
1099                    }
1100    
1101                    reindex(entry);
1102    
1103                    return entry;
1104            }
1105    
1106    }