- edited description
Renaming documents with the RestClient fails
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)
-
reporter -
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.
-
- changed status to open
-
- changed status to resolved
setting document user name to the loggedin user iff no user name is provided within the document fixes
#1951→ <<cset 16c254ec4e23>>
-
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.)
-
- changed version to 2.0.44
-
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!?
- Log in to comment