Search in sources :

Example 41 with DataIntegrityViolationException

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);
                    }
                }
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) HashMap(java.util.HashMap) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) SimpleJdbcInsert(org.springframework.jdbc.core.simple.SimpleJdbcInsert) ArrayList(java.util.ArrayList) List(java.util.List)

Example 42 with DataIntegrityViolationException

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);
                        }
                    }
                }
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) HashMap(java.util.HashMap) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) SimpleJdbcInsert(org.springframework.jdbc.core.simple.SimpleJdbcInsert) ArrayList(java.util.ArrayList) List(java.util.List)

Example 43 with DataIntegrityViolationException

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);
    }
}
Also used : Authorship(cz.metacentrum.perun.cabinet.model.Authorship) Thanks(cz.metacentrum.perun.cabinet.model.Thanks) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 44 with DataIntegrityViolationException

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);
    }
}
Also used : InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) AttributeDefinitionExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeDefinitionExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 45 with DataIntegrityViolationException

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;
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) QueryRuntimeException(org.hisp.dhis.common.QueryRuntimeException) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Aggregations

DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)117 Test (org.junit.Test)26 HashMap (java.util.HashMap)12 BadRequestException (com.sequenceiq.cloudbreak.controller.BadRequestException)11 Transactional (org.springframework.transaction.annotation.Transactional)11 Test (org.junit.jupiter.api.Test)10 Transactional (javax.transaction.Transactional)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 User (ca.corefacility.bioinformatics.irida.model.user.User)8 SQLException (java.sql.SQLException)8 ConstraintViolation (javax.validation.ConstraintViolation)8 HashSet (java.util.HashSet)7 Locale (java.util.Locale)6 ConstraintViolationException (javax.validation.ConstraintViolationException)6 Date (java.util.Date)5 List (java.util.List)5 Set (java.util.Set)5 EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)4 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)4 InvalidMetaException (com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)4