001    /**
002     * Copyright (c) 2000-2012 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                    _timeZone = TimeZoneUtil.getTimeZone(timeZone.getID());
049            }
050    
051            protected Calendar getAdjustedCalendar(Calendar candidateCalendar) {
052                    if (_timeZone == null) {
053                            return candidateCalendar;
054                    }
055    
056                    Calendar adjustedCalendar = new GregorianCalendar(_timeZone);
057    
058                    Date candidateDate = candidateCalendar.getTime();
059    
060                    long dailightSavingsTimeDelta =
061                            _timeZone.getOffset(candidateCalendar.getTimeInMillis()) -
062                                    _timeZone.getOffset(dtStart.getTimeInMillis());
063    
064                    adjustedCalendar.setTimeInMillis(
065                            candidateDate.getTime() - dailightSavingsTimeDelta);
066    
067                    return adjustedCalendar;
068            }
069    
070            @Override
071            protected boolean matchesByField(
072                    int[] array, int field, Calendar candidateCalendar,
073                    boolean allowNegative) {
074    
075                    Calendar adjustedCandidate = getAdjustedCalendar(candidateCalendar);
076    
077                    return super.matchesByField(
078                            array, field, adjustedCandidate, allowNegative);
079            }
080    
081            @Override
082            protected boolean matchesByMonth(Calendar candidateCalendar) {
083                    return matchesByField(
084                            byMonth, Calendar.MONTH, candidateCalendar, false);
085            }
086    
087            @Override
088            protected boolean matchesByMonthDay(Calendar candidateCalendar) {
089                    return matchesByField(
090                            byMonthDay, Calendar.DATE, candidateCalendar, true);
091            }
092    
093            @Override
094            protected boolean matchesByWeekNo(Calendar candidateCalendar) {
095                    return matchesByField(
096                            byWeekNo, Calendar.WEEK_OF_YEAR, candidateCalendar, true);
097            }
098    
099            @Override
100            protected boolean matchesByYearDay(Calendar candidateCalendar) {
101                    return matchesByField(
102                            byYearDay, Calendar.DAY_OF_YEAR, candidateCalendar, true);
103            }
104    
105            @Override
106            protected boolean matchesIndividualByDay(
107                    Calendar candidateCalendar, DayAndPosition dayAndPosition) {
108    
109                    Calendar adjustedCandidate = getAdjustedCalendar(candidateCalendar);
110    
111                    return super.matchesIndividualByDay(adjustedCandidate, dayAndPosition);
112            }
113    
114            private TimeZone _timeZone;
115    
116    }