001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.verify;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.util.PortalInstances;
020    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
021    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
022    import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
023    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
024    
025    import java.util.List;
026    
027    /**
028     * @author Raymond Aug??
029     * @author Alexander Chow
030     */
031    public class VerifyBookmarks extends VerifyProcess {
032    
033            @Override
034            protected void doVerify() throws Exception {
035                    updateEntryAssets();
036                    updateFolderAssets();
037                    verifyTree();
038            }
039    
040            protected void updateEntryAssets() throws Exception {
041                    List<BookmarksEntry> entries =
042                            BookmarksEntryLocalServiceUtil.getNoAssetEntries();
043    
044                    if (_log.isDebugEnabled()) {
045                            _log.debug(
046                                    "Processing " + entries.size() + " entries with no asset");
047                    }
048    
049                    for (BookmarksEntry entry : entries) {
050                            try {
051                                    BookmarksEntryLocalServiceUtil.updateAsset(
052                                            entry.getUserId(), entry, null, null, null);
053                            }
054                            catch (Exception e) {
055                                    if (_log.isWarnEnabled()) {
056                                            _log.warn(
057                                                    "Unable to update asset for entry " +
058                                                            entry.getEntryId() + ": " + e.getMessage());
059                                    }
060                            }
061                    }
062    
063                    if (_log.isDebugEnabled()) {
064                            _log.debug("Assets verified for entries");
065                    }
066            }
067    
068            protected void updateFolderAssets() throws Exception {
069                    List<BookmarksFolder> folders =
070                            BookmarksFolderLocalServiceUtil.getNoAssetFolders();
071    
072                    if (_log.isDebugEnabled()) {
073                            _log.debug(
074                                    "Processing " + folders.size() + " folders with no asset");
075                    }
076    
077                    for (BookmarksFolder folder : folders) {
078                            try {
079                                    BookmarksFolderLocalServiceUtil.updateAsset(
080                                            folder.getUserId(), folder, null, null, null);
081                            }
082                            catch (Exception e) {
083                                    if (_log.isWarnEnabled()) {
084                                            _log.warn(
085                                                    "Unable to update asset for folder " +
086                                                            folder.getFolderId() + ": " + e.getMessage());
087                                    }
088                            }
089                    }
090    
091                    if (_log.isDebugEnabled()) {
092                            _log.debug("Assets verified for folders");
093                    }
094            }
095    
096            protected void verifyTree() throws Exception {
097                    long[] companyIds = PortalInstances.getCompanyIdsBySQL();
098    
099                    for (long companyId : companyIds) {
100                            BookmarksFolderLocalServiceUtil.rebuildTree(companyId);
101                    }
102            }
103    
104            private static Log _log = LogFactoryUtil.getLog(VerifyBookmarks.class);
105    
106    }