1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.v5_1_2.util;
16  
17  import com.liferay.portal.kernel.cal.DayAndPosition;
18  import com.liferay.portal.kernel.cal.Duration;
19  import com.liferay.portal.kernel.cal.Recurrence;
20  import com.liferay.portal.kernel.cal.TZSRecurrence;
21  import com.liferay.portal.kernel.json.JSONException;
22  import com.liferay.portal.kernel.json.JSONFactoryUtil;
23  import com.liferay.portal.kernel.json.JSONObject;
24  import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
25  import com.liferay.portal.kernel.util.Base64;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.TimeZoneUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  
30  import java.util.TimeZone;
31  
32  /**
33   * <a href="CalEventRecurrenceUpgradeColumnImpl.java.html"><b><i>View Source</i>
34   * </b></a>
35   *
36   * @author     Samuel Kong
37   * @deprecated
38   */
39  public class CalEventRecurrenceUpgradeColumnImpl extends BaseUpgradeColumnImpl {
40  
41      public CalEventRecurrenceUpgradeColumnImpl(String name) {
42          super(name);
43      }
44  
45      public Object getNewValue(Object oldValue) throws Exception {
46          if (Validator.isNull(oldValue)) {
47              return StringPool.BLANK;
48          }
49  
50          String recurrence = (String)oldValue;
51  
52          Object obj = Base64.stringToObject(recurrence);
53  
54          if (obj instanceof Recurrence) {
55              Recurrence recurrenceObj = (Recurrence)obj;
56  
57              return serialize(recurrenceObj);
58          }
59          else if (obj instanceof com.liferay.util.cal.Recurrence) {
60              com.liferay.util.cal.Recurrence oldRecurrence =
61                  (com.liferay.util.cal.Recurrence)obj;
62  
63              com.liferay.util.cal.Duration oldDuration =
64                  oldRecurrence.getDuration();
65  
66              Duration duration = new Duration(
67                  oldDuration.getDays(), oldDuration.getHours(),
68                  oldDuration.getMinutes(), oldDuration.getSeconds());
69  
70              duration.setWeeks(oldDuration.getWeeks());
71              duration.setInterval(oldDuration.getInterval());
72  
73              Recurrence recurrenceObj = new Recurrence(
74                  oldRecurrence.getDtStart(), duration,
75                  oldRecurrence.getFrequency());
76  
77              com.liferay.util.cal.DayAndPosition[] oldDayPos =
78                  oldRecurrence.getByDay();
79  
80              DayAndPosition[] dayPos = null;
81  
82              if (oldDayPos != null) {
83                  dayPos = new DayAndPosition[oldDayPos.length];
84  
85                  for (int i = 0; i < oldDayPos.length; i++) {
86                      dayPos[i] = new DayAndPosition(
87                          oldDayPos[i].getDayOfWeek(),
88                          oldDayPos[i].getDayPosition());
89                  }
90              }
91  
92              recurrenceObj.setByDay(dayPos);
93              recurrenceObj.setByMonth(oldRecurrence.getByMonth());
94              recurrenceObj.setByMonthDay(oldRecurrence.getByMonthDay());
95              recurrenceObj.setInterval(oldRecurrence.getInterval());
96              recurrenceObj.setOccurrence(oldRecurrence.getOccurrence());
97              recurrenceObj.setWeekStart(oldRecurrence.getWeekStart());
98              recurrenceObj.setUntil(oldRecurrence.getUntil());
99  
100             return serialize(recurrenceObj);
101         }
102         else {
103             return StringPool.BLANK;
104         }
105     }
106 
107     protected String serialize(Recurrence recurrence) throws JSONException {
108         JSONObject recurrenceJSON = JSONFactoryUtil.createJSONObject(
109             JSONFactoryUtil.serialize(recurrence));
110 
111         recurrenceJSON.put("javaClass", TZSRecurrence.class.getName());
112 
113         TimeZone timeZone = TimeZoneUtil.getTimeZone(StringPool.UTC);
114 
115         JSONObject timeZoneJSON = JSONFactoryUtil.createJSONObject(
116             JSONFactoryUtil.serialize(timeZone));
117 
118         recurrenceJSON.put("timeZone", timeZoneJSON);
119 
120         return recurrenceJSON.toString();
121     }
122 
123 }