1 | package org.greenstone.gsdl3.build;
|
---|
2 | import org.greenstone.gsdl3.util.*;
|
---|
3 |
|
---|
4 | import org.w3c.dom.Element;
|
---|
5 | import org.w3c.dom.Document;
|
---|
6 | import java.io.File;
|
---|
7 |
|
---|
8 | /** the class containing the main program for collection building - is essentially a wrapper around a CollectionConstructor
|
---|
9 | * @see CollectionConstructor
|
---|
10 | */
|
---|
11 | public class ConstructCollection
|
---|
12 | implements ConstructionListener {
|
---|
13 |
|
---|
14 | static public void main(String [] args) {
|
---|
15 |
|
---|
16 | String site_home = null;
|
---|
17 | String coll_name = null;
|
---|
18 | int process_mode = -1;
|
---|
19 | if (args.length < 4) {
|
---|
20 | printUsage();
|
---|
21 | return;
|
---|
22 | }
|
---|
23 |
|
---|
24 | // parse the args
|
---|
25 |
|
---|
26 | XMLConverter converter = new XMLConverter();
|
---|
27 | Document doc = converter.newDOM();
|
---|
28 | Element option_list = doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
29 | Element option;
|
---|
30 |
|
---|
31 | for (int i=0; i<args.length-1; i++) {
|
---|
32 | String key = args[i];
|
---|
33 | if (!key.startsWith("-")) {
|
---|
34 | continue; // ignore options that dont start with '-'
|
---|
35 | }
|
---|
36 | key = key.substring(1);
|
---|
37 | String val = args[i+1];
|
---|
38 | if (val.startsWith("-")) {
|
---|
39 | val=null;
|
---|
40 | } else {
|
---|
41 | i++;
|
---|
42 | }
|
---|
43 |
|
---|
44 | if (key.equals("site")) {
|
---|
45 | site_home = val;
|
---|
46 | } else if (key.equals("mode")) {
|
---|
47 | if (val==null) {
|
---|
48 | continue;
|
---|
49 | }
|
---|
50 | if (val.equals("new")) {
|
---|
51 | process_mode = GS2PerlConstructor.NEW;
|
---|
52 | } else if (val.equals("import")) {
|
---|
53 | process_mode = GS2PerlConstructor.IMPORT;
|
---|
54 | } else if (val.equals("build")) {
|
---|
55 | process_mode = GS2PerlConstructor.BUILD;
|
---|
56 | } else if (val.equals("activate")) {
|
---|
57 | process_mode = GS2PerlConstructor.ACTIVATE;
|
---|
58 | }
|
---|
59 |
|
---|
60 | } else { // an option to pass to the builder
|
---|
61 | option = doc.createElement(GSXML.PARAM_ELEM);
|
---|
62 | option.setAttribute(GSXML.NAME_ATT, key);
|
---|
63 | if (val!=null) {
|
---|
64 | option.setAttribute(GSXML.VALUE_ATT, val);
|
---|
65 | }
|
---|
66 | option_list.appendChild(option);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | String last_arg = args[args.length-1];
|
---|
70 | if (last_arg.startsWith("-")) { // should be coll name
|
---|
71 | System.out.println("ERROR: the last arg should be the collection name!");
|
---|
72 | printUsage();
|
---|
73 | return;
|
---|
74 | }
|
---|
75 |
|
---|
76 | coll_name = last_arg;
|
---|
77 |
|
---|
78 | // check that we have the required fields
|
---|
79 | if (site_home==null || process_mode == -1 || coll_name==null) {
|
---|
80 | System.out.println("ERROR: you have not specified all the necessary args!");
|
---|
81 | printUsage();
|
---|
82 | return;
|
---|
83 | }
|
---|
84 |
|
---|
85 | // check that the collection name is valid - ie the directory exists if this is not a new one, or doesn't exist if we are creating a new one
|
---|
86 | File coll_dir = new File(GSFile.collectionBaseDir(site_home, coll_name));
|
---|
87 | if (process_mode != GS2PerlConstructor.NEW && !coll_dir.exists()) {
|
---|
88 | System.out.println("ERROR: Invalid collection ("+coll_name+").");
|
---|
89 | printUsage();
|
---|
90 | return;
|
---|
91 | } else if (process_mode == GS2PerlConstructor.NEW &&
|
---|
92 | coll_dir.exists()) {
|
---|
93 | System.out.println("ERROR: there is already a collection named "+coll_name);
|
---|
94 | printUsage();
|
---|
95 | return;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | ConstructCollection processor = new ConstructCollection();
|
---|
100 | GS2PerlConstructor constructor = new GS2PerlConstructor("perl_build");
|
---|
101 | if (!constructor.configure()) {
|
---|
102 | System.out.println("couldn't configure the constructor!!");
|
---|
103 | return;
|
---|
104 | }
|
---|
105 | constructor.setCollectionName(coll_name);
|
---|
106 | constructor.setSiteHome(site_home);
|
---|
107 | constructor.setProcessParams(option_list);
|
---|
108 | constructor.setActionType(process_mode);
|
---|
109 | constructor.addListener(processor);
|
---|
110 | constructor.start();
|
---|
111 | }
|
---|
112 |
|
---|
113 | static protected void printUsage() {
|
---|
114 | System.out.println("Usage: the following arguments need to be supplied:\n -site <site-home> -mode new|import|build|activate [options] <coll-name>");
|
---|
115 | System.out.println("Options available are:");
|
---|
116 | System.out.println("-maxdocs N\t process at most N documents\n"+
|
---|
117 | "-gs2\t use greenstone 2 style building");
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** This event handler used to signify that a task has been started */
|
---|
121 | public void processBegun(ConstructionEvent evt) {
|
---|
122 | System.out.println("begun: "+evt.getMessage());
|
---|
123 | }
|
---|
124 | /** This event handler used to signify that a task has been completed */
|
---|
125 | public void processComplete(ConstructionEvent evt){
|
---|
126 | System.out.println("complete: "+evt.getMessage());
|
---|
127 | }
|
---|
128 | /** This event handler used to send status updates as the task is progressing */
|
---|
129 | public void processStatus(ConstructionEvent evt){
|
---|
130 | System.out.println(evt.getMessage());
|
---|
131 | }
|
---|
132 | /** This event handler used to send any other messages to the listeners */
|
---|
133 | public void message(ConstructionEvent evt){
|
---|
134 | System.out.println(evt.getMessage());
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 |
|
---|
139 |
|
---|
140 | }
|
---|