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