Centralise Thumbnail logic, and improve DB efficiency in generating thumbnail links on item lists (in testing, at least 4x better)

git-svn-id: http://scm.dspace.org/svn/repo/branches/dspace-1_5_x@2470 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Graham Triggs
2008-01-02 09:30:40 +00:00
parent ada69cdad8
commit 5ed6765f73
8 changed files with 276 additions and 105 deletions

View File

@@ -33,7 +33,7 @@
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
package org.dspace.browse;
package org.dspace.content;
import org.dspace.content.Bitstream;

View File

@@ -0,0 +1,22 @@
package org.dspace.content.dao;
import org.dspace.core.Context;
import org.dspace.content.Bitstream;
import java.sql.SQLException;
public abstract class ItemDAO
{
protected Context context;
protected ItemDAO(Context ctx)
{
context = ctx;
}
public abstract Bitstream getPrimaryBitstream(int itemId, String bundleName) throws SQLException;
public abstract Bitstream getFirstBitstream(int itemId, String bundleName) throws SQLException;
public abstract Bitstream getNamedBitstream(int itemId, String bundleName, String fileName) throws SQLException;
}

View File

@@ -0,0 +1,24 @@
package org.dspace.content.dao;
import org.dspace.core.Context;
import org.dspace.core.ConfigurationManager;
/**
* Created by IntelliJ IDEA.
* User: Graham
* Date: 19-Dec-2007
* Time: 13:13:51
* To change this template use File | Settings | File Templates.
*/
public class ItemDAOFactory
{
public static ItemDAO getInstance(Context context)
{
if (ConfigurationManager.getProperty("db.name").equalsIgnoreCase("oracle"))
{
return new ItemDAOOracle(context);
}
return new ItemDAOPostgres(context);
}
}

View File

@@ -0,0 +1,26 @@
package org.dspace.content.dao;
import org.dspace.core.Context;
import org.dspace.content.Bitstream;
import java.sql.SQLException;
public class ItemDAOOracle extends ItemDAO
{
ItemDAOOracle(Context ctx)
{
super(ctx);
}
public Bitstream getPrimaryBitstream(int itemId, String bundleName) throws SQLException {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public Bitstream getFirstBitstream(int itemId, String bundleName) throws SQLException {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public Bitstream getNamedBitstream(int itemId, String bundleName, String fileName) throws SQLException {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}

View File

@@ -0,0 +1,116 @@
package org.dspace.content.dao;
import org.dspace.core.Context;
import org.dspace.content.Bitstream;
import org.dspace.storage.rdbms.DatabaseManager;
import org.dspace.storage.rdbms.TableRowIterator;
import org.dspace.storage.rdbms.TableRow;
import java.sql.SQLException;
public class ItemDAOPostgres extends ItemDAO
{
private final String selectPrimaryBitstreamID =
"SELECT bundle.primary_bitstream_id FROM item2bundle, bundle " +
"WHERE item2bundle.item_id=? AND item2bundle.bundle_id=bundle.bundle_id AND bundle.name=? LIMIT 1";
private final String selectFirstBitstreamID =
"SELECT bundle2bitstream.bitstream_id FROM item2bundle, bundle, bundle2bitstream " +
"WHERE item2bundle.item_id=? AND item2bundle.bundle_id=bundle.bundle_id AND bundle.name=? " +
"AND bundle.bundle_id=bundle2bitstream.bundle_id LIMIT 1";
// private final String selectFirstBitstreamID =
// "SELECT bitstream.bitstream_id FROM item2bundle, bundle, bundle2bitstream, bitstream " +
// "WHERE item2bundle.item_id=? AND item2bundle.bundle_id=bundle.bundle_id AND bundle.name=? " +
// "AND bundle.bundle_id=bundle2bitstream.bundle_id AND bundle2bitstream.bitstream_id=bitstream.bitstream_id " +
// " LIMIT 1";
// private final String selectFirstBitstreamID =
// "SELECT bitstream_id FROM (" +
// "SELECT bitstream.bitstream_id FROM item2bundle, bundle, bundle2bitstream, bitstream " +
// "WHERE item2bundle.item_id=? AND item2bundle.bundle_id=bundle.bundle_id AND bundle.name=? " +
// "AND bundle.bundle_id=bundle2bitstream.bundle_id AND bundle2bitstream.bitstream_id=bitstream.bitstream_id " +
// "ORDER BY bitstream.sequence_id" +
// ") allstreams LIMIT 1";
private final String selectNamedBitstreamID =
"SELECT bitstream.bitstream_id FROM item2bundle, bundle, bundle2bitstream, bitstream " +
"WHERE item2bundle.item_id=? AND item2bundle.bundle_id=bundle.bundle_id AND bundle.name=? " +
"AND bundle.bundle_id=bundle2bitstream.bundle_id AND bundle2bitstream.bitstream_id=bitstream.bitstream_id " +
"AND bitstream.name=?";
ItemDAOPostgres(Context ctx)
{
super(ctx);
}
public Bitstream getPrimaryBitstream(int itemId, String bundleName) throws SQLException
{
TableRowIterator tri = null;
try
{
tri = DatabaseManager.query(context, selectPrimaryBitstreamID, itemId, bundleName);
if (tri.hasNext())
{
TableRow row = tri.next();
int bid = row.getIntColumn("primary_bitstream_id");
return Bitstream.find(context, bid);
}
}
finally
{
if (tri != null)
tri.close();
}
return null;
}
public Bitstream getFirstBitstream(int itemId, String bundleName) throws SQLException
{
TableRowIterator tri = null;
try
{
tri = DatabaseManager.query(context, selectFirstBitstreamID, itemId, bundleName);
if (tri.hasNext())
{
TableRow row = tri.next();
int bid = row.getIntColumn("bitstream_id");
return Bitstream.find(context, bid);
}
}
finally
{
if (tri != null)
tri.close();
}
return null;
}
public Bitstream getNamedBitstream(int itemId, String bundleName, String fileName) throws SQLException
{
TableRowIterator tri = null;
try
{
tri = DatabaseManager.query(context, selectNamedBitstreamID, itemId, bundleName, fileName);
if (tri.hasNext())
{
TableRow row = tri.next();
int bid = row.getIntColumn("bitstream_id");
return Bitstream.find(context, bid);
}
}
finally
{
if (tri != null)
tri.close();
}
return null;
}
}

View File

@@ -0,0 +1,39 @@
package org.dspace.content.service;
import org.dspace.content.dao.ItemDAO;
import org.dspace.content.dao.ItemDAOFactory;
import org.dspace.content.Bitstream;
import org.dspace.content.Thumbnail;
import org.dspace.core.Context;
import java.sql.SQLException;
public class ItemService
{
public static Thumbnail getThumbnail(Context context, int itemId, boolean requireOriginal) throws SQLException
{
ItemDAO dao = ItemDAOFactory.getInstance(context);
Bitstream thumbBitstream = null;
Bitstream primaryBitstream = dao.getPrimaryBitstream(itemId, "ORIGINAL");
if (primaryBitstream != null)
{
if (primaryBitstream.getFormat().getMIMEType().equals("text/html"))
return null;
thumbBitstream = dao.getNamedBitstream(itemId, "THUMBNAIL", primaryBitstream.getName() + ".jpg");
}
else
{
if (requireOriginal)
primaryBitstream = dao.getFirstBitstream(itemId, "ORIGINAL");
thumbBitstream = dao.getFirstBitstream(itemId, "THUMBNAIL");
}
if (thumbBitstream != null)
return new Thumbnail(thumbBitstream, primaryBitstream);
return null;
}
}

View File

@@ -46,6 +46,8 @@ import org.dspace.content.Bitstream;
import org.dspace.content.DCDate;
import org.dspace.content.DCValue;
import org.dspace.content.Item;
import org.dspace.content.Thumbnail;
import org.dspace.content.service.ItemService;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Constants;
import org.dspace.core.Context;
@@ -743,9 +745,10 @@ public class BrowseListTag extends TagSupport
{
try
{
Thumbnail thumbnail = item.getThumbnail();
Context c = UIUtil.obtainContext(hrq);
Thumbnail thumbnail = ItemService.getThumbnail(c, item.getID(), linkToBitstream);
if (thumbnail == null)
if (thumbnail == null)
{
return "";
}
@@ -768,7 +771,13 @@ public class BrowseListTag extends TagSupport
String img = hrq.getContextPath() + "/retrieve/" + thumb.getID() + "/" +
UIUtil.encodeBitstreamName(thumb.getName(), Constants.DEFAULT_ENCODING);
String alt = thumb.getName();
thumbFrag.append("<img src=\"" + img + "\" alt=\"" + alt + "\" /></a>");
String scAttr = getScalingAttr(hrq, thumb);
thumbFrag.append("<img src=\"")
.append(img)
.append("\" alt=\"")
.append(alt + "\" ")
.append(scAttr)
.append("/></a>");
return thumbFrag.toString();
}

View File

@@ -48,6 +48,8 @@ import org.dspace.content.Bundle;
import org.dspace.content.DCDate;
import org.dspace.content.DCValue;
import org.dspace.content.Item;
import org.dspace.content.Thumbnail;
import org.dspace.content.service.ItemService;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Constants;
@@ -524,118 +526,51 @@ public class ItemListTag extends TagSupport
private String getThumbMarkup(HttpServletRequest hrq, Item item)
throws JspException
{
Bundle[] original = null;
try
{
original = item.getBundles("ORIGINAL");
}
catch(SQLException sqle)
{
throw new JspException(sqle.getMessage());
}
Context c = UIUtil.obtainContext(hrq);
Thumbnail thumbnail = ItemService.getThumbnail(c, item.getID(), linkToBitstream);
if (original.length == 0)
{
return "";
}
boolean html = false;
// if multiple bitstreams, check if the primary one is HTML
if (original[0].getBitstreams().length > 1)
{
Bitstream[] bitstreams = original[0].getBitstreams();
for (int i = 0; (i < bitstreams.length) && !html; i++)
if (thumbnail == null)
{
if (bitstreams[i].getID() == original[0]
.getPrimaryBitstreamID())
{
html = bitstreams[i].getFormat().getMIMEType().equals(
"text/html");
}
return "";
}
}
StringBuffer thumbFrag = new StringBuffer();
try
{
Bundle[] thumbs = item.getBundles("THUMBNAIL");
// if there are thumbs and we're not dealing with an HTML item
// then show the thumbnail
if ((thumbs.length > 0) && !html)
{
Context c = UIUtil.obtainContext(hrq);
Bitstream thumbnailBitstream;
Bitstream originalBitstream;
if ((original[0].getBitstreams().length > 1)
&& (original[0].getPrimaryBitstreamID() > -1))
{
originalBitstream = Bitstream.find(c, original[0]
.getPrimaryBitstreamID());
thumbnailBitstream = thumbs[0]
.getBitstreamByName(originalBitstream.getName()
+ ".jpg");
}
else
{
originalBitstream = original[0].getBitstreams()[0];
thumbnailBitstream = thumbs[0].getBitstreams()[0];
}
if ((thumbnailBitstream != null)
&& (AuthorizeManager.authorizeActionBoolean(c,
thumbnailBitstream, Constants.READ)))
{
StringBuffer thumbLink;
if (linkToBitstream)
{
thumbLink = new StringBuffer(
"<br/><a target=\"_blank\" href=\"").append(
hrq.getContextPath()).append("/bitstream/")
.append(item.getHandle()).append("/").append(
originalBitstream.getSequenceID())
.append("/").append(
UIUtil.encodeBitstreamName(originalBitstream
.getName(),
Constants.DEFAULT_ENCODING));
}
else
{
thumbLink = new StringBuffer("<br/><a href=\"").append(
hrq.getContextPath()).append("/handle/")
.append(item.getHandle());
}
thumbLink.append("\"><img src=\"").append(
hrq.getContextPath()).append("/retrieve/").append(
thumbnailBitstream.getID()).append("/").append(
UIUtil.encodeBitstreamName(thumbnailBitstream.getName(),
Constants.DEFAULT_ENCODING)).append(
"\" alt=\"").append(thumbnailBitstream.getName())
.append("\" ").append(
getScalingAttr(hrq, thumbnailBitstream))
.append("/></a>");
return thumbLink.toString();
}
if (linkToBitstream)
{
Bitstream original = thumbnail.getOriginal();
String link = hrq.getContextPath() + "/bitstream/" + item.getHandle() + "/" + original.getSequenceID() + "/" +
UIUtil.encodeBitstreamName(original.getName(), Constants.DEFAULT_ENCODING);
thumbFrag.append("<a target=\"_blank\" href=\"" + link + "\" />");
}
else
{
String link = hrq.getContextPath() + "/handle/" + item.getHandle();
thumbFrag.append("<a href=\"" + link + "\" />");
}
Bitstream thumb = thumbnail.getThumb();
String img = hrq.getContextPath() + "/retrieve/" + thumb.getID() + "/" +
UIUtil.encodeBitstreamName(thumb.getName(), Constants.DEFAULT_ENCODING);
String alt = thumb.getName();
String scAttr = getScalingAttr(hrq, thumb);
thumbFrag.append("<img src=\"")
.append(img)
.append("\" alt=\"")
.append(alt + "\" ")
.append(scAttr)
.append("/></a>");
return thumbFrag.toString();
}
catch (SQLException sqle)
{
throw new JspException(sqle.getMessage());
throw new JspException(sqle.getMessage());
}
catch (UnsupportedEncodingException e)
{
throw new JspException(
"Server does not support DSpace's default encoding. ",
e);
throw new JspException("Server does not support DSpace's default encoding. ", e);
}
return "";
}
}