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 " +
074                                            "CAST_TEXT(recurrence) = '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    
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 Log _log = LogFactoryUtil.getLog(VerifyCalendar.class);
145    
146    }