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