1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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   * @deprecated
45   */
46  public class CalEventRecurrenceUpgradeColumnImpl extends BaseUpgradeColumnImpl {
47  
48      public CalEventRecurrenceUpgradeColumnImpl(String name) {
49          super(name);
50      }
51  
52      public Object getNewValue(Object oldValue) throws Exception {
53          if (Validator.isNull(oldValue)) {
54              return StringPool.BLANK;
55          }
56  
57          String recurrence = (String)oldValue;
58  
59          Object obj = Base64.stringToObject(recurrence);
60  
61          if (obj instanceof Recurrence) {
62              Recurrence recurrenceObj = (Recurrence)obj;
63  
64              return serialize(recurrenceObj);
65          }
66          else if (obj instanceof com.liferay.util.cal.Recurrence) {
67              com.liferay.util.cal.Recurrence oldRecurrence =
68                  (com.liferay.util.cal.Recurrence)obj;
69  
70              com.liferay.util.cal.Duration oldDuration =
71                  oldRecurrence.getDuration();
72  
73              Duration duration = new Duration(
74                  oldDuration.getDays(), oldDuration.getHours(),
75                  oldDuration.getMinutes(), oldDuration.getSeconds());
76  
77              duration.setWeeks(oldDuration.getWeeks());
78              duration.setInterval(oldDuration.getInterval());
79  
80              Recurrence recurrenceObj = new Recurrence(
81                  oldRecurrence.getDtStart(), duration,
82                  oldRecurrence.getFrequency());
83  
84              com.liferay.util.cal.DayAndPosition[] oldDayPos =
85                  oldRecurrence.getByDay();
86  
87              DayAndPosition[] dayPos = null;
88  
89              if (oldDayPos != null) {
90                  dayPos = new DayAndPosition[oldDayPos.length];
91  
92                  for (int i = 0; i < oldDayPos.length; i++) {
93                      dayPos[i] = new DayAndPosition(
94                          oldDayPos[i].getDayOfWeek(),
95                          oldDayPos[i].getDayPosition());
96                  }
97              }
98  
99              recurrenceObj.setByDay(dayPos);
100             recurrenceObj.setByMonth(oldRecurrence.getByMonth());
101             recurrenceObj.setByMonthDay(oldRecurrence.getByMonthDay());
102             recurrenceObj.setInterval(oldRecurrence.getInterval());
103             recurrenceObj.setOccurrence(oldRecurrence.getOccurrence());
104             recurrenceObj.setWeekStart(oldRecurrence.getWeekStart());
105             recurrenceObj.setUntil(oldRecurrence.getUntil());
106 
107             return serialize(recurrenceObj);
108         }
109         else {
110             return StringPool.BLANK;
111         }
112     }
113 
114     protected String serialize(Recurrence recurrence) throws JSONException {
115         JSONObject recurrenceJSON = JSONFactoryUtil.createJSONObject(
116             JSONFactoryUtil.serialize(recurrence));
117 
118         recurrenceJSON.put("javaClass", TZSRecurrence.class.getName());
119 
120         TimeZone timeZone = TimeZone.getTimeZone(StringPool.UTC);
121 
122         JSONObject timeZoneJSON = JSONFactoryUtil.createJSONObject(
123             JSONFactoryUtil.serialize(timeZone));
124 
125         recurrenceJSON.put("timeZone", timeZoneJSON);
126 
127         return recurrenceJSON.toString();
128     }
129 
130 }