📄 bitstreamformat.java
字号:
/* * BitstreamFormat.java * * Version: $Revision: 1.22 $ * * Date: $Date: 2006/02/13 10:33:50 $ * * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts * Institute of Technology. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of the Hewlett-Packard Company nor the name of the * Massachusetts Institute of Technology nor the names of their * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. */package org.dspace.content;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import org.apache.log4j.Logger;import org.dspace.authorize.AuthorizeException;import org.dspace.authorize.AuthorizeManager;import org.dspace.core.ConfigurationManager;import org.dspace.core.Context;import org.dspace.core.LogManager;import org.dspace.storage.rdbms.DatabaseManager;import org.dspace.storage.rdbms.TableRow;import org.dspace.storage.rdbms.TableRowIterator;/** * Class representing a particular bitstream format. * <P> * Changes to the bitstream format metadata are only written to the database * when <code>update</code> is called. * * @author Robert Tansley * @version $Revision: 1.22 $ */public class BitstreamFormat{ /** log4j logger */ private static Logger log = Logger.getLogger(BitstreamFormat.class); /** * The "unknown" support level - for bitstream formats that are unknown to * the system */ public static final int UNKNOWN = 0; /** * The "known" support level - for bitstream formats that are known to the * system, but not fully supported */ public static final int KNOWN = 1; /** * The "supported" support level - for bitstream formats known to the system * and fully supported. */ public static final int SUPPORTED = 2; /** Our context */ private Context bfContext; /** The row in the table representing this format */ private TableRow bfRow; /** File extensions for this format */ private List extensions; /** * Class constructor for creating a BitstreamFormat object based on the * contents of a DB table row. * * @param context * the context this object exists in * @param row * the corresponding row in the table * @throws SQLException */ BitstreamFormat(Context context, TableRow row) throws SQLException { bfContext = context; bfRow = row; extensions = new ArrayList(); TableRowIterator tri = DatabaseManager.query(context, "SELECT * FROM fileextension WHERE bitstream_format_id=" + getID()); while (tri.hasNext()) { extensions.add(tri.next().getStringColumn("extension")); } // close the TableRowIterator to free up resources tri.close(); // Cache ourselves context.cache(this, row.getIntColumn("bitstream_format_id")); } /** * Get a bitstream format from the database. * * @param context * DSpace context object * @param id * ID of the bitstream format * * @return the bitstream format, or null if the ID is invalid. * @throws SQLException */ public static BitstreamFormat find(Context context, int id) throws SQLException { // First check the cache BitstreamFormat fromCache = (BitstreamFormat) context.fromCache( BitstreamFormat.class, id); if (fromCache != null) { return fromCache; } TableRow row = DatabaseManager.find(context, "bitstreamformatregistry", id); if (row == null) { if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_bitstream_format", "not_found,bitstream_format_id=" + id)); } return null; } // not null, return format object if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_bitstream", "bitstream_format_id=" + id)); } return new BitstreamFormat(context, row); } /** * Find a bitstream format by its (unique) MIME type. * If more than one bitstream format has the same MIME type, the * one returned is unpredictable. * * @param context * DSpace context object * @param mimeType * MIME type value * * @return the corresponding bitstream format, or <code>null</code> if * there's no bitstream format with the given MIMEtype. * @throws SQLException */ public static BitstreamFormat findByMIMEType(Context context, String mimeType) throws SQLException { // NOTE: Avoid internal formats since e.g. "License" also has // a MIMEtype of text/plain. TableRow formatRow = DatabaseManager.querySingle(context, "SELECT * FROM bitstreamformatregistry "+ "WHERE mimetype LIKE '"+mimeType+"' AND internal = '0';"); if (formatRow == null) return null; return findByFinish(context, formatRow); } /** * Find a bitstream format by its (unique) short description * * @param context * DSpace context object * @param desc * the short description * * @return the corresponding bitstream format, or <code>null</code> if * there's no bitstream format with the given short description * @throws SQLException */ public static BitstreamFormat findByShortDescription(Context context, String desc) throws SQLException { TableRow formatRow = DatabaseManager.findByUnique(context, "bitstreamformatregistry", "short_description", desc); if (formatRow == null) { return null; } return findByFinish(context, formatRow); } // shared final logic in findBy... methods; // use context's cache for object mapped from table row. private static BitstreamFormat findByFinish(Context context, TableRow formatRow) throws SQLException { // not null if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_bitstream", "bitstream_format_id=" + formatRow.getIntColumn("bitstream_format_id"))); } // From cache? BitstreamFormat fromCache = (BitstreamFormat) context.fromCache( BitstreamFormat.class, formatRow .getIntColumn("bitstream_format_id")); if (fromCache != null) { return fromCache; } return new BitstreamFormat(context, formatRow); } /** * Get the generic "unknown" bitstream format. * * @param context * DSpace context object * * @return the "unknown" bitstream format. * @throws SQLException * * @throws IllegalStateException * if the "unknown" bitstream format couldn't be found */ public static BitstreamFormat findUnknown(Context context) throws SQLException { BitstreamFormat bf = findByShortDescription(context, "Unknown"); if (bf == null) { throw new IllegalStateException( "No `Unknown' bitstream format in registry"); } return bf; } /** * Retrieve all bitstream formats from the registry, ordered by ID * * @param context * DSpace context object * * @return the bitstream formats. * @throws SQLException */ public static BitstreamFormat[] findAll(Context context) throws SQLException { List formats = new ArrayList(); TableRowIterator tri = DatabaseManager .query(context, "bitstreamformatregistry", "SELECT * FROM bitstreamformatregistry ORDER BY bitstream_format_id"); while (tri.hasNext()) { TableRow row = tri.next(); // From cache? BitstreamFormat fromCache = (BitstreamFormat) context.fromCache( BitstreamFormat.class, row .getIntColumn("bitstream_format_id")); if (fromCache != null) { formats.add(fromCache); } else { formats.add(new BitstreamFormat(context, row)); } } // close the TableRowIterator to free up resources tri.close(); // Return the formats as an array BitstreamFormat[] formatArray = new BitstreamFormat[formats.size()]; formatArray = (BitstreamFormat[]) formats.toArray(formatArray); return formatArray; } /** * Retrieve all non-internal bitstream formats from the registry. The * "unknown" format is not included, and the formats are ordered by support * level (highest first) first then short description.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -