19 Iunii 2006

DSpace item title hack

In honor of the DSpace 1.4 beta release today… Ever tried to Google something that you know is in a DSpace repository?

Yeah. I’m rolling my eyes too. The title that you get in your Google results reads: “DSpace: Item 1492/37.” Which tells you precisely nothing.

Fortunately, fixing this is an easy hack (now that I know how; it took me an hour and six Internal Server Errors to figure it out for myself, because my Java is just that bad). Pull up display-item.jsp. (The copy in your local folder, because you’re too smart to edit the default JSPs directly, right?)

Add the following line to the declarations near the top:

<%@ page import="org.dspace.content.DCValue" %>

Delete or comment out the following:

    String title = "";
    if (handle != null)
    {
        title = "Item " + handle;
    }
    else
    {
        title = "Workspace Item";
    }

Replace it with the following two lines:

DCValue[] titleValue = item.getDC(”title”, null, item.ANY);
String title = titleValue[0].value;

    DCValue[] titleValue = item.getDC(”title”, null, item.ANY);
    String title = “”;
    if (titleValue.length != 0)
    {
    	title = titleValue[0].value;
    }
    else
    {
    	title = “Workspace Item”;
    }

This relies on DSpace requiring a title for every item, which out-of-the-box DSpace does. If you’ve hacked your DSpace not to require a title, you should probably tell DSpace to fall back to the handle if the title is null.

I’ve got this running on my test server, and it’ll migrate to the production server shortly.

ETA: Bug, which the above non-deleted code fixes. A workspace item for which a title had not yet been established was causing Internal Server Errors. Shows what you get if you trust me to hack DSpace—though I will say in my own defense that this hack didn’t make it to the production server until I’d found and fixed the bug.