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

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

capitalised a few remaining field names that were lower case

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