001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.verify;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portlet.wiki.model.WikiPage;
023    import com.liferay.portlet.wiki.model.WikiPageResource;
024    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
025    import com.liferay.portlet.wiki.service.persistence.WikiPageResourceActionableDynamicQuery;
026    import com.liferay.portlet.wiki.util.comparator.PageVersionComparator;
027    
028    import java.util.Date;
029    import java.util.List;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class VerifyWiki extends VerifyProcess {
035    
036            @Override
037            protected void doVerify() throws Exception {
038                    verifyCreateDate();
039                    verifyNoAssetPages();
040            }
041    
042            protected void verifyCreateDate() throws Exception {
043                    ActionableDynamicQuery actionableDynamicQuery =
044                            new WikiPageResourceActionableDynamicQuery() {
045    
046                                    @Override
047                                    public void performAction(Object object) {
048                                            WikiPageResource pageResource = (WikiPageResource)object;
049    
050                                            try {
051                                                    verifyCreateDate(pageResource);
052                                            }
053                                            catch (Exception e) {
054                                                    _log.error(
055                                                            "Unable to update create date for pages " +
056                                                                    pageResource.getNodeId(),
057                                                            e);
058                                            }
059                                    }
060    
061                            };
062    
063                    actionableDynamicQuery.performActions();
064    
065                    if (_log.isDebugEnabled()) {
066                            _log.debug("Create dates verified for pages");
067                    }
068            }
069    
070            protected void verifyCreateDate(WikiPageResource pageResource)
071                    throws SystemException {
072    
073                    List<WikiPage> pages = WikiPageLocalServiceUtil.getPages(
074                            pageResource.getNodeId(), pageResource.getTitle(),
075                            QueryUtil.ALL_POS, QueryUtil.ALL_POS,
076                            new PageVersionComparator(true));
077    
078                    if (pages.size() <= 1) {
079                            return;
080                    }
081    
082                    WikiPage firstPage = pages.get(0);
083    
084                    Date createDate = firstPage.getCreateDate();
085    
086                    for (WikiPage page : pages) {
087                            if (!createDate.equals(page.getCreateDate())) {
088                                    page.setCreateDate(createDate);
089    
090                                    WikiPageLocalServiceUtil.updateWikiPage(page);
091                            }
092                    }
093            }
094    
095            protected void verifyNoAssetPages() throws Exception {
096                    List<WikiPage> pages = WikiPageLocalServiceUtil.getNoAssetPages();
097    
098                    if (_log.isDebugEnabled()) {
099                            _log.debug("Processing " + pages.size() + " pages with no asset");
100                    }
101    
102                    for (WikiPage page : pages) {
103                            try {
104                                    WikiPageLocalServiceUtil.updateAsset(
105                                            page.getUserId(), page, null, null, null);
106                            }
107                            catch (Exception e) {
108                                    if (_log.isWarnEnabled()) {
109                                            _log.warn(
110                                                    "Unable to update asset for page " + page.getPageId() +
111                                                            ": " + e.getMessage());
112                                    }
113                            }
114                    }
115    
116                    if (_log.isDebugEnabled()) {
117                            _log.debug("Assets verified for pages");
118                    }
119            }
120    
121            private static Log _log = LogFactoryUtil.getLog(VerifyWiki.class);
122    
123    }