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