ExportJob.java 3.16 KB
Newer Older
1
package eu.simstadt.nf4j;
2

3
import java.io.File;
4
5
6
7
8
9
10
import java.util.Objects;

/**
 * Export jobs are requests for CityGML models. Every valid export job has an id and a status.
 * 
 * @author Marcel Bruse
 */
11
public class ExportJob extends Job<ExportJobDescriptorImpl> {
12
	
13
	/**
14
15
16
	 * This constructor forces the job to have a description and a connector instance. Every job which
	 * is created by this constructor will have the status "local", because it is assumed that it has an unsent
	 * description and no job id yet.
17
	 * 
18
19
	 * @param connector The job will use this connector to synchronize itself with the nF.
	 * @param descriptor The description of this job.
20
	 */
21
22
23
	public ExportJob(ExportJobDescriptorImpl descriptor, NFConnector connector) {
		super(descriptor, connector);
		status = JobStatus.LOCAL;
24
	}
25

26
	/**
27
28
29
	 * This constructor forces the job to have a id and a connector instance. Every job which is created by this
	 * constructor will have the status "sent", because it is assumed that the job is already enqueued at the 
	 * nF job queue.  
30
	 * 
31
32
33
	 * @param id The job id. If you call updateStatus() and the nF "knows" the job id, then the job status
	 * will be updated. If you call updateStatus() and the job id is "unkown" on the server side, then     
	 * @param connector The job will use this connector to synchronize itself with the nF.
34
	 */
35
36
37
	public ExportJob(int id, NFConnector connector) {
		super(id, connector);
		status = JobStatus.SENT;
38
	}
39
40
41
42
43
44
45
46
47
	
	/**
	 * Connects to the nF and refreshes the status of this job. If there is no nF connector set,
	 * this operation will throw a FailedTransmissionException.
	 * 
	 * @throws FailedTransmissionException If the connection to the nF is broken you will get some of this. 
	 */
	public void updateStatus() throws FailedTransmissionException {
		if (Objects.nonNull(getNFConnector())) {
48
			Job<ExportJobDescriptorImpl> job = getNFConnector().requestExportJob(getId());
49
50
51
52
53
			setStatus(job.getStatus());
		} else {
			throw new FailedTransmissionException();
		}
	}
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

	/**
	 * Sets the status of this job depending on the given nF status code. nF status codes will be sent
	 * to you in http responses.
	 *  
	 * @param statusCode The nF status code for this job.
	 */
	@Override
	public void setStatusForCode(int statusCode) {
		switch (statusCode) {
		case 0:
			setStatus(JobStatus.PENDING); break;
		case 10:
			setStatus(JobStatus.RUNNING); break;
		case 20:
			setStatus(JobStatus.FAILED); break;
		case 30:
			setStatus(JobStatus.FINISHED); break;
		default:
			setStatus(JobStatus.UNKOWN);
		}
	}
	
	/**
	 * @return Returns true, if the job is definitely done. False, otherwise.
	 */
	public boolean isFinished() {
		return Objects.nonNull(getStatus()) && getStatus().equals(JobStatus.FINISHED);
	}
83
84
85
86
87
88
89
90
91
92
93
94

	@Override
	public void send() throws InvalidJobDescriptorException, FailedTransmissionException {
		connector.sendAndUpdateExportJob(this);
	}
	
	public File requestExportJobResult() throws FailedTransmissionException {
		if (!isFinished()) {
			throw new FailedTransmissionException("Job is not finished yet!");
		}
		return connector.requestExportJobResult(id);
	}
95
96
	
}