1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upgrade.v5_1_2.util;
24  
25  import com.liferay.portal.kernel.cal.DayAndPosition;
26  import com.liferay.portal.kernel.cal.Duration;
27  import com.liferay.portal.kernel.cal.Recurrence;
28  import com.liferay.portal.kernel.cal.TZSRecurrence;
29  import com.liferay.portal.kernel.json.JSONException;
30  import com.liferay.portal.kernel.json.JSONFactoryUtil;
31  import com.liferay.portal.kernel.json.JSONObject;
32  import com.liferay.portal.kernel.util.Base64;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.upgrade.util.BaseUpgradeColumnImpl;
36  
37  import java.util.TimeZone;
38  
39  /**
40   * <a href="CalEventRecurrenceUpgradeColumnImpl.java.html"><b><i>View Source</i>
41   * </b></a>
42   *
43   * @author Samuel Kong
44   *
45   * @deprecated
46   *
47   */
48  public class CalEventRecurrenceUpgradeColumnImpl extends BaseUpgradeColumnImpl {
49  
50      public CalEventRecurrenceUpgradeColumnImpl(String name) {
51          super(name);
52      }
53  
54      public Object getNewValue(Object oldValue) throws Exception {
55          if (Validator.isNull(oldValue)) {
56              return StringPool.BLANK;
57          }
58  
59          String recurrence = (String)oldValue;
60  
61          Object obj = Base64.stringToObject(recurrence);
62  
63          if (obj instanceof Recurrence) {
64              Recurrence recurrenceObj = (Recurrence)obj;
65  
66              return serialize(recurrenceObj);
67          }
68          else if (obj instanceof com.liferay.util.cal.Recurrence) {
69              com.liferay.util.cal.Recurrence oldRecurrence =
70                  (com.liferay.util.cal.Recurrence)obj;
71  
72              com.liferay.util.cal.Duration oldDuration =
73                  oldRecurrence.getDuration();
74  
75              Duration duration = new Duration(
76                  oldDuration.getDays(), oldDuration.getHours(),
77                  oldDuration.getMinutes(), oldDuration.getSeconds());
78  
79              duration.setWeeks(oldDuration.getWeeks());
80              duration.setInterval(oldDuration.getInterval());
81  
82              Recurrence recurrenceObj = new Recurrence(
83                  oldRecurrence.getDtStart(), duration,
84                  oldRecurrence.getFrequency());
85  
86              com.liferay.util.cal.DayAndPosition[] oldDayPos =
87                  oldRecurrence.getByDay();
88  
89              DayAndPosition[] dayPos = null;
90  
91              if (oldDayPos != null) {
92                  dayPos = new DayAndPosition[oldDayPos.length];
93  
94                  for (int i = 0; i < oldDayPos.length; i++) {
95                      dayPos[i] = new DayAndPosition(
96                          oldDayPos[i].getDayOfWeek(),
97                          oldDayPos[i].getDayPosition());
98                  }
99              }
100 
101             recurrenceObj.setByDay(dayPos);
102             recurrenceObj.setByMonth(oldRecurrence.getByMonth());
103             recurrenceObj.setByMonthDay(oldRecurrence.getByMonthDay());
104             recurrenceObj.setInterval(oldRecurrence.getInterval());
105             recurrenceObj.setOccurrence(oldRecurrence.getOccurrence());
106             recurrenceObj.setWeekStart(oldRecurrence.getWeekStart());
107             recurrenceObj.setUntil(oldRecurrence.getUntil());
108 
109             return serialize(recurrenceObj);
110         }
111         else {
112             return StringPool.BLANK;
113         }
114     }
115 
116     protected String serialize(Recurrence recurrence) throws JSONException {
117         JSONObject recurrenceJSON = JSONFactoryUtil.createJSONObject(
118             JSONFactoryUtil.serialize(recurrence));
119 
120         recurrenceJSON.put("javaClass", TZSRecurrence.class.getName());
121 
122         TimeZone timeZone = TimeZone.getTimeZone(StringPool.UTC);
123 
124         JSONObject timeZoneJSON = JSONFactoryUtil.createJSONObject(
125             JSONFactoryUtil.serialize(timeZone));
126 
127         recurrenceJSON.put("timeZone", timeZoneJSON);
128 
129         return recurrenceJSON.toString();
130     }
131 
132 }