Create metadata on file with template
I’m developing a app integration with Box. I’ve use Box Java SDK. But I have a problem when use Template.
I create a Template “TemplateTest” with fields “String | enum | Date”.
MetadataTemplate.Field titleField = new MetadataTemplate.Field(); titleField.setType("string"); titleField.setKey("title"); titleField.setDisplayName(“Title"); MetadataTemplate.Field authorField = new MetadataTemplate.Field(); authorField.setType("string"); authorField.setKey("author"); authorField.setDisplayName("Author"); MetadataTemplate.Field subjectField = new MetadataTemplate.Field(); subjectField.setType("string"); subjectField.setKey("subject"); subjectField.setDisplayName("Subject"); MetadataTemplate.Field amountField = new MetadataTemplate.Field(); amountField.setType("float"); amountField.setKey("amount"); amountField.setDisplayName("Amount"); MetadataTemplate.Field publishDateField = new MetadataTemplate.Field(); publishDateField.setType("date"); publishDateField.setKey("publishDate"); publishDateField.setDisplayName("PublishDate"); MetadataTemplate.Field categoryField = new MetadataTemplate.Field(); categoryField.setType("enum"); categoryField.setKey("category"); categoryField.setDisplayName("Category"); List options = new ArrayList(); options.add("novel"); options.add("comics"); options.add("science"); options.add("education"); categoryField.setOptions(options); List fields = new ArrayList(); fields.add(titleField); fields.add(authorField); fields.add(subjectField); fields.add(amountField); fields.add(publishDateField); fields.add(categoryField); try { MetadataTemplate template = MetadataTemplate.createMetadataTemplate(api, "enterprise", "TemplateTest", "Template Test", false, fields); } catch (BoxAPIException apiEx) { //Delete MetadataTemplate is yet to be supported. Due to that template might be existing already. //This expects the conflict error. Assert.assertEquals(apiEx.getResponseCode(), 409); Assert.assertTrue(apiEx.getResponse().contains("Template key already exists in this scope")); } MetadataTemplate storedTemplate = MetadataTemplate.getMetadataTemplate(api, "documentFlow03"); Assert.assertNotNull(storedTemplate); final JsonObject jsonObject = new JsonObject(); jsonObject.add("/title", "title"); jsonObject.add("/author", "author"); jsonObject.add("/amount", 10); Metadata customerMetaData = new Metadata(jsonObject); uploadedFile.createMetadata("TemplateTest", customerMetaData);It throws BoxException with responseCode 400 when creating Metadata with type Number and Date. Pls tell me how to resolve this problem if you have example about it, pls share with me.
-
Metadata customerMetaData = new Metadata(jsonObject);
The syntax above should not work unless you change the Metadata (JsonObject values) constructor to public.
My original code was
Metadata boxMetadata = new Metadata()
//For String
boxMetadata.add("/author", "Author");
//For Date
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
SimpleDateFormat outSdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date tempDate = sdf.parse("04/13/2017");
boxMetadata.add("/datePrinted", outSdf.format(tempDate));Metadata.Add() only supports String value and it will only work for String and Date fields but not Number/Float field. To accomodate for Number/Float field, I have to use the code below.
You don't need the "/" if you are passing jsonObject to the Metadata() constructor. Try this
final JsonObject jsonObject = new JsonObject(); //For String
jsonObject.add("title", "title"); jsonObject.add("author", "author");
//For Number
Float tempNumber =Float.parseFloat("10");
jsonObject.add("amount", tempNumber);
//For Date
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
SimpleDateFormat outSdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date tempDate = sdf.parse("04/13/2017");
jsonObject.add("datePrinted", outSdf.format(tempDate));
Metadata customerMetaData = new Metadata(jsonObject);
Please sign in to leave a comment.
Comments
2 comments