001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.asset.service.impl;
016    
017    import com.liferay.asset.kernel.AssetRendererFactoryRegistryUtil;
018    import com.liferay.asset.kernel.exception.NoSuchEntryException;
019    import com.liferay.asset.kernel.model.AssetCategory;
020    import com.liferay.asset.kernel.model.AssetCategoryConstants;
021    import com.liferay.asset.kernel.model.AssetEntry;
022    import com.liferay.asset.kernel.model.AssetLink;
023    import com.liferay.asset.kernel.model.AssetLinkConstants;
024    import com.liferay.asset.kernel.model.AssetRendererFactory;
025    import com.liferay.asset.kernel.model.AssetTag;
026    import com.liferay.asset.kernel.service.persistence.AssetEntryQuery;
027    import com.liferay.exportimport.kernel.lar.ExportImportThreadLocal;
028    import com.liferay.portal.kernel.dao.orm.QueryUtil;
029    import com.liferay.portal.kernel.exception.PortalException;
030    import com.liferay.portal.kernel.exception.SystemException;
031    import com.liferay.portal.kernel.increment.BufferedIncrement;
032    import com.liferay.portal.kernel.increment.NumberIncrement;
033    import com.liferay.portal.kernel.model.Group;
034    import com.liferay.portal.kernel.model.User;
035    import com.liferay.portal.kernel.search.Field;
036    import com.liferay.portal.kernel.search.Hits;
037    import com.liferay.portal.kernel.search.Indexer;
038    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
039    import com.liferay.portal.kernel.search.QueryConfig;
040    import com.liferay.portal.kernel.search.SearchContext;
041    import com.liferay.portal.kernel.security.permission.ActionKeys;
042    import com.liferay.portal.kernel.security.permission.PermissionChecker;
043    import com.liferay.portal.kernel.security.permission.PermissionThreadLocal;
044    import com.liferay.portal.kernel.social.SocialActivityManagerUtil;
045    import com.liferay.portal.kernel.util.ArrayUtil;
046    import com.liferay.portal.kernel.util.InstancePool;
047    import com.liferay.portal.kernel.util.PortalUtil;
048    import com.liferay.portal.kernel.util.StringPool;
049    import com.liferay.portal.kernel.util.StringUtil;
050    import com.liferay.portal.kernel.util.Validator;
051    import com.liferay.portal.util.PropsValues;
052    import com.liferay.portlet.asset.service.base.AssetEntryLocalServiceBaseImpl;
053    import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
054    import com.liferay.portlet.asset.util.AssetEntryValidator;
055    import com.liferay.portlet.asset.util.AssetSearcher;
056    import com.liferay.social.kernel.model.SocialActivityConstants;
057    
058    import java.util.ArrayList;
059    import java.util.Date;
060    import java.util.List;
061    
062    /**
063     * Provides the local service for accessing, deleting, updating, and validating
064     * asset entries.
065     *
066     * @author Brian Wing Shun Chan
067     * @author Bruno Farache
068     * @author Zsolt Berentey
069     */
070    public class AssetEntryLocalServiceImpl extends AssetEntryLocalServiceBaseImpl {
071    
072            @Override
073            public void deleteEntry(AssetEntry entry) throws PortalException {
074    
075                    // Entry
076    
077                    List<AssetTag> tags = assetEntryPersistence.getAssetTags(
078                            entry.getEntryId());
079    
080                    assetEntryPersistence.remove(entry);
081    
082                    // Links
083    
084                    assetLinkLocalService.deleteLinks(entry.getEntryId());
085    
086                    // Tags
087    
088                    for (AssetTag tag : tags) {
089                            if (entry.isVisible()) {
090                                    assetTagLocalService.decrementAssetCount(
091                                            tag.getTagId(), entry.getClassNameId());
092                            }
093                    }
094    
095                    // Social
096    
097                    SocialActivityManagerUtil.deleteActivities(entry);
098            }
099    
100            @Override
101            public void deleteEntry(long entryId) throws PortalException {
102                    AssetEntry entry = assetEntryPersistence.findByPrimaryKey(entryId);
103    
104                    deleteEntry(entry);
105            }
106    
107            @Override
108            public void deleteEntry(String className, long classPK)
109                    throws PortalException {
110    
111                    long classNameId = classNameLocalService.getClassNameId(className);
112    
113                    AssetEntry entry = assetEntryPersistence.fetchByC_C(
114                            classNameId, classPK);
115    
116                    if (entry != null) {
117                            deleteEntry(entry);
118                    }
119            }
120    
121            @Override
122            public void deleteGroupEntries(long groupId) throws PortalException {
123                    List<AssetEntry> assetEntries = getGroupEntries(groupId);
124    
125                    for (AssetEntry assetEntry : assetEntries) {
126                            deleteEntry(assetEntry);
127                    }
128            }
129    
130            @Override
131            public AssetEntry fetchEntry(long entryId) {
132                    return assetEntryPersistence.fetchByPrimaryKey(entryId);
133            }
134    
135            @Override
136            public AssetEntry fetchEntry(long groupId, String classUuid) {
137                    return assetEntryPersistence.fetchByG_CU(groupId, classUuid);
138            }
139    
140            @Override
141            public AssetEntry fetchEntry(String className, long classPK) {
142                    long classNameId = classNameLocalService.getClassNameId(className);
143    
144                    return assetEntryPersistence.fetchByC_C(classNameId, classPK);
145            }
146    
147            @Override
148            public List<AssetEntry> getAncestorEntries(long entryId)
149                    throws PortalException {
150    
151                    List<AssetEntry> entries = new ArrayList<>();
152    
153                    AssetEntry parentEntry = getParentEntry(entryId);
154    
155                    while (parentEntry != null) {
156                            entries.add(parentEntry);
157    
158                            parentEntry = getParentEntry(parentEntry.getEntryId());
159                    }
160    
161                    return entries;
162            }
163    
164            @Override
165            public List<AssetEntry> getChildEntries(long entryId)
166                    throws PortalException {
167    
168                    List<AssetEntry> entries = new ArrayList<>();
169    
170                    List<AssetLink> links = assetLinkLocalService.getDirectLinks(
171                            entryId, AssetLinkConstants.TYPE_CHILD);
172    
173                    for (AssetLink link : links) {
174                            AssetEntry curAsset = getEntry(link.getEntryId2());
175    
176                            entries.add(curAsset);
177                    }
178    
179                    return entries;
180            }
181    
182            @Override
183            public List<AssetEntry> getCompanyEntries(
184                    long companyId, int start, int end) {
185    
186                    return assetEntryPersistence.findByCompanyId(companyId, start, end);
187            }
188    
189            @Override
190            public int getCompanyEntriesCount(long companyId) {
191                    return assetEntryPersistence.countByCompanyId(companyId);
192            }
193    
194            @Override
195            public List<AssetEntry> getEntries(AssetEntryQuery entryQuery) {
196                    return assetEntryFinder.findEntries(entryQuery);
197            }
198    
199            @Override
200            public List<AssetEntry> getEntries(
201                    long[] groupIds, long[] classNameIds, String keywords, String userName,
202                    String title, String description, Boolean listable,
203                    boolean advancedSearch, boolean andOperator, int start, int end,
204                    String orderByCol1, String orderByCol2, String orderByType1,
205                    String orderByType2) {
206    
207                    AssetEntryQuery assetEntryQuery = getAssetEntryQuery(
208                            groupIds, classNameIds, keywords, userName, title, description,
209                            listable, advancedSearch, andOperator, start, end, orderByCol1,
210                            orderByCol2, orderByType1, orderByType2);
211    
212                    return getEntries(assetEntryQuery);
213            }
214    
215            @Override
216            public int getEntriesCount(AssetEntryQuery entryQuery) {
217                    return assetEntryFinder.countEntries(entryQuery);
218            }
219    
220            @Override
221            public int getEntriesCount(
222                    long[] groupIds, long[] classNameIds, String keywords, String userName,
223                    String title, String description, Boolean listable,
224                    boolean advancedSearch, boolean andOperator) {
225    
226                    AssetEntryQuery assetEntryQuery = getAssetEntryQuery(
227                            groupIds, classNameIds, keywords, userName, title, description,
228                            listable, advancedSearch, andOperator, QueryUtil.ALL_POS,
229                            QueryUtil.ALL_POS, null, null, null, null);
230    
231                    return getEntriesCount(assetEntryQuery);
232            }
233    
234            @Override
235            public AssetEntry getEntry(long entryId) throws PortalException {
236                    return assetEntryPersistence.findByPrimaryKey(entryId);
237            }
238    
239            @Override
240            public AssetEntry getEntry(long groupId, String classUuid)
241                    throws PortalException {
242    
243                    return assetEntryPersistence.findByG_CU(groupId, classUuid);
244            }
245    
246            @Override
247            public AssetEntry getEntry(String className, long classPK)
248                    throws PortalException {
249    
250                    long classNameId = classNameLocalService.getClassNameId(className);
251    
252                    return assetEntryPersistence.findByC_C(classNameId, classPK);
253            }
254    
255            @Override
256            public List<AssetEntry> getGroupEntries(long groupId) {
257                    return assetEntryPersistence.findByGroupId(groupId);
258            }
259    
260            @Override
261            public AssetEntry getNextEntry(long entryId) throws PortalException {
262                    try {
263                            getParentEntry(entryId);
264                    }
265                    catch (NoSuchEntryException nsee) {
266                            List<AssetEntry> childEntries = getChildEntries(entryId);
267    
268                            if (childEntries.isEmpty()) {
269                                    throw nsee;
270                            }
271    
272                            return childEntries.get(0);
273                    }
274    
275                    List<AssetLink> links = assetLinkLocalService.getDirectLinks(
276                            entryId, AssetLinkConstants.TYPE_CHILD);
277    
278                    for (int i = 0; i < links.size(); i++) {
279                            AssetLink link = links.get(i);
280    
281                            if (link.getEntryId2() == entryId) {
282                                    if ((i + 1) >= links.size()) {
283                                            throw new NoSuchEntryException("{entryId=" + entryId + "}");
284                                    }
285                                    else {
286                                            AssetLink nextLink = links.get(i + 1);
287    
288                                            return getEntry(nextLink.getEntryId2());
289                                    }
290                            }
291                    }
292    
293                    throw new NoSuchEntryException("{entryId=" + entryId + "}");
294            }
295    
296            @Override
297            public AssetEntry getParentEntry(long entryId) throws PortalException {
298                    List<AssetLink> links = assetLinkLocalService.getReverseLinks(
299                            entryId, AssetLinkConstants.TYPE_CHILD);
300    
301                    if (links.isEmpty()) {
302                            throw new NoSuchEntryException("{entryId=" + entryId + "}");
303                    }
304    
305                    AssetLink link = links.get(0);
306    
307                    return getEntry(link.getEntryId1());
308            }
309    
310            @Override
311            public AssetEntry getPreviousEntry(long entryId) throws PortalException {
312                    getParentEntry(entryId);
313    
314                    List<AssetLink> links = assetLinkLocalService.getDirectLinks(
315                            entryId, AssetLinkConstants.TYPE_CHILD);
316    
317                    for (int i = 0; i < links.size(); i++) {
318                            AssetLink link = links.get(i);
319    
320                            if (link.getEntryId2() == entryId) {
321                                    if (i == 0) {
322                                            throw new NoSuchEntryException("{entryId=" + entryId + "}");
323                                    }
324                                    else {
325                                            AssetLink nextAssetLink = links.get(i - 1);
326    
327                                            return getEntry(nextAssetLink.getEntryId2());
328                                    }
329                            }
330                    }
331    
332                    throw new NoSuchEntryException("{entryId=" + entryId + "}");
333            }
334    
335            @Override
336            public List<AssetEntry> getTopViewedEntries(
337                    String className, boolean asc, int start, int end) {
338    
339                    return getTopViewedEntries(new String[] {className}, asc, start, end);
340            }
341    
342            @Override
343            public List<AssetEntry> getTopViewedEntries(
344                    String[] className, boolean asc, int start, int end) {
345    
346                    long[] classNameIds = new long[className.length];
347    
348                    for (int i = 0; i < className.length; i++) {
349                            classNameIds[i] = classNameLocalService.getClassNameId(
350                                    className[i]);
351                    }
352    
353                    AssetEntryQuery entryQuery = new AssetEntryQuery();
354    
355                    entryQuery.setClassNameIds(classNameIds);
356                    entryQuery.setEnd(end);
357                    entryQuery.setExcludeZeroViewCount(true);
358                    entryQuery.setOrderByCol1("viewCount");
359                    entryQuery.setOrderByType1(asc ? "ASC" : "DESC");
360                    entryQuery.setStart(start);
361    
362                    return assetEntryFinder.findEntries(entryQuery);
363            }
364    
365            @Override
366            public AssetEntry incrementViewCounter(
367                            long userId, String className, long classPK)
368                    throws PortalException {
369    
370                    User user = userPersistence.findByPrimaryKey(userId);
371    
372                    assetEntryLocalService.incrementViewCounter(
373                            user.getUserId(), className, classPK, 1);
374    
375                    AssetEntry assetEntry = getEntry(className, classPK);
376    
377                    if (!user.isDefaultUser()) {
378                            SocialActivityManagerUtil.addActivity(
379                                    user.getUserId(), assetEntry, SocialActivityConstants.TYPE_VIEW,
380                                    StringPool.BLANK, 0);
381                    }
382    
383                    return assetEntry;
384            }
385    
386            @BufferedIncrement(
387                    configuration = "AssetEntry", incrementClass = NumberIncrement.class
388            )
389            @Override
390            public void incrementViewCounter(
391                    long userId, String className, long classPK, int increment) {
392    
393                    if (ExportImportThreadLocal.isImportInProcess() || (classPK <= 0)) {
394                            return;
395                    }
396    
397                    long classNameId = classNameLocalService.getClassNameId(className);
398    
399                    AssetEntry entry = assetEntryPersistence.fetchByC_C(
400                            classNameId, classPK);
401    
402                    if (entry == null) {
403                            return;
404                    }
405    
406                    entry.setModifiedDate(entry.getModifiedDate());
407                    entry.setViewCount(entry.getViewCount() + increment);
408    
409                    assetEntryPersistence.update(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            @Override
420            public Hits search(
421                    long companyId, long[] groupIds, long userId, String className,
422                    long classTypeId, String keywords, boolean showNonindexable, int status,
423                    int start, int end) {
424    
425                    return search(
426                            companyId, groupIds, userId, className, classTypeId, keywords,
427                            keywords, keywords, null, null, showNonindexable,
428                            new int[] {status}, false, start, end);
429            }
430    
431            @Override
432            public Hits search(
433                    long companyId, long[] groupIds, long userId, String className,
434                    long classTypeId, String keywords, boolean showNonindexable,
435                    int[] statuses, int start, int end) {
436    
437                    return search(
438                            companyId, groupIds, userId, className, classTypeId, keywords,
439                            keywords, keywords, null, null, showNonindexable, statuses, false,
440                            start, end);
441            }
442    
443            @Override
444            public Hits search(
445                    long companyId, long[] groupIds, long userId, String className,
446                    long classTypeId, String keywords, int status, int start, int end) {
447    
448                    return search(
449                            companyId, groupIds, userId, className, classTypeId, keywords,
450                            false, status, start, end);
451            }
452    
453            @Override
454            public Hits search(
455                    long companyId, long[] groupIds, long userId, String className,
456                    long classTypeId, String userName, String title, String description,
457                    String assetCategoryIds, String assetTagNames, boolean showNonindexable,
458                    int status, boolean andSearch, int start, int end) {
459    
460                    return search(
461                            companyId, groupIds, userId, className, classTypeId, userName,
462                            title, description, assetCategoryIds, assetTagNames,
463                            showNonindexable, new int[] {status}, andSearch, start, end);
464            }
465    
466            @Override
467            public Hits search(
468                    long companyId, long[] groupIds, long userId, String className,
469                    long classTypeId, String userName, String title, String description,
470                    String assetCategoryIds, String assetTagNames, boolean showNonindexable,
471                    int[] statuses, boolean andSearch, int start, int end) {
472    
473                    try {
474                            Indexer<?> indexer = AssetSearcher.getInstance();
475    
476                            AssetSearcher assetSearcher = (AssetSearcher)indexer;
477    
478                            AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
479    
480                            assetEntryQuery.setClassNameIds(
481                                    getClassNameIds(companyId, className));
482    
483                            SearchContext searchContext = new SearchContext();
484    
485                            searchContext.setAndSearch(andSearch);
486                            searchContext.setAssetCategoryIds(
487                                    StringUtil.split(assetCategoryIds, 0L));
488                            searchContext.setAssetTagNames(StringUtil.split(assetTagNames));
489                            searchContext.setAttribute(Field.DESCRIPTION, description);
490                            searchContext.setAttribute(Field.TITLE, title);
491                            searchContext.setAttribute(Field.USER_NAME, userName);
492                            searchContext.setAttribute("paginationType", "regular");
493                            searchContext.setAttribute("status", statuses);
494    
495                            if (classTypeId > 0) {
496                                    searchContext.setClassTypeIds(new long[] {classTypeId});
497                            }
498    
499                            if (showNonindexable) {
500                                    searchContext.setAttribute("showNonindexable", Boolean.TRUE);
501                            }
502    
503                            searchContext.setCompanyId(companyId);
504                            searchContext.setEnd(end);
505                            searchContext.setGroupIds(groupIds);
506                            searchContext.setStart(start);
507                            searchContext.setUserId(userId);
508    
509                            QueryConfig queryConfig = searchContext.getQueryConfig();
510    
511                            queryConfig.setHighlightEnabled(false);
512                            queryConfig.setScoreEnabled(false);
513    
514                            assetSearcher.setAssetEntryQuery(assetEntryQuery);
515    
516                            return assetSearcher.search(searchContext);
517                    }
518                    catch (Exception e) {
519                            throw new SystemException(e);
520                    }
521            }
522    
523            @Override
524            public Hits search(
525                    long companyId, long[] groupIds, long userId, String className,
526                    long classTypeId, String userName, String title, String description,
527                    String assetCategoryIds, String assetTagNames, int status,
528                    boolean andSearch, int start, int end) {
529    
530                    return search(
531                            companyId, groupIds, userId, className, classTypeId, userName,
532                            title, description, assetCategoryIds, assetTagNames, false,
533                            new int[] {status}, andSearch, start, end);
534            }
535    
536            @Override
537            public Hits search(
538                    long companyId, long[] groupIds, long userId, String className,
539                    String keywords, int status, int start, int end) {
540    
541                    return search(
542                            companyId, groupIds, userId, className, 0, keywords, status, start,
543                            end);
544            }
545    
546            @Override
547            public Hits search(
548                    long companyId, long[] groupIds, long userId, String className,
549                    String userName, String title, String description,
550                    String assetCategoryIds, String assetTagNames, int status,
551                    boolean andSearch, int start, int end) {
552    
553                    return search(
554                            companyId, groupIds, userId, className, 0, userName, title,
555                            description, assetCategoryIds, assetTagNames, status, andSearch,
556                            start, end);
557            }
558    
559            @Override
560            public AssetEntry updateEntry(
561                            long userId, long groupId, Date createDate, Date modifiedDate,
562                            String className, long classPK, String classUuid, long classTypeId,
563                            long[] categoryIds, String[] tagNames, boolean visible,
564                            Date startDate, Date endDate, Date expirationDate, String mimeType,
565                            String title, String description, String summary, String url,
566                            String layoutUuid, int height, int width, Double priority)
567                    throws PortalException {
568    
569                    // Entry
570    
571                    long classNameId = classNameLocalService.getClassNameId(className);
572    
573                    validate(groupId, className, classTypeId, categoryIds, tagNames);
574    
575                    AssetEntry entry = assetEntryPersistence.fetchByC_C(
576                            classNameId, classPK);
577    
578                    boolean oldVisible = false;
579    
580                    if (entry != null) {
581                            oldVisible = entry.isVisible();
582                    }
583    
584                    if (modifiedDate == null) {
585                            modifiedDate = new Date();
586                    }
587    
588                    if (entry == null) {
589                            long entryId = counterLocalService.increment();
590    
591                            entry = assetEntryPersistence.create(entryId);
592    
593                            Group group = groupLocalService.getGroup(groupId);
594    
595                            entry.setCompanyId(group.getCompanyId());
596    
597                            entry.setUserId(userId);
598    
599                            User user = userPersistence.fetchByPrimaryKey(userId);
600    
601                            if (user != null) {
602                                    entry.setUserName(user.getFullName());
603                            }
604                            else {
605                                    entry.setUserName(StringPool.BLANK);
606                            }
607    
608                            if (createDate == null) {
609                                    createDate = new Date();
610                            }
611    
612                            entry.setCreateDate(createDate);
613    
614                            entry.setClassNameId(classNameId);
615                            entry.setClassPK(classPK);
616                            entry.setClassUuid(classUuid);
617    
618                            if (priority == null) {
619                                    entry.setPriority(0);
620                            }
621    
622                            entry.setViewCount(0);
623                    }
624    
625                    entry.setGroupId(groupId);
626                    entry.setModifiedDate(modifiedDate);
627                    entry.setClassTypeId(classTypeId);
628    
629                    AssetRendererFactory<?> assetRendererFactory =
630                            AssetRendererFactoryRegistryUtil.
631                                    getAssetRendererFactoryByClassNameId(classNameId);
632    
633                    if (assetRendererFactory != null) {
634                            entry.setListable(assetRendererFactory.isListable(classPK));
635                    }
636    
637                    entry.setVisible(visible);
638                    entry.setStartDate(startDate);
639                    entry.setEndDate(endDate);
640                    entry.setExpirationDate(expirationDate);
641                    entry.setMimeType(mimeType);
642                    entry.setTitle(title);
643                    entry.setDescription(description);
644                    entry.setSummary(summary);
645                    entry.setUrl(url);
646                    entry.setLayoutUuid(layoutUuid);
647                    entry.setHeight(height);
648                    entry.setWidth(width);
649    
650                    if (priority != null) {
651                            entry.setPriority(priority.doubleValue());
652                    }
653    
654                    // Categories
655    
656                    if (categoryIds != null) {
657                            categoryIds = checkCategories(className, classPK, categoryIds);
658    
659                            assetEntryPersistence.setAssetCategories(
660                                    entry.getEntryId(), categoryIds);
661                    }
662    
663                    // Tags
664    
665                    if (tagNames != null) {
666                            long siteGroupId = PortalUtil.getSiteGroupId(groupId);
667    
668                            Group siteGroup = groupLocalService.getGroup(siteGroupId);
669    
670                            List<AssetTag> tags = assetTagLocalService.checkTags(
671                                    userId, siteGroup, tagNames);
672    
673                            List<AssetTag> oldTags = assetEntryPersistence.getAssetTags(
674                                    entry.getEntryId());
675    
676                            assetEntryPersistence.setAssetTags(entry.getEntryId(), tags);
677    
678                            if (entry.isVisible()) {
679                                    boolean isNew = entry.isNew();
680    
681                                    assetEntryPersistence.updateImpl(entry);
682    
683                                    if (isNew) {
684                                            for (AssetTag tag : tags) {
685                                                    assetTagLocalService.incrementAssetCount(
686                                                            tag.getTagId(), classNameId);
687                                            }
688                                    }
689                                    else {
690                                            for (AssetTag oldTag : oldTags) {
691                                                    if (!tags.contains(oldTag)) {
692                                                            assetTagLocalService.decrementAssetCount(
693                                                                    oldTag.getTagId(), classNameId);
694                                                    }
695                                            }
696    
697                                            for (AssetTag tag : tags) {
698                                                    if (!oldTags.contains(tag)) {
699                                                            assetTagLocalService.incrementAssetCount(
700                                                                    tag.getTagId(), classNameId);
701                                                    }
702                                            }
703                                    }
704                            }
705                            else if (oldVisible) {
706                                    for (AssetTag oldTag : oldTags) {
707                                            assetTagLocalService.decrementAssetCount(
708                                                    oldTag.getTagId(), classNameId);
709                                    }
710                            }
711                    }
712    
713                    // Update entry after tags so that entry listeners have access to the
714                    // saved categories and tags
715    
716                    assetEntryPersistence.update(entry);
717    
718                    // Indexer
719    
720                    reindex(entry);
721    
722                    return entry;
723            }
724    
725            /**
726             * @deprecated As of 7.0.0, replaced by {@link #updateEntry(long, long,
727             *             Date, Date, String, long, String, long, long[], String[],
728             *             boolean, Date, Date, Date, String, String, String, String,
729             *             String, String, int, int, Double)}
730             */
731            @Deprecated
732            @Override
733            public AssetEntry updateEntry(
734                            long userId, long groupId, Date createDate, Date modifiedDate,
735                            String className, long classPK, String classUuid, long classTypeId,
736                            long[] categoryIds, String[] tagNames, boolean visible,
737                            Date startDate, Date endDate, Date expirationDate, String mimeType,
738                            String title, String description, String summary, String url,
739                            String layoutUuid, int height, int width, Integer priority,
740                            boolean sync)
741                    throws PortalException {
742    
743                    Double priorityDouble = null;
744    
745                    if (priority != null) {
746                            priorityDouble = priority.doubleValue();
747                    }
748    
749                    return updateEntry(
750                            userId, groupId, createDate, modifiedDate, className, classPK,
751                            classUuid, classTypeId, categoryIds, tagNames, visible, startDate,
752                            endDate, expirationDate, mimeType, title, description, summary, url,
753                            layoutUuid, height, width, priorityDouble);
754            }
755    
756            @Override
757            public AssetEntry updateEntry(
758                            long userId, long groupId, String className, long classPK,
759                            long[] categoryIds, String[] tagNames)
760                    throws PortalException {
761    
762                    long classNameId = classNameLocalService.getClassNameId(className);
763    
764                    AssetEntry entry = assetEntryPersistence.fetchByC_C(
765                            classNameId, classPK);
766    
767                    if (entry != null) {
768                            return updateEntry(
769                                    userId, groupId, entry.getCreateDate(), entry.getModifiedDate(),
770                                    className, classPK, entry.getClassUuid(),
771                                    entry.getClassTypeId(), categoryIds, tagNames,
772                                    entry.isVisible(), entry.getStartDate(), entry.getEndDate(),
773                                    entry.getExpirationDate(), entry.getMimeType(),
774                                    entry.getTitle(), entry.getDescription(), entry.getSummary(),
775                                    entry.getUrl(), entry.getLayoutUuid(), entry.getHeight(),
776                                    entry.getWidth(), entry.getPriority());
777                    }
778    
779                    return updateEntry(
780                            userId, groupId, null, null, className, classPK, null, 0,
781                            categoryIds, tagNames, true, null, null, null, null, null, null,
782                            null, null, null, 0, 0, (Double)null);
783            }
784    
785            @Override
786            public AssetEntry updateEntry(
787                            String className, long classPK, Date publishDate, boolean visible)
788                    throws PortalException {
789    
790                    long classNameId = classNameLocalService.getClassNameId(className);
791    
792                    AssetEntry entry = assetEntryPersistence.findByC_C(
793                            classNameId, classPK);
794    
795                    AssetRendererFactory<?> assetRendererFactory =
796                            AssetRendererFactoryRegistryUtil.
797                                    getAssetRendererFactoryByClassNameId(classNameId);
798    
799                    entry.setListable(assetRendererFactory.isListable(classPK));
800    
801                    entry.setPublishDate(publishDate);
802    
803                    return updateVisible(entry, visible);
804            }
805    
806            @Override
807            public AssetEntry updateEntry(
808                            String className, long classPK, Date publishDate,
809                            Date expirationDate, boolean visible)
810                    throws PortalException {
811    
812                    long classNameId = classNameLocalService.getClassNameId(className);
813    
814                    AssetEntry entry = assetEntryPersistence.findByC_C(
815                            classNameId, classPK);
816    
817                    AssetRendererFactory<?> assetRendererFactory =
818                            AssetRendererFactoryRegistryUtil.
819                                    getAssetRendererFactoryByClassNameId(classNameId);
820    
821                    entry.setListable(assetRendererFactory.isListable(classPK));
822    
823                    entry.setExpirationDate(expirationDate);
824                    entry.setPublishDate(publishDate);
825    
826                    return updateVisible(entry, visible);
827            }
828    
829            @Override
830            public AssetEntry updateVisible(AssetEntry entry, boolean visible)
831                    throws PortalException {
832    
833                    if (visible == entry.isVisible()) {
834                            return assetEntryPersistence.update(entry);
835                    }
836    
837                    entry.setVisible(visible);
838    
839                    assetEntryPersistence.update(entry);
840    
841                    List<AssetTag> tags = assetEntryPersistence.getAssetTags(
842                            entry.getEntryId());
843    
844                    if (visible) {
845                            for (AssetTag tag : tags) {
846                                    assetTagLocalService.incrementAssetCount(
847                                            tag.getTagId(), entry.getClassNameId());
848                            }
849    
850                            socialActivityCounterLocalService.enableActivityCounters(
851                                    entry.getClassNameId(), entry.getClassPK());
852                    }
853                    else {
854                            for (AssetTag tag : tags) {
855                                    assetTagLocalService.decrementAssetCount(
856                                            tag.getTagId(), entry.getClassNameId());
857                            }
858    
859                            socialActivityCounterLocalService.disableActivityCounters(
860                                    entry.getClassNameId(), entry.getClassPK());
861                    }
862    
863                    return entry;
864            }
865    
866            @Override
867            public AssetEntry updateVisible(
868                            String className, long classPK, boolean visible)
869                    throws PortalException {
870    
871                    long classNameId = classNameLocalService.getClassNameId(className);
872    
873                    AssetEntry entry = assetEntryPersistence.findByC_C(
874                            classNameId, classPK);
875    
876                    return updateVisible(entry, visible);
877            }
878    
879            @Override
880            public void validate(
881                            long groupId, String className, long classTypePK,
882                            long[] categoryIds, String[] tagNames)
883                    throws PortalException {
884    
885                    if (ExportImportThreadLocal.isImportInProcess()) {
886                            return;
887                    }
888    
889                    AssetEntryValidator validator = (AssetEntryValidator)InstancePool.get(
890                            PropsValues.ASSET_ENTRY_VALIDATOR);
891    
892                    validator.validate(
893                            groupId, className, classTypePK, categoryIds, tagNames);
894            }
895    
896            /**
897             * @deprecated As of 7.0.0, replaced by {@link #validate(long, String, long,
898             *             long[], String[])}
899             */
900            @Deprecated
901            @Override
902            public void validate(
903                            long groupId, String className, long[] categoryIds,
904                            String[] tagNames)
905                    throws PortalException {
906    
907                    validate(
908                            groupId, className, AssetCategoryConstants.ALL_CLASS_TYPE_PK,
909                            categoryIds, tagNames);
910            }
911    
912            protected long[] checkCategories(
913                            String className, long classPK, long[] categoryIds)
914                    throws PortalException {
915    
916                    PermissionChecker permissionChecker =
917                            PermissionThreadLocal.getPermissionChecker();
918    
919                    if (permissionChecker == null) {
920                            return categoryIds;
921                    }
922    
923                    List<AssetCategory> oldCategories =
924                            assetCategoryLocalService.getCategories(className, classPK);
925    
926                    for (AssetCategory category : oldCategories) {
927                            if (!ArrayUtil.contains(
928                                            categoryIds, category.getCategoryId()) &&
929                                    !AssetCategoryPermission.contains(
930                                            permissionChecker, category, ActionKeys.VIEW)) {
931    
932                                    categoryIds = ArrayUtil.append(
933                                            categoryIds, category.getCategoryId());
934                            }
935                    }
936    
937                    return categoryIds;
938            }
939    
940            protected AssetEntryQuery getAssetEntryQuery(
941                    long[] groupIds, long[] classNameIds, String keywords, String userName,
942                    String title, String description, Boolean listable,
943                    boolean advancedSearch, boolean andOperator, int start, int end,
944                    String orderByCol1, String orderByCol2, String orderByType1,
945                    String orderByType2) {
946    
947                    AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
948    
949                    if (advancedSearch) {
950                            assetEntryQuery.setAndOperator(andOperator);
951                            assetEntryQuery.setDescription(description);
952                            assetEntryQuery.setTitle(title);
953                            assetEntryQuery.setUserName(userName);
954                    }
955                    else {
956                            assetEntryQuery.setKeywords(keywords);
957                    }
958    
959                    assetEntryQuery.setClassNameIds(classNameIds);
960                    assetEntryQuery.setEnd(end);
961                    assetEntryQuery.setGroupIds(groupIds);
962                    assetEntryQuery.setListable(listable);
963                    assetEntryQuery.setOrderByCol1(orderByCol1);
964                    assetEntryQuery.setOrderByCol2(orderByCol2);
965                    assetEntryQuery.setOrderByType1(orderByType1);
966                    assetEntryQuery.setOrderByType2(orderByType2);
967                    assetEntryQuery.setStart(start);
968    
969                    return assetEntryQuery;
970            }
971    
972            protected long[] getClassNameIds(long companyId, String className) {
973                    if (Validator.isNotNull(className)) {
974                            return new long[] {classNameLocalService.getClassNameId(className)};
975                    }
976    
977                    List<AssetRendererFactory<?>> rendererFactories =
978                            AssetRendererFactoryRegistryUtil.getAssetRendererFactories(
979                                    companyId);
980    
981                    long[] classNameIds = new long[rendererFactories.size()];
982    
983                    for (int i = 0; i < rendererFactories.size(); i++) {
984                            AssetRendererFactory<?> rendererFactory = rendererFactories.get(i);
985    
986                            classNameIds[i] = classNameLocalService.getClassNameId(
987                                    rendererFactory.getClassName());
988                    }
989    
990                    return classNameIds;
991            }
992    
993            protected void reindex(AssetEntry entry) throws PortalException {
994                    String className = PortalUtil.getClassName(entry.getClassNameId());
995    
996                    Indexer<?> indexer = IndexerRegistryUtil.nullSafeGetIndexer(className);
997    
998                    indexer.reindex(className, entry.getClassPK());
999            }
1000    
1001    }