1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
package jalview.urls; |
23 |
|
|
24 |
|
import static jalview.util.UrlConstants.DB_ACCESSION; |
25 |
|
import static jalview.util.UrlConstants.DELIM; |
26 |
|
import static jalview.util.UrlConstants.SEP; |
27 |
|
|
28 |
|
import jalview.util.JSONUtils; |
29 |
|
import jalview.util.UrlLink; |
30 |
|
|
31 |
|
import java.io.FileReader; |
32 |
|
import java.io.IOException; |
33 |
|
import java.util.ArrayList; |
34 |
|
import java.util.HashMap; |
35 |
|
import java.util.Iterator; |
36 |
|
import java.util.List; |
37 |
|
import java.util.Map; |
38 |
|
import java.util.StringTokenizer; |
39 |
|
|
40 |
|
import org.json.simple.parser.ParseException; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
@author |
48 |
|
@version |
49 |
|
|
|
|
| 85.3% |
Uncovered Elements: 16 (109) |
Complexity: 29 |
Complexity Density: 0.44 |
|
50 |
|
public class IdentifiersUrlProvider extends UrlProviderImpl |
51 |
|
{ |
52 |
|
|
53 |
|
private static final String LOCAL_KEY = "Local"; |
54 |
|
|
55 |
|
private static final String ID_ORG_KEY = "identifiers.org"; |
56 |
|
|
57 |
|
|
58 |
|
private HashMap<String, UrlLink> urls; |
59 |
|
|
60 |
|
|
61 |
|
private ArrayList<String> selectedUrls; |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
63 |
33 |
public IdentifiersUrlProvider(String cachedUrlList)... |
64 |
|
{ |
65 |
33 |
urls = readIdentifiers(IdOrgSettings.getDownloadLocation()); |
66 |
33 |
selectedUrls = new ArrayList<>(); |
67 |
33 |
checkSelectionMatchesUrls(cachedUrlList); |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
@param |
74 |
|
|
75 |
|
@return |
76 |
|
|
|
|
| 71% |
Uncovered Elements: 9 (31) |
Complexity: 6 |
Complexity Density: 0.26 |
|
77 |
33 |
@SuppressWarnings("unchecked")... |
78 |
|
private HashMap<String, UrlLink> readIdentifiers(String idFileName) |
79 |
|
{ |
80 |
|
|
81 |
33 |
HashMap<String, UrlLink> idData = new HashMap<>(); |
82 |
|
|
83 |
33 |
String errorMessage = null; |
84 |
33 |
try |
85 |
|
{ |
86 |
|
|
87 |
|
|
88 |
33 |
FileReader reader = new FileReader(idFileName); |
89 |
33 |
String key = ""; |
90 |
33 |
Map<String, Object> obj = (Map<String, Object>) JSONUtils.parse(reader); |
91 |
33 |
if (obj.containsKey(ID_ORG_KEY)) |
92 |
|
{ |
93 |
0 |
key = ID_ORG_KEY; |
94 |
|
} |
95 |
33 |
else if (obj.containsKey(LOCAL_KEY)) |
96 |
|
{ |
97 |
33 |
key = LOCAL_KEY; |
98 |
|
} |
99 |
|
else |
100 |
|
{ |
101 |
0 |
System.out.println( |
102 |
|
"Unexpected key returned from identifiers jalview service"); |
103 |
0 |
return idData; |
104 |
|
} |
105 |
|
|
106 |
33 |
List<Object> jsonarray = (List<Object>) obj.get(key); |
107 |
|
|
108 |
|
|
109 |
10885 |
for (int i = 0; i < jsonarray.size(); i++) |
110 |
|
{ |
111 |
10852 |
Map<String, Object> item = (Map<String, Object>) jsonarray.get(i); |
112 |
|
|
113 |
10852 |
String url = (String) item.get("url") + "/" + DELIM + DB_ACCESSION |
114 |
|
+ DELIM; |
115 |
10852 |
UrlLink link = new UrlLink((String) item.get("name"), url, |
116 |
|
(String) item.get("prefix")); |
117 |
10852 |
idData.put((String) item.get("id"), link); |
118 |
|
} |
119 |
|
} catch (IOException | ParseException e) |
120 |
|
{ |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
0 |
errorMessage = e.toString(); |
127 |
0 |
idData.clear(); |
128 |
|
} |
129 |
|
|
130 |
33 |
if (errorMessage != null) |
131 |
|
{ |
132 |
0 |
System.err.println("IdentifiersUrlProvider: cannot read " + idFileName + ": " + errorMessage); |
133 |
|
} |
134 |
33 |
return idData; |
135 |
|
} |
136 |
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
137 |
33 |
private void checkSelectionMatchesUrls(String cachedUrlList)... |
138 |
|
{ |
139 |
33 |
StringTokenizer st = new StringTokenizer(cachedUrlList, SEP); |
140 |
283 |
while (st.hasMoreElements()) |
141 |
|
{ |
142 |
250 |
String id = st.nextToken(); |
143 |
|
|
144 |
250 |
if (isMiriamId(id)) |
145 |
|
{ |
146 |
|
|
147 |
44 |
if (urls.containsKey(id)) |
148 |
|
{ |
149 |
44 |
selectedUrls.add(id); |
150 |
|
} |
151 |
|
} |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
33 |
setPrimaryUrl(primaryUrl); |
156 |
|
} |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
158 |
63 |
@Override... |
159 |
|
public boolean setPrimaryUrl(String id) |
160 |
|
{ |
161 |
63 |
if (urls.containsKey(id)) |
162 |
|
{ |
163 |
2 |
primaryUrl = id; |
164 |
|
} |
165 |
|
else |
166 |
|
{ |
167 |
61 |
primaryUrl = null; |
168 |
|
} |
169 |
|
|
170 |
63 |
return urls.containsKey(id); |
171 |
|
} |
172 |
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
173 |
5 |
@Override... |
174 |
|
public String writeUrlsAsString(boolean selected) |
175 |
|
{ |
176 |
5 |
if (!selected) |
177 |
|
{ |
178 |
2 |
return ""; |
179 |
|
} |
180 |
|
|
181 |
3 |
StringBuffer links = new StringBuffer(); |
182 |
3 |
if (!selectedUrls.isEmpty()) |
183 |
|
{ |
184 |
3 |
for (String k : selectedUrls) |
185 |
|
{ |
186 |
9 |
links.append(k); |
187 |
9 |
links.append(SEP); |
188 |
|
} |
189 |
|
|
190 |
3 |
links.setLength(links.length() - 1); |
191 |
|
} |
192 |
3 |
return links.toString(); |
193 |
|
} |
194 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
195 |
36 |
@Override... |
196 |
|
public List<String> getLinksForMenu() |
197 |
|
{ |
198 |
36 |
List<String> links = new ArrayList<>(); |
199 |
36 |
for (String key : selectedUrls) |
200 |
|
{ |
201 |
15 |
links.add(urls.get(key).toStringWithTarget()); |
202 |
|
} |
203 |
36 |
return links; |
204 |
|
} |
205 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
206 |
13 |
@Override... |
207 |
|
public List<UrlLinkDisplay> getLinksForTable() |
208 |
|
{ |
209 |
13 |
return super.getLinksForTable(urls, selectedUrls, false); |
210 |
|
} |
211 |
|
|
|
|
| 85% |
Uncovered Elements: 3 (20) |
Complexity: 6 |
Complexity Density: 0.6 |
|
212 |
9 |
@Override... |
213 |
|
public void setUrlData(List<UrlLinkDisplay> links) |
214 |
|
{ |
215 |
9 |
selectedUrls = new ArrayList<>(); |
216 |
|
|
217 |
9 |
Iterator<UrlLinkDisplay> it = links.iterator(); |
218 |
99 |
while (it.hasNext()) |
219 |
|
{ |
220 |
90 |
UrlLinkDisplay link = it.next(); |
221 |
|
|
222 |
|
|
223 |
90 |
if (isMiriamId(link.getId())) |
224 |
|
{ |
225 |
|
|
226 |
35 |
if (urls.containsKey(link.getId())) |
227 |
|
{ |
228 |
35 |
if (link.getIsSelected()) |
229 |
|
{ |
230 |
26 |
selectedUrls.add(link.getId()); |
231 |
|
} |
232 |
35 |
if (link.getIsPrimary()) |
233 |
|
{ |
234 |
0 |
setPrimaryUrl(link.getId()); |
235 |
|
} |
236 |
|
} |
237 |
|
} |
238 |
|
} |
239 |
|
} |
240 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
241 |
7 |
@Override... |
242 |
|
public String getPrimaryUrl(String seqid) |
243 |
|
{ |
244 |
7 |
return super.getPrimaryUrl(seqid, urls); |
245 |
|
} |
246 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
247 |
5 |
@Override... |
248 |
|
public String getPrimaryUrlId() |
249 |
|
{ |
250 |
5 |
return primaryUrl; |
251 |
|
} |
252 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
253 |
0 |
@Override... |
254 |
|
public String getPrimaryTarget(String seqid) |
255 |
|
{ |
256 |
0 |
return null; |
257 |
|
} |
258 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
259 |
1 |
@Override... |
260 |
|
public String choosePrimaryUrl() |
261 |
|
{ |
262 |
1 |
return null; |
263 |
|
} |
264 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
265 |
29 |
@Override... |
266 |
|
public boolean contains(String id) |
267 |
|
{ |
268 |
29 |
return (urls.containsKey(id)); |
269 |
|
} |
270 |
|
} |