use of org.springframework.dao.DataIntegrityViolationException in project goci by EBISPOT.
the class V1_9_9_011__Association_locus_links_for_haplotypes method migrate.
public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
// get all genes
IdAndStringRowHandler geneHandler = new IdAndStringRowHandler();
jdbcTemplate.query(SELECT_GENES, geneHandler);
final Map<Long, String> geneIdToNameMap = geneHandler.getIdToStringMap();
// get all snps
IdAndStringRowHandler snpHandler = new IdAndStringRowHandler();
jdbcTemplate.query(SELECT_SNPS, snpHandler);
final Map<Long, String> snpIdToRsIdMap = snpHandler.getIdToStringMap();
// get all associations and link to gene id
final Map<Long, Set<Long>> associationIdToGeneIds = new HashMap<>();
final Map<Long, List<Long>> associationIdToSnpIds = new HashMap<>();
final Map<Long, List<String>> associationIdToRiskAlleleNames = new HashMap<>();
final Map<Long, String> associationIdToMigratedDescription = new HashMap<>();
jdbcTemplate.query(SELECT_ASSOCIATIONS_AND_SNPS, (resultSet, i) -> {
long associationID = resultSet.getLong(1);
List<String> riskAlleles;
List<String> geneNames;
List<String> rsIds;
String riskAlleleStr = resultSet.getString(2);
if (riskAlleleStr != null) {
riskAlleles = split(resultSet.getString(2).trim(), "\\+");
} else {
riskAlleles = new ArrayList<>();
}
associationIdToMigratedDescription.put(associationID, riskAlleleStr);
String genesStr = resultSet.getString(3);
if (genesStr != null) {
geneNames = split(genesStr.trim());
} else {
geneNames = new ArrayList<>();
}
String snpsStr = resultSet.getString(4);
if (snpsStr != null) {
rsIds = split(snpsStr.trim());
} else {
rsIds = new ArrayList<>();
}
// in case we need to add new genes
SimpleJdbcInsert insertGene = new SimpleJdbcInsert(jdbcTemplate).withTableName("GENE").usingColumns("GENE_NAME").usingGeneratedKeyColumns("ID");
for (String geneName : geneNames) {
boolean found = false;
for (long geneID : geneIdToNameMap.keySet()) {
if (geneIdToNameMap.get(geneID).equals(geneName)) {
if (!associationIdToGeneIds.containsKey(associationID)) {
associationIdToGeneIds.put(associationID, new HashSet<>());
}
if (!associationIdToGeneIds.get(associationID).contains(geneID)) {
// add the new associated gene
associationIdToGeneIds.get(associationID).add(geneID);
}
found = true;
// we break here to handle duplicate entries in the gene table of the database
break;
}
}
if (!found) {
// the GENE with the GENE_NAME in GWASSTUDIESSNP doesn't exist in GWASGENE,
// so create a new GENE entry
Map<String, Object> geneArgs = new HashMap<>();
geneArgs.put("GENE_NAME", geneName);
long geneID = insertGene.executeAndReturnKey(geneArgs).longValue();
geneIdToNameMap.put(geneID, geneName);
if (!associationIdToGeneIds.containsKey(associationID)) {
associationIdToGeneIds.put(associationID, new HashSet<>());
}
if (!associationIdToGeneIds.get(associationID).contains(geneID)) {
// add the new associated gene
associationIdToGeneIds.get(associationID).add(geneID);
}
}
}
// in case we need to add new SNPs
SimpleJdbcInsert insertSnp = new SimpleJdbcInsert(jdbcTemplate).withTableName("SINGLE_NUCLEOTIDE_POLYMORPHISM").usingColumns("RS_ID").usingGeneratedKeyColumns("ID");
Iterator<String> rsIdIterator = rsIds.iterator();
Iterator<String> riskAlleleIterator = riskAlleles.iterator();
if (rsIds.size() == riskAlleles.size()) {
while (rsIdIterator.hasNext()) {
String rsId = rsIdIterator.next().trim();
String riskAllele = riskAlleleIterator.next().trim();
boolean foundSnp = false;
for (long snpID : snpIdToRsIdMap.keySet()) {
if (snpIdToRsIdMap.get(snpID).equals(rsId)) {
if (!associationIdToSnpIds.containsKey(associationID)) {
associationIdToSnpIds.put(associationID, new ArrayList<>());
associationIdToRiskAlleleNames.put(associationID, new ArrayList<>());
}
if (!associationIdToSnpIds.get(associationID).contains(snpID)) {
// add the new associated snp and risk allele
associationIdToSnpIds.get(associationID).add(snpID);
associationIdToRiskAlleleNames.get(associationID).add(riskAllele);
}
foundSnp = true;
// we break here to handle duplicate entries in the snp table of the database
break;
}
}
if (!foundSnp) {
// the SNP with the RS_ID in GWASSTUDIESSNP doesn't exist in GWASSNP,
// so create a new SINGLE_NUCLEOTIDE_POLYMORPHISM entry
Map<String, Object> snpArgs = new HashMap<>();
snpArgs.put("RS_ID", rsId);
insertSnp.execute(snpArgs);
long snpID = insertSnp.executeAndReturnKey(snpArgs).longValue();
snpIdToRsIdMap.put(snpID, rsId);
if (!associationIdToSnpIds.containsKey(associationID)) {
associationIdToSnpIds.put(associationID, new ArrayList<>());
}
if (!associationIdToSnpIds.get(associationID).contains(snpID)) {
// add the new associated gene
associationIdToSnpIds.get(associationID).add(snpID);
}
}
}
} else {
getLog().warn("Mismatched number of snps and risk alleles for " + "association " + associationID + " " + "(snp string = " + snpsStr + " and " + "risk allele string = " + riskAlleleStr + "). " + "Inferring risk alleles from SNP");
while (rsIdIterator.hasNext()) {
String rsId = rsIdIterator.next().trim();
String riskAllele = rsId + "-?";
for (String nextRiskAllele : riskAlleles) {
if (nextRiskAllele.contains(rsId)) {
// overwrite with actual value
riskAllele = nextRiskAllele;
break;
}
}
boolean foundSnp = false;
for (long snpID : snpIdToRsIdMap.keySet()) {
if (snpIdToRsIdMap.get(snpID).equals(rsId)) {
if (!associationIdToSnpIds.containsKey(associationID)) {
associationIdToSnpIds.put(associationID, new ArrayList<>());
associationIdToRiskAlleleNames.put(associationID, new ArrayList<>());
}
if (!associationIdToSnpIds.get(associationID).contains(snpID)) {
// add the new associated snp and risk allele
associationIdToSnpIds.get(associationID).add(snpID);
associationIdToRiskAlleleNames.get(associationID).add(riskAllele);
}
foundSnp = true;
// we break here to handle duplicate entries in the snp table of the database
break;
}
}
if (!foundSnp) {
// the SNP with the RS_ID in GWASSTUDIESSNP doesn't exist in GWASSNP,
// so create a new SINGLE_NUCLEOTIDE_POLYMORPHISM entry
Map<String, Object> snpArgs = new HashMap<>();
snpArgs.put("RS_ID", rsId);
insertSnp.execute(snpArgs);
long snpID = insertSnp.executeAndReturnKey(snpArgs).longValue();
snpIdToRsIdMap.put(snpID, rsId);
if (!associationIdToSnpIds.containsKey(associationID)) {
associationIdToSnpIds.put(associationID, new ArrayList<>());
associationIdToRiskAlleleNames.put(associationID, new ArrayList<>());
}
if (!associationIdToSnpIds.get(associationID).contains(snpID)) {
// add the new associated gene
associationIdToSnpIds.get(associationID).add(snpID);
associationIdToRiskAlleleNames.get(associationID).add(riskAllele);
}
}
}
}
return null;
});
SimpleJdbcInsert insertLocus = new SimpleJdbcInsert(jdbcTemplate).withTableName("LOCUS").usingColumns("HAPLOTYPE_SNP_COUNT", "DESCRIPTION", "MIGRATED_DESCRIPTION").usingGeneratedKeyColumns("ID");
SimpleJdbcInsert insertAssociationLocus = new SimpleJdbcInsert(jdbcTemplate).withTableName("ASSOCIATION_LOCUS").usingColumns("ASSOCIATION_ID", "LOCUS_ID");
SimpleJdbcInsert insertRiskAllele = new SimpleJdbcInsert(jdbcTemplate).withTableName("RISK_ALLELE").usingColumns("RISK_ALLELE_NAME").usingGeneratedKeyColumns("ID");
SimpleJdbcInsert insertLocusRiskAllele = new SimpleJdbcInsert(jdbcTemplate).withTableName("LOCUS_RISK_ALLELE").usingColumns("LOCUS_ID", "RISK_ALLELE_ID");
SimpleJdbcInsert insertRiskAlleleSnp = new SimpleJdbcInsert(jdbcTemplate).withTableName("RISK_ALLELE_SNP").usingColumns("RISK_ALLELE_ID", "SNP_ID");
SimpleJdbcInsert insertAuthorReportedGene = new SimpleJdbcInsert(jdbcTemplate).withTableName("AUTHOR_REPORTED_GENE").usingColumns("LOCUS_ID", "REPORTED_GENE_ID");
for (Long associationID : associationIdToSnpIds.keySet()) {
// get snp/risk allele pairs
List<Long> snps = associationIdToSnpIds.get(associationID);
List<String> riskAlleles = associationIdToRiskAlleleNames.get(associationID);
if (snps.size() != riskAlleles.size()) {
throw new RuntimeException("Mismatched SNP ID/Risk Allele name pairs for " + "association " + associationID + " (" + snps + ", " + riskAlleles + ")");
} else {
// create a single LOCUS and get the locus ID
Map<String, Object> locusArgs = new HashMap<>();
locusArgs.put("HAPLOTYPE_SNP_COUNT", snps.size());
locusArgs.put("DESCRIPTION", String.valueOf(snps.size()) + " SNP haplotype");
locusArgs.put("MIGRATED_DESCRIPTION", associationIdToMigratedDescription.get(associationID));
Number locusID = insertLocus.executeAndReturnKey(locusArgs);
// now create the ASSOCIATION_LOCUS link
Map<String, Object> associationLocusArgs = new HashMap<>();
associationLocusArgs.put("ASSOCIATION_ID", associationID);
associationLocusArgs.put("LOCUS_ID", locusID);
insertAssociationLocus.execute(associationLocusArgs);
Iterator<Long> snpIterator = snps.iterator();
Iterator<String> riskAlleleIterator = riskAlleles.iterator();
while (riskAlleleIterator.hasNext()) {
Long snpID = snpIterator.next();
String riskAlleleName = riskAlleleIterator.next();
// now create a single RISK_ALLELE and get the risk allele ID
Map<String, Object> riskAlleleArgs = new HashMap<>();
riskAlleleArgs.put("RISK_ALLELE_NAME", riskAlleleName);
Number riskAlleleID = insertRiskAllele.executeAndReturnKey(riskAlleleArgs);
// now create the LOCUS_RISK_ALLELE link
Map<String, Object> locusRiskAlleleArgs = new HashMap<>();
locusRiskAlleleArgs.put("LOCUS_ID", locusID.longValue());
locusRiskAlleleArgs.put("RISK_ALLELE_ID", riskAlleleID.longValue());
insertLocusRiskAllele.execute(locusRiskAlleleArgs);
// now create the RISK_ALLELE_SNP link
try {
Map<String, Object> riskAlleleSnpArgs = new HashMap<>();
riskAlleleSnpArgs.put("RISK_ALLELE_ID", riskAlleleID.longValue());
riskAlleleSnpArgs.put("SNP_ID", snpID);
insertRiskAlleleSnp.execute(riskAlleleSnpArgs);
} catch (DataIntegrityViolationException e) {
throw new RuntimeException("Failed to insert link between snp = " + snpID + " and risk allele = " + riskAlleleID, e);
}
}
// finally create the AUTHOR_REPORTED_GENE link
if (associationIdToGeneIds.containsKey(associationID)) {
for (Long geneID : associationIdToGeneIds.get(associationID)) {
try {
Map<String, Object> authorReportedGeneArgs = new HashMap<>();
authorReportedGeneArgs.put("LOCUS_ID", locusID.longValue());
authorReportedGeneArgs.put("REPORTED_GENE_ID", geneID);
insertAuthorReportedGene.execute(authorReportedGeneArgs);
} catch (DataIntegrityViolationException e) {
throw new RuntimeException("Failed to insert link between locus = " + locusID + " and reported gene = " + geneID, e);
}
}
}
}
}
}
use of org.springframework.dao.DataIntegrityViolationException in project goci by EBISPOT.
the class V1_9_9_012__Association_locus_links_for_interaction_studies method migrate.
public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
// get all genes
IdAndStringRowHandler geneHandler = new IdAndStringRowHandler();
jdbcTemplate.query(SELECT_GENES, geneHandler);
final Map<Long, String> geneIdToNameMap = geneHandler.getIdToStringMap();
// get all snps
IdAndStringRowHandler snpHandler = new IdAndStringRowHandler();
jdbcTemplate.query(SELECT_SNPS, snpHandler);
final Map<Long, String> snpIdToRsIdMap = snpHandler.getIdToStringMap();
// get all associations and link to gene id
final Map<Long, Set<Long>> associationIdToGeneIds = new HashMap<>();
final Map<Long, List<Long>> associationIdToSnpIds = new HashMap<>();
final Map<Long, List<String>> associationIdToRiskAlleleNames = new HashMap<>();
jdbcTemplate.query(SELECT_ASSOCIATIONS_AND_SNPS, (resultSet, i) -> {
long associationID = resultSet.getLong(1);
List<String> riskAlleles;
List<String> geneNames;
List<String> rsIds;
String riskAlleleStr = resultSet.getString(2);
if (riskAlleleStr != null) {
riskAlleles = split(resultSet.getString(2).trim(), "x", ":");
} else {
riskAlleles = new ArrayList<>();
}
String genesStr = resultSet.getString(3);
if (genesStr != null) {
geneNames = split(genesStr.trim());
} else {
geneNames = new ArrayList<>();
}
String snpsStr = resultSet.getString(4);
if (snpsStr != null) {
rsIds = split(snpsStr.trim(), "x", ":");
} else {
rsIds = new ArrayList<>();
}
// in case we need to add new genes
SimpleJdbcInsert insertGene = new SimpleJdbcInsert(jdbcTemplate).withTableName("GENE").usingColumns("GENE_NAME").usingGeneratedKeyColumns("ID");
for (String geneName : geneNames) {
boolean found = false;
for (long geneID : geneIdToNameMap.keySet()) {
if (geneIdToNameMap.get(geneID).equals(geneName)) {
if (!associationIdToGeneIds.containsKey(associationID)) {
associationIdToGeneIds.put(associationID, new HashSet<>());
}
if (!associationIdToGeneIds.get(associationID).contains(geneID)) {
// add the new associated gene
associationIdToGeneIds.get(associationID).add(geneID);
}
found = true;
// we break here to handle duplicate entries in the gene table of the database
break;
}
}
if (!found) {
// the GENE with the GENE_NAME in GWASSTUDIESSNP doesn't exist in GWASGENE,
// so create a new GENE entry
Map<String, Object> geneArgs = new HashMap<>();
geneArgs.put("GENE_NAME", geneName);
long geneID = insertGene.executeAndReturnKey(geneArgs).longValue();
geneIdToNameMap.put(geneID, geneName);
if (!associationIdToGeneIds.containsKey(associationID)) {
associationIdToGeneIds.put(associationID, new HashSet<>());
}
if (!associationIdToGeneIds.get(associationID).contains(geneID)) {
// add the new associated gene
associationIdToGeneIds.get(associationID).add(geneID);
}
}
}
// in case we need to add new SNPs
SimpleJdbcInsert insertSnp = new SimpleJdbcInsert(jdbcTemplate).withTableName("SINGLE_NUCLEOTIDE_POLYMORPHISM").usingColumns("RS_ID").usingGeneratedKeyColumns("ID");
Iterator<String> rsIdIterator = rsIds.iterator();
Iterator<String> riskAlleleIterator = riskAlleles.iterator();
if (rsIds.size() == riskAlleles.size()) {
while (rsIdIterator.hasNext()) {
String rsId = rsIdIterator.next().trim();
String riskAllele = riskAlleleIterator.next().trim();
boolean foundSnp = false;
for (long snpID : snpIdToRsIdMap.keySet()) {
if (snpIdToRsIdMap.get(snpID).equals(rsId)) {
if (!associationIdToSnpIds.containsKey(associationID)) {
associationIdToSnpIds.put(associationID, new ArrayList<>());
associationIdToRiskAlleleNames.put(associationID, new ArrayList<>());
}
if (!associationIdToSnpIds.get(associationID).contains(snpID)) {
// add the new associated snp and risk allele
associationIdToSnpIds.get(associationID).add(snpID);
associationIdToRiskAlleleNames.get(associationID).add(riskAllele);
}
foundSnp = true;
// we break here to handle duplicate entries in the snp table of the database
break;
}
}
if (!foundSnp) {
// the SNP with the RS_ID in GWASSTUDIESSNP doesn't exist in GWASSNP,
// so create a new SINGLE_NUCLEOTIDE_POLYMORPHISM entry
Map<String, Object> snpArgs = new HashMap<>();
snpArgs.put("RS_ID", rsId);
insertSnp.execute(snpArgs);
long snpID = insertSnp.executeAndReturnKey(snpArgs).longValue();
snpIdToRsIdMap.put(snpID, rsId);
if (!associationIdToSnpIds.containsKey(associationID)) {
associationIdToSnpIds.put(associationID, new ArrayList<>());
associationIdToRiskAlleleNames.put(associationID, new ArrayList<>());
}
if (!associationIdToSnpIds.get(associationID).contains(snpID)) {
// add the new associated gene
associationIdToSnpIds.get(associationID).add(snpID);
associationIdToRiskAlleleNames.get(associationID).add(riskAllele);
}
}
}
} else {
getLog().warn("Mismatched number of snps and risk alleles for " + "association " + associationID + " " + "(snp string = " + snpsStr + " and " + "risk allele string = " + riskAlleleStr + "). " + "Inferring risk alleles from SNP");
while (rsIdIterator.hasNext()) {
String rsId = rsIdIterator.next().trim();
String riskAllele = rsId + "-?";
for (String nextRiskAllele : riskAlleles) {
if (nextRiskAllele.contains(rsId)) {
// overwrite with actual value
riskAllele = nextRiskAllele;
break;
}
}
boolean foundSnp = false;
for (long snpID : snpIdToRsIdMap.keySet()) {
if (snpIdToRsIdMap.get(snpID).equals(rsId)) {
if (!associationIdToSnpIds.containsKey(associationID)) {
associationIdToSnpIds.put(associationID, new ArrayList<>());
associationIdToRiskAlleleNames.put(associationID, new ArrayList<>());
}
if (!associationIdToSnpIds.get(associationID).contains(snpID)) {
// add the new associated snp and risk allele
associationIdToSnpIds.get(associationID).add(snpID);
associationIdToRiskAlleleNames.get(associationID).add(riskAllele);
}
foundSnp = true;
// we break here to handle duplicate entries in the snp table of the database
break;
}
}
if (!foundSnp) {
// the SNP with the RS_ID in GWASSTUDIESSNP doesn't exist in GWASSNP,
// so create a new SINGLE_NUCLEOTIDE_POLYMORPHISM entry
Map<String, Object> snpArgs = new HashMap<>();
snpArgs.put("RS_ID", rsId);
insertSnp.execute(snpArgs);
long snpID = insertSnp.executeAndReturnKey(snpArgs).longValue();
snpIdToRsIdMap.put(snpID, rsId);
if (!associationIdToSnpIds.containsKey(associationID)) {
associationIdToSnpIds.put(associationID, new ArrayList<>());
associationIdToRiskAlleleNames.put(associationID, new ArrayList<>());
}
if (!associationIdToSnpIds.get(associationID).contains(snpID)) {
// add the new associated gene
associationIdToSnpIds.get(associationID).add(snpID);
associationIdToRiskAlleleNames.get(associationID).add(riskAllele);
}
}
}
}
return null;
});
SimpleJdbcInsert insertLocus = new SimpleJdbcInsert(jdbcTemplate).withTableName("LOCUS").usingColumns("HAPLOTYPE_SNP_COUNT", "DESCRIPTION").usingGeneratedKeyColumns("ID");
SimpleJdbcInsert insertAssociationLocus = new SimpleJdbcInsert(jdbcTemplate).withTableName("ASSOCIATION_LOCUS").usingColumns("ASSOCIATION_ID", "LOCUS_ID");
SimpleJdbcInsert insertRiskAllele = new SimpleJdbcInsert(jdbcTemplate).withTableName("RISK_ALLELE").usingColumns("RISK_ALLELE_NAME").usingGeneratedKeyColumns("ID");
SimpleJdbcInsert insertLocusRiskAllele = new SimpleJdbcInsert(jdbcTemplate).withTableName("LOCUS_RISK_ALLELE").usingColumns("LOCUS_ID", "RISK_ALLELE_ID");
SimpleJdbcInsert insertRiskAlleleSnp = new SimpleJdbcInsert(jdbcTemplate).withTableName("RISK_ALLELE_SNP").usingColumns("RISK_ALLELE_ID", "SNP_ID");
SimpleJdbcInsert insertAuthorReportedGene = new SimpleJdbcInsert(jdbcTemplate).withTableName("AUTHOR_REPORTED_GENE").usingColumns("LOCUS_ID", "REPORTED_GENE_ID");
for (Long associationID : associationIdToSnpIds.keySet()) {
// get snp/risk allele pairs
List<Long> snps = associationIdToSnpIds.get(associationID);
List<String> riskAlleles = associationIdToRiskAlleleNames.get(associationID);
if (snps.size() != riskAlleles.size()) {
throw new RuntimeException("Mismatched SNP ID/Risk Allele name pairs for " + "association " + associationID + " (" + snps + ", " + riskAlleles + ")");
} else {
// iterate over each SNP and risk allele
Iterator<Long> snpIterator = snps.iterator();
Iterator<String> riskAlleleIterator = riskAlleles.iterator();
while (riskAlleleIterator.hasNext()) {
// create multiple LOCI, one per SNP/risk allele pair, and get the locus ID
Map<String, Object> locusArgs = new HashMap<>();
locusArgs.put("HAPLOTYPE_SNP_COUNT", null);
String description = snps.size() == 2 ? "SNP x SNP interaction" : "SNP x SNP x SNP interaction";
locusArgs.put("DESCRIPTION", description);
Number locusID = insertLocus.executeAndReturnKey(locusArgs);
// now create the ASSOCIATION_LOCUS link
Map<String, Object> associationLocusArgs = new HashMap<>();
associationLocusArgs.put("ASSOCIATION_ID", associationID);
associationLocusArgs.put("LOCUS_ID", locusID);
insertAssociationLocus.execute(associationLocusArgs);
Long snpID = snpIterator.next();
String riskAlleleName = riskAlleleIterator.next();
// now create a single RISK_ALLELE and get the risk allele ID
Map<String, Object> riskAlleleArgs = new HashMap<>();
riskAlleleArgs.put("RISK_ALLELE_NAME", riskAlleleName);
Number riskAlleleID = insertRiskAllele.executeAndReturnKey(riskAlleleArgs);
// now create the LOCUS_RISK_ALLELE link
Map<String, Object> locusRiskAlleleArgs = new HashMap<>();
locusRiskAlleleArgs.put("LOCUS_ID", locusID.longValue());
locusRiskAlleleArgs.put("RISK_ALLELE_ID", riskAlleleID.longValue());
insertLocusRiskAllele.execute(locusRiskAlleleArgs);
// now create the RISK_ALLELE_SNP link
try {
Map<String, Object> riskAlleleSnpArgs = new HashMap<>();
riskAlleleSnpArgs.put("RISK_ALLELE_ID", riskAlleleID.longValue());
riskAlleleSnpArgs.put("SNP_ID", snpID);
insertRiskAlleleSnp.execute(riskAlleleSnpArgs);
} catch (DataIntegrityViolationException e) {
throw new RuntimeException("Failed to insert link between snp = " + snpID + " and risk allele = " + riskAlleleID, e);
}
// finally create the AUTHOR_REPORTED_GENE link
if (associationIdToGeneIds.containsKey(associationID)) {
for (Long geneID : associationIdToGeneIds.get(associationID)) {
try {
Map<String, Object> authorReportedGeneArgs = new HashMap<>();
authorReportedGeneArgs.put("LOCUS_ID", locusID.longValue());
authorReportedGeneArgs.put("REPORTED_GENE_ID", geneID);
insertAuthorReportedGene.execute(authorReportedGeneArgs);
} catch (DataIntegrityViolationException e) {
throw new RuntimeException("Failed to insert link between locus = " + locusID + " and reported gene = " + geneID, e);
}
}
}
}
}
}
}
use of org.springframework.dao.DataIntegrityViolationException in project perun by CESNET.
the class PublicationManagerBlImpl method deletePublication.
@Override
public void deletePublication(PerunSession sess, Publication publication) throws CabinetException {
try {
// delete authors
for (Authorship a : getAuthorshipManagerBl().getAuthorshipsByPublicationId(publication.getId())) {
getAuthorshipManagerBl().deleteAuthorship(sess, a);
}
// delete thanks
for (Thanks t : getThanksManagerBl().getThanksByPublicationId(publication.getId())) {
getThanksManagerBl().deleteThanks(sess, t);
}
// delete publication
getPublicationManagerDao().deletePublication(publication);
log.debug("{} deleted.", publication);
// publications without authors are: "to be deleted by perun admin"
} catch (DataIntegrityViolationException ex) {
throw new CabinetException("Can't delete publication with Authors or Thanks. Please remove them first in order to delete publication.", ErrorCodes.PUBLICATION_HAS_AUTHORS_OR_THANKS);
} catch (PerunException ex) {
throw new CabinetException(ErrorCodes.PERUN_EXCEPTION, ex);
}
}
use of org.springframework.dao.DataIntegrityViolationException in project perun by CESNET.
the class AttributesManagerImpl method createAttribute.
@Override
public AttributeDefinition createAttribute(PerunSession sess, AttributeDefinition attribute) throws AttributeDefinitionExistsException {
if (!attribute.getFriendlyName().matches(AttributesManager.ATTRIBUTES_REGEXP)) {
throw new InternalErrorException(new IllegalArgumentException("Wrong attribute name " + attribute.getFriendlyName() + ", attribute name must match " + AttributesManager.ATTRIBUTES_REGEXP));
}
try {
int attributeId = Utils.getNewId(jdbc, "attr_names_id_seq");
jdbc.update("insert into attr_names (id, attr_name, type, dsc, namespace, friendly_name, display_name, is_unique, created_by, created_at, modified_by, modified_at, created_by_uid, modified_by_uid) " + "values (?,?,?,?,?,?,?,?,?," + Compatibility.getSysdate() + ",?," + Compatibility.getSysdate() + ",?,?)", attributeId, attribute.getName(), attribute.getType(), attribute.getDescription(), attribute.getNamespace(), attribute.getFriendlyName(), attribute.getDisplayName(), attribute.isUnique(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), sess.getPerunPrincipal().getUserId());
attribute.setId(attributeId);
log.debug("Attribute created: {}.", attribute);
return attribute;
} catch (DataIntegrityViolationException e) {
throw new AttributeDefinitionExistsException("Attribute " + attribute.getName() + " already exists", attribute, e);
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
}
use of org.springframework.dao.DataIntegrityViolationException in project dhis2-core by dhis2.
the class JdbcEventAnalyticsManager method getEventCount.
@Override
public long getEventCount(EventQueryParams params) {
String sql = "select count(psi) ";
sql += getFromClause(params);
sql += getWhereClause(params);
long count = 0;
try {
log.debug("Analytics event count SQL: " + sql);
if (params.analyzeOnly()) {
executionPlanStore.addExecutionPlan(params.getExplainOrderId(), sql);
} else {
count = jdbcTemplate.queryForObject(sql, Long.class);
}
} catch (BadSqlGrammarException ex) {
log.info(AnalyticsUtils.ERR_MSG_TABLE_NOT_EXISTING, ex);
} catch (DataAccessResourceFailureException ex) {
log.warn(E7131.getMessage(), ex);
throw new QueryRuntimeException(E7131, ex);
} catch (DataIntegrityViolationException ex) {
log.warn(E7132.getMessage(), ex);
throw new QueryRuntimeException(E7132, ex);
}
return count;
}
Aggregations