source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSNamespace.java@ 8699

Last change on this file since 8699 was 8461, checked in by kjdon, 20 years ago

added in Chi's changes for METS documents. mostly the addition of new/improved parseXML methods

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