/*
* ItemViewer.java
*
* Version: $Revision: 3705 $
*
* Date: $Date: 2009-04-11 18:02:24 +0100 (Sat, 11 Apr 2009) $
*
* Copyright (c) 2002, 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.app.xmlui.aspect.artifactbrowser;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import org.apache.cocoon.caching.CacheableProcessingComponent;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.util.HashUtil;
import org.apache.excalibur.source.SourceValidity;
import org.apache.log4j.Logger;
import org.dspace.app.xmlui.cocoon.AbstractDSpaceTransformer;
import org.dspace.app.xmlui.utils.DSpaceValidity;
import org.dspace.app.xmlui.utils.HandleUtil;
import org.dspace.app.xmlui.utils.UIException;
import org.dspace.app.xmlui.utils.UsageEvent;
import org.dspace.app.xmlui.wing.Message;
import org.dspace.app.xmlui.wing.WingException;
import org.dspace.app.xmlui.wing.element.Body;
import org.dspace.app.xmlui.wing.element.Division;
import org.dspace.app.xmlui.wing.element.ReferenceSet;
import org.dspace.app.xmlui.wing.element.PageMeta;
import org.dspace.app.xmlui.wing.element.Para;
import org.dspace.app.xmlui.wing.element.Xref;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.Collection;
import org.dspace.content.DCValue;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.crosswalk.CrosswalkException;
import org.dspace.content.crosswalk.DisseminationCrosswalk;
import org.dspace.core.Constants;
import org.dspace.core.LogManager;
import org.dspace.core.PluginManager;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.Group;
import org.dspace.handle.HandleManager;
import org.dspace.storage.rdbms.DatabaseManager;
import org.dspace.storage.rdbms.TableRow;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.xml.sax.SAXException;
import uk.ac.jorum.licence.ItemLicence;
import uk.ac.jorum.licence.LicenceController;
/**
* Display a single item.
*
* @author Scott Phillips
*/
public class ItemViewer extends AbstractDSpaceTransformer implements CacheableProcessingComponent
{
private static final Logger log = Logger.getLogger(ItemViewer.class);
/** Language strings */
private static final Message T_dspace_home =
message("xmlui.general.dspace_home");
private static final Message T_trail =
message("xmlui.ArtifactBrowser.ItemViewer.trail");
private static final Message T_show_simple =
message("xmlui.ArtifactBrowser.ItemViewer.show_simple");
private static final Message T_show_full =
message("xmlui.ArtifactBrowser.ItemViewer.show_full");
private static final Message T_head_parent_collections =
message("xmlui.ArtifactBrowser.ItemViewer.head_parent_collections");
/** Cached validity object */
private SourceValidity validity = null;
/** XHTML crosswalk instance */
private DisseminationCrosswalk xHTMLHeadCrosswalk = null;
/**
* Generate the unique caching key.
* This key must be unique inside the space of this component.
*/
public Serializable getKey() {
try {
DSpaceObject dso = HandleUtil.obtainHandle(objectModel);
if (dso == null)
return "0"; // no item, something is wrong.
return HashUtil.hash(dso.getHandle() + "full:" + showFullItem(objectModel));
}
catch (SQLException sqle)
{
// Ignore all errors and just return that the component is not cachable.
return "0";
}
}
/**
* Generate the cache validity object.
*
* The validity object will include the item being viewed,
* along with all bundles & bitstreams.
*/
public SourceValidity getValidity()
{
DSpaceObject dso = null;
if (this.validity == null)
{
try {
dso = HandleUtil.obtainHandle(objectModel);
DSpaceValidity validity = new DSpaceValidity();
validity.add(dso);
this.validity = validity.complete();
}
catch (Exception e)
{
// Ignore all errors and just invalidate the cache.
}
// add log message that we are viewing the item
// done here, as the serialization may not occur if the cache is valid
log.info(LogManager.getHeader(context, "view_item", "handle=" + (dso == null ? "" : dso.getHandle())));
}
return this.validity;
}
/**
* Add the item's title and trail links to the page's metadata.
*/
public void addPageMeta(PageMeta pageMeta) throws SAXException,
WingException, UIException, SQLException, IOException,
AuthorizeException
{
DSpaceObject dso = HandleUtil.obtainHandle(objectModel);
if (!(dso instanceof Item))
return;
Item item = (Item) dso;
// START GWaller 13/9/10 IssueID #303 Support for multiple licence options
// Check to see if the current user is authorised to view the item
EPerson currUser = this.eperson;
boolean authorised = false;
ItemLicence itemLicence = LicenceController.getItemLicence(item);
if (itemLicence != null){
Group[] authorisedGroups = itemLicence.authorisedGroupsForViewing();
if (authorisedGroups.length == 0){
authorised = true;
} else {
// Check to see if the current user belongs to one of the groups
for (Group g: authorisedGroups){
if (Group.isMember(this.context, g.getID())){
authorised = true;
break;
}
}
}
}
// Now add in a metadata element indicating if the user is authorised to view the item
pageMeta.addMetadata("viewAuthorised").addContent(Boolean.toString(authorised));
// END GWaller 13/9/10 IssueID #303 Support for multiple licence options
// Set the page title
String title = getItemTitle(item);
if (title != null)
pageMeta.addMetadata("title").addContent(title);
else
pageMeta.addMetadata("title").addContent(item.getHandle());
pageMeta.addTrailLink(contextPath + "/",T_dspace_home);
HandleUtil.buildHandleTrail(item,pageMeta,contextPath);
pageMeta.addTrail().addContent(T_trail);
// Metadata for <head> element
if (xHTMLHeadCrosswalk == null)
{
xHTMLHeadCrosswalk = (DisseminationCrosswalk) PluginManager.getNamedPlugin(
DisseminationCrosswalk.class, "XHTML_HEAD_ITEM");
}
// Produce <meta> elements for header from crosswalk
try
{
List l = xHTMLHeadCrosswalk.disseminateList(item);
StringWriter sw = new StringWriter();
XMLOutputter xmlo = new XMLOutputter();
for (int i = 0; i < l.size(); i++)
{
Element e = (Element) l.get(i);
// FIXME: we unset the Namespace so it's not printed.
// This is fairly yucky, but means the same crosswalk should
// work for Manakin as well as the JSP-based UI.
e.setNamespace(null);
xmlo.output(e, sw);
}
pageMeta.addMetadata("xhtml_head_item").addContent(sw.toString());
}
catch (CrosswalkException ce)
{
// TODO: Is this the right exception class?
throw new WingException(ce);
}
}
/**
* Display a single item
*/
public void addBody(Body body) throws SAXException, WingException,
UIException, SQLException, IOException, AuthorizeException
{
DSpaceObject dso = HandleUtil.obtainHandle(objectModel);
if (!(dso instanceof Item))
return;
Item item = (Item) dso;
int itemID = item.getID();
// Build the item viewer division.
Division division = body.addDivision("item-view","primary");
String title = getItemTitle(item);
if (title != null)
division.setHead(title);
else
division.setHead(item.getHandle());
Para showfullPara = division.addPara(null, "item-view-toggle item-view-toggle-top");
if (showFullItem(objectModel))
{
String link = contextPath + "/handle/" + item.getHandle();
showfullPara.addXref(link).addContent(T_show_simple);
}
else
{
String link = contextPath + "/handle/" + item.getHandle()
+ "?show=full";
showfullPara.addXref(link).addContent(T_show_full);
// Added by CG to log item view in stats database
// Note this event is only fired in simple item view, not full item view
new UsageEvent().fire((Request) ObjectModelHelper.getRequest(objectModel),
context, UsageEvent.VIEW, Constants.ITEM, itemID);
}
ReferenceSet referenceSet;
if (showFullItem(objectModel))
{
referenceSet = division.addReferenceSet("collection-viewer",
ReferenceSet.TYPE_DETAIL_VIEW);
}
else
{
referenceSet = division.addReferenceSet("collection-viewer",
ReferenceSet.TYPE_SUMMARY_VIEW);
}
// GWaller 1/10/09 Add XRefs for each of the related content packages (if any)
Division relatedCPDiv = division.addDivision("related_div");
Bundle[] bundles = item.getBundles(Constants.RELATED_CONTENT_PACKAGE_BUNDLE);
for (Bundle r : bundles){
// Get the bitstreams
Bitstream[] streams = r.getBitstreams();
for (Bitstream s:streams){
String link = contextPath + "/handle/" + s.getName();
// Lookup the item referenced and get the name for the link
DSpaceObject obj = HandleManager.resolveToObject(context, s.getName());
if (obj != null && obj instanceof Item){
Xref ref = relatedCPDiv.addPara(null,null).addXref(link);
ref.setRend("norender");
ref.addContent(((Item)obj).getName());
}
}
}
// Refrence the actual Item
ReferenceSet appearsInclude = referenceSet.addReference(item).addReferenceSet(ReferenceSet.TYPE_DETAIL_LIST,null,"hierarchy");
appearsInclude.setHead(T_head_parent_collections);
// Reference all collections the item appears in.
for (Collection collection : item.getCollections())
{
appearsInclude.addReference(collection);
}
showfullPara = division.addPara(null,"item-view-toggle item-view-toggle-bottom");
/***Added by CG ****/
//Adds cc licence name and location of appropriate cc licence icon to the dri
Division ccLicence = division.addDivision("cc_licence_div");
ccLicence.addHidden("cc_licence_name").setValue(LicenceController.getLicenseName(item));
ccLicence.addHidden("cc_licence_icon_location").setValue((LicenceController.getLicenseIconLocation(item)));
/***End addition by CG ****/
//We now need to query the stats database to see how many times this item has been viewed
String views = getViews(itemID);
//Add the results to the DRI
Division counts = division.addDivision("view_counts");
counts.addPara("This item has been viewed "+views + " times");
if (showFullItem(objectModel))
{
String link = contextPath + "/handle/" + item.getHandle();
showfullPara.addXref(link).addContent(T_show_simple);
}
else
{
String link = contextPath + "/handle/" + item.getHandle()
+ "?show=full";
showfullPara.addXref(link).addContent(T_show_full);
}
}
/**
* Gets count of how many times an item has been viewed
* @param itemID
* @return
* @throws SQLException
*/
private String getViews(int itemID) throws SQLException {
//Get views
String sql = "select count(*) from stats.view where item_id= ?";
TableRow row = DatabaseManager.querySingle(context, sql,itemID);
String views = row.getBigIntColumn("count");
return views;
}
/**
* Determine if the full item should be referenced or just a summary.
*/
public static boolean showFullItem(Map objectModel)
{
Request request = ObjectModelHelper.getRequest(objectModel);
String show = request.getParameter("show");
if (show != null && show.length() > 0)
return true;
return false;
}
/**
* Obtain the item's title.
*/
public static String getItemTitle(Item item)
{
DCValue[] titles = item.getDC("title", Item.ANY, Item.ANY);
String title;
if (titles != null && titles.length > 0)
title = titles[0].value;
else
title = null;
return title;
}
/**
* Recycle
*/
public void recycle() {
this.validity = null;
super.recycle();
}
}