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

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

Extensive additions to metadata

  • 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.List;
6
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 boolean write(PrintWriter writer);
103 // public abstract boolean writeSQL(GS3SQLConnection connection);
104
105 /**
106 * Write out the metadata to an SQL database through a <code>GS3SQLConnection</code>.
107 *
108 * @param <code>GS3SQLConnection</code> the SQL database to use.
109 */
110 public boolean writeSQL(int parentId, GS3SQLConnection connection)
111 {
112 GS3SQLAction action;
113
114 // If the namespace is not null, then set it...
115 if (this.id != null) {
116 // use an update action in this case...
117 GS3SQLUpdate update = new GS3SQLUpdate("namespaces");
118 update.addValue("NamespaceID", this.id);
119
120 // set up the where clause
121 GS3SQLWhere where =
122 new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", this.id,
123 GS3SQLField.INTEGER_TYPE));
124 update.setWhere(where);
125 action = update;
126 }
127 else {
128 GS3SQLInsert insert = new GS3SQLInsert("namespaces");
129 action = insert;
130 }
131
132 if (this.location != null) {
133 action.addValue("fileLoc", this.location.getLocation().toString());
134 action.addValue("fileType", "URL");
135 }
136 else {
137 // no location stuff
138 }
139 action.addValue("MetadataRef", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE);
140 action.addValue("NamespaceType", this.name);
141
142 // Execute the action
143 connection.execute(action.toString());
144
145 // then get the namespace reference number if needsbe...
146 if (this.id == null) {
147 GS3SQLSelect select = new GS3SQLSelect("namespaces");
148 select.addField("NamespaceRef");
149 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE);
150 GS3SQLWhere where = new GS3SQLWhere(whereItem);
151 whereItem = new GS3SQLWhereItem("NamespaceType", "=", this.name);
152 where.add(where);
153 try {
154 connection.execute(select.toString());
155
156 ResultSet result = connection.getResultSet();
157 result.last();
158 this.id = Integer.toString(result.getInt("NamespaceRef"));
159 }
160 catch (SQLException sqlex)
161 { this.id = null;
162 System.err.println(sqlex);
163 return false;
164 }
165 }
166
167 return true;
168 }
169
170 public static METSNamespace readSQL(GS3SQLConnection connection, ResultSet resultSet)
171 {
172 METSLocation metsLocation = null;
173
174 try {
175 String name = resultSet.getString("NamespaceType");
176 String id = resultSet.getString("NamespaceRef");
177 String location = resultSet.getString("fileLoc");
178 String type = resultSet.getString("fileType");
179 if (location != null && type != null) {
180 metsLocation = new METSLocation(type, location);
181 }
182
183 METSNamespace namespace = NamespaceFactory.initNamespace(name, metsLocation);
184 namespace.id = id;
185
186 int namespaceRef = resultSet.getInt("NamespaceRef");
187
188 GS3SQLSelect select = new GS3SQLSelect("mdvalues");
189 select.addField("*");
190 GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", Integer.toString(namespaceRef),
191 GS3SQLField.INTEGER_TYPE));
192 select.setWhere(where);
193
194 connection.execute(select.toString());
195
196 ResultSet valuesSet = connection.getResultSet();
197 if (valuesSet != null && valuesSet.first()) {
198 do {
199 String label = valuesSet.getString("label");
200 String value = valuesSet.getString("value");
201
202 namespace.addMetadata(label, value);
203 }
204 while (valuesSet.next());
205 }
206
207 return namespace;
208 }
209 catch (java.net.MalformedURLException urlEx)
210 { System.out.println(urlEx);
211 }
212 catch (SQLException sqlEx)
213 { System.out.println(sqlEx);
214 }
215 return null;
216 }
217}
Note: See TracBrowser for help on using the repository browser.