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