Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit d2ab3bd5 authored by Joze RIHTARSIC's avatar Joze RIHTARSIC
Browse files

Add option for database column description

parent 79d3bf9e
No related branches found
No related tags found
No related merge requests found
package eu.europa.ec.edelivery.smp.data.dao.utils;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Anotation is just for addding column description. At generation DDL description is handled by hibernate
* It seems that hbm supports column description but annotations does not! - hibernate version 5.4.0.Final
*
*/
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ColumnDescription {
String comment() default "";
}
......@@ -4,17 +4,17 @@ import eu.europa.ec.edelivery.smp.logging.SMPLogger;
import eu.europa.ec.edelivery.smp.logging.SMPLoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.mapping.Column;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.lang.reflect.Field;
import java.util.*;
/**
* Class generates DDL script for SMP. Purpose of script is to manually run SQL script to create database. And to
......@@ -80,8 +80,16 @@ public class SMPSchemaGenerator {
metadata.addAnnotatedClass(clazz);
}
}
// build metadata implementor
MetadataImplementor metadataImplementor = (MetadataImplementor) metadata.buildMetadata();
// add column description
metadata.getAnnotatedClasses().forEach(clzz-> {
for(Field fld: clzz.getDeclaredFields()){
updateColumnComment(clzz.getName(), fld, metadataImplementor);
}
});
// create schema exporter
SchemaExport export = new SchemaExport();
File file = new File(exportFolder, filename);
......@@ -95,10 +103,20 @@ public class SMPSchemaGenerator {
//can change the output here
EnumSet<TargetType> enumSet = EnumSet.of(TargetType.SCRIPT);
export.execute(enumSet, SchemaExport.Action.CREATE, metadataImplementor);
}
private void updateColumnComment(String entityName, Field field, MetadataImplementor metadataImplementor){
javax.persistence.Column column = field.getAnnotation(javax.persistence.Column.class);
ColumnDescription columnDesc = field.getAnnotation(ColumnDescription.class);
if (column!=null && columnDesc!=null){
Column c = metadataImplementor.getEntityBinding(entityName).getTable().getColumn(Identifier.toIdentifier( column.name(), false ));
c.setComment(columnDesc.comment());
}
}
/**
* Method creates filename based on dialect and version
*
......
......@@ -12,6 +12,7 @@
*/
package eu.europa.ec.edelivery.smp.data.model;
import eu.europa.ec.edelivery.smp.data.dao.utils.ColumnDescription;
import org.hibernate.envers.Audited;
import javax.persistence.*;
......@@ -28,26 +29,35 @@ import java.util.Objects;
@Entity
@Audited
@Table(name = "SMP_CERTIFICATE")
@org.hibernate.annotations.Table(appliesTo = "SMP_CERTIFICATE", comment = "SMP user certificates")
public class DBCertificate extends BaseEntity {
@Id
@Column(name = "ID")
@ColumnDescription(comment = "Shared primary key with master table SMP_USER")
Long id;
@Column(name = "CERTIFICATE_ID", length = CommonColumnsLengths.MAX_MEDIUM_TEXT_LENGTH, unique = true)
@ColumnDescription(comment = "Formatted Certificate id using tags: cn, o, c:serialNumber")
private String certificateId;
@Column(name = "VALID_FROM")
@ColumnDescription(comment = "Certificate valid from date.")
private LocalDateTime validFrom;
@Column(name = "VALID_TO")
@ColumnDescription(comment = "Certificate valid to date.")
private LocalDateTime validTo;
@Column(name = "SUBJECT", length = CommonColumnsLengths.MAX_MEDIUM_TEXT_LENGTH)
@ColumnDescription(comment = "Certificate subject (canonical form)" )
private String subject;
@Column(name = "ISSUER", length = CommonColumnsLengths.MAX_MEDIUM_TEXT_LENGTH)
@ColumnDescription(comment = "Certificate issuer (canonical form)" )
private String issuer;
@Column(name = "SERIALNUMBER", length = CommonColumnsLengths.MAX_TEXT_LENGTH_128)
@ColumnDescription(comment = "Certificate serial number" )
private String serialNumber;
@Column(name = "CREATED_ON" , nullable = false)
LocalDateTime createdOn;
@Column(name = "LAST_UPDATED_ON", nullable = false)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment