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