Skip to main content
summaryrefslogtreecommitdiffstats
blob: c2665799014e3cbd0814fecbfd9190466844724e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*******************************************************************************
 * Copyright (c) 2004, 2013 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.core;

import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.mylyn.tasks.core.IRepositoryElement;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.osgi.util.NLS;

/**
 * @author Rob Elves
 * @author Mik Kersten
 * @author Sam Davis
 */
public class ScheduledTaskContainer extends AbstractTaskContainer {

	private final TaskActivityManager activityManager;

	private final String summary;

	private final DateRange range;

	private String shortSummary;

	public ScheduledTaskContainer(TaskActivityManager activityManager, DateRange range, String summary) {
		super(summary == null ? range.toString(false) : summary);
		this.activityManager = activityManager;
		this.range = range;
		if (summary == null) {
			if (range instanceof DayDateRange && TaskActivityUtil.getNextWeek().includes(range)) {
				DayDateRange dayRange = (DayDateRange) range;
				String day = NLS.bind(Messages.ScheduledTaskContainer_Next_Day, dayRange.getDayOfWeek());
//				String shortDate = DateFormat.getDateInstance(DateFormat.SHORT).format(
//						dayRange.getStartDate().getTime());
//				this.summary = NLS.bind(Messages.ScheduledTaskContainer_Dash_Pattern, day, shortDate);
				this.summary = day;
				this.shortSummary = day;
			} else {
				this.summary = range.toString(false);
				this.shortSummary = this.summary;
			}
		} else {
			this.summary = summary;
		}
	}

	public ScheduledTaskContainer(TaskActivityManager taskActivityManager, DateRange day) {
		this(taskActivityManager, day, null);
	}

	public boolean isFuture() {
		return !isPresent() && range.getStartDate().after(Calendar.getInstance());
	}

	public boolean isPresent() {
		return range.getStartDate().before(Calendar.getInstance()) && range.getEndDate().after(Calendar.getInstance());
	}

//	public boolean isWeekDay() {
//		return TaskActivityUtil.getCurrentWeek().isCurrentWeekDay(range);
//	}

//	public boolean isToday() {
//		if (range instanceof DayDateRange) {
//			return ((DayDateRange) range).isToday();
//		}
//		return false;
//	}

//	public Collection<ITask> getChildren() {
//		Set<ITask> children = new HashSet<ITask>();
//		Calendar beginning = TaskActivityUtil.getCalendar();
//		beginning.setTimeInMillis(0);
//		if (isFloating() && !isFuture()) {
//			for (ITask task : activityManager.getScheduledTasks(rangebeginning, getEndDate())) {
//				if (task.internalIsFloatingScheduledDate()) {
//					children.add(task);
//				}
//			}
//		} else if (isPresent()) {
//			// add all due/overdue
//			Calendar end = TaskActivityUtil.getCalendar();
//			end.set(5000, 12, 1);
//			for (ITask task : activityManager.getDueTasks(beginning, getEndDate())) {
//				if (activityManager.isOwnedByUser(task)) {
//					children.add(task);
//				}
//			}
//
//			// add all scheduled/overscheduled
//			for (ITask task : activityManager.getScheduledTasks(beginning, getEndDate())) {
//				if (!task.internalIsFloatingScheduledDate() && !task.isCompleted()) {
//					children.add(task);
//				}
//			}
//
//			// if not scheduled or due in future, and is active, place in today bin
//			ITask activeTask = activityManager.getActiveTask();
//			if (activeTask != null && !children.contains(activeTask)) {
//				Set<ITask> futureScheduled = activityManager.getScheduledTasks(getStartDate(), end);
//				for (ITask task : activityManager.getDueTasks(getStartDate(), end)) {
//					if (activityManager.isOwnedByUser(task)) {
//						futureScheduled.add(task);
//					}
//				}
//				if (!futureScheduled.contains(activeTask)) {
//					children.add(activeTask);
//				}
//			}
//		} else if (isFuture()) {
//			children.addAll(activityManager.getScheduledTasks(getStartDate(), getEndDate()));
//			for (ITask task : activityManager.getDueTasks(getStartDate(), getEndDate())) {
//				if (activityManager.isOwnedByUser(task)) {
//					children.add(task);
//				}
//			}
//		} else {
//			children.addAll(activityManager.getActiveTasks(range.getStartDate(), range.getEndDate()));
//		}
//		return children;
//	}

	@Override
	public Collection<ITask> getChildren() {
		// TODO: Cache this information until the next modification to pertinent data
		Calendar now = Calendar.getInstance();
		// extend range to include tasks scheduled for this container in different time zones
		// timezones range from UTC-12 to UTC+14, but we will ignore UTC+12 to +14 since we can't distinguish them from UTC-12 to -10
		// use minutes to allow for partial hour timezone offsets
		int offsetInMinutes = range.getStartDate().getTimeZone().getOffset(now.getTimeInMillis()) / 1000 / 60;
		if (offsetInMinutes > 11 * 60) {
			// when in UTC+12 to +14, show tasks scheduled in those time zones in the correct bin. This causes tasks scheduled in other time
			// zones to show a day late; tasks scheduled in other time zones for WeekDateRanges may not show at all
			offsetInMinutes = -(24 * 60 - offsetInMinutes);
		}
		int minutesForwardToDateline = 11 * 60 - offsetInMinutes;
		int minutesBackwardToDateline = 23 * 60 - minutesForwardToDateline;
		Calendar start = Calendar.getInstance();
		start.setTimeInMillis(range.getStartDate().getTimeInMillis());
		start.add(Calendar.MINUTE, -minutesForwardToDateline);
		Calendar end = Calendar.getInstance();
		end.setTimeInMillis(range.getEndDate().getTimeInMillis());
		end.add(Calendar.MINUTE, minutesBackwardToDateline);
		// All tasks scheduled for this date range
		Set<ITask> tasks = activityManager.getScheduledTasks(start, end);
		if (range instanceof WeekDateRange) {
			// remove tasks not scheduled for the week container itself, except for 2 weeks, in which case only remove 
			// if they will show under future
			for (Iterator<ITask> iterator = tasks.iterator(); iterator.hasNext();) {
				ITask task = iterator.next();
				if (task instanceof AbstractTask) {
					DateRange scheduledDate = ((AbstractTask) task).getScheduledForDate();
					if (!(scheduledDate instanceof WeekDateRange)
							&& (TaskActivityUtil.getNextWeek().next().compareTo(range) != 0 || scheduledDate.getEndDate()
									.after(end))) {
						iterator.remove();
					}
				}
			}
		}
		Set<ITask> children = new HashSet<ITask>();
		Calendar cal = TaskActivityUtil.getCalendar();
		for (ITask task : tasks) {
			if (!task.isCompleted() || isCompletedToday(task)) {

				if (isDueBeforeScheduled(task) && activityManager.isOwnedByUser(task)) {
					continue;
				}

				if (isThisWeekBin() && isScheduledForAWeek(task)) {
					// is due this week
					if (task.getDueDate() != null) {
						cal.setTime(task.getDueDate());
						if (range.includes(cal) && activityManager.isOwnedByUser(task)) {
							continue;
						}
					}

					addChild(children, task);
				}

				addChild(children, task);
			}
		}

		// Add due tasks if not the This Week container, and not scheduled for earlier date
		if (!TaskActivityUtil.getCurrentWeek().equals(range) && !TaskActivityUtil.getNextWeek().equals(range)) {
			// tasks are due at the start of a day, so only search in the range of times that correspond to 
			// the start of the day in some time zone
			Calendar endDueSearch = Calendar.getInstance();
			endDueSearch.setTimeInMillis(range.getStartDate().getTimeInMillis());
			endDueSearch.add(Calendar.MINUTE, minutesBackwardToDateline);
			for (ITask task : activityManager.getDueTasks(start, endDueSearch)) {
				if (isScheduledBeforeDue(task)) {
					continue;
				}
				if (activityManager.isOwnedByUser(task)) {
					addChild(children, task);
				}
			}
		}

		// All over due/scheduled tasks are present in the Today folder
		if (isTodayBin()) {
			for (ITask task : activityManager.getOverScheduledTasks()) {
				if (isScheduledForADay(task)) {
					addChild(children, task);
				}
			}
			// add tasks whose scheduled date starts before today and ends today
			Calendar searchFrom = Calendar.getInstance();
			searchFrom.setTimeInMillis(start.getTimeInMillis());
			searchFrom.add(Calendar.DAY_OF_MONTH, -1);
			for (ITask task : activityManager.getScheduledTasks(searchFrom, end)) {
				if (isScheduledForADay(task)) {
					addChild(children, task);
				}
			}
			for (ITask task : activityManager.getOverDueTasks()) {
				addChild(children, task);
			}

			ITask activeTask = activityManager.getActiveTask();
			if (activeTask != null && !children.contains(activeTask)) {
				addChild(children, activeTask);
			}
		}

		if (range instanceof WeekDateRange && ((WeekDateRange) range).isThisWeek()) {
			for (ITask task : activityManager.getOverScheduledTasks()) {
				if (isScheduledForAWeek(task)) {
					addChild(children, task);
				}
			}
		}

		return children;
	}

	private boolean isTodayBin() {
		return range instanceof DayDateRange && ((DayDateRange) range).isPresent();
	}

	private boolean isThisWeekBin() {

		return range instanceof WeekDateRange && ((WeekDateRange) range).isThisWeek();
	}

	private boolean isScheduledForAWeek(ITask task) {
		return task instanceof AbstractTask && ((AbstractTask) task).getScheduledForDate() instanceof WeekDateRange;
	}

	public boolean isDueBeforeScheduled(ITask task) {
		return task.getDueDate() != null
				&& task.getDueDate().before(((AbstractTask) task).getScheduledForDate().getEndDate().getTime());
	}

	private boolean isScheduledForADay(ITask task) {
		return task instanceof AbstractTask && !(((AbstractTask) task).getScheduledForDate() instanceof WeekDateRange);
	}

	private boolean isScheduledBeforeDue(ITask task) {
		return ((AbstractTask) task).getScheduledForDate() != null
				&& ((AbstractTask) task).getScheduledForDate().before(range.getStartDate());
	}

	private boolean isCompletedToday(ITask task) {
		return (task.isCompleted() && TaskActivityUtil.getDayOf(task.getCompletionDate()).isPresent());
	}

	private void addChild(Set<ITask> collection, ITask task) {
//		if (task.getSynchronizationState().isOutgoing()) {
//			return;
//		}

		collection.add(task);
	}

	@Override
	public String getSummary() {
		if (summary != null) {
			return summary;
		}
		return range.toString();
	}

	public String getShortSummary() {
		if (shortSummary != null) {
			return shortSummary;
		}
		return range.toString();
	}

	@Override
	public String getHandleIdentifier() {
		return summary;
	}

	@Override
	public String getPriority() {
		return ""; //$NON-NLS-1$
	}

	@Override
	public String getUrl() {
		return ""; //$NON-NLS-1$
	}

	@Override
	public int compareTo(IRepositoryElement element) {
		if (element instanceof ScheduledTaskContainer) {
			ScheduledTaskContainer container = ((ScheduledTaskContainer) element);
			return range.compareTo(container.getDateRange());
		}
		return 0;
	}

	public DateRange getDateRange() {
		return range;
	}

	public Calendar getEnd() {
		return range.getEndDate();
	}

	public Calendar getStart() {
		return range.getStartDate();
	}

	public boolean includes(Calendar pastWeeksTaskStart) {
		return range.includes(pastWeeksTaskStart);
	}

}

Back to the top