- Timestamp:
- 2004-11-30T14:39:46+13:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSNamespace.java
r8461 r8708 17 17 public abstract class METSNamespace 18 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 } 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 connection.execute(action.toString()); 154 155 // then get the namespace reference number if needsbe... 156 if (this.id == null) { 157 GS3SQLSelect select = new GS3SQLSelect("namespaces"); 158 select.addField("NamespaceRef"); 159 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE); 160 GS3SQLWhere where = new GS3SQLWhere(whereItem); 161 whereItem = new GS3SQLWhereItem("NamespaceType", "=", this.name); 162 where.add(where); 163 try { 164 connection.execute(select.toString()); 165 166 ResultSet result = connection.getResultSet(); 167 result.last(); 168 this.id = Integer.toString(result.getInt("NamespaceRef")); 169 } 170 catch (SQLException sqlex){ 171 this.id = null; 172 System.err.println(sqlex); 173 return false; 174 } 175 } 58 176 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()); 177 return true; 178 } 179 180 public static METSNamespace readSQL(GS3SQLConnection connection, ResultSet resultSet) 181 { 182 METSLocation metsLocation = null; 183 184 try { 185 String name = resultSet.getString("NamespaceType"); 186 String id = resultSet.getString("NamespaceRef"); 187 String location = resultSet.getString("FileLoc"); 188 String type = resultSet.getString("FileType"); 189 if (location != null && type != null) { 190 metsLocation = new METSLocation(type, location); 191 } 192 193 METSNamespace namespace = NamespaceFactory.initNamespace(name, metsLocation); 194 namespace.id = id; 195 196 int namespaceRef = resultSet.getInt("NamespaceRef"); 157 197 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"); 198 GS3SQLSelect select = new GS3SQLSelect("mdvalues"); 199 select.addField("*"); 200 GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", Integer.toString(namespaceRef), 201 GS3SQLField.INTEGER_TYPE)); 202 select.setWhere(where); 203 204 connection.execute(select.toString()); 189 205 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 } 206 ResultSet valuesSet = connection.getResultSet(); 207 if (valuesSet != null && valuesSet.first()) { 208 do { 209 String label = valuesSet.getString("Label"); 210 String value = valuesSet.getString("Value"); 211 212 namespace.addMetadata(label, value); 213 } 214 while (valuesSet.next()); 215 } 216 217 return namespace; 218 } 219 catch (java.net.MalformedURLException urlEx){ 220 System.out.println(urlEx); 221 } 222 catch (SQLException sqlEx){ 223 System.out.println(sqlEx); 224 } 225 return null; 226 } 219 227 }
Note:
See TracChangeset
for help on using the changeset viewer.