Search code examples
javadspace

Getting a modified version of ItemRequestForm.java to work in DSpace version 6x


I have a modified version of ItemRequestForm.java that previously worked in version 5x. In item-view.xsl, I created a link that when clicked, will redirect the user to this modified form. The URL pattern of this link is http://example.com/documentdelivery/123456789/1234. When I upgrade my DSpace version to 6x, I have difficulty in making it work. Due to major code refactoring between versions 5 and 6, I find it hard to migrate my code to the latest version.

Below is part of the code that worked in version 5x (DocumentDeliveryForm.java)

The code is mostly based on this answer: How can I get the title of the referring page (item) from a modified version of feedback page in DSpace?

    String handle=parameters.getParameter("handle","unknown");
    DSpaceObject dso = HandleManager.resolveToObject(context, handle);
    if (!(dso instanceof Item)) {
        return;
    }
    Request request = ObjectModelHelper.getRequest(objectModel);
    boolean firstVisit=Boolean.valueOf(request.getParameter("firstVisit"));

    Item item = (Item) dso;

    // Build the item viewer division.
    Division documentdelivery = body.addInteractiveDivision("DocumentDelivery-form",
            contextPath+"/documentdelivery/"+parameters.getParameter("handle","unknown"),Division.METHOD_POST,"primary");

When I upgrade to version 6, I found out that DSpaceObject dso = HandleManager.resolveToObject(context, handle) no longer worked, so I replaced it with DSpaceObject dso = handleService.resolveToObject(context, handle).

Below is my attempt to migrate my 5x code to 6x (Result: java.lang.NullPointerException)

    String handle=parameters.getParameter("handle","unknown");
    DSpaceObject dso = handleService.resolveToObject(context, handle);
    if (!(dso instanceof Item)) {
        return;
    }
    Request request = ObjectModelHelper.getRequest(objectModel);
    Item item = (Item) dso;

    // Build the item viewer division.
    Division documentdelivery = body.addInteractiveDivision("DocumentDelivery-form",
            request.getRequestURI() + "/documentdelivery/" + item.getHandle(), Division.METHOD_POST,"primary");

Below is another attempt that resulted in Handle is null

    Request request = ObjectModelHelper.getRequest(objectModel);
    String handle = request.getParameter("handle");
    DSpaceObject dso = handleService.resolveToObject(context, handle);
    if (!(dso instanceof Item)) {
        return;
    }
    boolean firstVisit=Boolean.valueOf(request.getParameter("firstVisit"));

    Item item = (Item) dso;

    // Build the item viewer division.
    Division documentdelivery = body.addInteractiveDivision("DocumentDelivery-form",
            contextPath+"/documentdelivery/"+parameters.getParameter("handle","unknown"),Division.METHOD_POST,"primary");

Looking at the java stacktrace, it is pointing to this line of code: DSpaceObject dso = handleService.resolveToObject(context, handle). It seems that the value for handle is not being loaded.

What part of my code should I modify so that I can successfully redirect users to http://example.com/documentdelivery/123456789/1234 from http://example.com/handle/123456789/1234?

Which construction of item viewer division is correct?

    Division documentdelivery = body.addInteractiveDivision("DocumentDelivery-form",
            request.getRequestURI() + "/documentdelivery/" + item.getHandle(), Division.METHOD_POST,"primary");

OR

    Division documentdelivery = body.addInteractiveDivision("DocumentDelivery-form",
            contextPath+"/documentdelivery/"+parameters.getParameter("handle","unknown"),Division.METHOD_POST,"primary");

Thanks in advance.


Solution

  • Finally, I managed to get it to work. I also displayed other metadata fields based on my previous post here: Getting other metadata in ItemRequestForm in DSpace 6x

    public void addBody(Body body) throws SAXException, WingException,
            UIException, SQLException, IOException, AuthorizeException
    {
        Request request = ObjectModelHelper.getRequest(objectModel);
    
        // Build the item viewer division.
        Division documentdelivery = body.addInteractiveDivision("documentdelivery-form",
                contextPath+"/documentdelivery/"+parameters.getParameter("handle", "unknown"),Division.METHOD_POST,"primary");
    
        documentdelivery.setHead(T_head);
    
        String handle = parameters.getParameter("handle","unknown");
        DSpaceObject dso = handleService.resolveToObject(context, handle);
        Item item = (Item) dso;
    
        String citationDC = itemService.getMetadataFirstValue(item, "dc", "identifier", "citation", org.dspace.content.Item.ANY);
        String titleDC = item.getName();
        String title = "";
        if (citationDC != null && citationDC.length() > 0) {
            title = citationDC;
        } else {
            if (titleDC != null && titleDC.length() > 0)
                title = titleDC;
        }
        documentdelivery.addPara(title);
    

    I also added the necessary imports:

    import org.dspace.content.service.ItemService;
    import org.dspace.handle.factory.HandleServiceFactory;
    import org.dspace.handle.service.HandleService;
    

    And also I added these:

        private final transient ItemService itemService = ContentServiceFactory.getInstance().getItemService();
        private final transient HandleService handleService = HandleServiceFactory.getInstance().getHandleService();