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