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