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