⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 metadataschema.java

📁 DSPACE的源码 dspace-1.4-source
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                namespace);        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 MetadataSchema(row);        }    }    /**     * Update the metadata schema in the database.     *     * @param context DSpace context     * @throws SQLException     * @throws AuthorizeException     * @throws NonUniqueMetadataException     */    public void update(Context context) throws SQLException,            AuthorizeException, NonUniqueMetadataException    {        // Check authorisation: Only admins may update the metadata registry        if (!AuthorizeManager.isAdmin(context))        {            throw new AuthorizeException(                    "Only administrators may modify the metadata registry");        }        // Ensure the schema name is unique        if (!uniqueShortName(context, name))        {            throw new NonUniqueMetadataException("Please make the name " + name                    + " unique");        }        // Ensure the schema namespace is unique        if (!uniqueNamespace(context, namespace))        {            throw new NonUniqueMetadataException("Please make the namespace " + namespace                    + " unique");        }                row.setColumn("namespace", getNamespace());        row.setColumn("short_id", getName());        DatabaseManager.update(context, row);        decache();        log.info(LogManager.getHeader(context, "update_metadata_schema",                "metadata_schema_id=" + getSchemaID() + "namespace="                        + getNamespace() + "name=" + getName()));    }    /**     * Delete the metadata schema.     *     * @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_schema",                "metadata_schema_id=" + getSchemaID()));        DatabaseManager.delete(context, row);    }    /**     * Return all metadata schemas.     *     * @param context DSpace context     * @return array of metadata schemas     * @throws SQLException     */    public static MetadataSchema[] findAll(Context context) throws SQLException    {        List schemas = new ArrayList();        // Get all the metadataschema rows        TableRowIterator tri = DatabaseManager.queryTable(context, "MetadataSchemaRegistry",                        "SELECT * FROM MetadataSchemaRegistry ORDER BY metadata_schema_id");        // Make into DC Type objects        while (tri.hasNext())        {            schemas.add(new MetadataSchema(tri.next()));        }        // close the TableRowIterator to free up resources        tri.close();        // Convert list into an array        MetadataSchema[] typeArray = new MetadataSchema[schemas.size()];        return (MetadataSchema[]) schemas.toArray(typeArray);    }    /**     * Return true if and only if the passed name appears within the allowed     * number of times in the current schema.     *     * @param context DSpace context     * @param namespace namespace URI to match     * @return true of false     * @throws SQLException     */    private boolean uniqueNamespace(Context context, String namespace)            throws SQLException    {        Connection con = context.getDBConnection();        TableRow reg = DatabaseManager.row("MetadataSchemaRegistry");                String query = "SELECT COUNT(*) FROM " + reg.getTable() + " " +        		"WHERE metadata_schema_id != ? " +         		"AND namespace= ? ";        PreparedStatement statement = con.prepareStatement(query);        statement.setInt(1,schemaID);        statement.setString(2,namespace);                ResultSet rs = statement.executeQuery();        int count = 0;        if (rs.next())        {            count = rs.getInt(1);        }        return (count == 0);    }    /**     * Return true if and only if the passed name is unique.     *     * @param context DSpace context     * @param name  short name of schema     * @return true of false     * @throws SQLException     */    private boolean uniqueShortName(Context context, String name)            throws SQLException    {        Connection con = context.getDBConnection();        TableRow reg = DatabaseManager.row("MetadataSchemaRegistry");                String query = "SELECT COUNT(*) FROM " + reg.getTable() + " " +        		"WHERE metadata_schema_id != ? " +                "AND short_id = ? ";                PreparedStatement statement = con.prepareStatement(query);        statement.setInt(1,schemaID);        statement.setString(2,name);                ResultSet rs = statement.executeQuery();        int count = 0;        if (rs.next())        {            count = rs.getInt(1);        }        return (count == 0);    }    /**     * Get the schema corresponding with this 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 schema ID     * @return the metadata schema object     * @throws SQLException     */    public static MetadataSchema find(Context context, int id)            throws SQLException    {        initCache(context);        Integer iid = new Integer(id);        // sanity check        if (!id2schema.containsKey(iid))            return null;        return (MetadataSchema) id2schema.get(iid);    }    /**     * Get the schema corresponding with this short name.     *     * @param context     *            context, in case we need to read it in from DB     * @param shortName     *            the short name for the schema     * @return the metadata schema object     * @throws SQLException     */    public static MetadataSchema find(Context context, String shortName)        throws SQLException    {        // If we are not passed a valid schema name then return        if (shortName == null)            return null;        initCache(context);        if (!name2schema.containsKey(shortName))            return null;        return (MetadataSchema) name2schema.get(shortName);    }    // invalidate the cache e.g. after something modifies DB state.    private static void decache()    {        id2schema = null;        name2schema = null;    }    // load caches if necessary    private static void initCache(Context context) throws SQLException    {        if (id2schema != null && name2schema != null)            return;        log.info("Loading schema cache for fast finds");        id2schema = new HashMap();        name2schema = new HashMap();        TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataSchemaRegistry",                "SELECT * from MetadataSchemaRegistry");        while (tri.hasNext())        {            TableRow row = tri.next();            MetadataSchema s = new MetadataSchema(row);            id2schema.put(new Integer(s.schemaID), s);            name2schema.put(s.name, s);        }        // close the TableRowIterator to free up resources        tri.close();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -