1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler.task; |
24 |
|
25 |
import java.util.logging.Level; |
26 |
|
27 |
import org.jsampler.CC; |
28 |
import org.jsampler.HF; |
29 |
|
30 |
import org.linuxsampler.lscp.DbDirectoryInfo; |
31 |
import org.linuxsampler.lscp.DbInstrumentInfo; |
32 |
import org.linuxsampler.lscp.DbSearchQuery; |
33 |
import org.linuxsampler.lscp.ScanJobInfo; |
34 |
|
35 |
import static org.jsampler.JSI18n.i18n; |
36 |
import static org.linuxsampler.lscp.Client.ScanMode; |
37 |
|
38 |
/** |
39 |
* Provides the instruments database specific tasks. |
40 |
* @author Grigor Iliev |
41 |
*/ |
42 |
public class InstrumentsDb { |
43 |
|
44 |
/** Forbits the instantiation of this class. */ |
45 |
private InstrumentsDb() { } |
46 |
|
47 |
/** |
48 |
* This task retrieves the number of directories in the specified directory. |
49 |
*/ |
50 |
public static class GetDirectoryCount extends EnhancedTask<Integer> { |
51 |
private String dir; |
52 |
private boolean recursive; |
53 |
|
54 |
/** |
55 |
* Creates a new instance of <code>GetDirectoryCount</code>. |
56 |
* @param dir The absolute path name of the directory. |
57 |
* @param recursive If <code>true</code>, the number of all directories |
58 |
* in the specified subtree will be returned. |
59 |
*/ |
60 |
public |
61 |
GetDirectoryCount(String dir, boolean recursive) { |
62 |
setTitle("InstrumentsDb.GetDirectoryCount_task"); |
63 |
setDescription(i18n.getMessage("InstrumentsDb.GetDirectoryCount.desc")); |
64 |
this.dir = dir; |
65 |
this.recursive = recursive; |
66 |
} |
67 |
|
68 |
/** The entry point of the task. */ |
69 |
public void |
70 |
run() { |
71 |
try { setResult(CC.getClient().getDbDirectoryCount(dir, recursive)); } |
72 |
catch(Exception x) { |
73 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
74 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
75 |
} |
76 |
} |
77 |
} |
78 |
|
79 |
/** |
80 |
* This task retrieves the number of instruments in the specified directory. |
81 |
*/ |
82 |
public static class GetInstrumentCount extends EnhancedTask<Integer> { |
83 |
private String dir; |
84 |
private boolean recursive; |
85 |
|
86 |
/** |
87 |
* Creates a new instance of <code>GetInstrumentCount</code>. |
88 |
* @param dir The absolute path name of the directory. |
89 |
* @param recursive If <code>true</code>, the number of all instruments |
90 |
* in the specified subtree will be returned. |
91 |
*/ |
92 |
public |
93 |
GetInstrumentCount(String dir, boolean recursive) { |
94 |
setTitle("InstrumentsDb.GetInstrumentCount_task"); |
95 |
setDescription(i18n.getMessage("InstrumentsDb.GetInstrumentCount.desc")); |
96 |
this.dir = dir; |
97 |
this.recursive = recursive; |
98 |
} |
99 |
|
100 |
/** The entry point of the task. */ |
101 |
public void |
102 |
run() { |
103 |
try { setResult(CC.getClient().getDbInstrumentCount(dir, recursive)); } |
104 |
catch(Exception x) { |
105 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
106 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
107 |
} |
108 |
} |
109 |
} |
110 |
|
111 |
/** |
112 |
* This task retrieves the list of directories in the specified directory. |
113 |
*/ |
114 |
public static class GetDrectories extends EnhancedTask<DbDirectoryInfo[]> { |
115 |
private String dir; |
116 |
|
117 |
/** |
118 |
* Creates a new instance of <code>GetDrectories</code>. |
119 |
* @param dir The absolute path name of the directory. |
120 |
*/ |
121 |
public |
122 |
GetDrectories(String dir) { |
123 |
setTitle("InstrumentsDb.GetDrectories_task"); |
124 |
setDescription(i18n.getMessage("InstrumentsDb.GetDrectories.desc")); |
125 |
this.dir = dir; |
126 |
} |
127 |
|
128 |
/** The entry point of the task. */ |
129 |
public void |
130 |
run() { |
131 |
try { setResult(CC.getClient().getDbDirectories(dir)); } |
132 |
catch(Exception x) { |
133 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
134 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
135 |
} |
136 |
} |
137 |
|
138 |
public String |
139 |
getDirectory() { return dir; } |
140 |
|
141 |
/** |
142 |
* Used to decrease the traffic. All task in the queue |
143 |
* equal to this are removed if added using {@link org.jsampler.CC#scheduleTask}. |
144 |
* @see org.jsampler.CC#addTask |
145 |
*/ |
146 |
public boolean |
147 |
equals(Object obj) { |
148 |
if(obj == null) return false; |
149 |
if(!(obj instanceof GetDrectories)) return false; |
150 |
String d = ((GetDrectories)obj).getDirectory(); |
151 |
if(getDirectory() == null) { |
152 |
return d == null; |
153 |
} |
154 |
if(!getDirectory().equals(d)) return false; |
155 |
|
156 |
return true; |
157 |
} |
158 |
} |
159 |
|
160 |
|
161 |
/** |
162 |
* This task retrieves information about a directory. |
163 |
*/ |
164 |
public static class GetDrectory extends EnhancedTask<DbDirectoryInfo> { |
165 |
private String dir; |
166 |
|
167 |
/** |
168 |
* Creates a new instance of <code>GetDrectory</code>. |
169 |
* @param dir The absolute path name of the directory. |
170 |
*/ |
171 |
public |
172 |
GetDrectory(String dir) { |
173 |
setTitle("InstrumentsDb.GetDrectory_task"); |
174 |
setDescription(i18n.getMessage("InstrumentsDb.GetDrectory.desc")); |
175 |
this.dir = dir; |
176 |
} |
177 |
|
178 |
/** The entry point of the task. */ |
179 |
public void |
180 |
run() { |
181 |
try { setResult(CC.getClient().getDbDirectoryInfo(dir)); } |
182 |
catch(Exception x) { |
183 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
184 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
185 |
} |
186 |
} |
187 |
} |
188 |
|
189 |
/** |
190 |
* This task creates a new directory. |
191 |
*/ |
192 |
public static class CreateDirectory extends EnhancedTask { |
193 |
private String dir; |
194 |
|
195 |
/** |
196 |
* Creates a new instance of <code>CreateDirectory</code>. |
197 |
* @param dir The absolute path name of the directory to add. |
198 |
*/ |
199 |
public |
200 |
CreateDirectory(String dir) { |
201 |
setTitle("InstrumentsDb.CreateDirectory_task"); |
202 |
setDescription(i18n.getMessage("InstrumentsDb.CreateDirectory.desc")); |
203 |
this.dir = dir; |
204 |
} |
205 |
|
206 |
/** The entry point of the task. */ |
207 |
public void |
208 |
run() { |
209 |
try { CC.getClient().addDbDirectory(dir); } |
210 |
catch(Exception x) { |
211 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
212 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
213 |
} |
214 |
} |
215 |
} |
216 |
|
217 |
/** |
218 |
* This task renames the specified directory. |
219 |
*/ |
220 |
public static class RenameDirectory extends EnhancedTask { |
221 |
private String dir; |
222 |
private String newName; |
223 |
|
224 |
/** |
225 |
* Creates a new instance of <code>RenameDirectory</code>. |
226 |
* @param dir The absolute path name of the directory to rename. |
227 |
* @param newName The new name for the specified directory. |
228 |
*/ |
229 |
public |
230 |
RenameDirectory(String dir, String newName) { |
231 |
setTitle("InstrumentsDb.RenameDirectory_task"); |
232 |
setDescription(i18n.getMessage("InstrumentsDb.RenameDirectory.desc")); |
233 |
this.dir = dir; |
234 |
this.newName = newName; |
235 |
} |
236 |
|
237 |
/** The entry point of the task. */ |
238 |
public void |
239 |
run() { |
240 |
try { CC.getClient().renameDbDirectory(dir, newName); } |
241 |
catch(Exception x) { |
242 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
243 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
244 |
} |
245 |
} |
246 |
} |
247 |
|
248 |
/** |
249 |
* This task changes the description of a directory. |
250 |
*/ |
251 |
public static class SetDirectoryDescription extends EnhancedTask { |
252 |
private String dir; |
253 |
private String desc; |
254 |
|
255 |
/** |
256 |
* Creates a new instance of <code>SetDirectoryDescription</code>. |
257 |
* @param dir The absolute path name of the directory. |
258 |
* @param desc The new description for the directory. |
259 |
*/ |
260 |
public |
261 |
SetDirectoryDescription(String dir, String desc) { |
262 |
setTitle("InstrumentsDb.SetDirectoryDescription_task"); |
263 |
String s = i18n.getMessage("InstrumentsDb.SetDirectoryDescription.desc"); |
264 |
setDescription(s); |
265 |
this.dir = dir; |
266 |
this.desc = desc; |
267 |
} |
268 |
|
269 |
/** The entry point of the task. */ |
270 |
public void |
271 |
run() { |
272 |
try { CC.getClient().setDbDirectoryDescription(dir, desc); } |
273 |
catch(Exception x) { |
274 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
275 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
276 |
} |
277 |
} |
278 |
} |
279 |
|
280 |
/** |
281 |
* This task removes the specified directories. |
282 |
*/ |
283 |
public static class RemoveDirectories extends EnhancedTask { |
284 |
private DbDirectoryInfo[] directories; |
285 |
|
286 |
/** |
287 |
* Creates a new instance of <code>RemoveDirectories</code>. |
288 |
* @param directories The directories to remove. |
289 |
*/ |
290 |
public |
291 |
RemoveDirectories(DbDirectoryInfo[] directories) { |
292 |
setTitle("InstrumentsDb.RemoveDirectories_task"); |
293 |
setDescription(i18n.getMessage("InstrumentsDb.RemoveDirectories.desc")); |
294 |
this.directories = directories; |
295 |
} |
296 |
|
297 |
/** The entry point of the task. */ |
298 |
public void |
299 |
run() { |
300 |
try { |
301 |
removeDirectories(); |
302 |
} catch(Exception x) { |
303 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
304 |
setErrorDetails(x); |
305 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
306 |
} |
307 |
} |
308 |
|
309 |
private void |
310 |
removeDirectories() throws Exception { |
311 |
if(directories == null || directories.length == 0) return; |
312 |
if(directories.length == 1) { |
313 |
String path = directories[0].getDirectoryPath(); |
314 |
CC.getClient().removeDbDirectory(path, true); |
315 |
} else { |
316 |
String[] dirs = new String[directories.length]; |
317 |
for(int i = 0; i < directories.length; i++) { |
318 |
dirs[i] = directories[i].getDirectoryPath(); |
319 |
} |
320 |
|
321 |
CC.getClient().removeDbDirectories(dirs, true); |
322 |
} |
323 |
} |
324 |
} |
325 |
|
326 |
/** |
327 |
* This task finds all directories in the specified directory |
328 |
* that corresponds to the provided search criterias. |
329 |
*/ |
330 |
public static class FindDirectories extends EnhancedTask<DbDirectoryInfo[]> { |
331 |
private String dir; |
332 |
private DbSearchQuery query; |
333 |
|
334 |
/** |
335 |
* Creates a new instance of <code>FindDirectories</code>. |
336 |
* @param dir The absolute path name of the directory. |
337 |
* @param query Provides the search criterias. |
338 |
*/ |
339 |
public |
340 |
FindDirectories(String dir, DbSearchQuery query) { |
341 |
setTitle("InstrumentsDb.FindDirectories_task"); |
342 |
setDescription(i18n.getMessage("InstrumentsDb.FindDirectories.desc")); |
343 |
this.dir = dir; |
344 |
this.query = query; |
345 |
} |
346 |
|
347 |
/** The entry point of the task. */ |
348 |
public void |
349 |
run() { |
350 |
try { setResult(CC.getClient().findDbDirectories(dir, query)); } |
351 |
catch(Exception x) { |
352 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
353 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
354 |
} |
355 |
} |
356 |
} |
357 |
|
358 |
/** |
359 |
* This task retrieves the list of instruments in the specified directory. |
360 |
*/ |
361 |
public static class GetInstruments extends EnhancedTask<DbInstrumentInfo[]> { |
362 |
private String dir; |
363 |
|
364 |
/** |
365 |
* Creates a new instance of <code>GetInstruments</code>. |
366 |
* @param dir The absolute path name of the directory. |
367 |
*/ |
368 |
public |
369 |
GetInstruments(String dir) { |
370 |
setTitle("InstrumentsDb.GetInstruments_task"); |
371 |
setDescription(i18n.getMessage("InstrumentsDb.GetInstruments.desc")); |
372 |
this.dir = dir; |
373 |
} |
374 |
|
375 |
/** The entry point of the task. */ |
376 |
public void |
377 |
run() { |
378 |
try { setResult(CC.getClient().getDbInstruments(dir)); } |
379 |
catch(Exception x) { |
380 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
381 |
setErrorDetails(x); |
382 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
383 |
} |
384 |
} |
385 |
|
386 |
public String |
387 |
getDirectory() { return dir; } |
388 |
|
389 |
/** |
390 |
* Used to decrease the traffic. All task in the queue |
391 |
* equal to this are removed if added using {@link org.jsampler.CC#scheduleTask}. |
392 |
* @see org.jsampler.CC#addTask |
393 |
*/ |
394 |
public boolean |
395 |
equals(Object obj) { |
396 |
if(obj == null) return false; |
397 |
if(!(obj instanceof GetInstruments)) return false; |
398 |
String d = ((GetInstruments)obj).getDirectory(); |
399 |
if(getDirectory() == null) { |
400 |
return d == null; |
401 |
} |
402 |
if(!getDirectory().equals(d)) return false; |
403 |
return true; |
404 |
} |
405 |
} |
406 |
|
407 |
/** |
408 |
* This task retrieves information about an instrument. |
409 |
*/ |
410 |
public static class GetInstrument extends EnhancedTask<DbInstrumentInfo> { |
411 |
private String instr; |
412 |
|
413 |
/** |
414 |
* Creates a new instance of <code>GetInstrument</code>. |
415 |
* @param instr The absolute path name of the instrument. |
416 |
*/ |
417 |
public |
418 |
GetInstrument(String instr) { |
419 |
setTitle("InstrumentsDb.GetInstrument_task"); |
420 |
setDescription(i18n.getMessage("InstrumentsDb.GetInstrument.desc")); |
421 |
this.instr = instr; |
422 |
} |
423 |
|
424 |
/** The entry point of the task. */ |
425 |
public void |
426 |
run() { |
427 |
try { setResult(CC.getClient().getDbInstrumentInfo(instr)); } |
428 |
catch(Exception x) { |
429 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
430 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
431 |
} |
432 |
} |
433 |
} |
434 |
|
435 |
/** |
436 |
* This task finds all instruments in the specified directory |
437 |
* that corresponds to the provided search criterias. |
438 |
*/ |
439 |
public static class FindInstruments extends EnhancedTask<DbInstrumentInfo[]> { |
440 |
private String dir; |
441 |
private DbSearchQuery query; |
442 |
|
443 |
/** |
444 |
* Creates a new instance of <code>FindInstruments</code>. |
445 |
* @param dir The absolute path name of the directory. |
446 |
* @param query Provides the search criterias. |
447 |
*/ |
448 |
public |
449 |
FindInstruments(String dir, DbSearchQuery query) { |
450 |
setTitle("InstrumentsDb.FindInstruments_task"); |
451 |
setDescription(i18n.getMessage("InstrumentsDb.FindInstruments.desc")); |
452 |
this.dir = dir; |
453 |
this.query = query; |
454 |
} |
455 |
|
456 |
/** The entry point of the task. */ |
457 |
public void |
458 |
run() { |
459 |
try { setResult(CC.getClient().findDbInstruments(dir, query)); } |
460 |
catch(Exception x) { |
461 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
462 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
463 |
} |
464 |
} |
465 |
} |
466 |
|
467 |
/** |
468 |
* This task renames the specified instrument. |
469 |
*/ |
470 |
public static class RenameInstrument extends EnhancedTask { |
471 |
private String instr; |
472 |
private String newName; |
473 |
|
474 |
/** |
475 |
* Creates a new instance of <code>RenameInstrument</code>. |
476 |
* @param instr The absolute path name of the instrument to rename. |
477 |
* @param newName The new name for the specified instrument. |
478 |
*/ |
479 |
public |
480 |
RenameInstrument(String instr, String newName) { |
481 |
setTitle("InstrumentsDb.RenameInstrument_task"); |
482 |
setDescription(i18n.getMessage("InstrumentsDb.RenameInstrument.desc")); |
483 |
this.instr = instr; |
484 |
this.newName = newName; |
485 |
} |
486 |
|
487 |
/** The entry point of the task. */ |
488 |
public void |
489 |
run() { |
490 |
try { CC.getClient().renameDbInstrument(instr, newName); } |
491 |
catch(Exception x) { |
492 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
493 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
494 |
} |
495 |
} |
496 |
} |
497 |
|
498 |
/** |
499 |
* This task changes the description of an instrument. |
500 |
*/ |
501 |
public static class SetInstrumentDescription extends EnhancedTask { |
502 |
private String instr; |
503 |
private String desc; |
504 |
|
505 |
/** |
506 |
* Creates a new instance of <code>SetInstrumentDescription</code>. |
507 |
* @param instr The absolute path name of the instrument. |
508 |
* @param desc The new description for the instrument. |
509 |
*/ |
510 |
public |
511 |
SetInstrumentDescription(String instr, String desc) { |
512 |
setTitle("InstrumentsDb.SetInstrumentDescription_task"); |
513 |
String s = i18n.getMessage("InstrumentsDb.SetInstrumentDescription.desc"); |
514 |
setDescription(s); |
515 |
this.instr = instr; |
516 |
this.desc = desc; |
517 |
} |
518 |
|
519 |
/** The entry point of the task. */ |
520 |
public void |
521 |
run() { |
522 |
try { CC.getClient().setDbInstrumentDescription(instr, desc); } |
523 |
catch(Exception x) { |
524 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
525 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
526 |
} |
527 |
} |
528 |
} |
529 |
|
530 |
/** |
531 |
* This task removes the specified instruments. |
532 |
*/ |
533 |
public static class RemoveInstruments extends EnhancedTask { |
534 |
private DbInstrumentInfo[] instruments; |
535 |
|
536 |
/** |
537 |
* Creates a new instance of <code>RemoveInstruments</code>. |
538 |
* @param instruments The instruments to remove. |
539 |
*/ |
540 |
public |
541 |
RemoveInstruments(DbInstrumentInfo[] instruments) { |
542 |
setTitle("InstrumentsDb.RemoveInstruments_task"); |
543 |
setDescription(i18n.getMessage("InstrumentsDb.RemoveInstruments.desc")); |
544 |
this.instruments = instruments; |
545 |
} |
546 |
|
547 |
/** The entry point of the task. */ |
548 |
public void |
549 |
run() { |
550 |
try { |
551 |
removeInstruments(); |
552 |
} catch(Exception x) { |
553 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
554 |
setErrorDetails(x); |
555 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
556 |
} |
557 |
} |
558 |
|
559 |
private void |
560 |
removeInstruments() throws Exception { |
561 |
if(instruments == null || instruments.length == 0) return; |
562 |
if(instruments.length == 1) { |
563 |
String path = instruments[0].getInstrumentPath(); |
564 |
CC.getClient().removeDbInstrument(path); |
565 |
} else { |
566 |
String[] instrs = new String[instruments.length]; |
567 |
for(int i = 0; i < instruments.length; i++) { |
568 |
instrs[i] = instruments[i].getInstrumentPath(); |
569 |
} |
570 |
|
571 |
CC.getClient().removeDbInstruments(instrs); |
572 |
} |
573 |
} |
574 |
} |
575 |
|
576 |
/** |
577 |
* This task adds instruments from an instrument file to the instruments database. |
578 |
*/ |
579 |
public static class AddInstrumentsFromFile extends EnhancedTask<Integer> { |
580 |
private String dbDir; |
581 |
private String filePath; |
582 |
private int instrIndex; |
583 |
|
584 |
/** |
585 |
* Creates a new instance of <code>AddInstrumentsFromFile</code>. |
586 |
* @param dbDir The absolute path name of the database directory |
587 |
* in which all instruments from the specified instrument file will be added. |
588 |
* @param filePath The absolute path name of the instrument file. |
589 |
*/ |
590 |
public |
591 |
AddInstrumentsFromFile(String dbDir, String filePath) { |
592 |
this(dbDir, filePath, -1); |
593 |
} |
594 |
|
595 |
/** |
596 |
* Creates a new instance of <code>AddInstrumentsFromFile</code>. |
597 |
* @param dbDir The absolute path name of the database directory |
598 |
* in which the specified instrument will be added. |
599 |
* @param filePath The absolute path name of the instrument file. |
600 |
* @param instrIndex The index of the instrument |
601 |
* (in the given instrument file) to add. If -1 is specified, all |
602 |
* instruments in the given instrument file will be added. |
603 |
*/ |
604 |
public |
605 |
AddInstrumentsFromFile(String dbDir, String filePath, int instrIndex) { |
606 |
setTitle("InstrumentsDb.AddInstrumentsFromFile_task"); |
607 |
String s = i18n.getMessage("InstrumentsDb.AddInstrumentsFromFile.desc"); |
608 |
setDescription(s); |
609 |
this.dbDir = dbDir; |
610 |
this.filePath = filePath; |
611 |
this.instrIndex = instrIndex; |
612 |
} |
613 |
|
614 |
/** The entry point of the task. */ |
615 |
public void |
616 |
run() { |
617 |
try { |
618 |
int i; |
619 |
if(instrIndex != -1) { |
620 |
i = CC.getClient().addDbInstrument ( |
621 |
dbDir, filePath, instrIndex, true |
622 |
); |
623 |
} else { |
624 |
i = CC.getClient().addDbInstruments(dbDir, filePath, true); |
625 |
} |
626 |
|
627 |
setResult(i); |
628 |
} catch(Exception x) { |
629 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
630 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
631 |
} |
632 |
} |
633 |
} |
634 |
|
635 |
/** |
636 |
* This task adds instruments from a directory to the instruments database. |
637 |
*/ |
638 |
public static class AddInstruments extends EnhancedTask<Integer> { |
639 |
private String dbDir; |
640 |
private String fsDir; |
641 |
private boolean flat; |
642 |
private boolean insDir; |
643 |
|
644 |
/** |
645 |
* Creates a new instance of <code>AddInstruments</code>. |
646 |
* @param dbDir The absolute path name of the database directory |
647 |
* in which all instruments from the specified directory will be added. |
648 |
* @param fsDir The absolute path name of the file system directory. |
649 |
*/ |
650 |
public |
651 |
AddInstruments(String dbDir, String fsDir) { |
652 |
this(dbDir, fsDir, false); |
653 |
} |
654 |
|
655 |
/** |
656 |
* Creates a new instance of <code>AddInstruments</code>. |
657 |
* @param dbDir The absolute path name of the database directory |
658 |
* in which all instruments from the specified directory will be added. |
659 |
* @param fsDir The absolute path name of the file system directory. |
660 |
* @param flat If <code>true</code>, the respective subdirectory structure |
661 |
* will not be re-created in the supplied database directory. |
662 |
*/ |
663 |
public |
664 |
AddInstruments(String dbDir, String fsDir, boolean flat) { |
665 |
this(dbDir, fsDir, flat, false); |
666 |
} |
667 |
|
668 |
/** |
669 |
* Creates a new instance of <code>AddInstruments</code>. |
670 |
* @param dbDir The absolute path name of the database directory |
671 |
* in which all instruments from the specified directory will be added. |
672 |
* @param fsDir The absolute path name of the file system directory. |
673 |
* @param flat If <code>true</code>, the respective subdirectory structure |
674 |
* will not be re-created in the supplied database directory. |
675 |
* @param insDir If <code>true</code>, a directory will be created for each |
676 |
* instrument file |
677 |
*/ |
678 |
public |
679 |
AddInstruments(String dbDir, String fsDir, boolean flat, boolean insDir) { |
680 |
setTitle("InstrumentsDb.AddInstruments_task"); |
681 |
String s = i18n.getMessage("InstrumentsDb.AddInstruments.desc"); |
682 |
setDescription(s); |
683 |
this.dbDir = dbDir; |
684 |
this.fsDir = fsDir; |
685 |
this.flat = flat; |
686 |
this.insDir = insDir; |
687 |
} |
688 |
|
689 |
/** The entry point of the task. */ |
690 |
public void |
691 |
run() { |
692 |
try { |
693 |
ScanMode scanMode = flat ? ScanMode.FLAT : ScanMode.RECURSIVE; |
694 |
int i = CC.getClient().addDbInstruments ( |
695 |
scanMode, dbDir, fsDir, true, insDir |
696 |
); |
697 |
|
698 |
setResult(i); |
699 |
} |
700 |
catch(Exception x) { |
701 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
702 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
703 |
} |
704 |
} |
705 |
} |
706 |
|
707 |
/** |
708 |
* This task adds instruments from a file system directory (excluding |
709 |
* the instruments in the subdirectories) to the instruments database. |
710 |
*/ |
711 |
public static class AddInstrumentsNonrecursive extends EnhancedTask<Integer> { |
712 |
private String dbDir; |
713 |
private String fsDir; |
714 |
private boolean insDir; |
715 |
|
716 |
/** |
717 |
* Creates a new instance of <code>AddInstrumentsNonrecursive</code>. |
718 |
* @param dbDir The absolute path name of the database directory |
719 |
* in which the instruments from the specified directory (excluding |
720 |
* the instruments in the subdirectories) will be added. |
721 |
* @param fsDir The absolute path name of the file system directory. |
722 |
*/ |
723 |
public |
724 |
AddInstrumentsNonrecursive(String dbDir, String fsDir) { |
725 |
this(dbDir, fsDir, false); |
726 |
} |
727 |
|
728 |
/** |
729 |
* Creates a new instance of <code>AddInstrumentsNonrecursive</code>. |
730 |
* @param dbDir The absolute path name of the database directory |
731 |
* in which the instruments from the specified directory (excluding |
732 |
* the instruments in the subdirectories) will be added. |
733 |
* @param fsDir The absolute path name of the file system directory. |
734 |
* @param insDir If <code>true</code> a directory is add for each |
735 |
* instrument file. |
736 |
*/ |
737 |
public |
738 |
AddInstrumentsNonrecursive(String dbDir, String fsDir, boolean insDir) { |
739 |
setTitle("InstrumentsDb.AddInstrumentsNonrecursive_task"); |
740 |
String s = i18n.getMessage("InstrumentsDb.AddInstrumentsNonrecursive.desc"); |
741 |
setDescription(s); |
742 |
this.dbDir = dbDir; |
743 |
this.fsDir = fsDir; |
744 |
this.insDir = insDir; |
745 |
} |
746 |
|
747 |
/** The entry point of the task. */ |
748 |
public void |
749 |
run() { |
750 |
try { |
751 |
int i = CC.getClient().addDbInstruments ( |
752 |
ScanMode.NON_RECURSIVE, dbDir, fsDir, true, insDir |
753 |
); |
754 |
setResult(i); |
755 |
} |
756 |
catch(Exception x) { |
757 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
758 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
759 |
} |
760 |
} |
761 |
} |
762 |
|
763 |
/** |
764 |
* This task moves the specified instruments |
765 |
* and directories to the specified location. |
766 |
*/ |
767 |
public static class Move extends EnhancedTask { |
768 |
private DbDirectoryInfo[] directories; |
769 |
private DbInstrumentInfo[] instruments; |
770 |
private String dest; |
771 |
|
772 |
/** |
773 |
* Creates a new instance of <code>Move</code>. |
774 |
* @param directories The directories to move. |
775 |
* @param instruments The instruments to move. |
776 |
* @param dest The absolute path name of the directory where |
777 |
* the specified instruments and directories will be moved to. |
778 |
*/ |
779 |
public |
780 |
Move(DbDirectoryInfo[] directories, DbInstrumentInfo[] instruments, String dest) { |
781 |
setTitle("InstrumentsDb.Move_task"); |
782 |
setDescription(i18n.getMessage("InstrumentsDb.Move.desc")); |
783 |
this.directories = directories; |
784 |
this.instruments = instruments; |
785 |
this.dest = dest; |
786 |
} |
787 |
|
788 |
/** The entry point of the task. */ |
789 |
public void |
790 |
run() { |
791 |
try { |
792 |
moveInstruments(); |
793 |
moveDirectories(); |
794 |
} catch(Exception x) { |
795 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
796 |
setErrorDetails(x); |
797 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
798 |
} |
799 |
} |
800 |
|
801 |
private void |
802 |
moveInstruments() throws Exception { |
803 |
if(instruments == null || instruments.length == 0) return; |
804 |
if(instruments.length == 1) { |
805 |
String path = instruments[0].getInstrumentPath(); |
806 |
CC.getClient().moveDbInstrument(path, dest); |
807 |
} else { |
808 |
String[] instrs = new String[instruments.length]; |
809 |
for(int i = 0; i < instruments.length; i++) { |
810 |
instrs[i] = instruments[i].getInstrumentPath(); |
811 |
} |
812 |
|
813 |
CC.getClient().moveDbInstruments(instrs, dest); |
814 |
} |
815 |
} |
816 |
|
817 |
private void |
818 |
moveDirectories() throws Exception { |
819 |
if(directories == null || directories.length == 0) return; |
820 |
if(directories.length == 1) { |
821 |
String path = directories[0].getDirectoryPath(); |
822 |
CC.getClient().moveDbDirectory(path, dest); |
823 |
} else { |
824 |
String[] dirs = new String[directories.length]; |
825 |
for(int i = 0; i < directories.length; i++) { |
826 |
dirs[i] = directories[i].getDirectoryPath(); |
827 |
} |
828 |
|
829 |
CC.getClient().moveDbDirectories(dirs, dest); |
830 |
} |
831 |
} |
832 |
} |
833 |
|
834 |
/** |
835 |
* This task copies the specified instruments |
836 |
* and directories to the specified location. |
837 |
*/ |
838 |
public static class Copy extends EnhancedTask { |
839 |
private DbDirectoryInfo[] directories; |
840 |
private DbInstrumentInfo[] instruments; |
841 |
private String dest; |
842 |
|
843 |
/** |
844 |
* Creates a new instance of <code>Copy</code>. |
845 |
* @param directories The directories to copy. |
846 |
* @param instruments The instruments to copy. |
847 |
* @param dest The absolute path name of the directory where |
848 |
* the specified instruments and directories will be copied to. |
849 |
*/ |
850 |
public |
851 |
Copy(DbDirectoryInfo[] directories, DbInstrumentInfo[] instruments, String dest) { |
852 |
setTitle("InstrumentsDb.Copy_task"); |
853 |
setDescription(i18n.getMessage("InstrumentsDb.Copy.desc")); |
854 |
this.directories = directories; |
855 |
this.instruments = instruments; |
856 |
this.dest = dest; |
857 |
} |
858 |
|
859 |
/** The entry point of the task. */ |
860 |
public void |
861 |
run() { |
862 |
try { |
863 |
copyInstruments(); |
864 |
copyDirectories(); |
865 |
} catch(Exception x) { |
866 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
867 |
setErrorDetails(x); |
868 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
869 |
} |
870 |
} |
871 |
|
872 |
private void |
873 |
copyInstruments() throws Exception { |
874 |
if(instruments == null || instruments.length == 0) return; |
875 |
if(instruments.length == 1) { |
876 |
String path = instruments[0].getInstrumentPath(); |
877 |
CC.getClient().copyDbInstrument(path, dest); |
878 |
} else { |
879 |
String[] instrs = new String[instruments.length]; |
880 |
for(int i = 0; i < instruments.length; i++) { |
881 |
instrs[i] = instruments[i].getInstrumentPath(); |
882 |
} |
883 |
|
884 |
CC.getClient().copyDbInstruments(instrs, dest); |
885 |
} |
886 |
} |
887 |
|
888 |
private void |
889 |
copyDirectories() throws Exception { |
890 |
if(directories == null || directories.length == 0) return; |
891 |
if(directories.length == 1) { |
892 |
String path = directories[0].getDirectoryPath(); |
893 |
CC.getClient().copyDbDirectory(path, dest); |
894 |
} else { |
895 |
String[] dirs = new String[directories.length]; |
896 |
for(int i = 0; i < directories.length; i++) { |
897 |
dirs[i] = directories[i].getDirectoryPath(); |
898 |
} |
899 |
|
900 |
CC.getClient().copyDbDirectories(dirs, dest); |
901 |
} |
902 |
} |
903 |
} |
904 |
|
905 |
/** |
906 |
* This task gets a list of all instrument files in the database |
907 |
* that that doesn't exist in the filesystem. |
908 |
*/ |
909 |
public static class FindLostInstrumentFiles extends EnhancedTask<String[]> { |
910 |
|
911 |
/** |
912 |
* Creates a new instance of <code>FindLostInstrumentFiles</code>. |
913 |
*/ |
914 |
public |
915 |
FindLostInstrumentFiles() { |
916 |
setTitle("InstrumentsDb.FindLostInstrumentFiles_task"); |
917 |
setDescription(i18n.getMessage("InstrumentsDb.FindLostInstrumentFiles.desc")); |
918 |
} |
919 |
|
920 |
/** The entry point of the task. */ |
921 |
public void |
922 |
run() { |
923 |
try { setResult(CC.getClient().findLostDbInstrumentFiles()); } |
924 |
catch(Exception x) { |
925 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
926 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
927 |
} |
928 |
} |
929 |
} |
930 |
|
931 |
/** |
932 |
* This task substitutes all occurrences of the specified instrument file |
933 |
* in the database, with the specified new path. |
934 |
*/ |
935 |
public static class SetInstrumentFilePath extends EnhancedTask { |
936 |
private String oldPath; |
937 |
private String newPath; |
938 |
|
939 |
/** |
940 |
* Creates a new instance of <code>SetInstrumentFilePath</code>. |
941 |
* @param oldPath The absolute path name of the instrument file to substitute. |
942 |
* @param newPath The new absolute path name. |
943 |
*/ |
944 |
public |
945 |
SetInstrumentFilePath(String oldPath, String newPath) { |
946 |
setTitle("InstrumentsDb.SetInstrumentFilePath_task"); |
947 |
setDescription(i18n.getMessage("InstrumentsDb.SetInstrumentFilePath.desc")); |
948 |
this.oldPath = oldPath; |
949 |
this.newPath = newPath; |
950 |
} |
951 |
|
952 |
/** The entry point of the task. */ |
953 |
public void |
954 |
run() { |
955 |
try { CC.getClient().setDbInstrumentFilePath(oldPath, newPath); } |
956 |
catch(Exception x) { |
957 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
958 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
959 |
} |
960 |
} |
961 |
} |
962 |
|
963 |
/** |
964 |
* This task retrieves information about a scan job. |
965 |
*/ |
966 |
public static class GetScanJobInfo extends EnhancedTask<ScanJobInfo> { |
967 |
private int jobId; |
968 |
|
969 |
/** |
970 |
* Creates a new instance of <code>GetScanJobInfo</code>. |
971 |
* @param jobId The ID of the scan job. |
972 |
*/ |
973 |
public |
974 |
GetScanJobInfo(int jobId) { |
975 |
setTitle("InstrumentsDb.GetScanJobInfo_task"); |
976 |
setDescription(i18n.getMessage("InstrumentsDb.GetScanJobInfo.desc")); |
977 |
this.jobId = jobId; |
978 |
} |
979 |
|
980 |
/** The entry point of the task. */ |
981 |
public void |
982 |
run() { |
983 |
try { setResult(CC.getClient().getDbInstrumentsJobInfo(jobId)); } |
984 |
catch(Exception x) { |
985 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
986 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
987 |
} |
988 |
} |
989 |
|
990 |
public int |
991 |
getJobId() { return jobId; } |
992 |
|
993 |
/** |
994 |
* Used to decrease the traffic. All task in the queue |
995 |
* equal to this are removed if added using {@link org.jsampler.CC#scheduleTask}. |
996 |
* @see org.jsampler.CC#addTask |
997 |
*/ |
998 |
public boolean |
999 |
equals(Object obj) { |
1000 |
if(obj == null) return false; |
1001 |
if(!(obj instanceof GetScanJobInfo)) return false; |
1002 |
if(((GetScanJobInfo)obj).getJobId() != getJobId()) return false; |
1003 |
|
1004 |
return true; |
1005 |
} |
1006 |
} |
1007 |
|
1008 |
/** |
1009 |
* This task formats the instruments database. |
1010 |
*/ |
1011 |
public static class Format extends EnhancedTask { |
1012 |
/** |
1013 |
* Formats the instruments database.. |
1014 |
*/ |
1015 |
public |
1016 |
Format() { |
1017 |
setTitle("InstrumentsDb.Format_task"); |
1018 |
String s = i18n.getMessage("InstrumentsDb.Format.desc"); |
1019 |
setDescription(s); |
1020 |
} |
1021 |
|
1022 |
/** The entry point of the task. */ |
1023 |
public void |
1024 |
run() { |
1025 |
try { CC.getClient().formatInstrumentsDb(); } |
1026 |
catch(Exception x) { |
1027 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
1028 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
1029 |
} |
1030 |
} |
1031 |
} |
1032 |
} |