001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.cal;
016    
017    import com.liferay.portal.kernel.util.TimeZoneUtil;
018    
019    import java.util.Calendar;
020    import java.util.Date;
021    import java.util.GregorianCalendar;
022    import java.util.TimeZone;
023    
024    /**
025     * @author Samuel Kong
026     * @author Angelo Jefferson
027     */
028    public class TZSRecurrence extends Recurrence {
029    
030            public TZSRecurrence() {
031            }
032    
033            public TZSRecurrence(Calendar startCalendar, Duration duration) {
034                    super(startCalendar, duration);
035            }
036    
037            public TZSRecurrence(
038                    Calendar startCalendar, Duration duration, int frequency) {
039    
040                    super(startCalendar, duration, frequency);
041            }
042    
043            public TimeZone getTimeZone() {
044                    return _timeZone;
045            }
046    
047            public void setTimeZone(TimeZone timeZone) {
048                    if (timeZone == null) {
049                            _timeZone = null;
050                    }
051                    else {
052                            _timeZone = TimeZoneUtil.getTimeZone(timeZone.getID());
053                    }
054            }
055    
056            protected Calendar getAdjustedCalendar(Calendar candidateCalendar) {
057                    if (_timeZone == null) {
058                            return candidateCalendar;
059                    }
060    
061                    Calendar adjustedCalendar = new GregorianCalendar(_timeZone);
062    
063                    Date candidateDate = candidateCalendar.getTime();
064    
065                    long dailightSavingsTimeDelta =
066                            _timeZone.getOffset(candidateCalendar.getTimeInMillis()) -
067                                    _timeZone.getOffset(dtStart.getTimeInMillis());
068    
069                    adjustedCalendar.setTimeInMillis(
070                            candidateDate.getTime() - dailightSavingsTimeDelta);
071    
072                    return adjustedCalendar;
073            }
074    
075            @Override
076            protected boolean matchesByField(
077                    int[] array, int field, Calendar candidateCalendar,
078                    boolean allowNegative) {
079    
080                    Calendar adjustedCandidate = getAdjustedCalendar(candidateCalendar);
081    
082                    return super.matchesByField(
083                            array, field, adjustedCandidate, allowNegative);
084            }
085    
086            @Override
087            protected boolean matchesByMonth(Calendar candidateCalendar) {
088                    return matchesByField(
089                            byMonth, Calendar.MONTH, candidateCalendar, false);
090            }
091    
092            @Override
093            protected boolean matchesByMonthDay(Calendar candidateCalendar) {
094                    return matchesByField(
095                            byMonthDay, Calendar.DATE, candidateCalendar, true);
096            }
097    
098            @Override
099            protected boolean matchesByWeekNo(Calendar candidateCalendar) {
100                    return matchesByField(
101                            byWeekNo, Calendar.WEEK_OF_YEAR, candidateCalendar, true);
102            }
103    
104            @Override
105            protected boolean matchesByYearDay(Calendar candidateCalendar) {
106                    return matchesByField(
107                            byYearDay, Calendar.DAY_OF_YEAR, candidateCalendar, true);
108            }
109    
110            @Override
111            protected boolean matchesIndividualByDay(
112                    Calendar candidateCalendar, DayAndPosition dayAndPosition) {
113    
114                    Calendar adjustedCandidate = getAdjustedCalendar(candidateCalendar);
115    
116                    return super.matchesIndividualByDay(adjustedCandidate, dayAndPosition);
117            }
118    
119            private TimeZone _timeZone;
120    
121    }