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

Last change on this file since 5800 was 5798, checked in by cs025, 21 years ago

Adding gs3build code

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