JAL-3878 Add annotation operation and worker to the services.
[jalview.git] / src / jalview / ws2 / gui / WebServiceInfoUpdater.java
1 package jalview.ws2.gui;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import java.util.Objects;
6
7 import jalview.gui.WebserviceInfo;
8 import jalview.ws2.WSJob;
9 import jalview.ws2.WSJobStatus;
10 import jalview.ws2.operations.WebServiceWorkerI;
11
12 /**
13  * A helper class that can be attached as a listener to the {@link WSJob}
14  * object. It updates the job status in the {@link jalview.gui.WebServiceInfo}
15  * window according to the state changes of the job object.
16  * 
17  * The {@link WebServiceInfoUpdater} object allows to decouple GUI updates
18  * from the web service worker logic.
19  * 
20  * @author mmwarowny
21  *
22  */
23 public class WebServiceInfoUpdater implements PropertyChangeListener
24 {
25   private final WebServiceWorkerI worker;
26   private final WebserviceInfo wsInfo;
27
28   private String outputHeader = "";
29
30   public WebServiceInfoUpdater(WebServiceWorkerI worker, WebserviceInfo wsInfo)
31   {
32     this.worker = worker;
33     this.wsInfo = wsInfo;
34   }
35
36   public String getOutputHeader()
37   {
38     return outputHeader;
39   }
40
41   public void setOutputHeader(String header)
42   {
43     this.outputHeader = header;
44   }
45
46   @Override
47   public void propertyChange(PropertyChangeEvent evt)
48   {
49     switch (evt.getPropertyName())
50     {
51     case "status":
52       statusChanged(evt);
53       break;
54     case "log":
55       logChanged(evt);
56       break;
57     case "errorLog":
58       errorLogChanged(evt);
59       break;
60     }
61   }
62
63   private void statusChanged(PropertyChangeEvent evt)
64   {
65     WSJob job = (WSJob) evt.getSource();
66     WSJobStatus status = (WSJobStatus) evt.getNewValue();
67     int wsInfoStatus = 0;
68     switch (status)
69     {
70     case READY:
71     case SUBMITTED:
72     case QUEUED:
73       wsInfoStatus = WebserviceInfo.STATE_QUEUING;
74       break;
75     case RUNNING:
76       wsInfoStatus = WebserviceInfo.STATE_RUNNING;
77       break;
78     case FINISHED:
79       wsInfoStatus = WebserviceInfo.STATE_STOPPED_OK;
80       break;
81     case CANCELLED:
82       wsInfoStatus = WebserviceInfo.STATE_CANCELLED_OK;
83       break;
84     case INVALID:
85     case BROKEN:
86     case FAILED:
87     case UNKNOWN:
88       wsInfoStatus = WebserviceInfo.STATE_STOPPED_ERROR;
89       break;
90     case SERVER_ERROR:
91       wsInfoStatus = WebserviceInfo.STATE_STOPPED_SERVERERROR;
92       break;
93     }
94     wsInfo.setStatus(job.getJobNum(), wsInfoStatus);
95     updateWSInfoGlobalStatus();
96   }
97
98   private void logChanged(PropertyChangeEvent evt)
99   {
100     WSJob job = (WSJob) evt.getSource();
101     String oldLog = (String) evt.getOldValue();
102     String newLog = (String) evt.getNewValue();
103     wsInfo.appendProgressText(job.getJobNum(),
104             newLog.substring(oldLog.length()));
105   }
106
107   private void errorLogChanged(PropertyChangeEvent evt)
108   {
109     WSJob job = (WSJob) evt.getSource();
110     String oldLog = (String) evt.getOldValue();
111     String newLog = (String) evt.getNewValue();
112     wsInfo.appendProgressText(job.getJobNum(),
113             newLog.substring(oldLog.length()));
114   }
115
116
117   private void updateWSInfoGlobalStatus()
118   {
119     var jobs = worker.getJobs();
120     if (jobs.countRunning() > 0)
121     {
122       wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
123     }
124     else if (jobs.countQueuing() > 0
125             || jobs.countSubmitted() < jobs.size())
126     {
127       wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
128     }
129     else
130     {
131       if (jobs.countSuccessful() > 0)
132       {
133         wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
134       }
135       else if (jobs.countCancelled() > 0)
136       {
137         wsInfo.setStatus(WebserviceInfo.STATE_CANCELLED_OK);
138       }
139       else if (jobs.countFailed() > 0)
140       {
141         wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
142       }
143     }
144   }
145 }