mirror of
https://github.com/sismics/docs.git
synced 2025-12-13 01:36:18 +00:00
#300: custom metadata fields: API write
This commit is contained in:
@@ -27,7 +27,6 @@ public class CommentDao {
|
||||
* @param comment Comment
|
||||
* @param userId User ID
|
||||
* @return New ID
|
||||
* @throws Exception
|
||||
*/
|
||||
public String create(Comment comment, String userId) {
|
||||
// Create the UUID
|
||||
@@ -99,7 +98,7 @@ public class CommentDao {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Object[]> l = q.getResultList();
|
||||
|
||||
List<CommentDto> commentDtoList = new ArrayList<CommentDto>();
|
||||
List<CommentDto> commentDtoList = new ArrayList<>();
|
||||
for (Object[] o : l) {
|
||||
int i = 0;
|
||||
CommentDto commentDto = new CommentDto();
|
||||
@@ -107,7 +106,7 @@ public class CommentDao {
|
||||
commentDto.setContent((String) o[i++]);
|
||||
commentDto.setCreateTimestamp(((Timestamp) o[i++]).getTime());
|
||||
commentDto.setCreatorName((String) o[i++]);
|
||||
commentDto.setCreatorEmail((String) o[i++]);
|
||||
commentDto.setCreatorEmail((String) o[i]);
|
||||
commentDtoList.add(commentDto);
|
||||
}
|
||||
return commentDtoList;
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.sismics.docs.core.dao;
|
||||
|
||||
import com.sismics.docs.core.constant.MetadataType;
|
||||
import com.sismics.docs.core.dao.dto.DocumentMetadataDto;
|
||||
import com.sismics.docs.core.model.jpa.DocumentMetadata;
|
||||
import com.sismics.util.context.ThreadLocalContext;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Document metadata DAO.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class DocumentMetadataDao {
|
||||
/**
|
||||
* Creates a new document metadata.
|
||||
*
|
||||
* @param documentMetadata Document metadata
|
||||
* @return New ID
|
||||
*/
|
||||
public String create(DocumentMetadata documentMetadata) {
|
||||
// Create the UUID
|
||||
documentMetadata.setId(UUID.randomUUID().toString());
|
||||
|
||||
// Create the document metadata
|
||||
EntityManager em = ThreadLocalContext.get().getEntityManager();
|
||||
em.persist(documentMetadata);
|
||||
|
||||
return documentMetadata.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a document metadata.
|
||||
*
|
||||
* @param documentMetadata Document metadata
|
||||
* @return Updated document metadata
|
||||
*/
|
||||
public DocumentMetadata update(DocumentMetadata documentMetadata) {
|
||||
EntityManager em = ThreadLocalContext.get().getEntityManager();
|
||||
|
||||
// Get the document metadata
|
||||
Query q = em.createQuery("select u from DocumentMetadata u where u.id = :id");
|
||||
q.setParameter("id", documentMetadata.getId());
|
||||
DocumentMetadata documentMetadataDb = (DocumentMetadata) q.getSingleResult();
|
||||
|
||||
// Update the document metadata
|
||||
documentMetadataDb.setValue(documentMetadata.getValue());
|
||||
|
||||
return documentMetadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of all metadata values on a document.
|
||||
*
|
||||
* @param documentId Document ID
|
||||
* @return List of metadata
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<DocumentMetadataDto> getByDocumentId(String documentId) {
|
||||
EntityManager em = ThreadLocalContext.get().getEntityManager();
|
||||
StringBuilder sb = new StringBuilder("select dm.DME_ID_C, dm.DME_IDDOCUMENT_C, dm.DME_IDMETADATA_C, dm.DME_VALUE_C, m.MET_TYPE_C");
|
||||
sb.append(" from T_DOCUMENT_METADATA dm, T_METADATA m ");
|
||||
sb.append(" where dm.DME_IDMETADATA_C = m.MET_ID_C and dm.DME_IDDOCUMENT_C = :documentId and m.MET_DELETEDATE_D is null");
|
||||
|
||||
// Perform the search
|
||||
Query q = em.createNativeQuery(sb.toString());
|
||||
q.setParameter("documentId", documentId);
|
||||
List<Object[]> l = q.getResultList();
|
||||
|
||||
// Assemble results
|
||||
List<DocumentMetadataDto> dtoList = new ArrayList<>();
|
||||
for (Object[] o : l) {
|
||||
int i = 0;
|
||||
DocumentMetadataDto dto = new DocumentMetadataDto();
|
||||
dto.setId((String) o[i++]);
|
||||
dto.setDocumentId((String) o[i++]);
|
||||
dto.setMetadataId((String) o[i++]);
|
||||
dto.setValue((String) o[i++]);
|
||||
dto.setType(MetadataType.valueOf((String) o[i]));
|
||||
dtoList.add(dto);
|
||||
}
|
||||
return dtoList;
|
||||
}
|
||||
}
|
||||
@@ -36,13 +36,13 @@ public class RelationDao {
|
||||
List<Object[]> l = q.getResultList();
|
||||
|
||||
// Assemble results
|
||||
List<RelationDto> relationDtoList = new ArrayList<RelationDto>();
|
||||
List<RelationDto> relationDtoList = new ArrayList<>();
|
||||
for (Object[] o : l) {
|
||||
int i = 0;
|
||||
RelationDto relationDto = new RelationDto();
|
||||
relationDto.setId((String) o[i++]);
|
||||
relationDto.setTitle((String) o[i++]);
|
||||
String fromDocId = (String) o[i++];
|
||||
String fromDocId = (String) o[i];
|
||||
relationDto.setSource(documentId.equals(fromDocId));
|
||||
relationDtoList.add(relationDto);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.sismics.docs.core.dao.dto;
|
||||
|
||||
import com.sismics.docs.core.constant.MetadataType;
|
||||
|
||||
/**
|
||||
* Document metadata DTO.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class DocumentMetadataDto {
|
||||
/**
|
||||
* Document metadata ID.
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* Document ID.
|
||||
*/
|
||||
private String documentId;
|
||||
|
||||
/**
|
||||
* Metadata ID.
|
||||
*/
|
||||
private String metadataId;
|
||||
|
||||
/**
|
||||
* Name.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Value.
|
||||
*/
|
||||
private String value;
|
||||
|
||||
/**
|
||||
* Type.
|
||||
*/
|
||||
private MetadataType type;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public DocumentMetadataDto setId(String id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public DocumentMetadataDto setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MetadataType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public DocumentMetadataDto setType(MetadataType type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getDocumentId() {
|
||||
return documentId;
|
||||
}
|
||||
|
||||
public DocumentMetadataDto setDocumentId(String documentId) {
|
||||
this.documentId = documentId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getMetadataId() {
|
||||
return metadataId;
|
||||
}
|
||||
|
||||
public DocumentMetadataDto setMetadataId(String metadataId) {
|
||||
this.metadataId = metadataId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public DocumentMetadataDto setValue(String value) {
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.sismics.docs.core.model.jpa;
|
||||
|
||||
import com.google.common.base.MoreObjects;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Link between a document and a metadata, holding the value.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "T_DOCUMENT_METADATA")
|
||||
public class DocumentMetadata implements Serializable {
|
||||
/**
|
||||
* Serial version UID.
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Document metadata ID.
|
||||
*/
|
||||
@Id
|
||||
@Column(name = "DME_ID_C", length = 36)
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* Document ID.
|
||||
*/
|
||||
@Column(name = "DME_IDDOCUMENT_C", nullable = false, length = 36)
|
||||
private String documentId;
|
||||
|
||||
/**
|
||||
* Metadata ID.
|
||||
*/
|
||||
@Column(name = "DME_IDMETADATA_C", nullable = false, length = 36)
|
||||
private String metadataId;
|
||||
|
||||
/**
|
||||
* Value.
|
||||
*/
|
||||
@Column(name = "DME_VALUE_C", nullable = false, length = 4000)
|
||||
private String value;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDocumentId() {
|
||||
return documentId;
|
||||
}
|
||||
|
||||
public void setDocumentId(String documentId) {
|
||||
this.documentId = documentId;
|
||||
}
|
||||
|
||||
public String getMetadataId() {
|
||||
return metadataId;
|
||||
}
|
||||
|
||||
public DocumentMetadata setMetadataId(String metadataId) {
|
||||
this.metadataId = metadataId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public DocumentMetadata setValue(String value) {
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return MoreObjects.toStringHelper(this)
|
||||
.add("id", id)
|
||||
.add("documentId", documentId)
|
||||
.add("metadataId", metadataId)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package com.sismics.docs.core.util;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.sismics.docs.core.constant.MetadataType;
|
||||
import com.sismics.docs.core.dao.DocumentMetadataDao;
|
||||
import com.sismics.docs.core.dao.MetadataDao;
|
||||
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 java.text.MessageFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Metadata utilities.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class MetadataUtil {
|
||||
/**
|
||||
* Update custom metadata on a document.
|
||||
*
|
||||
* @param documentId Document ID
|
||||
* @param metadataIdList Metadata ID list
|
||||
* @param metadataValueList Metadata value list
|
||||
*/
|
||||
public static void updateMetadata(String documentId, List<String> metadataIdList, List<String> metadataValueList) throws Exception {
|
||||
if (metadataIdList == null || metadataValueList == null || metadataIdList.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (metadataIdList.size() != metadataValueList.size()) {
|
||||
throw new Exception("metadata_id and metadata_value must have the same length");
|
||||
}
|
||||
|
||||
Map<String, String> newValues = Maps.newHashMap();
|
||||
for (int i = 0; i < metadataIdList.size(); i++) {
|
||||
newValues.put(metadataIdList.get(i), metadataValueList.get(i));
|
||||
}
|
||||
|
||||
MetadataDao metadataDao = new MetadataDao();
|
||||
DocumentMetadataDao documentMetadataDao = new DocumentMetadataDao();
|
||||
List<MetadataDto> metadataDtoList = metadataDao.findByCriteria(new MetadataCriteria(), null);
|
||||
List<DocumentMetadataDto> documentMetadataDtoList = documentMetadataDao.getByDocumentId(documentId);
|
||||
|
||||
// Update existing values
|
||||
for (DocumentMetadataDto documentMetadataDto : documentMetadataDtoList) {
|
||||
if (newValues.containsKey(documentMetadataDto.getMetadataId())) {
|
||||
// Update the value
|
||||
String value = newValues.get(documentMetadataDto.getMetadataId());
|
||||
validateValue(documentMetadataDto.getType(), value);
|
||||
updateValue(documentMetadataDto.getId(), value);
|
||||
newValues.remove(documentMetadataDto.getMetadataId());
|
||||
} else {
|
||||
// Remove the value
|
||||
updateValue(documentMetadataDto.getId(), null);
|
||||
}
|
||||
}
|
||||
|
||||
// Create new values
|
||||
for (Map.Entry<String, String> entry : newValues.entrySet()) {
|
||||
// Search the metadata definition
|
||||
MetadataDto metadata = null;
|
||||
for (MetadataDto metadataDto : metadataDtoList) {
|
||||
if (metadataDto.getId().equals(entry.getKey())) {
|
||||
metadata = metadataDto;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (metadata == null) {
|
||||
throw new Exception(MessageFormat.format("Metadata not found: {0}", entry.getKey()));
|
||||
}
|
||||
|
||||
// Add the value
|
||||
validateValue(metadata.getType(), entry.getValue());
|
||||
createValue(documentId, entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a custom metadata value.
|
||||
*
|
||||
* @param type Metadata type
|
||||
* @param value Value
|
||||
* @throws Exception In case of validation error
|
||||
*/
|
||||
private static void validateValue(MetadataType type, String value) throws Exception {
|
||||
switch (type) {
|
||||
case STRING:
|
||||
case BOOLEAN:
|
||||
return;
|
||||
case DATE:
|
||||
try {
|
||||
Long.parseLong(value);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new Exception("Date value not parsable as timestamp");
|
||||
}
|
||||
break;
|
||||
case FLOAT:
|
||||
try {
|
||||
Float.parseFloat(value);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new Exception("Float value not parsable");
|
||||
}
|
||||
break;
|
||||
case INTEGER:
|
||||
try {
|
||||
Integer.parseInt(value);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new Exception("Integer value not parsable");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a custom metadata value on a document.
|
||||
*
|
||||
* @param documentId Document ID
|
||||
* @param metadataId Metadata ID
|
||||
* @param value Value
|
||||
*/
|
||||
private static void createValue(String documentId, String metadataId, String value) {
|
||||
DocumentMetadataDao documentMetadataDao = new DocumentMetadataDao();
|
||||
DocumentMetadata documentMetadata = new DocumentMetadata();
|
||||
documentMetadata.setDocumentId(documentId);
|
||||
documentMetadata.setMetadataId(metadataId);
|
||||
documentMetadata.setValue(value);
|
||||
documentMetadataDao.create(documentMetadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a custom metadata value.
|
||||
*
|
||||
* @param documentMetadataId Document metadata ID
|
||||
* @param value Value
|
||||
*/
|
||||
private static void updateValue(String documentMetadataId, String value) {
|
||||
DocumentMetadataDao documentMetadataDao = new DocumentMetadataDao();
|
||||
DocumentMetadata documentMetadata = new DocumentMetadata();
|
||||
documentMetadata.setId(documentMetadataId);
|
||||
documentMetadata.setValue(value);
|
||||
documentMetadataDao.update(documentMetadata);
|
||||
}
|
||||
}
|
||||
@@ -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, DME_DELETEDATE_D datetime, 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) not 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';
|
||||
|
||||
Reference in New Issue
Block a user