Skip to content

Commit

Permalink
Merge pull request #140 from mcneilco/bugfix/label-sequence-30-charac…
Browse files Browse the repository at this point in the history
…ters-limit

#139: label sequences should be truncated at 30 characters
  • Loading branch information
brianbolt authored Feb 18, 2018
2 parents ee46470 + d7b8d6d commit 28a2da2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
32 changes: 26 additions & 6 deletions src/main/java/com/labsynch/labseer/domain/LabelSequence.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.labsynch.labseer.domain;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -22,7 +23,6 @@

import org.hibernate.Session;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.engine.jdbc.dialect.internal.StandardDialectResolver;
Expand Down Expand Up @@ -95,17 +95,37 @@ public LabelSequence save() {
}
}
}
//first create the database sequence, then persist the object
EntityManager em = LabelSequence.entityManager();
if (this.getDbSequence() == null) {
String dbSequence = "labelseq_"+this.getLabelPrefix()+"_"+this.getLabelTypeAndKind()+"_"+this.getThingTypeAndKind();
//set to temp sequence first so we can persist, get and id and then use the id to create a unique sequence name
this.setDbSequence("tempseq");
this.persist();
String dbSequence = "labelseq_"+this.getId()+"_"+this.getLabelPrefix()+"_"+this.getLabelTypeAndKind()+"_"+this.getThingTypeAndKind();
dbSequence = dbSequence.replaceAll("[^a-zA-Z0-9_]+", "_");
this.setDbSequence(dbSequence);
//Limit to 30 characters to be compatible with Oracle Version <= 12.1 if applicable
String databaseType = null;
Float databaseVersion = 0.0f;
try{
org.hibernate.engine.spi.SessionImplementor sessionImp =
(org.hibernate.engine.spi.SessionImplementor) em.getDelegate();
DatabaseMetaData metadata = sessionImp.connection().getMetaData();
databaseType = metadata.getDatabaseProductName();
databaseVersion = Float.parseFloat(metadata.getDatabaseMajorVersion()+"."+metadata.getDatabaseMinorVersion());
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(databaseType.equalsIgnoreCase("Oracle") && (databaseVersion < 12.2f)) {
int MAX_CHAR = 30;
int maxLength = (dbSequence.length() < MAX_CHAR)?dbSequence.length():MAX_CHAR;
dbSequence = dbSequence.substring(0, maxLength);
this.setDbSequence(dbSequence);
}
}
if (this.getStartingNumber() < 1L) this.setStartingNumber(1L);
EntityManager em = LabelSequence.entityManager();
Query q = em.createNativeQuery("CREATE SEQUENCE "+this.dbSequence+" START WITH "+this.getStartingNumber());
q.executeUpdate();
this.persist();
this.merge();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public DDictValue saveDataDictionaryValue(DDictValue dDict, Boolean createTypeAn
logger.debug("boolean flag = " + createTypeAndKind);
if (dDictVals.size() == 0){
if (createTypeAndKind){
logger.info("attemp to create type and kind");
logger.info("attempting to create type and kind");
getOrCreateDDictType(dDict.getLsType());
getOrCreateDDictKind(dDict.getLsType(), dDict.getLsKind());
}
Expand Down

0 comments on commit 28a2da2

Please sign in to comment.