JAL-3878 Add WebServiceJob class.
[jalview.git] / src / jalview / ws2 / client / api / WebServiceJob.java
1 package jalview.ws2.client.api;
2
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
5 import java.util.Date;
6
7 import jalview.util.MathUtils;
8 import jalview.ws2.api.JobStatus;
9
10 /**
11  * {@code WebServiceJob} represents a job running on a remote server. The object
12  * contains all the information needed to associate the job with an originating
13  * client and url, service being run and to poll the job and retrieve the
14  * results from the server. The {@code WebServiceJob} object is provided by the
15  * {@link WebServiceClientI#submit} method when the job is created.
16  * 
17  * @see WebServiceClientI
18  * 
19  * @author mmwarowny
20  */
21 public class WebServiceJob
22 {
23   /** Unique id used internally by Jalview */
24   private final long internalId = MathUtils.getUID();
25
26   /** Name of the related client */
27   private final String serviceClient;
28
29   /** Name of the related service */
30   private final String serviceName;
31
32   /** URL the job is valid for */
33   private final String url;
34
35   /** External job id as given by the server */
36   private final String jobId;
37
38   /** Current status of the job */
39   private JobStatus status = JobStatus.READY;
40
41   private String log = "";
42
43   private String errorLog = "";
44
45   private Date creationTime = new Date();
46
47   public WebServiceJob(String serviceClient, String serviceName,
48       String url, String jobId)
49   {
50     this.serviceClient = serviceClient;
51     this.serviceName = serviceName;
52     this.url = url;
53     this.jobId = jobId;
54   }
55
56   /**
57    * Get a unique job id used internally by Jalview.
58    * 
59    * @return unique job id
60    */
61   public long getInternalId()
62   {
63     return internalId;
64   }
65
66   /**
67    * Get a URL this job originates from.
68    * 
69    * @return job URL
70    */
71   public String getUrl()
72   {
73     return url;
74   }
75
76   /**
77    * Get an id assigned to the job by the server.
78    * 
79    * @return job id handle
80    */
81   public String getJobId()
82   {
83     return jobId;
84   }
85
86   /**
87    * @return job status
88    */
89   public JobStatus getStatus()
90   {
91     return status;
92   }
93
94   /**
95    * Update status of the job and notify the listeners. This method should only
96    * be used by {@link WebServiceClientI} implementations when polling the job.
97    * 
98    * @param status
99    *          new job status
100    */
101   public void setStatus(JobStatus status)
102   {
103     JobStatus oldStatus = this.status;
104     this.status = status;
105     pcs.firePropertyChange("status", oldStatus, status);
106   }
107
108   /**
109    * @return standard progress log
110    */
111   public String getLog()
112   {
113     return log;
114   }
115
116   /**
117    * Update log string with data received from the server and notify the
118    * listeners. This method should only be used by {@link WebServiceClientI}
119    * implementations when polling the job.
120    * 
121    * @param log
122    *          new log string
123    */
124   public void setLog(String log)
125   {
126     String oldLog = this.log;
127     this.log = log;
128     pcs.firePropertyChange("log", oldLog, log);
129   }
130
131   /**
132    * @return error log
133    */
134   public String getErrorLog()
135   {
136     return errorLog;
137   }
138
139   /**
140    * Update error log string with data received from the server and notify the
141    * listeners. This method should only be used by {@link WebServiceClientI}
142    * implementations when polling the job
143    * 
144    * @param errorLog
145    *          new error log string
146    */
147   public void setErrorLog(String errorLog)
148   {
149     String oldLog = this.errorLog;
150     this.errorLog = errorLog;
151     pcs.firePropertyChange("errorLog", oldLog, errorLog);
152   }
153
154   /**
155    * @return Job creation time
156    */
157   public Date getCreationTime()
158   {
159     return creationTime;
160   }
161
162   public String toString()
163   {
164     return String.format("%s:%s [%s] Created %s", serviceClient, serviceName,
165         jobId, creationTime);
166   }
167
168   /* Methods related to bean listeners */
169   private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
170
171   public void addPropertyChangeListener(PropertyChangeListener listener)
172   {
173     pcs.addPropertyChangeListener(listener);
174   }
175
176   public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
177   {
178     pcs.addPropertyChangeListener(propertyName, listener);
179   }
180
181   public void removePropertyChangeListener(PropertyChangeListener listener)
182   {
183     pcs.removePropertyChangeListener(listener);
184   }
185
186   public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
187   {
188     pcs.removePropertyChangeListener(propertyName, listener);
189   }
190 }