1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.Validator;
28  
29  import java.util.TimeZone;
30  
31  /**
32   * <a href="CalEventRecurrenceUpgradeColumnImpl.java.html"><b><i>View Source</i>
33   * </b></a>
34   *
35   * @author     Samuel Kong
36   * @deprecated
37   */
38  public class CalEventRecurrenceUpgradeColumnImpl extends BaseUpgradeColumnImpl {
39  
40      public CalEventRecurrenceUpgradeColumnImpl(String name) {
41          super(name);
42      }
43  
44      public Object getNewValue(Object oldValue) throws Exception {
45          if (Validator.isNull(oldValue)) {
46              return StringPool.BLANK;
47          }
48  
49          String recurrence = (String)oldValue;
50  
51          Object obj = Base64.stringToObject(recurrence);
52  
53          if (obj instanceof Recurrence) {
54              Recurrence recurrenceObj = (Recurrence)obj;
55  
56              return serialize(recurrenceObj);
57          }
58          else if (obj instanceof com.liferay.util.cal.Recurrence) {
59              com.liferay.util.cal.Recurrence oldRecurrence =
60                  (com.liferay.util.cal.Recurrence)obj;
61  
62              com.liferay.util.cal.Duration oldDuration =
63                  oldRecurrence.getDuration();
64  
65              Duration duration = new Duration(
66                  oldDuration.getDays(), oldDuration.getHours(),
67                  oldDuration.getMinutes(), oldDuration.getSeconds());
68  
69              duration.setWeeks(oldDuration.getWeeks());
70              duration.setInterval(oldDuration.getInterval());
71  
72              Recurrence recurrenceObj = new Recurrence(
73                  oldRecurrence.getDtStart(), duration,
74                  oldRecurrence.getFrequency());
75  
76              com.liferay.util.cal.DayAndPosition[] oldDayPos =
77                  oldRecurrence.getByDay();
78  
79              DayAndPosition[] dayPos = null;
80  
81              if (oldDayPos != null) {
82                  dayPos = new DayAndPosition[oldDayPos.length];
83  
84                  for (int i = 0; i < oldDayPos.length; i++) {
85                      dayPos[i] = new DayAndPosition(
86                          oldDayPos[i].getDayOfWeek(),
87                          oldDayPos[i].getDayPosition());
88                  }
89              }
90  
91              recurrenceObj.setByDay(dayPos);
92              recurrenceObj.setByMonth(oldRecurrence.getByMonth());
93              recurrenceObj.setByMonthDay(oldRecurrence.getByMonthDay());
94              recurrenceObj.setInterval(oldRecurrence.getInterval());
95              recurrenceObj.setOccurrence(oldRecurrence.getOccurrence());
96              recurrenceObj.setWeekStart(oldRecurrence.getWeekStart());
97              recurrenceObj.setUntil(oldRecurrence.getUntil());
98  
99              return serialize(recurrenceObj);
100         }
101         else {
102             return StringPool.BLANK;
103         }
104     }
105 
106     protected String serialize(Recurrence recurrence) throws JSONException {
107         JSONObject recurrenceJSON = JSONFactoryUtil.createJSONObject(
108             JSONFactoryUtil.serialize(recurrence));
109 
110         recurrenceJSON.put("javaClass", TZSRecurrence.class.getName());
111 
112         TimeZone timeZone = TimeZone.getTimeZone(StringPool.UTC);
113 
114         JSONObject timeZoneJSON = JSONFactoryUtil.createJSONObject(
115             JSONFactoryUtil.serialize(timeZone));
116 
117         recurrenceJSON.put("timeZone", timeZoneJSON);
118 
119         return recurrenceJSON.toString();
120     }
121 
122 }