1 | package org.greenstone.gsdl3.gs3build;
|
---|
2 |
|
---|
3 | import java.util.Date;
|
---|
4 | import java.util.Calendar;
|
---|
5 | import java.util.GregorianCalendar;
|
---|
6 |
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.IOException;
|
---|
9 |
|
---|
10 | import javax.xml.parsers.*;
|
---|
11 |
|
---|
12 | import org.w3c.dom.Document;
|
---|
13 | import org.w3c.dom.Element;
|
---|
14 | import org.w3c.dom.NamedNodeMap;
|
---|
15 | import org.w3c.dom.Node;
|
---|
16 | import org.w3c.dom.NodeList;
|
---|
17 | import org.w3c.dom.Text;
|
---|
18 |
|
---|
19 | import org.xml.sax.SAXException;
|
---|
20 | import org.xml.sax.SAXParseException;
|
---|
21 |
|
---|
22 | import org.greenstone.gsdl3.gs3build.collection.*;
|
---|
23 |
|
---|
24 | import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
|
---|
25 | import org.greenstone.gsdl3.gs3build.util.GS3SQLConnectionFactory;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Store and hold collection-level configuration information for a collection.
|
---|
29 | * This should be used by BuildManager to work out which classes, etc. to load
|
---|
30 | * at build time, and as a repository for the collection-level metadata, and
|
---|
31 | * a means of loading and saving the same to a file or database, as is seen
|
---|
32 | * fit in the final development of gs3.
|
---|
33 | */
|
---|
34 |
|
---|
35 | public class CollectionManager
|
---|
36 | {
|
---|
37 | GregorianCalendar lastBuildDate; // pretty obvious
|
---|
38 | String adminEmail; // the email address of the administrator of the
|
---|
39 | // collection
|
---|
40 | int buildDocNo; // used to generate document identifiers
|
---|
41 | CollectionMetadata metadata; // collection-level metadata
|
---|
42 | GS3SQLConnection database; // the database to store everything in
|
---|
43 | String collectionHome;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Create the collection manager for a given collection
|
---|
47 | *
|
---|
48 | * @param <code>String</code> the name of the collection
|
---|
49 | */
|
---|
50 | public CollectionManager(String collection)
|
---|
51 | { String collectRoot = System.getProperty("GSDL3HOME");
|
---|
52 |
|
---|
53 | this.database = GS3SQLConnectionFactory.createConnection(collection);
|
---|
54 | if (this.database == null) {
|
---|
55 | this.database = GS3SQLConnectionFactory.createConnection("test");
|
---|
56 | this.database.initCollection(collection);
|
---|
57 | }
|
---|
58 |
|
---|
59 | this.metadata = new CollectionMetadata();
|
---|
60 |
|
---|
61 | if (collectRoot == null)
|
---|
62 | { System.out.println("Unable to locate GSDL3HOME");
|
---|
63 | // System.exit(1);
|
---|
64 | return;
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (collectRoot.endsWith(System.getProperty("file.separator")))
|
---|
68 | { this.collectionHome = collectRoot + "web/sites/localsite/collect" + System.getProperty("file.separator") + collection;
|
---|
69 | }
|
---|
70 | else
|
---|
71 | { this.collectionHome = collectRoot + System.getProperty("file.separator") + "web/sites/localsite/collect" + System.getProperty("file.separator") + collection;
|
---|
72 | }
|
---|
73 |
|
---|
74 | File buildDirectory = new File(this.collectionHome, "building");
|
---|
75 | if (!buildDirectory.exists()) {
|
---|
76 | buildDirectory.mkdir();
|
---|
77 | }
|
---|
78 |
|
---|
79 | File archiveDirectory = new File(this.collectionHome, "archives");
|
---|
80 | if (!archiveDirectory.exists()) {
|
---|
81 | archiveDirectory.mkdir();
|
---|
82 | }
|
---|
83 |
|
---|
84 | this.buildDocNo = 1;
|
---|
85 |
|
---|
86 | File collectionConfig = new File(collectionHome, "/etc/collectionConfig.xml");
|
---|
87 |
|
---|
88 | // get the File and read it in
|
---|
89 | try
|
---|
90 | {
|
---|
91 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
---|
92 | DocumentBuilder builder = factory.newDocumentBuilder();
|
---|
93 | Document document = builder.parse(collectionConfig);
|
---|
94 |
|
---|
95 | // TODO: report an error
|
---|
96 | if (document == null)
|
---|
97 | {
|
---|
98 | }
|
---|
99 |
|
---|
100 | // now parse the manager file...
|
---|
101 | Element rootElement = document.getDocumentElement();
|
---|
102 |
|
---|
103 | if (rootElement.getTagName() != "collectionConfig")
|
---|
104 | { // TODO: throw exception
|
---|
105 | }
|
---|
106 |
|
---|
107 | NodeList children = rootElement.getChildNodes();
|
---|
108 | for (int c = 0; c < children.getLength(); c ++)
|
---|
109 | { // assume that non-element children are irrelevant
|
---|
110 | if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE)
|
---|
111 | { continue;
|
---|
112 | }
|
---|
113 |
|
---|
114 | String name = children.item(c).getNodeName();
|
---|
115 |
|
---|
116 | // the name is a plugin element
|
---|
117 | if (name.equals("search"))
|
---|
118 | {
|
---|
119 | }
|
---|
120 | else if (name.equals("browse"))
|
---|
121 | {
|
---|
122 | }
|
---|
123 | else if (name.equals("classify"))
|
---|
124 | {
|
---|
125 | }
|
---|
126 | // TODO: other elements - make a factory-method approach here...
|
---|
127 | else
|
---|
128 | {
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | catch (FactoryConfigurationError e) {
|
---|
133 | System.out.println(e);
|
---|
134 | }
|
---|
135 | catch (ParserConfigurationException ex) {
|
---|
136 | System.out.println(ex);
|
---|
137 | }
|
---|
138 | catch (SAXException ex) {
|
---|
139 | System.out.println(ex);
|
---|
140 | }
|
---|
141 | catch (IOException ex)
|
---|
142 | {
|
---|
143 | System.out.println(ex);
|
---|
144 | }
|
---|
145 |
|
---|
146 | System.out.println("<<<Obtaining database>>>>");
|
---|
147 | }
|
---|
148 |
|
---|
149 | public String getEtcDirectory()
|
---|
150 | { return this.collectionHome + File.separator + "etc";
|
---|
151 | }
|
---|
152 |
|
---|
153 | public String getImportDirectory()
|
---|
154 | { return this.collectionHome + File.separator + "import";
|
---|
155 | }
|
---|
156 |
|
---|
157 | public String getBuildDirectory()
|
---|
158 | { return this.collectionHome + File.separator + "building";
|
---|
159 | }
|
---|
160 |
|
---|
161 | public String getArchiveDirectory()
|
---|
162 | { return this.collectionHome + File.separator + "archives";
|
---|
163 | }
|
---|
164 |
|
---|
165 | public GS3SQLConnection getDatabase()
|
---|
166 | {
|
---|
167 | return this.database;
|
---|
168 | }
|
---|
169 |
|
---|
170 | public void startBuild()
|
---|
171 | { GregorianCalendar today = new GregorianCalendar();
|
---|
172 |
|
---|
173 | if (this.lastBuildDate != null)
|
---|
174 | { // if the build date is different to the last build date, then reset the build
|
---|
175 | // document number
|
---|
176 | if (today.get(Calendar.YEAR) != this.lastBuildDate.get(Calendar.YEAR) ||
|
---|
177 | today.get(Calendar.MONTH) != this.lastBuildDate.get(Calendar.MONTH) ||
|
---|
178 | today.get(Calendar.DAY_OF_MONTH) != this.lastBuildDate.get(Calendar.DAY_OF_MONTH))
|
---|
179 | { this.buildDocNo = 1;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | this.lastBuildDate = today;
|
---|
183 | }
|
---|
184 |
|
---|
185 | public void endBuild()
|
---|
186 | {
|
---|
187 | Date startDate = this.lastBuildDate.getTime();
|
---|
188 | Date date = new Date();
|
---|
189 |
|
---|
190 | long startTime = startDate.getTime();
|
---|
191 | long endTime = date.getTime();
|
---|
192 |
|
---|
193 | long difference = ((endTime - startTime) + 500) / 1000;
|
---|
194 |
|
---|
195 | System.out.println("Build completed");
|
---|
196 | System.out.println("---------------");
|
---|
197 | System.out.println("Total Documents: " + this.getCollectionMetadata("gsdl3", "documentCount"));
|
---|
198 | System.out.println("Total Time : " + (difference / 60) + " min. " + (difference % 60) + " secs.");
|
---|
199 | }
|
---|
200 |
|
---|
201 | public String getNextDocumentID()
|
---|
202 | { StringBuffer ID = new StringBuffer();
|
---|
203 |
|
---|
204 | int value;
|
---|
205 | ID.append(lastBuildDate.get(Calendar.YEAR));
|
---|
206 |
|
---|
207 | // the use of month is a little odd, hence the following
|
---|
208 | // code. Calendar.MONTH yields 0 = January, 1 = February,
|
---|
209 | // etc. hence there is a '+1' added to the month to make
|
---|
210 | // it into January = 1, etc., and the padding is altered
|
---|
211 | // correspondingly.
|
---|
212 | value = lastBuildDate.get(Calendar.MONTH);
|
---|
213 | if (value < 9)
|
---|
214 | { ID.append("0");
|
---|
215 | }
|
---|
216 | ID.append(value + 1);
|
---|
217 | value = lastBuildDate.get(Calendar.DAY_OF_MONTH);
|
---|
218 | if (value < 10)
|
---|
219 | ID.append("0");
|
---|
220 | ID.append(value);
|
---|
221 |
|
---|
222 |
|
---|
223 | value = this.buildDocNo;
|
---|
224 | this.buildDocNo ++;
|
---|
225 |
|
---|
226 | ID.append(":");
|
---|
227 | ID.append(Integer.toString(value));
|
---|
228 | return ID.toString();
|
---|
229 | }
|
---|
230 |
|
---|
231 | public int getDocumentNumber()
|
---|
232 | { this.buildDocNo ++;
|
---|
233 | return this.buildDocNo - 1;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Get the collection metadata item in the given namespace
|
---|
238 | *
|
---|
239 | * @param <code>String</code> the namespace
|
---|
240 | * @param <code>String</code> the label of the metadata
|
---|
241 | */
|
---|
242 | public String getCollectionMetadata(String namespace, String label)
|
---|
243 | { return this.metadata.getCollectionMetadata(namespace, label).get(0).toString();
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Set the collection metadata item in the given namespace
|
---|
248 | *
|
---|
249 | * @param <code>String</code> the namespace
|
---|
250 | * @param <code>String</code> the label
|
---|
251 | * @param <code>String</code> the value
|
---|
252 | */
|
---|
253 | public void setCollectionMetadata(String namespace, String label, String value)
|
---|
254 | { this.metadata.setCollectionMetadata(namespace, label, value);
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|