📄 metadatafield.java
字号:
* @throws AuthorizeException */ public static MetadataField findByElement(Context context, int schemaID, String element, String qualifier) throws SQLException, AuthorizeException { // Grab rows from DB String qualifierClause = (qualifier == null ? "qualifier IS NULL" : "qualifier='" + qualifier + "'"); TableRowIterator tri = DatabaseManager.query(context, "MetadataFieldRegistry", "SELECT * FROM MetadataFieldRegistry WHERE metadata_schema_id=" + schemaID + " AND element='" + element + "' AND " + qualifierClause); TableRow row = null; if (tri.hasNext()) { row = tri.next(); } // close the TableRowIterator to free up resources tri.close(); if (row == null) { return null; } else { return new MetadataField(row); } } /** * Retrieve all Dublin Core types from the registry * * @param context dspace context * @return an array of all the Dublin Core types * @throws SQLException */ public static MetadataField[] findAll(Context context) throws SQLException { List fields = new ArrayList(); // Get all the metadatafieldregistry rows TableRowIterator tri = DatabaseManager .query(context, "MetadataFieldRegistry", "SELECT * FROM MetadataFieldRegistry ORDER BY element, qualifier"); // Make into DC Type objects while (tri.hasNext()) { fields.add(new MetadataField(tri.next())); } // close the TableRowIterator to free up resources tri.close(); // Convert list into an array MetadataField[] typeArray = new MetadataField[fields.size()]; return (MetadataField[]) fields.toArray(typeArray); } /** * Return all metadata fields that are found in a given schema. * * @param context dspace context * @param schemaID schema by db ID * @return array of metadata fields * @throws SQLException */ public static MetadataField[] findAllInSchema(Context context, int schemaID) throws SQLException { List fields = new ArrayList(); // Get all the metadatafieldregistry rows TableRowIterator tri = DatabaseManager.query(context, "MetadataFieldRegistry", "SELECT * FROM MetadataFieldRegistry WHERE metadata_schema_id=" + schemaID + " ORDER BY element, qualifier"); // Make into DC Type objects while (tri.hasNext()) { fields.add(new MetadataField(tri.next())); } // close the TableRowIterator to free up resources tri.close(); // Convert list into an array MetadataField[] typeArray = new MetadataField[fields.size()]; return (MetadataField[]) fields.toArray(typeArray); } /** * Update the metadata field in the database. * * @param context dspace context * @throws SQLException * @throws AuthorizeException * @throws NonUniqueMetadataException * @throws IOException */ public void update(Context context) throws SQLException, AuthorizeException, NonUniqueMetadataException, IOException { // Check authorisation: Only admins may update the metadata registry if (!AuthorizeManager.isAdmin(context)) { throw new AuthorizeException( "Only administrators may modiffy the Dublin Core registry"); } // Check to see if the schema ID was altered. If is was then we will // query to ensure that there is not already a duplicate name field. if (row.getIntColumn("metadata_schema_id") != schemaID) { if (MetadataField.hasElement(context, schemaID, element, qualifier)) { throw new NonUniqueMetadataException( "Duplcate field name found in target schema"); } } // Ensure the element and qualifier are unique within a given schema. if (!unique(context, schemaID, element, qualifier)) { throw new NonUniqueMetadataException("Please make " + element + "." + qualifier); } row.setColumn("metadata_schema_id", schemaID); row.setColumn("element", element); row.setColumn("qualifier", qualifier); row.setColumn("scope_note", scopeNote); DatabaseManager.update(context, row); decache(); log.info(LogManager.getHeader(context, "update_metadatafieldregistry", "metadata_field_id=" + getFieldID() + "element=" + getElement() + "qualifier=" + getQualifier())); } /** * Return true if and only if the schema has a field with the given element * and qualifier pair. * * @param context dspace context * @param schemaID schema by ID * @param element element name * @param qualifier qualifier name * @return true if the field exists * @throws SQLException * @throws AuthorizeException */ private static boolean hasElement(Context context, int schemaID, String element, String qualifier) throws SQLException, AuthorizeException { return MetadataField.findByElement(context, schemaID, element, qualifier) != null; } /** * Delete the metadata field. * * @param context dspace context * @throws SQLException * @throws AuthorizeException */ public void delete(Context context) throws SQLException, AuthorizeException { // Check authorisation: Only admins may create DC types if (!AuthorizeManager.isAdmin(context)) { throw new AuthorizeException( "Only administrators may modify the metadata registry"); } log.info(LogManager.getHeader(context, "delete_metadata_field", "metadata_field_id=" + getFieldID())); DatabaseManager.delete(context, row); decache(); } /** * A sanity check that ensures a given element and qualifier are unique * within a given schema. The check happens in code as we cannot use a * database constraint. * * @param context dspace context * @param schemaID * @param element * @param qualifier * @return true if unique * @throws AuthorizeException * @throws SQLException * @throws IOException */ private boolean unique(Context context, int schemaID, String element, String qualifier) throws IOException, SQLException, AuthorizeException { Connection con = context.getDBConnection(); TableRow reg = DatabaseManager.row("MetadataFieldRegistry"); ResultSet rs = con.createStatement().executeQuery( "SELECT COUNT(*) FROM " + reg.getTable() + " WHERE metadata_schema_id=" + schemaID + " and metadata_field_id != " + fieldID + " and element='" + element + "' and qualifier='" + qualifier + "'"); int count = 0; if (rs.next()) { count = rs.getInt(1); } return (count == 0); } /** * Return the HTML FORM key for the given field. * * @param schema * @param element * @param qualifier * @return HTML FORM key */ public static String formKey(String schema, String element, String qualifier) { if (qualifier == null) { return schema + "_" + element; } else { return schema + "_" + element + "_" + qualifier; } } /** * Find the field corresponding to the given numeric ID. The ID is * a database key internal to DSpace. * * @param context * context, in case we need to read it in from DB * @param id * the metadata field ID * @return the metadata field object * @throws SQLException */ public static MetadataField find(Context context, int id) throws SQLException { initCache(context); // 'sanity check' first. Integer iid = new Integer(id); if (!id2field.containsKey(iid)) return null; return (MetadataField) id2field.get(iid); } // invalidate the cache e.g. after something modifies DB state. private static void decache() { id2field = null; } // load caches if necessary private static void initCache(Context context) throws SQLException { if (id2field != null) return; id2field = new HashMap(); log.info("Loading MetadataField elements into cache."); // Grab rows from DB TableRowIterator tri = DatabaseManager.query(context, "MetadataFieldRegistry", "SELECT * from MetadataFieldRegistry"); while (tri.hasNext()) { TableRow row = tri.next(); int fieldID = row.getIntColumn("metadata_field_id"); id2field.put(new Integer(fieldID), new MetadataField(row)); } // close the TableRowIterator to free up resources tri.close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -