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