ee40a94f77c03429d8b597fd5e22827a305e741d
[jabaws.git] / datamodel / compbio / metadata / LimitsManager.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0 \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.metadata;\r
20 \r
21 import java.util.List;\r
22 \r
23 import javax.xml.bind.ValidationException;\r
24 import javax.xml.bind.annotation.XmlAccessType;\r
25 import javax.xml.bind.annotation.XmlAccessorType;\r
26 import javax.xml.bind.annotation.XmlRootElement;\r
27 \r
28 import compbio.util.Util;\r
29 \r
30 /**\r
31  * A collection of Limits\r
32  * \r
33  * @see Limit\r
34  * @author pvtroshin\r
35  * \r
36  * @version 1.0 January 2010\r
37  * @param <T>\r
38  *            executable type\r
39  */\r
40 @XmlRootElement(name = "limits")\r
41 @XmlAccessorType(XmlAccessType.FIELD)\r
42 public class LimitsManager<T> {\r
43 \r
44         String runnerClassName;\r
45         List<Limit<T>> limit;\r
46 \r
47         /**\r
48          * \r
49          * @return all limits defined for an executable T\r
50          */\r
51         public List<Limit<T>> getLimits() {\r
52                 return limit;\r
53         }\r
54 \r
55         @Override\r
56         public String toString() {\r
57                 String value = "";\r
58                 if (null != limit) {\r
59                         for (Limit<T> lim : limit) {\r
60                                 value += lim.toString();\r
61                         }\r
62                 }\r
63                 return value;\r
64         }\r
65 \r
66         /**\r
67          * \r
68          * @param presetName\r
69          * @return Limit defined for the executable T and presetName. If no limit is\r
70          *         defined for the presetName then default Limit is returned. If\r
71          *         presetName is empty or null than the default Limit will be\r
72          *         returned. If not limit defined for the type T than NULL will be\r
73          *         returned\r
74          */\r
75         public Limit<T> getLimitByName(String presetName) {\r
76                 if (limit == null) {\r
77                         return null;\r
78                 }\r
79                 if (Util.isEmpty(presetName)) {\r
80                         // return default limit\r
81                         return getDefaultLimit();\r
82                 }\r
83                 for (Limit<T> lim : limit) {\r
84                         String preset = lim.getPreset();\r
85                         if (preset == null) {\r
86                                 continue;\r
87                         }\r
88                         if (preset.equalsIgnoreCase(presetName)) {\r
89                                 return lim;\r
90                         }\r
91                 }\r
92                 return null;\r
93         }\r
94 \r
95         /**\r
96          * \r
97          * @return the default Limit for an executable type T\r
98          */\r
99         public Limit<T> getDefaultLimit() {\r
100                 for (Limit<T> lim : limit) {\r
101                         if (lim.isDefault) {\r
102                                 return lim;\r
103                         }\r
104                 }\r
105                 return null;\r
106         }\r
107 \r
108         /**\r
109          * Validate Limits\r
110          * \r
111          * @see Limit\r
112          * @see Preset\r
113          * @param presets\r
114          * @throws ValidationException\r
115          *             if any of the Limit defined is found to be invalid. That is\r
116          *             when\r
117          * \r
118          *             1) No default limit is defined\r
119          * \r
120          *             2) More than 1 default limit is defined\r
121          * \r
122          *             3) Limit's preset name does not match any presets for type T\r
123          */\r
124         public void validate(PresetManager<T> presets) throws ValidationException {\r
125                 int defaults = 0;\r
126                 for (Limit<T> lim : limit) {\r
127                         if (lim.isDefault) {\r
128                                 defaults++;\r
129                         }\r
130                 }\r
131                 if (defaults == 0) {\r
132                         throw new ValidationException("Default limit is not set!");\r
133                 }\r
134                 if (defaults > 1) {\r
135                         throw new ValidationException(\r
136                                         "Default limit is set more than once!");\r
137                 }\r
138                 if (presets != null) {\r
139                         for (Limit<T> lim : limit) {\r
140                                 lim.validate();\r
141                                 String presetName = lim.getPreset();\r
142                                 // skip "special" preset\r
143                                 if (presetName != null\r
144                                                 && !presetName\r
145                                                                 .equals(PresetManager.LOCAL_ENGINE_LIMIT_PRESET)) {\r
146                                         Preset<T> preset = presets.getPresetByName(presetName);\r
147                                         if (preset == null) {\r
148                                                 throw new ValidationException("Preset " + presetName\r
149                                                                 + " is not found!");\r
150                                         }\r
151                                 }\r
152                         }\r
153                 }\r
154         }\r
155 \r
156 }\r