1 | package org.greenstone.gsdl3.gs3build.metadata;
|
---|
2 |
|
---|
3 | import java.io.PrintWriter;
|
---|
4 |
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.Iterator;
|
---|
9 |
|
---|
10 | import java.sql.SQLException;
|
---|
11 | import java.sql.ResultSet;
|
---|
12 |
|
---|
13 | import org.w3c.dom.Document;
|
---|
14 | import org.w3c.dom.Element;
|
---|
15 | import org.w3c.dom.NamedNodeMap;
|
---|
16 | import org.w3c.dom.Node;
|
---|
17 | import org.w3c.dom.NodeList;
|
---|
18 | import org.w3c.dom.Text;
|
---|
19 |
|
---|
20 | import org.xml.sax.SAXException;
|
---|
21 | import org.xml.sax.SAXParseException;
|
---|
22 |
|
---|
23 | import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
|
---|
24 |
|
---|
25 | import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
|
---|
26 | import org.greenstone.gsdl3.gs3build.database.*;
|
---|
27 |
|
---|
28 | public class METSDescriptiveSet
|
---|
29 | { Map children;
|
---|
30 | Map childrenById;
|
---|
31 | DescriptiveIdentifierFactory identifierFactory;
|
---|
32 |
|
---|
33 | class DescriptiveIdentifierFactory extends AbstractIdentifierFactory
|
---|
34 | { public DescriptiveIdentifierFactory()
|
---|
35 | { super("DM");
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | public METSDescriptiveSet()
|
---|
40 | { this.children = new HashMap();
|
---|
41 | this.childrenById = new HashMap();
|
---|
42 |
|
---|
43 | this.identifierFactory = new DescriptiveIdentifierFactory();
|
---|
44 |
|
---|
45 | this.createDescriptive("default");
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Create a new descriptive child, adding it automatically to this descriptive set.
|
---|
50 | *
|
---|
51 | * @param <code>String</code> a name to describe the descriptive set.
|
---|
52 | */
|
---|
53 | public METSDescriptive createDescriptive(String name)
|
---|
54 | { METSDescriptive descriptive = new METSDescriptive(this.identifierFactory.getNextIdentifier(), name);
|
---|
55 |
|
---|
56 | this.children.put(name, descriptive);
|
---|
57 | this.childrenById.put(descriptive.getID(), descriptive);
|
---|
58 | return descriptive;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Add a descriptive item to this set.
|
---|
63 | *
|
---|
64 | * @param <code>METSDescriptive</code> the descriptive set
|
---|
65 | */
|
---|
66 | public void addDescriptive(METSDescriptive metadata)
|
---|
67 | { // insert the item into both maps...
|
---|
68 | this.children.put(metadata.getName(), metadata);
|
---|
69 | this.childrenById.put(metadata.getID(), metadata);
|
---|
70 |
|
---|
71 | // when a descriptive block is added, check that it hasn't been used before...
|
---|
72 | //this.identifierFactory.noteIdentifier(metadata.getID());
|
---|
73 | this.identifierFactory.noteIdentifier(metadata.getName());
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Get a descriptive item by its name(GROUPID in METS file)
|
---|
78 | */
|
---|
79 | public METSDescriptive getDescriptive(String name)
|
---|
80 | { return (METSDescriptive) this.children.get(name);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Get a descriptive item by its identifier
|
---|
85 | */
|
---|
86 | public METSDescriptive getDescriptiveById(String id)
|
---|
87 | { return (METSDescriptive) this.childrenById.get(id);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void addNamespace(String descriptiveName, METSNamespace namespace)
|
---|
91 | { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
|
---|
92 | if (descriptive != null)
|
---|
93 | { descriptive.addNamespace(namespace);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public METSNamespace getNamespace(String descriptiveName, String namespace)
|
---|
98 | { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
|
---|
99 | if (descriptive != null)
|
---|
100 | { return descriptive.getNamespace(namespace);
|
---|
101 | }
|
---|
102 | return null;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public METSNamespace getOpenNamespace(String descriptiveName, String namespace)
|
---|
106 | { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
|
---|
107 |
|
---|
108 | if (descriptive != null)
|
---|
109 | { return descriptive.getOpenNamespace(namespace);
|
---|
110 | }
|
---|
111 | return null;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void addMetadata(String descriptiveName, String namespace, String label, String value)
|
---|
115 | { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
|
---|
116 |
|
---|
117 | if (descriptive != null)
|
---|
118 | { descriptive.addMetadata(namespace, label, value);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void setMetadata(String descriptiveName, String namespace, String label, String value)
|
---|
123 | { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
|
---|
124 |
|
---|
125 | if (descriptive != null)
|
---|
126 | { descriptive.setMetadata(namespace, label, value);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | public void removeMetadata(String descriptiveName, String namespace, String label)
|
---|
131 | { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
|
---|
132 |
|
---|
133 | if (descriptive != null)
|
---|
134 | { descriptive.removeMetadata(namespace, label);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | public void removeAllMetadata(String namespace, String label)
|
---|
139 | { Iterator descriptiveIter = this.children.values().iterator();
|
---|
140 |
|
---|
141 | while (descriptiveIter.hasNext()) {
|
---|
142 | METSDescriptive descriptive = (METSDescriptive) descriptiveIter.next();
|
---|
143 |
|
---|
144 | descriptive.removeMetadata(namespace, label);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | public List getMetadata(String descriptiveName, String namespace, String label)
|
---|
149 | { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
|
---|
150 |
|
---|
151 | if (descriptive != null)
|
---|
152 | { return descriptive.getMetadata(namespace, label);
|
---|
153 | }
|
---|
154 | return null;
|
---|
155 | }
|
---|
156 |
|
---|
157 | public void write(PrintWriter writer)
|
---|
158 | { Iterator groups = this.children.values().iterator();
|
---|
159 |
|
---|
160 | while (groups.hasNext())
|
---|
161 | { METSDescriptive group = (METSDescriptive) groups.next();
|
---|
162 |
|
---|
163 | group.write(writer);
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
|
---|
169 | { Iterator groups = this.children.values().iterator();
|
---|
170 |
|
---|
171 | while (groups.hasNext())
|
---|
172 | { METSDescriptive group = (METSDescriptive) groups.next();
|
---|
173 |
|
---|
174 | if (!group.writeSQL(document, connection))
|
---|
175 | { return false;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | return true;
|
---|
179 | }
|
---|
180 | /*
|
---|
181 | public static METSDescriptiveSet parseXML(NodeList fileSecs)
|
---|
182 | {
|
---|
183 | METSDescriptiveSet set = new METSDescriptiveSet();
|
---|
184 |
|
---|
185 | for (int g = 0; g < fileSecs.getLength(); g ++) {
|
---|
186 | // Schema schema = new Schema(schemas.item(s));
|
---|
187 | METSDescriptive metadata = METSDescriptive.parseXML((Element) fileSecs.item(g));
|
---|
188 | set.addDescriptive(metadata);
|
---|
189 | }
|
---|
190 | return set;
|
---|
191 | }
|
---|
192 | */
|
---|
193 |
|
---|
194 | /* public static METSDescriptiveSet parseXML(NodeList dmdSecs)
|
---|
195 | {
|
---|
196 | METSDescriptiveSet set = new METSDescriptiveSet();
|
---|
197 | METSDescriptive thisDescriptive = null;
|
---|
198 |
|
---|
199 | for (int g = 0; g < dmdSecs.getLength(); g ++) {
|
---|
200 | Element dmdNode = (Element) dmdSecs.item(g);
|
---|
201 | String ID = dmdNode.getAttribute("ID");
|
---|
202 | String label = dmdNode.getAttribute("GROUPID");
|
---|
203 | System.err.println("###Label="+label);
|
---|
204 |
|
---|
205 | if (label.equals("1")) {
|
---|
206 | thisDescriptive = set.getDescriptive("default");
|
---|
207 | System.err.println("####thisDescriptiveDefault=" + thisDescriptive);
|
---|
208 | } else {
|
---|
209 | thisDescriptive = new METSDescriptive(ID, label);
|
---|
210 | System.err.println("####thisDescriptive=" + thisDescriptive);
|
---|
211 | }
|
---|
212 |
|
---|
213 | METSDescriptive metadata = METSDescriptive.parseXML((Element) dmdSecs.item(g), thisDescriptive);
|
---|
214 |
|
---|
215 | set.addDescriptive(metadata);
|
---|
216 | }
|
---|
217 | return set;
|
---|
218 | }*/
|
---|
219 | public static METSDescriptiveSet parseXML(NodeList dmdSecs)
|
---|
220 | {
|
---|
221 | METSDescriptiveSet set = new METSDescriptiveSet();
|
---|
222 |
|
---|
223 | for (int g = 0; g < dmdSecs.getLength(); g ++) {
|
---|
224 | METSDescriptive metadata = METSDescriptive.parseXML((Element) dmdSecs.item(g));
|
---|
225 |
|
---|
226 | set.addDescriptive(metadata);
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | System.err.println("******** Away to do transfer!!!!!!");
|
---|
231 |
|
---|
232 | METSDescriptive top_level_metadata = set.getDescriptive("1");
|
---|
233 | METSDescriptive tl2_metadata = set.getDescriptive("1");
|
---|
234 | METSDescriptive opo_metadata = set.getDescriptive("1.1");
|
---|
235 | METSDescriptive default_metadata = set.getDescriptive("default");
|
---|
236 |
|
---|
237 | if (top_level_metadata != null) {
|
---|
238 | if (default_metadata != null) {
|
---|
239 | top_level_metadata.copy_metadata(default_metadata);
|
---|
240 | }
|
---|
241 | else {
|
---|
242 | System.err.println("Warning: Could not find default metadata");
|
---|
243 | }
|
---|
244 | }
|
---|
245 | else {
|
---|
246 | System.err.println("Warning: Could not find top level metadata for groupID=1");
|
---|
247 | }
|
---|
248 | return set;
|
---|
249 | }
|
---|
250 |
|
---|
251 | public static METSDescriptiveSet readSQL(DocumentInterface document, GS3SQLConnection connection)
|
---|
252 | {
|
---|
253 | METSDescriptiveSet set = new METSDescriptiveSet();
|
---|
254 |
|
---|
255 | GS3SQLSelect select = new GS3SQLSelect("metadata");
|
---|
256 | select.addField("*");
|
---|
257 | GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
|
---|
258 | select.setWhere(new GS3SQLWhere(whereItem));
|
---|
259 | connection.execute(select.toString());
|
---|
260 |
|
---|
261 | // start going through the matching metadata blocks
|
---|
262 | try {
|
---|
263 | ResultSet resultSet = connection.getResultSet();
|
---|
264 | resultSet.first();
|
---|
265 | do {
|
---|
266 | METSDescriptive descriptive = METSDescriptive.readSQL(document, connection, resultSet);
|
---|
267 | if (descriptive != null) {
|
---|
268 | set.addDescriptive(descriptive);
|
---|
269 | }
|
---|
270 | else {
|
---|
271 | System.out.println("Null descriptive");
|
---|
272 | }
|
---|
273 | } while (resultSet.next());
|
---|
274 | }
|
---|
275 | catch (SQLException sqlEx) {
|
---|
276 | System.out.println(sqlEx);
|
---|
277 | System.exit(1);
|
---|
278 | }
|
---|
279 |
|
---|
280 | return set;
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|