1 | package org.greenstone.gsdl3.gs3build.metadata;
|
---|
2 |
|
---|
3 | import java.io.PrintWriter;
|
---|
4 |
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Iterator;
|
---|
7 | import java.sql.SQLException;
|
---|
8 | import java.sql.ResultSet;
|
---|
9 |
|
---|
10 | import org.greenstone.gsdl3.gs3build.database.*;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Implement a single namespace
|
---|
14 | */
|
---|
15 |
|
---|
16 | public abstract class METSNamespace
|
---|
17 | {
|
---|
18 | protected String type; // the type of the metadata
|
---|
19 | protected String otherType; // if a non-standard type, then...
|
---|
20 | protected String name; // e.g. "DC" for Dublin Core
|
---|
21 | protected METSLocation location; // the location of the corresponding file
|
---|
22 | protected String group; // the group of metadata namespaces to which
|
---|
23 | // this namespace belongs
|
---|
24 | protected String id; // the ID for this namespace
|
---|
25 |
|
---|
26 | public METSNamespace(String name)
|
---|
27 | {
|
---|
28 | this.name = name;
|
---|
29 | this.location = null;
|
---|
30 | this.group = null;
|
---|
31 | this.id = null;
|
---|
32 | }
|
---|
33 |
|
---|
34 | public METSNamespace(String name, METSLocation location)
|
---|
35 | {
|
---|
36 | this.location = location;
|
---|
37 | this.name = name;
|
---|
38 | this.group = null;
|
---|
39 | this.id = null;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Get the name of the namespace...
|
---|
44 | *
|
---|
45 | * @return <code>String</code> the name of the namespace
|
---|
46 | */
|
---|
47 | public String getName()
|
---|
48 | {
|
---|
49 | return this.name;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Get the group of namespaces to which this namespace belongs.
|
---|
54 | *
|
---|
55 | * @return <code>String</code> the name of the namespace group
|
---|
56 | */
|
---|
57 | public String getGroup()
|
---|
58 | {
|
---|
59 | return this.group;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Set the group of namespaces to which this namespace belongs.
|
---|
64 | *
|
---|
65 | * @param <code>String</code> the name of the namespace group
|
---|
66 | */
|
---|
67 | public void setGroup(String group)
|
---|
68 | {
|
---|
69 | this.group = group;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Get the ID for this namespace.
|
---|
74 | *
|
---|
75 | * @return <code>String</code> the id of the namespace
|
---|
76 | */
|
---|
77 | public String getID()
|
---|
78 | {
|
---|
79 | return this.id;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Set the id of this namespace.
|
---|
84 | *
|
---|
85 | * @param <code>String</code> the name of the namespace group
|
---|
86 | */
|
---|
87 | public void setID(String id)
|
---|
88 | {
|
---|
89 | this.id = id;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * <p>Indicate whether this namespace is open to being changed or not.</p>
|
---|
94 | * <p>Metadata which is created from a distinct file cannot be changed,
|
---|
95 | * only those which have no associated file can be modified.
|
---|
96 | *
|
---|
97 | * @return <code>boolean</code> whether this namespace can be altered.
|
---|
98 | */
|
---|
99 | public boolean isEditable()
|
---|
100 | {
|
---|
101 | return (this.location == null);
|
---|
102 | }
|
---|
103 |
|
---|
104 | public abstract boolean addMetadata(String label, String value);
|
---|
105 | public abstract boolean setMetadata(String label, String value);
|
---|
106 | public abstract boolean removeMetadata(String label);
|
---|
107 | public abstract boolean removeMetadata(String label, String value);
|
---|
108 | public abstract List getMetadata(String label);
|
---|
109 | public abstract Iterator getMetadataNames();
|
---|
110 | public abstract boolean write(PrintWriter writer);
|
---|
111 | // public abstract boolean writeSQL(GS3SQLConnection connection);
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Write out the metadata to an SQL database through a <code>GS3SQLConnection</code>.
|
---|
115 | *
|
---|
116 | * @param <code>int</code> the sql identifier of the parent object (in "metadata") table.
|
---|
117 | * @param <code>GS3SQLConnection</code> the SQL database to use.
|
---|
118 | */
|
---|
119 | public boolean writeSQL(int parentId, GS3SQLConnection connection)
|
---|
120 | {
|
---|
121 | GS3SQLAction action;
|
---|
122 |
|
---|
123 | // If the namespace is not null, then set it...
|
---|
124 | if (this.id != null) {
|
---|
125 | // use an update action in this case...
|
---|
126 | GS3SQLUpdate update = new GS3SQLUpdate("namespaces");
|
---|
127 | update.addValue("NamespaceID", this.id);
|
---|
128 |
|
---|
129 | // set up the where clause
|
---|
130 | GS3SQLWhere where =
|
---|
131 | new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", this.id,
|
---|
132 | GS3SQLField.INTEGER_TYPE));
|
---|
133 | update.setWhere(where);
|
---|
134 | action = update;
|
---|
135 | }
|
---|
136 | else {
|
---|
137 | GS3SQLInsert insert = new GS3SQLInsert("namespaces");
|
---|
138 | action = insert;
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (this.location != null) {
|
---|
142 | action.addValue("FileLoc", this.location.getLocation().toString());
|
---|
143 | action.addValue("FileType", "URL");
|
---|
144 | }
|
---|
145 | else {
|
---|
146 | // no location stuff
|
---|
147 | }
|
---|
148 | action.addValue("MetadataRef", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE);
|
---|
149 | action.addValue("NamespaceType", this.name);
|
---|
150 |
|
---|
151 | // Execute the action
|
---|
152 | connection.execute(action.toString());
|
---|
153 |
|
---|
154 | // then get the namespace reference number if needsbe...
|
---|
155 | if (this.id == null) {
|
---|
156 | GS3SQLSelect select = new GS3SQLSelect("namespaces");
|
---|
157 | select.addField("NamespaceRef");
|
---|
158 | GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE);
|
---|
159 | GS3SQLWhere where = new GS3SQLWhere(whereItem);
|
---|
160 | whereItem = new GS3SQLWhereItem("NamespaceType", "=", this.name);
|
---|
161 | where.add(where);
|
---|
162 | try {
|
---|
163 | connection.execute(select.toString());
|
---|
164 |
|
---|
165 | ResultSet result = connection.getResultSet();
|
---|
166 | result.last();
|
---|
167 | this.id = Integer.toString(result.getInt("NamespaceRef"));
|
---|
168 | }
|
---|
169 | catch (SQLException sqlex){
|
---|
170 | this.id = null;
|
---|
171 | System.err.println(sqlex);
|
---|
172 | return false;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | return true;
|
---|
177 | }
|
---|
178 |
|
---|
179 | public static METSNamespace readSQL(GS3SQLConnection connection, ResultSet resultSet)
|
---|
180 | {
|
---|
181 | METSLocation metsLocation = null;
|
---|
182 |
|
---|
183 | try {
|
---|
184 | String name = resultSet.getString("NamespaceType");
|
---|
185 | String id = resultSet.getString("NamespaceRef");
|
---|
186 | String location = resultSet.getString("FileLoc");
|
---|
187 | String type = resultSet.getString("FileType");
|
---|
188 | if (location != null && type != null) {
|
---|
189 | metsLocation = new METSLocation(type, location);
|
---|
190 | }
|
---|
191 |
|
---|
192 | METSNamespace namespace = NamespaceFactory.initNamespace(name, metsLocation);
|
---|
193 | namespace.id = id;
|
---|
194 |
|
---|
195 | int namespaceRef = resultSet.getInt("NamespaceRef");
|
---|
196 |
|
---|
197 | GS3SQLSelect select = new GS3SQLSelect("mdvalues");
|
---|
198 | select.addField("*");
|
---|
199 | GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", Integer.toString(namespaceRef),
|
---|
200 | GS3SQLField.INTEGER_TYPE));
|
---|
201 | select.setWhere(where);
|
---|
202 |
|
---|
203 | connection.execute(select.toString());
|
---|
204 |
|
---|
205 | ResultSet valuesSet = connection.getResultSet();
|
---|
206 | if (valuesSet != null && valuesSet.first()) {
|
---|
207 | do {
|
---|
208 | String label = valuesSet.getString("Label");
|
---|
209 | String value = valuesSet.getString("Value");
|
---|
210 |
|
---|
211 | namespace.addMetadata(label, value);
|
---|
212 | }
|
---|
213 | while (valuesSet.next());
|
---|
214 | }
|
---|
215 |
|
---|
216 | return namespace;
|
---|
217 | }
|
---|
218 | catch (java.net.MalformedURLException urlEx){
|
---|
219 | System.out.println(urlEx);
|
---|
220 | }
|
---|
221 | catch (SQLException sqlEx){
|
---|
222 | System.out.println(sqlEx);
|
---|
223 | }
|
---|
224 | return null;
|
---|
225 | }
|
---|
226 | }
|
---|