metadataschema.java

来自「dspace 用j2ee架构的一个数字图书馆.开源程序」· Java 代码 · 共 513 行 · 第 1/2 页

JAVA
513
字号
        // Grab rows from DB        TableRowIterator tri = DatabaseManager.query(context,                "MetadataSchemaRegistry",                "SELECT * FROM MetadataSchemaRegistry WHERE namespace='"                        + 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                .query(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");        ResultSet rs = con.createStatement().executeQuery(                "SELECT COUNT(*) FROM " + reg.getTable()                        + " WHERE metadata_schema_id!=" + schemaID                        + " AND namespace='" + namespace + "'");        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");        ResultSet rs = con.createStatement().executeQuery(                "SELECT COUNT(*) FROM " + reg.getTable()                        + " WHERE metadata_schema_id!=" + schemaID                        + " AND short_id='" + name + "'");        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.query(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 + =
减小字号Ctrl + -
显示快捷键?