001    /**
002     * Copyright (c) 2000-present 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.dao.orm.DynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.Property;
019    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONFactoryUtil;
021    import com.liferay.portal.kernel.json.JSONObject;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.model.SystemEvent;
025    import com.liferay.portal.model.SystemEventConstants;
026    import com.liferay.portal.service.SystemEventLocalServiceUtil;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.journal.model.JournalArticle;
029    import com.liferay.portlet.journal.model.JournalArticleResource;
030    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
031    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
032    
033    import java.util.List;
034    
035    /**
036     * @author Daniel Kocsis
037     */
038    public class VerifySystemEvent extends VerifyProcess {
039    
040            @Override
041            protected void doVerify() throws Exception {
042                    verifyJournalArticleDeleteSystemEvents();
043            }
044    
045            protected void verifyJournalArticleDeleteSystemEvents() throws Exception {
046                    DynamicQuery dynamicQuery = SystemEventLocalServiceUtil.dynamicQuery();
047    
048                    Property classNameIdProperty = PropertyFactoryUtil.forName(
049                            "classNameId");
050    
051                    dynamicQuery.add(
052                            classNameIdProperty.eq(
053                                    PortalUtil.getClassNameId(JournalArticle.class)));
054    
055                    Property typeProperty = PropertyFactoryUtil.forName("type");
056    
057                    dynamicQuery.add(typeProperty.eq(SystemEventConstants.TYPE_DELETE));
058    
059                    List<SystemEvent> systemEvents =
060                            SystemEventLocalServiceUtil.dynamicQuery(dynamicQuery);
061    
062                    if (_log.isDebugEnabled()) {
063                            _log.debug(
064                                    "Processing " + systemEvents.size() + " delete system events " +
065                                            "for journal articles");
066                    }
067    
068                    for (SystemEvent systemEvent : systemEvents) {
069                            JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject(
070                                    systemEvent.getExtraData());
071    
072                            if (extraDataJSONObject.has("uuid") ||
073                                    !extraDataJSONObject.has("version")) {
074    
075                                    continue;
076                            }
077    
078                            JournalArticleResource journalArticleResource =
079                                    JournalArticleResourceLocalServiceUtil.
080                                            fetchJournalArticleResourceByUuidAndGroupId(
081                                                    systemEvent.getClassUuid(), systemEvent.getGroupId());
082    
083                            if (journalArticleResource == null) {
084                                    continue;
085                            }
086    
087                            JournalArticle journalArticle =
088                                    JournalArticleLocalServiceUtil.fetchArticle(
089                                            systemEvent.getGroupId(),
090                                            journalArticleResource.getArticleId(),
091                                            extraDataJSONObject.getDouble("version"));
092    
093                            if ((journalArticle == null) || journalArticle.isInTrash()) {
094                                    continue;
095                            }
096    
097                            SystemEventLocalServiceUtil.deleteSystemEvent(systemEvent);
098                    }
099    
100                    if (_log.isDebugEnabled()) {
101                            _log.debug("Delete system events verified for journal articles");
102                    }
103            }
104    
105            private static final Log _log = LogFactoryUtil.getLog(
106                    VerifySystemEvent.class);
107    
108    }