diff --git a/src/com/rjconsultores/ventaboletos/entidad/CustomIdGenerator.java b/src/com/rjconsultores/ventaboletos/entidad/CustomIdGenerator.java new file mode 100644 index 000000000..a9b41d0d9 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/entidad/CustomIdGenerator.java @@ -0,0 +1,73 @@ +package com.rjconsultores.ventaboletos.entidad; + +import java.io.Serializable; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import org.apache.commons.lang.StringUtils; +import org.hibernate.HibernateException; +import org.hibernate.MappingException; +import org.hibernate.dialect.Dialect; +import org.hibernate.engine.SessionImplementor; +import org.hibernate.id.Configurable; +import org.hibernate.id.IdentifierGenerator; + +import com.rjconsultores.ventaboletos.utilerias.ApplicationProperties; + +public class CustomIdGenerator implements IdentifierGenerator, Configurable { + + private String seqName; + private Integer tamanho; + + public Serializable generate(SessionImplementor session, Object object) + throws HibernateException { + + Connection connection = session.connection(); + try { + + PreparedStatement ps = connection + .prepareStatement("SELECT " + seqName + ".NEXTVAL AS NEXTVAL FROM DUAL"); + + ResultSet rs = ps.executeQuery(); + + if (rs.next()) { + Integer id = rs.getInt("NEXTVAL"); + String newValue = id.toString(); + if (ApplicationProperties.getInstance().habilitarCustomSequence()) { + newValue = "1" + (StringUtils.leftPad(id.toString(), tamanho, "0")); + } + return Integer.valueOf(newValue); + } + + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void configure(org.hibernate.type.Type arg0, Properties arg1, Dialect arg2) throws MappingException { + setTamanho(Integer.valueOf(arg1.getProperty("tamanho"))); + setSeqName(arg1.getProperty("seqName")); + } + + public Integer getTamanho() { + return tamanho; + } + + public void setTamanho(Integer tamanho) { + this.tamanho = tamanho; + } + + public String getSeqName() { + return seqName; + } + + public void setSeqName(String seqName) { + this.seqName = seqName; + } + +} \ No newline at end of file diff --git a/src/com/rjconsultores/ventaboletos/entidad/FolioPreimpreso.java b/src/com/rjconsultores/ventaboletos/entidad/FolioPreimpreso.java index 67158d500..43d73390e 100644 --- a/src/com/rjconsultores/ventaboletos/entidad/FolioPreimpreso.java +++ b/src/com/rjconsultores/ventaboletos/entidad/FolioPreimpreso.java @@ -16,6 +16,9 @@ import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.annotations.Parameter; + @Entity @SequenceGenerator(name = "FOLIO_PREIMPRESO_SEQ", sequenceName = "FOLIO_PREIMPRESO_SEQ", allocationSize = 1) @Table(name = "FOLIO_PREIMPRESO") @@ -24,6 +27,11 @@ public class FolioPreimpreso implements Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) + @GenericGenerator(name = "FOLIO_PREIMPRESO_SEQ", strategy = "com.rjconsultores.ventaboletos.entidad.CustomIdGenerator" , + parameters = { + @Parameter(name="tamanho", value="8"), + @Parameter(name="seqName", value="FOLIO_PREIMPRESO_SEQ") + } ) @GeneratedValue(strategy = GenerationType.AUTO, generator = "FOLIO_PREIMPRESO_SEQ") @Column(name = "FOLIOPREIMPRESO_ID") private Integer foliopreimpresoId; diff --git a/src/com/rjconsultores/ventaboletos/utilerias/ApplicationProperties.java b/src/com/rjconsultores/ventaboletos/utilerias/ApplicationProperties.java index deca2381f..e52d320b5 100644 --- a/src/com/rjconsultores/ventaboletos/utilerias/ApplicationProperties.java +++ b/src/com/rjconsultores/ventaboletos/utilerias/ApplicationProperties.java @@ -132,4 +132,9 @@ public class ApplicationProperties { return property.equals("1"); } + public boolean habilitarCustomSequence() { + String property = p.getProperty("custom.sequence", "0"); + return property.equals("1"); + } + }