获取在DSpace版本6x中运行的ItemRequestForm.java修改版本。

huangapple go评论85阅读模式
英文:

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

问题

我有一个经过修改的 ItemRequestForm.java 版本,在 5x 版本中之前运行正常。在 item-view.xsl 中,我创建了一个链接,当用户点击时,会将用户重定向到这个修改过的表单。这个链接的 URL 模式是 http://example.com/documentdelivery/123456789/1234。当我将 DSpace 版本升级到 6x 时,我在使它正常工作方面遇到了困难。由于版本 5 和 6 之间进行了重大的代码重构,我发现很难将我的代码迁移到最新版本。

下面是在 5x 版本中工作的部分代码DocumentDeliveryForm.java

该代码主要基于这个答案: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");

当我升级到版本 6 时,我发现 DSpaceObject dso = HandleManager.resolveToObject(context, handle) 不再起作用,所以我用 DSpaceObject dso = handleService.resolveToObject(context, handle) 进行了替换。

下面是我尝试将我的 5x 代码迁移到 6x 的情况(结果: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");

下面是另一个尝试,结果是 Handle 为 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");

查看 Java 堆栈跟踪,它指向这行代码:DSpaceObject dso = handleService.resolveToObject(context, handle)。似乎 handle 的值没有被加载。

我应该修改代码的哪个部分,才能成功地将用户从 http://example.com/handle/123456789/1234 重定向到 http://example.com/documentdelivery/123456789/1234

哪种构建 item viewer division 的方式是正确的?

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

还是

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

提前感谢您的帮助。

英文:

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.

答案1

得分: 0

最终,我成功使它工作起来了。根据我之前在这里发布的帖子,我还显示了其他基于元数据字段:在 DSpace 6x 的 ItemRequestForm 中获取其他元数据

public void addBody(Body body) throws SAXException, WingException,
        UIException, SQLException, IOException, AuthorizeException
{
    Request request = ObjectModelHelper.getRequest(objectModel);

    // 构建项目查看器部分。
    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);
}

// 以下是添加的必要导入部分:

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

// 还添加了以下内容:

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

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();

huangapple
  • 本文由 发表于 2020年5月4日 12:58:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61585459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定