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