source: trunk/greenstone3-extensions/gs3build/src/org/greenstone/gsdl3/gs3build/metadata/METSNamespace.java@ 12188

Last change on this file since 12188 was 12188, checked in by kjdon, 18 years ago

Initial revision

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