Merge branch 'Jalview-JS/develop' into merge_js_develop
[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 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    * default WsParamFile generator method - clients with custom formats should
103    * override and implement their own
104    * 
105    * @return stringified representation of the parameters for this setting
106    */
107   public String getWsParamFile()
108   {
109     List<ArgumentI> opts = null;
110     if (jobArgset != null)
111     {
112       opts = jobArgset;
113     }
114     else
115     {
116       if (preset != null)
117       {
118         opts = preset.getArguments();
119       }
120     }
121     if (opts == null || opts.size() == 0)
122     {
123       return "";
124     }
125     StringBuffer pset = new StringBuffer();
126     for (ArgumentI ps : opts)
127     {
128       pset.append(ps.getName() + "\t" + ps.getValue());
129       pset.append("\n");
130     }
131     return pset.toString();
132   }
133   public ServiceWithParameters getService()
134   {
135     return service;
136   }
137
138   public void setService(ServiceWithParameters service)
139   {
140     this.service = service;
141     if (preset != null)
142     {
143       // check if we need to migrate preset to a new service URL
144       for (String url : preset.getApplicableUrls())
145       {
146         if (url.equals(service.getUri()))
147         {
148           // preset already verified
149           return;
150         }
151       }
152       WsParamSetI pr = service.getParamStore().getPreset(preset.getName());
153   
154       // TODO: decide of this distinction between preset and args are needed.
155       //
156       // if (pr instanceof JabaPreset && preset instanceof JabaPreset)
157       // {
158       // // easy - Presets are identical (we assume)
159       // preset = pr;
160       // return;
161       // }
162   
163       // this verifies that all arguments in the existing preset are the same as
164       // the parameters for the preset provided by the service parameter store.
165       // ie the LastUsed settings or a predefined preset.
166   
167       List<ArgumentI> oldargs = new ArrayList<>(),
168               newargs = new ArrayList<>();
169       oldargs.addAll(preset.getArguments());
170       // need to compare parameters
171       for (ArgumentI newparg : pr.getArguments())
172       {
173         if (!oldargs.remove(newparg))
174         {
175           newargs.add(newparg);
176         }
177       }
178       if (oldargs.size() == 0 && newargs.size() == 0)
179       {
180         // exact match.
181         preset = pr;
182         return;
183       }
184       // Try even harder to migrate arguments.
185       throw new Error(MessageManager
186               .getString("error.parameter_migration_not_implemented_yet"));
187     }
188   }
189
190 }