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.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            @Override
043            public Object getNewValue(Object oldValue) throws Exception {
044                    if (Validator.isNull(oldValue)) {
045                            return StringPool.BLANK;
046                    }
047    
048                    String recurrence = (String)oldValue;
049    
050                    Object obj = Base64.stringToObject(recurrence);
051    
052                    if (obj instanceof Recurrence) {
053                            Recurrence recurrenceObj = (Recurrence)obj;
054    
055                            return serialize(recurrenceObj);
056                    }
057                    else if (obj instanceof com.liferay.util.cal.Recurrence) {
058                            com.liferay.util.cal.Recurrence oldRecurrence =
059                                    (com.liferay.util.cal.Recurrence)obj;
060    
061                            com.liferay.util.cal.Duration oldDuration =
062                                    oldRecurrence.getDuration();
063    
064                            Duration duration = new Duration(
065                                    oldDuration.getDays(), oldDuration.getHours(),
066                                    oldDuration.getMinutes(), oldDuration.getSeconds());
067    
068                            duration.setWeeks(oldDuration.getWeeks());
069                            duration.setInterval(oldDuration.getInterval());
070    
071                            Recurrence recurrenceObj = new Recurrence(
072                                    oldRecurrence.getDtStart(), duration,
073                                    oldRecurrence.getFrequency());
074    
075                            com.liferay.util.cal.DayAndPosition[] oldDayPos =
076                                    oldRecurrence.getByDay();
077    
078                            DayAndPosition[] dayPos = null;
079    
080                            if (oldDayPos != null) {
081                                    dayPos = new DayAndPosition[oldDayPos.length];
082    
083                                    for (int i = 0; i < oldDayPos.length; i++) {
084                                            dayPos[i] = new DayAndPosition(
085                                                    oldDayPos[i].getDayOfWeek(),
086                                                    oldDayPos[i].getDayPosition());
087                                    }
088                            }
089    
090                            recurrenceObj.setByDay(dayPos);
091                            recurrenceObj.setByMonth(oldRecurrence.getByMonth());
092                            recurrenceObj.setByMonthDay(oldRecurrence.getByMonthDay());
093                            recurrenceObj.setInterval(oldRecurrence.getInterval());
094                            recurrenceObj.setOccurrence(oldRecurrence.getOccurrence());
095                            recurrenceObj.setWeekStart(oldRecurrence.getWeekStart());
096                            recurrenceObj.setUntil(oldRecurrence.getUntil());
097    
098                            return serialize(recurrenceObj);
099                    }
100                    else {
101                            return StringPool.BLANK;
102                    }
103            }
104    
105            protected String serialize(Recurrence recurrence) throws JSONException {
106                    JSONObject recurrenceJSON = JSONFactoryUtil.createJSONObject(
107                            JSONFactoryUtil.serialize(recurrence));
108    
109                    recurrenceJSON.put("javaClass", TZSRecurrence.class.getName());
110    
111                    TimeZone timeZone = TimeZoneUtil.getTimeZone(StringPool.UTC);
112    
113                    JSONObject timeZoneJSON = JSONFactoryUtil.createJSONObject(
114                            JSONFactoryUtil.serialize(timeZone));
115    
116                    recurrenceJSON.put("timeZone", timeZoneJSON);
117    
118                    return recurrenceJSON.toString();
119            }
120    
121    }