55eeb50692dfa6e2e5da22b003496c68448afb20
[jalview.git] / src / jalview / ws / params / AutoCalcSetting.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ws.params;
22
23 import jalview.util.MessageManager;
24 import jalview.ws.api.ServiceWithParameters;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 public abstract class AutoCalcSetting
30 {
31
32   protected boolean autoUpdate;
33
34   protected WsParamSetI preset;
35
36   protected List<ArgumentI> jobArgset;
37
38   protected ServiceWithParameters service;
39
40   public AutoCalcSetting(ServiceWithParameters service2,
41           WsParamSetI preset2, List<ArgumentI> jobArgset2,
42           boolean autoUpdate2)
43   {
44     service = service2;
45     autoUpdate = autoUpdate2;
46     preset = preset2;
47     jobArgset = jobArgset2;
48   }
49
50   public boolean isAutoUpdate()
51   {
52     return autoUpdate;
53   }
54
55   public void setAutoUpdate(boolean autoUpdate)
56   {
57     this.autoUpdate = autoUpdate;
58   }
59
60   public WsParamSetI getPreset()
61   {
62     return preset;
63   }
64
65   public void setPreset(WsParamSetI preset)
66   {
67     // TODO: test if service URL is in presets
68     this.preset = preset;
69   }
70
71   public List<ArgumentI> getArgumentSet()
72   {
73     return jobArgset;
74   }
75
76   /**
77    * TODO: refactor to ServiceWithParameters ?
78    * 
79    * @return characteristic URI for this service. The URI should reflect the
80    *         type and version of this service, enabling the service client code
81    *         to recover the correct client for this calculation.
82    */
83   public String getServiceURI()
84   {
85     return service.getNameURI();
86   }
87
88   /**
89    * TODO: refactor to ServiceWithParameters ?
90    * 
91    * return any concrete service endpoints associated with this calculation.
92    * built in services should return a zero length array
93    * 
94    * @return
95    */
96   public String[] getServiceURLs()
97   {
98     return new String[] { service.getUri() };
99   }
100
101   /**
102    * 
103    * @return stringified representation of the parameters for this setting
104    */
105   public abstract String getWsParamFile();
106
107   public ServiceWithParameters getService()
108   {
109     return service;
110   }
111
112   public void setService(ServiceWithParameters service)
113   {
114     this.service = service;
115     if (preset != null)
116     {
117       // check if we need to migrate preset to a new service URL
118       for (String url : preset.getApplicableUrls())
119       {
120         if (url.equals(service.getUri()))
121         {
122           // preset already verified
123           return;
124         }
125       }
126       WsParamSetI pr = service.getParamStore().getPreset(preset.getName());
127   
128       // TODO: decide of this distinction between preset and args are needed.
129       //
130       // if (pr instanceof JabaPreset && preset instanceof JabaPreset)
131       // {
132       // // easy - Presets are identical (we assume)
133       // preset = pr;
134       // return;
135       // }
136   
137       // this verifies that all arguments in the existing preset are the same as
138       // the parameters for the preset provided by the service parameter store.
139       // ie the LastUsed settings or a predefined preset.
140   
141       List<ArgumentI> oldargs = new ArrayList<>(),
142               newargs = new ArrayList<>();
143       oldargs.addAll(preset.getArguments());
144       // need to compare parameters
145       for (ArgumentI newparg : pr.getArguments())
146       {
147         if (!oldargs.remove(newparg))
148         {
149           newargs.add(newparg);
150         }
151       }
152       if (oldargs.size() == 0 && newargs.size() == 0)
153       {
154         // exact match.
155         preset = pr;
156         return;
157       }
158       // Try even harder to migrate arguments.
159       throw new Error(MessageManager
160               .getString("error.parameter_migration_not_implemented_yet"));
161     }
162   }
163
164 }