source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSDescriptiveSet.java@ 6738

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

Added remove function, or tidied as appropriate.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1package org.greenstone.gsdl3.gs3build.metadata;
2
3import java.io.PrintWriter;
4
5import java.util.HashMap;
6import java.util.List;
7import java.util.Map;
8import java.util.Iterator;
9
10import java.sql.SQLException;
11import java.sql.ResultSet;
12
13import org.w3c.dom.Document;
14import org.w3c.dom.Element;
15import org.w3c.dom.NamedNodeMap;
16import org.w3c.dom.Node;
17import org.w3c.dom.NodeList;
18import org.w3c.dom.Text;
19
20import org.xml.sax.SAXException;
21import org.xml.sax.SAXParseException;
22
23import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
24
25import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
26import org.greenstone.gsdl3.gs3build.database.*;
27
28public 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 }
74
75 /**
76 * Get a descriptive item by its name
77 */
78 public METSDescriptive getDescriptive(String name)
79 { return (METSDescriptive) this.children.get(name);
80 }
81
82 /**
83 * Get a descriptive item by its identifier
84 */
85 public METSDescriptive getDescriptiveById(String id)
86 { return (METSDescriptive) this.childrenById.get(id);
87 }
88
89 public void addNamespace(String descriptiveName, METSNamespace namespace)
90 { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
91 if (descriptive != null)
92 { descriptive.addNamespace(namespace);
93 }
94 }
95
96 public METSNamespace getNamespace(String descriptiveName, String namespace)
97 { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
98 if (descriptive != null)
99 { return descriptive.getNamespace(namespace);
100 }
101 return null;
102 }
103
104 public METSNamespace getOpenNamespace(String descriptiveName, String namespace)
105 { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
106
107 if (descriptive != null)
108 { return descriptive.getOpenNamespace(namespace);
109 }
110 return null;
111 }
112
113 public void addMetadata(String descriptiveName, String namespace, String label, String value)
114 { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
115
116 if (descriptive != null)
117 { descriptive.addMetadata(namespace, label, value);
118 }
119 }
120
121 public void setMetadata(String descriptiveName, String namespace, String label, String value)
122 { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
123
124 if (descriptive != null)
125 { descriptive.setMetadata(namespace, label, value);
126 }
127 }
128
129 public void removeMetadata(String descriptiveName, String namespace, String label)
130 { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
131
132 if (descriptive != null)
133 { descriptive.removeMetadata(namespace, label);
134 }
135 }
136
137 public void removeAllMetadata(String namespace, String label)
138 { Iterator descriptiveIter = this.children.values().iterator();
139
140 while (descriptiveIter.hasNext()) {
141 METSDescriptive descriptive = (METSDescriptive) descriptiveIter.next();
142
143 descriptive.removeMetadata(namespace, label);
144 }
145 }
146
147 public List getMetadata(String descriptiveName, String namespace, String label)
148 { METSDescriptive descriptive = this.getDescriptive(descriptiveName);
149
150 if (descriptive != null)
151 { return descriptive.getMetadata(namespace, label);
152 }
153 return null;
154 }
155
156 public void write(PrintWriter writer)
157 { Iterator groups = this.children.values().iterator();
158
159 while (groups.hasNext())
160 { METSDescriptive group = (METSDescriptive) groups.next();
161
162 group.write(writer);
163 }
164 }
165
166
167 public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
168 { Iterator groups = this.children.values().iterator();
169
170 while (groups.hasNext())
171 { METSDescriptive group = (METSDescriptive) groups.next();
172
173 if (!group.writeSQL(document, connection))
174 { return false;
175 }
176 }
177 return true;
178 }
179
180 public static METSDescriptiveSet parseXML(NodeList fileSecs)
181 {
182 METSDescriptiveSet set = new METSDescriptiveSet();
183
184 for (int g = 0; g < fileSecs.getLength(); g ++) {
185 // Schema schema = new Schema(schemas.item(s));
186 METSDescriptive metadata = METSDescriptive.parseXML((Element) fileSecs.item(g));
187 set.addDescriptive(metadata);
188 }
189 return set;
190 }
191
192 public static METSDescriptiveSet readSQL(DocumentInterface document, GS3SQLConnection connection)
193 {
194 METSDescriptiveSet set = new METSDescriptiveSet();
195
196 GS3SQLSelect select = new GS3SQLSelect("metadata");
197 select.addField("*");
198 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
199 select.setWhere(new GS3SQLWhere(whereItem));
200 connection.execute(select.toString());
201
202 // start going through the matching metadata blocks
203 try {
204 ResultSet resultSet = connection.getResultSet();
205 resultSet.first();
206 do {
207 METSDescriptive descriptive = METSDescriptive.readSQL(document, connection, resultSet);
208 if (descriptive != null) {
209 set.addDescriptive(descriptive);
210 }
211 else {
212 System.out.println("Null descriptive");
213 }
214 } while (resultSet.next());
215 }
216 catch (SQLException sqlEx) {
217 System.out.println(sqlEx);
218 System.exit(1);
219 }
220
221 return set;
222 }
223}
224
Note: See TracBrowser for help on using the repository browser.