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

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

Added support for naming indexes

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