source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/CollectionManager.java@ 6696

Last change on this file since 6696 was 6696, checked in by cs025, 20 years ago

Comments, modifications to BuildManager, corrections to CollectionManager
reading of collectConfigure.xml.

  • Property svn:keywords set to Author Date Id Revision
File size: 12.7 KB
Line 
1package org.greenstone.gsdl3.gs3build;
2
3import java.util.Date;
4import java.util.Calendar;
5import java.util.List;
6import java.util.ArrayList;
7import java.util.Map;
8import java.util.HashMap;
9import java.util.Iterator;
10import java.util.GregorianCalendar;
11
12import java.io.File;
13import java.io.IOException;
14
15import java.net.URL;
16
17import javax.xml.parsers.*;
18
19import org.w3c.dom.Document;
20import org.w3c.dom.Element;
21import org.w3c.dom.NamedNodeMap;
22import org.w3c.dom.Node;
23import org.w3c.dom.NodeList;
24import org.w3c.dom.Text;
25
26import org.xml.sax.SAXException;
27import org.xml.sax.SAXParseException;
28
29import org.greenstone.gsdl3.gs3build.collection.*;
30import org.greenstone.gsdl3.gs3build.classifier.*;
31import org.greenstone.gsdl3.gs3build.indexers.*;
32
33import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
34import org.greenstone.gsdl3.gs3build.util.GS3SQLConnectionFactory;
35import org.greenstone.gsdl3.gs3build.util.DOMUtils;
36
37/**
38 * Store and hold collection-level configuration information for a collection.
39 * This should be used by BuildManager to work out which classes, etc. to load
40 * at build time, and as a repository for the collection-level metadata, and
41 * a means of loading and saving the same to a file or database, as is seen
42 * fit in the final development of gs3.
43 */
44
45public class CollectionManager
46{
47 GregorianCalendar lastBuildDate; // pretty obvious
48 String adminEmail; // the email address of the administrator of the
49 // collection
50 int buildDocNo; // used to generate document identifiers
51 CollectionMetadata metadata; // collection-level metadata
52 GS3SQLConnection database; // the database to store everything in
53 String collectionHome;
54 String collectionName;
55
56 class CollectionClassifier
57 { File file;
58 String type;
59 List fields;
60 String sort;
61
62 public CollectionClassifier(String type, Node node)
63 { this.type = type;
64 this.fields = new ArrayList();
65
66 NodeList children = node.getChildNodes();
67 for (int c = 0; c < children.getLength(); c ++) {
68 Node child = children.item(c);
69
70 if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
71 String name = child.getNodeName();
72
73 if (name.equals("file")) {
74 NamedNodeMap atts = children.item(c).getAttributes();
75 Node attribute = atts.getNamedItem("URL");
76 String urlString = attribute.getNodeValue();
77 if (urlString == null)
78 continue;
79
80 this.file = new File(urlString);
81 }
82 else if (name.equals("field")) {
83 String fieldName = DOMUtils.getNodeChildText(children.item(c));
84 this.fields.add(fieldName.toString());
85 }
86 else if (name.equals("sort")) {
87 String sortName = DOMUtils.getNodeChildText(children.item(c));
88 this.sort = sortName;
89 }
90 }
91 }
92 }
93
94 public ClassifierInterface getClassifier()
95 {
96 if (this.type == null) {
97 return null;
98 }
99 if (this.type.toLowerCase().equals("hierarchy")) {
100 return new HierarchyClassifier(this.file, this.fields, this.sort);
101 }
102 else if (this.type.toLowerCase().equals("azlist")) {
103 return new AZListClassifier(this.fields);
104 }
105
106 return null;
107 }
108 }
109
110 class CollectionIndexer
111 { String level;
112 String type;
113 String name;
114 List fields;
115 String sort;
116
117 public CollectionIndexer(String type, String indexName, Node node)
118 { this.type = type.toLowerCase();
119 this.fields = new ArrayList();
120 this.name = indexName;
121 this.level = IndexerManager.DEFAULT_LEVEL;
122
123 NodeList children = node.getChildNodes();
124 for (int c = 0; c < children.getLength(); c ++) {
125 Node child = children.item(c);
126
127 if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
128 String name = child.getNodeName();
129
130 if (name.equals("level")) {
131 String levelName = DOMUtils.getNodeChildText(children.item(c));
132 this.level = levelName;
133 }
134 else if (name.equals("field")) {
135 String fieldName = DOMUtils.getNodeChildText(children.item(c));
136 this.fields.add(fieldName.toString());
137 }
138 }
139 }
140 if (this.fields.size() == 0) {
141 this.fields.add(IndexerManager.DEFAULT_FIELD);
142 }
143 }
144
145 public void prepareIndexer(Map indexerMap)
146 { IndexerInterface indexer = null;
147
148 System.out.println("Index: " + this.type);
149
150 if (!indexerMap.containsKey(this.type)) {
151 if (this.type.toLowerCase().equals("mg")) {
152 indexer = new MGIndexer();
153 }
154 if (indexer != null) {
155 indexerMap.put(this.type, indexer);
156 }
157 }
158
159 indexer = (IndexerInterface) indexerMap.get(this.type);
160 if (indexer != null) {
161 indexer.addIndex(this.level, this.fields.get(0).toString());
162 }
163 }
164 }
165
166 /**
167 * Create the collection manager for a given collection
168 *
169 * @param <code>String</code> the name of the collection
170 */
171 public CollectionManager(String collection)
172 { String collectRoot = System.getProperty("GSDL3HOME");
173
174 this.database = GS3SQLConnectionFactory.createConnection(collection);
175 /* if (this.database != null) {
176 this.database.clearCollection(collection);
177 this.database = null;
178 }
179 */
180 if (this.database == null) {
181 this.database = GS3SQLConnectionFactory.createConnection("test");
182 this.database.initCollection(collection);
183 }
184
185 this.metadata = new CollectionMetadata();
186
187 if (collectRoot == null)
188 { System.out.println("Unable to locate GSDL3HOME");
189 // System.exit(1);
190 return;
191 }
192
193 if (collectRoot.endsWith(System.getProperty("file.separator")))
194 { this.collectionHome = collectRoot + "web/sites/localsite/collect" + System.getProperty("file.separator") + collection;
195 }
196 else
197 { this.collectionHome = collectRoot + System.getProperty("file.separator") + "web/sites/localsite/collect" + System.getProperty("file.separator") + collection;
198 }
199 this.collectionName = collection;
200
201 File buildDirectory = new File(this.collectionHome, "building");
202 if (!buildDirectory.exists()) {
203 buildDirectory.mkdir();
204 }
205
206 File archiveDirectory = new File(this.collectionHome, "archives");
207 if (!archiveDirectory.exists()) {
208 archiveDirectory.mkdir();
209 }
210
211 this.buildDocNo = 1;
212 }
213
214 private void configureBrowsers(BuildManager buildManager, Node node)
215 { CollectionClassifier classifier = null;
216
217 NodeList children = node.getChildNodes();
218 for (int c = 0; c < children.getLength(); c ++)
219 { // assume that non-element children are irrelevant
220 if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE)
221 { continue;
222 }
223
224 String name = children.item(c).getNodeName();
225 System.out.println(name);
226
227 if (name.equals("classifier"))
228 {
229 NamedNodeMap atts = children.item(c).getAttributes();
230 Node attribute = atts.getNamedItem("type");
231 if (attribute == null) {
232 continue;
233 }
234
235 String type = attribute.getNodeValue();
236 classifier = new CollectionClassifier(type, children.item(c));
237
238 System.out.println("Found classifier " + type);
239
240 // attach the classifier
241 ClassifierInterface classify = classifier.getClassifier();
242 buildManager.getClassifierManager().addClassifier(classify);
243 }
244 }
245 }
246
247 public void configureCollection(BuildManager buildManager)
248 { File collectionConfig = new File(collectionHome, "/etc/collectionConfig.xml");
249
250 // get the File and read it in
251 try
252 {
253 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
254 DocumentBuilder builder = factory.newDocumentBuilder();
255 Document document = builder.parse(collectionConfig);
256
257 Map indexerMap = new HashMap();
258
259 // TODO: report an error
260 if (document == null)
261 {
262 }
263
264 // now parse the manager file...
265 Element rootElement = document.getDocumentElement();
266
267 if (rootElement.getTagName() != "collectionConfig")
268 { // TODO: throw exception
269 }
270
271 System.out.println("Configuring collection");
272
273 NodeList children = rootElement.getChildNodes();
274 for (int c = 0; c < children.getLength(); c ++)
275 { // assume that non-element children are irrelevant
276 if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE)
277 { continue;
278 }
279
280 String name = children.item(c).getNodeName();
281
282 // the name is a plugin element
283 if (name.equals("search")) {
284 NodeList indexChildren = children.item(c).getChildNodes();
285
286 for (int i = 0; i < indexChildren.getLength(); i ++) {
287 String childName = indexChildren.item(i).getNodeName();
288
289 if (childName.equals("index"))
290 { NamedNodeMap atts = indexChildren.item(i).getAttributes();
291 Node attribute = atts.getNamedItem("type");
292 String type = attribute.getNodeValue();
293
294 attribute = atts.getNamedItem("name");
295 String indexName = attribute.getNodeValue();
296
297 // create the local indexer representation
298 CollectionIndexer indexer = new CollectionIndexer(type, indexName, indexChildren.item(i));
299
300 // prepare it...
301 indexer.prepareIndexer(indexerMap);
302 }
303 }
304 }
305 else if (name.equals("browse"))
306 { this.configureBrowsers(buildManager, children.item(c));
307 }
308 // TODO: other elements - make a factory-method approach here...
309 else
310 {
311 }
312 }
313
314 Iterator indexers = indexerMap.values().iterator();
315 while (indexers.hasNext()) {
316 IndexerInterface indexer = (IndexerInterface) indexers.next();
317
318 // attach the classifier
319 buildManager.addIndexer(indexer);
320 }
321 }
322 catch (FactoryConfigurationError e) {
323 System.out.println(e);
324 }
325 catch (ParserConfigurationException ex) {
326 System.out.println(ex);
327 }
328 catch (SAXException ex) {
329 System.out.println(ex);
330 }
331 catch (IOException ex)
332 {
333 System.out.println(ex);
334 }
335
336 System.out.println("<<<Obtaining database>>>>");
337 }
338
339 public String getEtcDirectory()
340 { return this.collectionHome + File.separator + "etc";
341 }
342
343 public String getImportDirectory()
344 { return this.collectionHome + File.separator + "import";
345 }
346
347 public String getBuildDirectory()
348 { return this.collectionHome + File.separator + "building";
349 }
350
351 public String getArchiveDirectory()
352 { return this.collectionHome + File.separator + "archives";
353 }
354
355 public GS3SQLConnection getDatabase()
356 {
357 return this.database;
358 }
359
360 public void startBuild()
361 { GregorianCalendar today = new GregorianCalendar();
362
363 if (this.lastBuildDate != null)
364 { // if the build date is different to the last build date, then reset the build
365 // document number
366 if (today.get(Calendar.YEAR) != this.lastBuildDate.get(Calendar.YEAR) ||
367 today.get(Calendar.MONTH) != this.lastBuildDate.get(Calendar.MONTH) ||
368 today.get(Calendar.DAY_OF_MONTH) != this.lastBuildDate.get(Calendar.DAY_OF_MONTH))
369 { this.buildDocNo = 1;
370 }
371 }
372 this.lastBuildDate = today;
373 }
374
375 public void endBuild()
376 {
377 Date startDate = this.lastBuildDate.getTime();
378 Date date = new Date();
379
380 long startTime = startDate.getTime();
381 long endTime = date.getTime();
382
383 long difference = ((endTime - startTime) + 500) / 1000;
384
385 System.out.println("Build completed");
386 System.out.println("---------------");
387 System.out.println("Total Documents: " + this.getCollectionMetadata("gsdl3", "documentCount"));
388 System.out.println("Total Time : " + (difference / 60) + " min. " + (difference % 60) + " secs.");
389 }
390
391 public String getNextDocumentID()
392 { StringBuffer ID = new StringBuffer();
393
394 int value;
395 ID.append(lastBuildDate.get(Calendar.YEAR));
396
397 // the use of month is a little odd, hence the following
398 // code. Calendar.MONTH yields 0 = January, 1 = February,
399 // etc. hence there is a '+1' added to the month to make
400 // it into January = 1, etc., and the padding is altered
401 // correspondingly.
402 value = lastBuildDate.get(Calendar.MONTH);
403 if (value < 9)
404 { ID.append("0");
405 }
406 ID.append(value + 1);
407 value = lastBuildDate.get(Calendar.DAY_OF_MONTH);
408 if (value < 10)
409 ID.append("0");
410 ID.append(value);
411
412
413 value = this.buildDocNo;
414 this.buildDocNo ++;
415
416 ID.append(":");
417 ID.append(Integer.toString(value));
418 return ID.toString();
419 }
420
421 public int getDocumentNumber()
422 { this.buildDocNo ++;
423 return this.buildDocNo - 1;
424 }
425
426 /**
427 * Get the collection metadata item in the given namespace
428 *
429 * @param <code>String</code> the namespace
430 * @param <code>String</code> the label of the metadata
431 */
432 public String getCollectionMetadata(String namespace, String label)
433 { return this.metadata.getCollectionMetadata(namespace, label).get(0).toString();
434 }
435
436 /**
437 * Set the collection metadata item in the given namespace
438 *
439 * @param <code>String</code> the namespace
440 * @param <code>String</code> the label
441 * @param <code>String</code> the value
442 */
443 public void setCollectionMetadata(String namespace, String label, String value)
444 { this.metadata.setCollectionMetadata(namespace, label, value);
445 }
446}
447
Note: See TracBrowser for help on using the repository browser.