#300: custom metadata fields: API read

This commit is contained in:
Benjamin Gamard 2019-05-20 15:08:16 +02:00
parent 5fd4d37972
commit 2db263fb68
5 changed files with 155 additions and 10 deletions

View File

@ -43,7 +43,7 @@ public class DocumentMetadata implements Serializable {
/**
* Value.
*/
@Column(name = "DME_VALUE_C", nullable = false, length = 4000)
@Column(name = "DME_VALUE_C", length = 4000)
private String value;
public String getId() {

View File

@ -8,7 +8,12 @@ import com.sismics.docs.core.dao.criteria.MetadataCriteria;
import com.sismics.docs.core.dao.dto.DocumentMetadataDto;
import com.sismics.docs.core.dao.dto.MetadataDto;
import com.sismics.docs.core.model.jpa.DocumentMetadata;
import com.sismics.docs.core.util.jpa.SortCriteria;
import com.sismics.util.JsonUtil;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
@ -144,4 +149,31 @@ public class MetadataUtil {
documentMetadata.setValue(value);
documentMetadataDao.update(documentMetadata);
}
/**
* Add custom metadata to a JSON response.
*
* @param json JSON
* @param documentId Document ID
*/
public static void addMetadata(JsonObjectBuilder json, String documentId) {
DocumentMetadataDao documentMetadataDao = new DocumentMetadataDao();
MetadataDao metadataDao = new MetadataDao();
List<MetadataDto> metadataDtoList = metadataDao.findByCriteria(new MetadataCriteria(), new SortCriteria(1, true));
List<DocumentMetadataDto> documentMetadataDtoList = documentMetadataDao.getByDocumentId(documentId);
JsonArrayBuilder metadata = Json.createArrayBuilder();
for (MetadataDto metadataDto : metadataDtoList) {
JsonObjectBuilder meta = Json.createObjectBuilder()
.add("id", metadataDto.getId())
.add("name", metadataDto.getName())
.add("type", metadataDto.getType().name());
for (DocumentMetadataDto documentMetadataDto : documentMetadataDtoList) {
if (documentMetadataDto.getMetadataId().equals(metadataDto.getId())) {
meta.add("value", JsonUtil.nullable(documentMetadataDto.getValue()));
}
}
metadata.add(meta);
}
json.add("metadata", metadata);
}
}

View File

@ -1,5 +1,5 @@
create cached table T_METADATA ( MET_ID_C varchar(36) not null, MET_NAME_C varchar(50) not null, MET_TYPE_C varchar(20) not null, MET_DELETEDATE_D datetime, primary key (MET_ID_C) );
create cached table T_DOCUMENT_METADATA ( DME_ID_C varchar(36) not null, DME_IDDOCUMENT_C varchar(36) not null, DME_IDMETADATA_C varchar(36) not null, DME_VALUE_C varchar(4000) not null, primary key (DME_ID_C) );
create cached table T_DOCUMENT_METADATA ( DME_ID_C varchar(36) not null, DME_IDDOCUMENT_C varchar(36) not null, DME_IDMETADATA_C varchar(36) not null, DME_VALUE_C varchar(4000) null, primary key (DME_ID_C) );
alter table T_DOCUMENT_METADATA add constraint FK_DME_IDDOCUMENT_C foreign key (DME_IDDOCUMENT_C) references T_DOCUMENT (DOC_ID_C) on delete restrict on update restrict;
alter table T_DOCUMENT_METADATA add constraint FK_DME_IDMETADATA_C foreign key (DME_IDMETADATA_C) references T_METADATA (MET_ID_C) on delete restrict on update restrict;
update T_CONFIG set CFG_VALUE_C = '24' where CFG_ID_C = 'DB_VERSION';

View File

@ -234,7 +234,10 @@ public class DocumentResource extends BaseResource {
step.add("transitionable", getTargetIdList(null).contains(routeStepDto.getTargetId()));
document.add("route_step", step);
}
// Add custom metadata
MetadataUtil.addMetadata(document, documentId);
return Response.ok().entity(document.build()).build();
}

View File

@ -838,31 +838,31 @@ public class TestDocumentResource extends BaseJerseyTest {
JsonObject json = target().path("/metadata").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.put(Entity.form(new Form()
.param("name", "str")
.param("name", "0str")
.param("type", "STRING")), JsonObject.class);
String metadataStrId = json.getString("id");
json = target().path("/metadata").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.put(Entity.form(new Form()
.param("name", "int")
.param("name", "1int")
.param("type", "INTEGER")), JsonObject.class);
String metadataIntId = json.getString("id");
json = target().path("/metadata").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.put(Entity.form(new Form()
.param("name", "float")
.param("name", "2float")
.param("type", "FLOAT")), JsonObject.class);
String metadataFloatId = json.getString("id");
json = target().path("/metadata").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.put(Entity.form(new Form()
.param("name", "date")
.param("name", "3date")
.param("type", "DATE")), JsonObject.class);
String metadataDateId = json.getString("id");
json = target().path("/metadata").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.put(Entity.form(new Form()
.param("name", "bool")
.param("name", "4bool")
.param("type", "BOOLEAN")), JsonObject.class);
String metadataBoolId = json.getString("id");
@ -880,7 +880,40 @@ public class TestDocumentResource extends BaseJerseyTest {
.param("metadata_value", "12.4")), JsonObject.class);
String document1Id = json.getString("id");
// Update the document with metadata1
// Check the values
json = target().path("/document/" + document1Id).request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, metadata1Token)
.get(JsonObject.class);
JsonArray metadata = json.getJsonArray("metadata");
Assert.assertEquals(5, metadata.size());
JsonObject meta = metadata.getJsonObject(0);
Assert.assertEquals(metadataStrId, meta.getString("id"));
Assert.assertEquals("0str", meta.getString("name"));
Assert.assertEquals("STRING", meta.getString("type"));
Assert.assertEquals("my string", meta.getString("value"));
meta = metadata.getJsonObject(1);
Assert.assertEquals(metadataIntId, meta.getString("id"));
Assert.assertEquals("1int", meta.getString("name"));
Assert.assertEquals("INTEGER", meta.getString("type"));
Assert.assertEquals("50", meta.getString("value"));
meta = metadata.getJsonObject(2);
Assert.assertEquals(metadataFloatId, meta.getString("id"));
Assert.assertEquals("2float", meta.getString("name"));
Assert.assertEquals("FLOAT", meta.getString("type"));
Assert.assertEquals("12.4", meta.getString("value"));
meta = metadata.getJsonObject(3);
Assert.assertEquals(metadataDateId, meta.getString("id"));
Assert.assertEquals("3date", meta.getString("name"));
Assert.assertEquals("DATE", meta.getString("type"));
Assert.assertFalse(meta.containsKey("value"));
meta = metadata.getJsonObject(4);
Assert.assertEquals(metadataBoolId, meta.getString("id"));
Assert.assertEquals("4bool", meta.getString("name"));
Assert.assertEquals("BOOLEAN", meta.getString("type"));
Assert.assertFalse(meta.containsKey("value"));
// Update the document with metadata1 (add more metadata)
String dateValue = Long.toString(new Date().getTime());
target().path("/document/" + document1Id).request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, metadata1Token)
.post(Entity.form(new Form()
@ -894,7 +927,84 @@ public class TestDocumentResource extends BaseJerseyTest {
.param("metadata_value", "my string 2")
.param("metadata_value", "52")
.param("metadata_value", "14.4")
.param("metadata_value", Long.toString(new Date().getTime()))
.param("metadata_value", dateValue)
.param("metadata_value", "true")), JsonObject.class);
// Check the values
json = target().path("/document/" + document1Id).request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, metadata1Token)
.get(JsonObject.class);
metadata = json.getJsonArray("metadata");
Assert.assertEquals(5, metadata.size());
meta = metadata.getJsonObject(0);
Assert.assertEquals(metadataStrId, meta.getString("id"));
Assert.assertEquals("0str", meta.getString("name"));
Assert.assertEquals("STRING", meta.getString("type"));
Assert.assertEquals("my string 2", meta.getString("value"));
meta = metadata.getJsonObject(1);
Assert.assertEquals(metadataIntId, meta.getString("id"));
Assert.assertEquals("1int", meta.getString("name"));
Assert.assertEquals("INTEGER", meta.getString("type"));
Assert.assertEquals("52", meta.getString("value"));
meta = metadata.getJsonObject(2);
Assert.assertEquals(metadataFloatId, meta.getString("id"));
Assert.assertEquals("2float", meta.getString("name"));
Assert.assertEquals("FLOAT", meta.getString("type"));
Assert.assertEquals("14.4", meta.getString("value"));
meta = metadata.getJsonObject(3);
Assert.assertEquals(metadataDateId, meta.getString("id"));
Assert.assertEquals("3date", meta.getString("name"));
Assert.assertEquals("DATE", meta.getString("type"));
Assert.assertEquals(dateValue, meta.getString("value"));
meta = metadata.getJsonObject(4);
Assert.assertEquals(metadataBoolId, meta.getString("id"));
Assert.assertEquals("4bool", meta.getString("name"));
Assert.assertEquals("BOOLEAN", meta.getString("type"));
Assert.assertEquals("true", meta.getString("value"));
// Update the document with metadata1 (remove some metadata)
target().path("/document/" + document1Id).request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, metadata1Token)
.post(Entity.form(new Form()
.param("title", "Metadata 1")
.param("language", "eng")
.param("metadata_id", metadataFloatId)
.param("metadata_id", metadataDateId)
.param("metadata_id", metadataBoolId)
.param("metadata_value", "14.4")
.param("metadata_value", dateValue)
.param("metadata_value", "true")), JsonObject.class);
// Check the values
json = target().path("/document/" + document1Id).request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, metadata1Token)
.get(JsonObject.class);
metadata = json.getJsonArray("metadata");
Assert.assertEquals(5, metadata.size());
meta = metadata.getJsonObject(0);
Assert.assertEquals(metadataStrId, meta.getString("id"));
Assert.assertEquals("0str", meta.getString("name"));
Assert.assertEquals("STRING", meta.getString("type"));
Assert.assertTrue(meta.isNull("value"));
meta = metadata.getJsonObject(1);
Assert.assertEquals(metadataIntId, meta.getString("id"));
Assert.assertEquals("1int", meta.getString("name"));
Assert.assertEquals("INTEGER", meta.getString("type"));
Assert.assertTrue(meta.isNull("value"));
meta = metadata.getJsonObject(2);
Assert.assertEquals(metadataFloatId, meta.getString("id"));
Assert.assertEquals("2float", meta.getString("name"));
Assert.assertEquals("FLOAT", meta.getString("type"));
Assert.assertEquals("14.4", meta.getString("value"));
meta = metadata.getJsonObject(3);
Assert.assertEquals(metadataDateId, meta.getString("id"));
Assert.assertEquals("3date", meta.getString("name"));
Assert.assertEquals("DATE", meta.getString("type"));
Assert.assertEquals(dateValue, meta.getString("value"));
meta = metadata.getJsonObject(4);
Assert.assertEquals(metadataBoolId, meta.getString("id"));
Assert.assertEquals("4bool", meta.getString("name"));
Assert.assertEquals("BOOLEAN", meta.getString("type"));
Assert.assertEquals("true", meta.getString("value"));
}
}