001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.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     */
038    public class VerifyCalendar extends VerifyProcess {
039    
040            @Override
041            protected void doVerify() throws Exception {
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 verifyNoAssets() throws Exception {
069                    List<CalEvent> events = CalEventLocalServiceUtil.getNoAssetEvents();
070    
071                    if (_log.isDebugEnabled()) {
072                            _log.debug("Processing " + events.size() + " events with no asset");
073                    }
074    
075                    for (CalEvent event : events) {
076                            try {
077                                    CalEventLocalServiceUtil.updateAsset(
078                                            event.getUserId(), event, null, null, null);
079                            }
080                            catch (Exception e) {
081                                    if (_log.isWarnEnabled()) {
082                                            _log.warn(
083                                                    "Unable to update asset for event " +
084                                                            event.getEventId() + ": " + e.getMessage());
085                                    }
086                            }
087                    }
088    
089                    if (_log.isDebugEnabled()) {
090                            _log.debug("Assets verified for events");
091                    }
092    
093            }
094    
095            protected void verifyRecurrence() throws Exception {
096                    JSONSerializer jsonSerializer = new JSONSerializer();
097    
098                    jsonSerializer.registerDefaultSerializers();
099    
100                    Connection con = null;
101                    PreparedStatement ps = null;
102                    ResultSet rs = null;
103    
104                    try {
105                            con = DataAccess.getUpgradeOptimizedConnection();
106    
107                            String sql =
108                                    "select eventId, recurrence from CalEvent where (CAST_TEXT(" +
109                                            "recurrence) is not null or CAST_TEXT(recurrence) != '') " +
110                                                    "and recurrence not like '%serializable%'";
111    
112                            sql = SQLTransformer.transform(sql);
113    
114                            ps = con.prepareStatement(sql);
115    
116                            rs = ps.executeQuery();
117    
118                            while (rs.next()) {
119                                    long eventId = rs.getLong("eventId");
120                                    String recurrence = rs.getString("recurrence");
121    
122                                    TZSRecurrence recurrenceObj =
123                                            (TZSRecurrence)jsonSerializer.fromJSON(recurrence);
124    
125                                    String newRecurrence = JSONFactoryUtil.serialize(recurrenceObj);
126    
127                                    updateEvent(eventId, newRecurrence);
128                            }
129                    }
130                    finally {
131                            DataAccess.cleanUp(con, ps, rs);
132                    }
133            }
134    
135            private static Log _log = LogFactoryUtil.getLog(VerifyCalendar.class);
136    
137    }