001    /**
002     * Copyright (c) 2000-2010 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.upgrade.v5_1_2.util;
016    
017    import com.liferay.portal.kernel.cal.DayAndPosition;
018    import com.liferay.portal.kernel.cal.Duration;
019    import com.liferay.portal.kernel.cal.Recurrence;
020    import com.liferay.portal.kernel.cal.TZSRecurrence;
021    import com.liferay.portal.kernel.json.JSONException;
022    import com.liferay.portal.kernel.json.JSONFactoryUtil;
023    import com.liferay.portal.kernel.json.JSONObject;
024    import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
025    import com.liferay.portal.kernel.util.Base64;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.TimeZoneUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    
030    import java.util.TimeZone;
031    
032    /**
033     * @author         Samuel Kong
034     * @deprecated
035     */
036    public class CalEventRecurrenceUpgradeColumnImpl extends BaseUpgradeColumnImpl {
037    
038            public CalEventRecurrenceUpgradeColumnImpl(String name) {
039                    super(name);
040            }
041    
042            public Object getNewValue(Object oldValue) throws Exception {
043                    if (Validator.isNull(oldValue)) {
044                            return StringPool.BLANK;
045                    }
046    
047                    String recurrence = (String)oldValue;
048    
049                    Object obj = Base64.stringToObject(recurrence);
050    
051                    if (obj instanceof Recurrence) {
052                            Recurrence recurrenceObj = (Recurrence)obj;
053    
054                            return serialize(recurrenceObj);
055                    }
056                    else if (obj instanceof com.liferay.util.cal.Recurrence) {
057                            com.liferay.util.cal.Recurrence oldRecurrence =
058                                    (com.liferay.util.cal.Recurrence)obj;
059    
060                            com.liferay.util.cal.Duration oldDuration =
061                                    oldRecurrence.getDuration();
062    
063                            Duration duration = new Duration(
064                                    oldDuration.getDays(), oldDuration.getHours(),
065                                    oldDuration.getMinutes(), oldDuration.getSeconds());
066    
067                            duration.setWeeks(oldDuration.getWeeks());
068                            duration.setInterval(oldDuration.getInterval());
069    
070                            Recurrence recurrenceObj = new Recurrence(
071                                    oldRecurrence.getDtStart(), duration,
072                                    oldRecurrence.getFrequency());
073    
074                            com.liferay.util.cal.DayAndPosition[] oldDayPos =
075                                    oldRecurrence.getByDay();
076    
077                            DayAndPosition[] dayPos = null;
078    
079                            if (oldDayPos != null) {
080                                    dayPos = new DayAndPosition[oldDayPos.length];
081    
082                                    for (int i = 0; i < oldDayPos.length; i++) {
083                                            dayPos[i] = new DayAndPosition(
084                                                    oldDayPos[i].getDayOfWeek(),
085                                                    oldDayPos[i].getDayPosition());
086                                    }
087                            }
088    
089                            recurrenceObj.setByDay(dayPos);
090                            recurrenceObj.setByMonth(oldRecurrence.getByMonth());
091                            recurrenceObj.setByMonthDay(oldRecurrence.getByMonthDay());
092                            recurrenceObj.setInterval(oldRecurrence.getInterval());
093                            recurrenceObj.setOccurrence(oldRecurrence.getOccurrence());
094                            recurrenceObj.setWeekStart(oldRecurrence.getWeekStart());
095                            recurrenceObj.setUntil(oldRecurrence.getUntil());
096    
097                            return serialize(recurrenceObj);
098                    }
099                    else {
100                            return StringPool.BLANK;
101                    }
102            }
103    
104            protected String serialize(Recurrence recurrence) throws JSONException {
105                    JSONObject recurrenceJSON = JSONFactoryUtil.createJSONObject(
106                            JSONFactoryUtil.serialize(recurrence));
107    
108                    recurrenceJSON.put("javaClass", TZSRecurrence.class.getName());
109    
110                    TimeZone timeZone = TimeZoneUtil.getTimeZone(StringPool.UTC);
111    
112                    JSONObject timeZoneJSON = JSONFactoryUtil.createJSONObject(
113                            JSONFactoryUtil.serialize(timeZone));
114    
115                    recurrenceJSON.put("timeZone", timeZoneJSON);
116    
117                    return recurrenceJSON.toString();
118            }
119    
120    }