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

Last change on this file since 6012 was 6009, checked in by cs025, 21 years ago

Alterations for new collection identifiers, etc.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1package org.greenstone.gsdl3.gs3build;
2
3import java.util.Date;
4import java.util.Calendar;
5import java.util.GregorianCalendar;
6
7import java.io.File;
8import java.io.IOException;
9
10import javax.xml.parsers.*;
11
12import org.w3c.dom.Document;
13import org.w3c.dom.Element;
14import org.w3c.dom.NamedNodeMap;
15import org.w3c.dom.Node;
16import org.w3c.dom.NodeList;
17import org.w3c.dom.Text;
18
19import org.xml.sax.SAXException;
20import org.xml.sax.SAXParseException;
21
22import org.greenstone.gsdl3.gs3build.collection.*;
23
24import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
25import 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
35public 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 getImportDirectory()
150 { return this.collectionHome + File.separator + "import";
151 }
152
153 public String getBuildDirectory()
154 { return this.collectionHome + File.separator + "building";
155 }
156
157 public String getArchiveDirectory()
158 { return this.collectionHome + File.separator + "archives";
159 }
160
161 public GS3SQLConnection getDatabase()
162 {
163 return this.database;
164 }
165
166 public void startBuild()
167 { GregorianCalendar today = new GregorianCalendar();
168
169 if (this.lastBuildDate != null)
170 { // if the build date is different to the last build date, then reset the build
171 // document number
172 if (today.get(Calendar.YEAR) != this.lastBuildDate.get(Calendar.YEAR) ||
173 today.get(Calendar.MONTH) != this.lastBuildDate.get(Calendar.MONTH) ||
174 today.get(Calendar.DAY_OF_MONTH) != this.lastBuildDate.get(Calendar.DAY_OF_MONTH))
175 { this.buildDocNo = 1;
176 }
177 }
178 this.lastBuildDate = today;
179 }
180
181 public void endBuild()
182 {
183 Date startDate = this.lastBuildDate.getTime();
184 Date date = new Date();
185
186 long startTime = startDate.getTime();
187 long endTime = date.getTime();
188
189 long difference = ((endTime - startTime) + 500) / 1000;
190
191 System.out.println("Build completed");
192 System.out.println("---------------");
193 System.out.println("Total Documents: " + this.getCollectionMetadata("gsdl3", "documentCount"));
194 System.out.println("Total Time : " + (difference / 60) + " min. " + (difference % 60) + " secs.");
195 }
196
197 public String getNextDocumentID()
198 { StringBuffer ID = new StringBuffer();
199
200 int value;
201 ID.append(lastBuildDate.get(Calendar.YEAR));
202
203 // the use of month is a little odd, hence the following
204 // code. Calendar.MONTH yields 0 = January, 1 = February,
205 // etc. hence there is a '+1' added to the month to make
206 // it into January = 1, etc., and the padding is altered
207 // correspondingly.
208 value = lastBuildDate.get(Calendar.MONTH);
209 if (value < 9)
210 { ID.append("0");
211 }
212 ID.append(value + 1);
213 value = lastBuildDate.get(Calendar.DAY_OF_MONTH);
214 if (value < 10)
215 ID.append("0");
216 ID.append(value);
217
218
219 value = this.buildDocNo;
220 this.buildDocNo ++;
221
222 ID.append(":");
223 ID.append(Integer.toString(value));
224 return ID.toString();
225 }
226
227 public int getDocumentNumber()
228 { this.buildDocNo ++;
229 return this.buildDocNo - 1;
230 }
231
232 /**
233 * Get the collection metadata item in the given namespace
234 *
235 * @param <code>String</code> the namespace
236 * @param <code>String</code> the label of the metadata
237 */
238 public String getCollectionMetadata(String namespace, String label)
239 { return this.metadata.getCollectionMetadata(namespace, label).get(0).toString();
240 }
241
242 /**
243 * Set the collection metadata item in the given namespace
244 *
245 * @param <code>String</code> the namespace
246 * @param <code>String</code> the label
247 * @param <code>String</code> the value
248 */
249 public void setCollectionMetadata(String namespace, String label, String value)
250 { this.metadata.setCollectionMetadata(namespace, label, value);
251 }
252}
253
Note: See TracBrowser for help on using the repository browser.