summaryrefslogtreecommitdiff
path: root/src/traffic-incidents-service/org.genivi.trafficinfo/src/org/genivi/trafficincidentsservice/refimpl/ExpirationHandler.java
blob: ccd894d73c5635c0572a156905e7ec7b4abb6785 (plain)
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
/**
 * Copyright (C) 2013 TomTom International B.V.
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.genivi.trafficincidentsservice.refimpl;

import java.util.Date;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;

public class ExpirationHandler extends Thread {
	TrafficIncidentsService trafficInfoStore;
	
    public ExpirationHandler(TrafficIncidentsService trafficInfoStore) {
    	this.trafficInfoStore = trafficInfoStore;
    }
    
    public void run() {
    	for ( ; ; ) {
    		try {
				sleep(1000 * 60);
			} catch (InterruptedException e) {
			}
    		
    		cancelExpiredMessages();
    	}
    }

	private void cancelExpiredMessages() {
		Date currentTime = new Date();
		EList<String> cancelList = new BasicEList<>();
		
		for (ManagedTrafficIncident managedTrafficIncident: trafficInfoStore.getManagedEvents()) {
			Date expirationTime = managedTrafficIncident.getxTrafficIncident().getExpirationTime();
			if (expirationTime != null  &&  currentTime.after(expirationTime)) {
				cancelList.add(managedTrafficIncident.getxTrafficIncident().getTrafficIncidentId());
			}
		}
		
		if (cancelList.size() != 0) {
		  trafficInfoStore.updateIncidents(-1, cancelList, null);
		}		
	}
}