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

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

Minor changes to build and collection managers

  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 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.toLowerCase().equals("hierarchy")) {
97 return new HierarchyClassifier(this.file, this.fields, this.sort);
98 }
99
100 return null;
101 }
102 }
103
104 class CollectionIndexer
105 { String level;
106 String type;
107 String name;
108 List fields;
109 String sort;
110
111 public CollectionIndexer(String type, String indexName, Node node)
112 { this.type = type.toLowerCase();
113 this.fields = new ArrayList();
114 this.name = indexName;
115 this.level = IndexerManager.DEFAULT_LEVEL;
116
117 NodeList children = node.getChildNodes();
118 for (int c = 0; c < children.getLength(); c ++) {
119 Node child = children.item(c);
120
121 if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
122 String name = child.getNodeName();
123
124 if (name.equals("level")) {
125 String levelName = DOMUtils.getNodeChildText(children.item(c));
126 this.level = levelName;
127 }
128 else if (name.equals("field")) {
129 String fieldName = DOMUtils.getNodeChildText(children.item(c));
130 this.fields.add(fieldName.toString());
131 }
132 }
133 }
134 if (this.fields.size() == 0) {
135 this.fields.add(IndexerManager.DEFAULT_FIELD);
136 }
137 }
138
139 public void prepareIndexer(Map indexerMap)
140 { IndexerInterface indexer = null;
141
142 if (!indexerMap.containsKey(this.type)) {
143 if (this.type.toLowerCase().equals("mg")) {
144 indexer = new MGIndexer();
145 }
146 if (indexer != null) {
147 indexerMap.put(this.type, indexer);
148 }
149 }
150
151 indexer = (IndexerInterface) indexerMap.get(this.type);
152 if (indexer != null) {
153 indexer.addIndex(this.level, this.fields.get(0).toString());
154 }
155 }
156 }
157
158 /**
159 * Create the collection manager for a given collection
160 *
161 * @param <code>String</code> the name of the collection
162 */
163 public CollectionManager(String collection)
164 { String collectRoot = System.getProperty("GSDL3HOME");
165
166 this.database = GS3SQLConnectionFactory.createConnection(collection);
167 if (this.database != null) {
168 this.database.clearCollection(collection);
169 this.database = null;
170 }
171 if (this.database == null) {
172 this.database = GS3SQLConnectionFactory.createConnection("test");
173 this.database.initCollection(collection);
174 }
175
176 this.metadata = new CollectionMetadata();
177
178 if (collectRoot == null)
179 { System.out.println("Unable to locate GSDL3HOME");
180 // System.exit(1);
181 return;
182 }
183
184 if (collectRoot.endsWith(System.getProperty("file.separator")))
185 { this.collectionHome = collectRoot + "web/sites/localsite/collect" + System.getProperty("file.separator") + collection;
186 }
187 else
188 { this.collectionHome = collectRoot + System.getProperty("file.separator") + "web/sites/localsite/collect" + System.getProperty("file.separator") + collection;
189 }
190 this.collectionName = collection;
191
192 File buildDirectory = new File(this.collectionHome, "building");
193 if (!buildDirectory.exists()) {
194 buildDirectory.mkdir();
195 }
196
197 File archiveDirectory = new File(this.collectionHome, "archives");
198 if (!archiveDirectory.exists()) {
199 archiveDirectory.mkdir();
200 }
201
202 this.buildDocNo = 1;
203 }
204
205 public void configureCollection(BuildManager buildManager)
206 { File collectionConfig = new File(collectionHome, "/etc/collectionConfig.xml");
207
208 // get the File and read it in
209 try
210 {
211 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
212 DocumentBuilder builder = factory.newDocumentBuilder();
213 Document document = builder.parse(collectionConfig);
214
215 CollectionClassifier classifier = null;
216
217 Map indexerMap = new HashMap();
218
219 // TODO: report an error
220 if (document == null)
221 {
222 }
223
224 // now parse the manager file...
225 Element rootElement = document.getDocumentElement();
226
227 if (rootElement.getTagName() != "collectionConfig")
228 { // TODO: throw exception
229 }
230
231 NodeList children = rootElement.getChildNodes();
232 for (int c = 0; c < children.getLength(); c ++)
233 { // assume that non-element children are irrelevant
234 if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE)
235 { continue;
236 }
237
238 String name = children.item(c).getNodeName();
239
240 // the name is a plugin element
241 if (name.equals("index"))
242 { NamedNodeMap atts = children.item(c).getAttributes();
243 Node attribute = atts.getNamedItem("type");
244 String type = attribute.getNodeValue();
245
246 attribute = atts.getNamedItem("name");
247 String indexName = attribute.getNodeValue();
248
249 // create the local indexer representation
250 CollectionIndexer indexer = new CollectionIndexer(type, indexName, children.item(c));
251
252 // prepare it...
253 indexer.prepareIndexer(indexerMap);
254 }
255 else if (name.equals("browse"))
256 {
257 }
258 else if (name.equals("classifier"))
259 {
260 NamedNodeMap atts = children.item(c).getAttributes();
261 Node attribute = atts.getNamedItem("type");
262 String type = attribute.getNodeValue();
263 classifier = new CollectionClassifier(type, children.item(c));
264
265 // attach the classifier
266 ClassifierInterface classify = classifier.getClassifier();
267 buildManager.getClassifierManager().addClassifier(classify);
268 }
269 // TODO: other elements - make a factory-method approach here...
270 else
271 {
272 }
273 }
274
275 Iterator indexers = indexerMap.values().iterator();
276 while (indexers.hasNext()) {
277 IndexerInterface indexer = (IndexerInterface) indexers.next();
278
279 // attach the classifier
280 buildManager.getIndexerManager().addIndexer(indexer);
281 }
282 }
283 catch (FactoryConfigurationError e) {
284 System.out.println(e);
285 }
286 catch (ParserConfigurationException ex) {
287 System.out.println(ex);
288 }
289 catch (SAXException ex) {
290 System.out.println(ex);
291 }
292 catch (IOException ex)
293 {
294 System.out.println(ex);
295 }
296
297 System.out.println("<<<Obtaining database>>>>");
298 }
299
300 public String getEtcDirectory()
301 { return this.collectionHome + File.separator + "etc";
302 }
303
304 public String getImportDirectory()
305 { return this.collectionHome + File.separator + "import";
306 }
307
308 public String getBuildDirectory()
309 { return this.collectionHome + File.separator + "building";
310 }
311
312 public String getArchiveDirectory()
313 { return this.collectionHome + File.separator + "archives";
314 }
315
316 public GS3SQLConnection getDatabase()
317 {
318 return this.database;
319 }
320
321 public void startBuild()
322 { GregorianCalendar today = new GregorianCalendar();
323
324 if (this.lastBuildDate != null)
325 { // if the build date is different to the last build date, then reset the build
326 // document number
327 if (today.get(Calendar.YEAR) != this.lastBuildDate.get(Calendar.YEAR) ||
328 today.get(Calendar.MONTH) != this.lastBuildDate.get(Calendar.MONTH) ||
329 today.get(Calendar.DAY_OF_MONTH) != this.lastBuildDate.get(Calendar.DAY_OF_MONTH))
330 { this.buildDocNo = 1;
331 }
332 }
333 this.lastBuildDate = today;
334 }
335
336 public void endBuild()
337 {
338 Date startDate = this.lastBuildDate.getTime();
339 Date date = new Date();
340
341 long startTime = startDate.getTime();
342 long endTime = date.getTime();
343
344 long difference = ((endTime - startTime) + 500) / 1000;
345
346 System.out.println("Build completed");
347 System.out.println("---------------");
348 System.out.println("Total Documents: " + this.getCollectionMetadata("gsdl3", "documentCount"));
349 System.out.println("Total Time : " + (difference / 60) + " min. " + (difference % 60) + " secs.");
350 }
351
352 public String getNextDocumentID()
353 { StringBuffer ID = new StringBuffer();
354
355 int value;
356 ID.append(lastBuildDate.get(Calendar.YEAR));
357
358 // the use of month is a little odd, hence the following
359 // code. Calendar.MONTH yields 0 = January, 1 = February,
360 // etc. hence there is a '+1' added to the month to make
361 // it into January = 1, etc., and the padding is altered
362 // correspondingly.
363 value = lastBuildDate.get(Calendar.MONTH);
364 if (value < 9)
365 { ID.append("0");
366 }
367 ID.append(value + 1);
368 value = lastBuildDate.get(Calendar.DAY_OF_MONTH);
369 if (value < 10)
370 ID.append("0");
371 ID.append(value);
372
373
374 value = this.buildDocNo;
375 this.buildDocNo ++;
376
377 ID.append(":");
378 ID.append(Integer.toString(value));
379 return ID.toString();
380 }
381
382 public int getDocumentNumber()
383 { this.buildDocNo ++;
384 return this.buildDocNo - 1;
385 }
386
387 /**
388 * Get the collection metadata item in the given namespace
389 *
390 * @param <code>String</code> the namespace
391 * @param <code>String</code> the label of the metadata
392 */
393 public String getCollectionMetadata(String namespace, String label)
394 { return this.metadata.getCollectionMetadata(namespace, label).get(0).toString();
395 }
396
397 /**
398 * Set the collection metadata item in the given namespace
399 *
400 * @param <code>String</code> the namespace
401 * @param <code>String</code> the label
402 * @param <code>String</code> the value
403 */
404 public void setCollectionMetadata(String namespace, String label, String value)
405 { this.metadata.setCollectionMetadata(namespace, label, value);
406 }
407}
408
Note: See TracBrowser for help on using the repository browser.