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.cal.TZSRecurrence;
018    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.sql.Connection;
025    import java.sql.PreparedStatement;
026    import java.sql.ResultSet;
027    
028    import java.util.List;
029    
030    import org.jabsorb.JSONSerializer;
031    
032    /**
033     * @author Juan Fern??ndez
034     * @author Matthew Kong
035     * @author Mate Thurzo
036     */
037    public class VerifyCalendar extends VerifyProcess {
038    
039            @Override
040            protected void doVerify() throws Exception {
041                    verifyEndDate();
042                    verifyNoAssets();
043                    verifyRecurrence();
044            }
045    
046            protected void updateEvent(long eventId, String recurrence)
047                    throws Exception {
048    
049                    Connection con = null;
050                    PreparedStatement ps = null;
051    
052                    try {
053                            con = DataAccess.getUpgradeOptimizedConnection();
054    
055                            ps = con.prepareStatement(
056                                    "update CalEvent set recurrence = ? where eventId = ?");
057    
058                            ps.setString(1, recurrence);
059                            ps.setLong(2, eventId);
060    
061                            ps.executeUpdate();
062                    }
063                    finally {
064                            DataAccess.cleanUp(con, ps);
065                    }
066            }
067    
068            protected void verifyEndDate() throws Exception {
069                    runSQL(
070                            "update CalEvent set endDate = null where endDate is not null " +
071                                    "and (recurrence like '%\"until\":null%' or recurrence like " +
072                                            "'null')");
073            }
074    
075            @SuppressWarnings("deprecation")
076            protected void verifyNoAssets() throws Exception {
077                    List<com.liferay.portlet.calendar.model.CalEvent> events =
078                            com.liferay.portlet.calendar.service.CalEventLocalServiceUtil.
079                                    getNoAssetEvents();
080    
081                    if (_log.isDebugEnabled()) {
082                            _log.debug("Processing " + events.size() + " events with no asset");
083                    }
084    
085                    for (com.liferay.portlet.calendar.model.CalEvent event : events) {
086                            try {
087                                    com.liferay.portlet.calendar.service.CalEventLocalServiceUtil.
088                                            updateAsset(event.getUserId(), event, null, null, null);
089                            }
090                            catch (Exception e) {
091                                    if (_log.isWarnEnabled()) {
092                                            _log.warn(
093                                                    "Unable to update asset for event " +
094                                                            event.getEventId() + ": " + e.getMessage());
095                                    }
096                            }
097                    }
098    
099                    if (_log.isDebugEnabled()) {
100                            _log.debug("Assets verified for events");
101                    }
102            }
103    
104            protected void verifyRecurrence() throws Exception {
105                    JSONSerializer jsonSerializer = new JSONSerializer();
106    
107                    jsonSerializer.registerDefaultSerializers();
108    
109                    Connection con = null;
110                    PreparedStatement ps = null;
111                    ResultSet rs = null;
112    
113                    try {
114                            con = DataAccess.getUpgradeOptimizedConnection();
115    
116                            ps = con.prepareStatement(
117                                    "select eventId, recurrence from CalEvent where (CAST_TEXT(" +
118                                            "recurrence) != '') and recurrence not like " +
119                                                    "'%serializable%'");
120    
121                            rs = ps.executeQuery();
122    
123                            while (rs.next()) {
124                                    long eventId = rs.getLong("eventId");
125                                    String recurrence = rs.getString("recurrence");
126    
127                                    TZSRecurrence recurrenceObj = null;
128    
129                                    if (Validator.isNotNull(recurrence)) {
130                                            recurrenceObj = (TZSRecurrence)jsonSerializer.fromJSON(
131                                                    recurrence);
132                                    }
133    
134                                    String newRecurrence = JSONFactoryUtil.serialize(recurrenceObj);
135    
136                                    updateEvent(eventId, newRecurrence);
137                            }
138                    }
139                    finally {
140                            DataAccess.cleanUp(con, ps, rs);
141                    }
142            }
143    
144            private static final Log _log = LogFactoryUtil.getLog(VerifyCalendar.class);
145    
146    }