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