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