Renaming documents with the RestClient fails

Issue #1951 resolved
Robert Jäschke created an issue

Running code like this:

import java.util.*;
import org.bibsonomy.common.enums.*;
import org.bibsonomy.model.*;
import org.bibsonomy.model.logic.*;
import org.bibsonomy.model.enums.*;
import org.bibsonomy.model.util.*;
import org.bibsonomy.rest.client.*;

class Test {

    public static void main(String args[]) {
        LogicInterface logic = new RestLogicFactory().getLogicAccess("jaeschke", "TODO");

        List<Post<BibTex>> posts = logic.getPosts(BibTex.class, GroupingEntity.USER, "jaeschke", null, null, null, null, Order.ADDED, null, null, 0, 1);

        for (Post<BibTex> post: posts) {
            BibTex publication = post.getResource();
            System.out.println(publication.getTitle());

            List<Document> documents = publication.getDocuments();

            // create a nice key for the publication                                                                                                         
            String key = BibTexUtils.generateBibtexKey(publication);

            for (Document document: documents) {
                System.out.println("  " + document.getFileName());
                String newFileName = key + "_" + document.getFileName();
                logic.updateDocument(document, publication.getIntraHash(), newFileName);
                System.out.println("  ... renamed to " + newFileName);
            }
        }
    }
}

results in

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
    at java.lang.Thread.run(Thread.java:679)
Caused by: java.lang.IllegalStateException: no user name given
    at org.bibsonomy.rest.client.queries.put.ChangeDocumentNameQuery.<init>(ChangeDocumentNameQuery.java:58)
    at org.bibsonomy.rest.client.RestLogic.updateDocument(RestLogic.java:702)
    at Test.main(Test.java:28)
    ... 6 more

Comments (7)

  1. Daniel Zoller

    You are not setting the user name in the document for the update (currently you are only allowed to change your own document, but maybe later you can also change the document names of e.g group posts). See Java Sample Section https://bitbucket.org/bibsonomy/bibsonomy/wiki/documentation/api/Java%20API%20Examples#markdown-header-renaming-a-document-of-a-post.

    Nevertheless, I updated the rest logic that it sets the username to the logged in user if no user name is specified within the document.

  2. Robert Jäschke reporter

    Did you see that I re-use the document I retrieved earlier from the API? Thus, I think a better solution would be to return documents which already contain the user name. Then the above code would work. We can still keep your fallback, although it is a side-effect that not everybody will be aware of.

    Finally, why do we provide the resourceHash as parameter to the method but not the user name? Wouldn't the cleanest method be, to provide the user name as well?

    (Finally2: I really had to think about "resourceHash" because unfortunately, the resource has no method "getResourceHash", only "getIntraHash" ... which is not so obvious for other users.)

  3. Daniel Zoller

    Yes. Maybe the username was not set to reduce redundant information, because the document owner of a post is the post owner. Another solution would be to set the username by the REST-model <-> Model converter without actually sending the information.

    +1 for cleaning up the method signature. Open a new issue!?

  4. Log in to comment