1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.upgrade.v5_1_2.util;
21  
22  import com.liferay.portal.kernel.cal.DayAndPosition;
23  import com.liferay.portal.kernel.cal.Duration;
24  import com.liferay.portal.kernel.cal.Recurrence;
25  import com.liferay.portal.kernel.cal.TZSRecurrence;
26  import com.liferay.portal.kernel.json.JSONException;
27  import com.liferay.portal.kernel.json.JSONFactoryUtil;
28  import com.liferay.portal.kernel.json.JSONObject;
29  import com.liferay.portal.kernel.util.Base64;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.upgrade.util.BaseUpgradeColumnImpl;
33  
34  import java.util.TimeZone;
35  
36  /**
37   * <a href="CalEventRecurrenceUpgradeColumnImpl.java.html"><b><i>View Source</i>
38   * </b></a>
39   *
40   * @author Samuel Kong
41   *
42   * @deprecated
43   *
44   */
45  public class CalEventRecurrenceUpgradeColumnImpl extends BaseUpgradeColumnImpl {
46  
47      public CalEventRecurrenceUpgradeColumnImpl(String name) {
48          super(name);
49      }
50  
51      public Object getNewValue(Object oldValue) throws Exception {
52          if (Validator.isNull(oldValue)) {
53              return StringPool.BLANK;
54          }
55  
56          String recurrence = (String)oldValue;
57  
58          Object obj = Base64.stringToObject(recurrence);
59  
60          if (obj instanceof Recurrence) {
61              Recurrence recurrenceObj = (Recurrence)obj;
62  
63              return serialize(recurrenceObj);
64          }
65          else if (obj instanceof com.liferay.util.cal.Recurrence) {
66              com.liferay.util.cal.Recurrence oldRecurrence =
67                  (com.liferay.util.cal.Recurrence)obj;
68  
69              com.liferay.util.cal.Duration oldDuration =
70                  oldRecurrence.getDuration();
71  
72              Duration duration = new Duration(
73                  oldDuration.getDays(), oldDuration.getHours(),
74                  oldDuration.getMinutes(), oldDuration.getSeconds());
75  
76              duration.setWeeks(oldDuration.getWeeks());
77              duration.setInterval(oldDuration.getInterval());
78  
79              Recurrence recurrenceObj = new Recurrence(
80                  oldRecurrence.getDtStart(), duration,
81                  oldRecurrence.getFrequency());
82  
83              com.liferay.util.cal.DayAndPosition[] oldDayPos =
84                  oldRecurrence.getByDay();
85  
86              DayAndPosition[] dayPos = null;
87  
88              if (oldDayPos != null) {
89                  dayPos = new DayAndPosition[oldDayPos.length];
90  
91                  for (int i = 0; i < oldDayPos.length; i++) {
92                      dayPos[i] = new DayAndPosition(
93                          oldDayPos[i].getDayOfWeek(),
94                          oldDayPos[i].getDayPosition());
95                  }
96              }
97  
98              recurrenceObj.setByDay(dayPos);
99              recurrenceObj.setByMonth(oldRecurrence.getByMonth());
100             recurrenceObj.setByMonthDay(oldRecurrence.getByMonthDay());
101             recurrenceObj.setInterval(oldRecurrence.getInterval());
102             recurrenceObj.setOccurrence(oldRecurrence.getOccurrence());
103             recurrenceObj.setWeekStart(oldRecurrence.getWeekStart());
104             recurrenceObj.setUntil(oldRecurrence.getUntil());
105 
106             return serialize(recurrenceObj);
107         }
108         else {
109             return StringPool.BLANK;
110         }
111     }
112 
113     protected String serialize(Recurrence recurrence) throws JSONException {
114         JSONObject recurrenceJSON = JSONFactoryUtil.createJSONObject(
115             JSONFactoryUtil.serialize(recurrence));
116 
117         recurrenceJSON.put("javaClass", TZSRecurrence.class.getName());
118 
119         TimeZone timeZone = TimeZone.getTimeZone(StringPool.UTC);
120 
121         JSONObject timeZoneJSON = JSONFactoryUtil.createJSONObject(
122             JSONFactoryUtil.serialize(timeZone));
123 
124         recurrenceJSON.put("timeZone", timeZoneJSON);
125 
126         return recurrenceJSON.toString();
127     }
128 
129 }