List of usage examples for org.hibernate SQLQuery setParameterList
@Override NativeQuery<T> setParameterList(String name, Object[] values);
From source file:org.generationcp.middleware.dao.GermplasmDAO.java
License:Open Source License
public Map<Integer, Integer> getMethodIdsByGids(final List<Integer> gids) { final Map<Integer, Integer> resultMap = new HashMap<>(); final SQLQuery query = this.getSession().createSQLQuery(Germplasm.GET_METHOD_IDS_BY_GIDS); query.setParameterList("gids", gids); @SuppressWarnings("rawtypes") final List results = query.list(); for (final Object result : results) { final Object[] resultArray = (Object[]) result; final Integer gid = (Integer) resultArray[0]; final Integer methodId = (Integer) resultArray[1]; resultMap.put(gid, methodId);/*w ww .j ava 2 s . c o m*/ } return resultMap; }
From source file:org.generationcp.middleware.dao.GermplasmDAO.java
License:Open Source License
@SuppressWarnings("unchecked") public Map<Integer, String[]> getParentsInfoByGIDList(final List<Integer> gidList) { try {/*ww w . j a v a 2 s. c o m*/ final Map<Integer, String[]> pedigreeMap = new HashMap<>(); final SQLQuery query = this.getSession() .createSQLQuery(Germplasm.GET_PREFERRED_NAME_AND_PARENT_FOR_A_GID_LIST); query.setParameterList("gidList", gidList); query.addScalar("gid"); query.addScalar("pedigree"); query.addScalar("nval"); final List<Object[]> results = query.list(); for (final Object[] result : results) { pedigreeMap.put((Integer) result[0], new String[] { (String) result[1], (String) result[2] }); } if (gidList.contains(0)) { pedigreeMap.put(0, new String[] { Name.UNKNOWN, Name.UNKNOWN }); } return pedigreeMap; } catch (final HibernateException e) { final String message = "Error with getPedigreeByGIDList(GIDS=" + gidList + ") : " + e.getMessage(); GermplasmDAO.LOG.error(message, e); throw new MiddlewareQueryException(message, e); } }
From source file:org.generationcp.middleware.dao.GermplasmDAO.java
License:Open Source License
public void deleteGermplasms(final List<Integer> gids) { final StringBuilder queryString = new StringBuilder(); try {/*www . ja va 2 s. co m*/ this.getSession().flush(); queryString.append("UPDATE germplsm SET deleted = 1 where gid in (:gids)"); final SQLQuery query = this.getSession().createSQLQuery(queryString.toString()); query.setParameterList("gids", gids); query.executeUpdate(); } catch (final HibernateException e) { final String message = "Error with deleteGermplasms(GIDS=" + gids + ") " + e.getMessage(); GermplasmDAO.LOG.error(message, e); throw new MiddlewareQueryException(message, e); } }
From source file:org.generationcp.middleware.dao.GermplasmDAO.java
License:Open Source License
/** * Only return germplasm with no group assigned (mgid = 0 or mgid is null) * @param gids// w ww. jav a 2 s.com * @return */ public List<Germplasm> getGermplasmWithoutGroup(final List<Integer> gids) { if (gids.isEmpty()) { return new ArrayList<>(); } try { final StringBuilder queryString = new StringBuilder(); queryString.append("SELECT {g.*} FROM germplsm g WHERE "); queryString.append("g.gid IN( :gids ) AND (g.mgid = 0 || g.mgid IS NULL)"); final SQLQuery query = this.getSession().createSQLQuery(queryString.toString()); query.setParameterList("gids", gids); query.addEntity("g", Germplasm.class); return query.list(); } catch (final HibernateException e) { final String message = "Error with getGermplasmWithoutGroup(gids=" + gids.toString() + ") " + e.getMessage(); GermplasmDAO.LOG.error(message, e); throw new MiddlewareQueryException(message, e); } }
From source file:org.generationcp.middleware.dao.GermplasmDAO.java
License:Open Source License
/** * Resets the mgids of a given list of germplasm to zero. * @param gids//from ww w.j a v a2 s. com */ public void resetGermplasmGroup(final List<Integer> gids) { try { final SQLQuery query = this.getSession() .createSQLQuery("UPDATE germplsm SET mgid = 0 WHERE gid IN (:gids)"); query.setParameterList("gids", gids); query.executeUpdate(); } catch (final HibernateException e) { final String message = "Error with resetGermplasmGroup(gids=" + gids.toString() + ") " + e.getMessage(); GermplasmDAO.LOG.error(message, e); throw new MiddlewareQueryException(message, e); } }
From source file:org.generationcp.middleware.dao.GermplasmDAO.java
License:Open Source License
public List<GermplasmDTO> getGermplasmDTOList(final GermplasmSearchRequestDto germplasmSearchRequestDTO, final Integer page, final Integer pageSize) { try {/*ww w. j a va 2 s . c o m*/ String queryString = "SELECT convert(g.gid, char) AS germplasmDbId, reference.btable AS germplasmPUI, " // + " (SELECT n.nval FROM names n " // + " INNER JOIN udflds u ON (u.ftable = 'NAMES' AND u.fcode = 'ACCNO' AND u.fldno = n.ntype)" // + " WHERE (n.gid = g.gid) LIMIT 1) AS accessionNumber, " // + " STR_TO_DATE (convert(g.gdate,char), '%Y%m%d') AS acquisitionDate," // + " (SELECT a.aval FROM atributs a " // + " INNER JOIN udflds u ON (u.ftable = 'ATRIBUTS' AND u.fcode = 'ORI_COUN' AND u.fldno = a.atype)" // + " WHERE (a.gid = g.gid) LIMIT 1) AS countryOfOriginCode, " // + " (SELECT n.nval FROM names n WHERE n.nstat = 1 AND n.gid = g.gid LIMIT 1) AS germplasmName," // + " (SELECT n.nval FROM names n " // + " INNER JOIN udflds u ON (u.ftable = 'NAMES' AND u.fcode = 'GENUS' AND u.fldno = n.ntype)" // + " WHERE (n.gid = g.gid) LIMIT 1) AS genus," // + " (SELECT ld.source FROM listdata ld" // + " WHERE ld.gid = g.gid LIMIT 1) AS germplasmSeedSource, " // + " (SELECT a.aval FROM atributs a " // + " INNER JOIN udflds u ON (u.ftable = 'ATRIBUTS' AND u.fcode = 'SPNAM' AND u.fldno = a.atype)" // + " WHERE (a.gid = g.gid) LIMIT 1) AS species, " // + " (SELECT a.aval FROM atributs a " // + " INNER JOIN udflds u ON (u.ftable = 'ATRIBUTS' AND u.fcode = 'SPAUTH' AND u.fldno = a.atype)" // + " WHERE (a.gid = g.gid) LIMIT 1) AS speciesAuthority, " // + " (SELECT a.aval FROM atributs a " // + " INNER JOIN udflds u ON (u.ftable = 'ATRIBUTS' AND u.fcode = 'SUBTAX' AND u.fldno = a.atype)" // + " WHERE (a.gid = g.gid) LIMIT 1) AS subtaxa, " // + " (SELECT a.aval FROM atributs a " // + " INNER JOIN udflds u ON (u.ftable = 'ATRIBUTS' AND u.fcode = 'STAUTH' AND u.fldno = a.atype)" // + " WHERE (a.gid = g.gid) LIMIT 1) AS subtaxaAuthority, " // + " (SELECT a.aval FROM atributs a " // + " INNER JOIN udflds u ON (u.ftable = 'ATRIBUTS' AND u.fcode = 'PROGM' AND u.fldno = a.atype)" // + " WHERE (a.gid = g.gid) LIMIT 1) AS instituteCode, " // + " m.mname as breedingMethodDbId " // + " FROM germplsm g " // + " LEFT JOIN reflinks reference ON reference.brefid = g.gref " // + " LEFT join methods m ON g.methn = m.mid " // + " WHERE g.deleted = 0" // + " AND g.grplce = 0"; // if (StringUtils.isNoneBlank(germplasmSearchRequestDTO.getPreferredName())) { queryString = queryString + " AND (SELECT n.nval" // + " FROM names n" // + " WHERE n.nstat = 1 AND n.gid = g.gid" // + " LIMIT 1) like :likeCondition "; // } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getAccessionNumbers())) { queryString = queryString + " AND EXISTS (SELECT 1" // + " FROM names n" // + " INNER JOIN udflds u ON (u.ftable = 'NAMES' AND u.fcode = 'ACCNO' AND u.fldno = n.ntype)" // + " WHERE n.gid = g.gid AND n.nval IN (:accessionNumbers)) "; // } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getCommonCropNames())) { queryString = queryString + " AND EXISTS (SELECT 1" // + " FROM atributs a" // + " INNER JOIN udflds u ON (u.ftable = 'ATRIBUTS' AND u.fcode = 'CROPNM' AND u.fldno = a.atype)" // + " WHERE a.gid = g.gid AND a.aval IN (:commonCropNames)) "; // } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmDbIds())) { queryString = queryString + " AND g.gid IN (:germplasmDbIds) "; } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmGenus())) { queryString = queryString + " AND EXISTS (SELECT 1" // + " FROM names n" // + " INNER JOIN udflds u ON (u.ftable = 'NAMES' AND u.fcode = 'GENUS' AND u.fldno = n.ntype)" // + " WHERE n.gid = g.gid AND n.nval IN (:germplasmGenus)) "; // } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmNames())) { queryString = queryString + " AND EXISTS (SELECT 1" // + " FROM names n" // + " WHERE n.gid = g.gid AND n.nval IN (:germplasmNames)) "; // } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmPUIs())) { queryString = queryString + " AND reference.btable IN (:germplasmPUIs) "; // } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmSpecies())) { queryString = queryString + " AND EXISTS (SELECT 1" // + " FROM atributs a" // + " INNER JOIN udflds u ON (u.ftable = 'ATRIBUTS' AND u.fcode = 'SPNAM' AND u.fldno = a.atype)" // + " WHERE a.gid = g.gid AND a.aval IN (:germplasmSpecies)) "; // } final SQLQuery sqlQuery = this.getSession().createSQLQuery(queryString); sqlQuery.addScalar("germplasmDbId").addScalar("germplasmPUI").addScalar("accessionNumber") .addScalar("acquisitionDate").addScalar("countryOfOriginCode").addScalar("germplasmName") .addScalar("genus").addScalar("germplasmSeedSource").addScalar("species") .addScalar("speciesAuthority").addScalar("subtaxa").addScalar("subtaxaAuthority") .addScalar("instituteCode").addScalar("breedingMethodDbId") // .setResultTransformer(new AliasToBeanResultTransformer(GermplasmDTO.class)); if (StringUtils.isNoneBlank(germplasmSearchRequestDTO.getPreferredName())) { sqlQuery.setParameter("likeCondition", "%" + germplasmSearchRequestDTO.getPreferredName() + "%"); // } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getAccessionNumbers())) { sqlQuery.setParameterList("accessionNumbers", germplasmSearchRequestDTO.getAccessionNumbers()); } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getCommonCropNames())) { sqlQuery.setParameterList("commonCropNames", germplasmSearchRequestDTO.getCommonCropNames()); } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmDbIds())) { sqlQuery.setParameterList("germplasmDbIds", germplasmSearchRequestDTO.getGermplasmDbIds()); } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmGenus())) { sqlQuery.setParameterList("germplasmGenus", germplasmSearchRequestDTO.getGermplasmGenus()); } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmNames())) { sqlQuery.setParameterList("germplasmNames", germplasmSearchRequestDTO.getGermplasmNames()); } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmPUIs())) { sqlQuery.setParameterList("germplasmPUIs", germplasmSearchRequestDTO.getGermplasmPUIs()); } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmSpecies())) { sqlQuery.setParameterList("germplasmSpecies", germplasmSearchRequestDTO.getGermplasmSpecies()); } if (page != null && pageSize != null) { sqlQuery.setFirstResult(pageSize * page); sqlQuery.setMaxResults(pageSize); } final List<GermplasmDTO> germplasmDTOList = sqlQuery.list(); return germplasmDTOList; } catch (final HibernateException e) { final String message = "Error with getGermplasmDTOList" + e.getMessage(); GermplasmDAO.LOG.error(message, e); throw new MiddlewareQueryException(message, e); } }
From source file:org.generationcp.middleware.dao.GermplasmDAO.java
License:Open Source License
public long countGermplasmDTOs(final GermplasmSearchRequestDto germplasmSearchRequestDTO) { String queryString = "SELECT COUNT(1) " + " FROM germplsm g " // + " LEFT JOIN reflinks reference ON reference.brefid = g.gref WHERE g.deleted = 0 AND g.grplce = 0"; // if (StringUtils.isNoneBlank(germplasmSearchRequestDTO.getPreferredName())) { queryString = queryString + " AND (SELECT n.nval" // + " FROM names n" // + " WHERE n.nstat = 1 AND n.gid = g.gid" // + " LIMIT 1) like :likeCondition "; // }// w ww .j ava 2 s . co m if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getAccessionNumbers())) { queryString = queryString + " AND EXISTS (SELECT 1" // + " FROM names n" // + " INNER JOIN udflds u ON (u.ftable = 'NAMES' AND u.fcode = 'ACCNO' AND u.fldno = n.ntype)" // + " WHERE n.gid = g.gid AND n.nval IN (:accessionNumbers)) "; // } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getCommonCropNames())) { queryString = queryString + " AND EXISTS (SELECT 1" // + " FROM atributs a" // + " INNER JOIN udflds u ON (u.ftable = 'ATRIBUTS' AND u.fcode = 'CROPNM' AND u.fldno = a.atype)" // + " WHERE a.gid = g.gid AND a.aval IN (:commonCropNames)) "; // } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmDbIds())) { queryString = queryString + " AND g.gid IN (:germplasmDbIds) "; } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmGenus())) { queryString = queryString + " AND EXISTS (SELECT 1" // + " FROM names n" // + " INNER JOIN udflds u ON (u.ftable = 'NAMES' AND u.fcode = 'GENUS' AND u.fldno = n.ntype)" // + " WHERE n.gid = g.gid AND n.nval IN (:germplasmGenus)) "; // } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmNames())) { queryString = queryString + " AND EXISTS (SELECT 1" // + " FROM names n" // + " WHERE n.gid = g.gid AND n.nval IN (:germplasmNames)) "; // } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmPUIs())) { queryString = queryString + " AND reference.btable IN (:germplasmPUIs) "; // } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmSpecies())) { queryString = queryString + " AND EXISTS (SELECT 1" // + " FROM atributs a" // + " INNER JOIN udflds u ON (u.ftable = 'ATRIBUTS' AND u.fcode = 'SPNAM' AND u.fldno = a.atype)" // + " WHERE a.gid = g.gid AND a.aval IN (:germplasmSpecies)) "; // } final SQLQuery sqlQuery = this.getSession().createSQLQuery(queryString); if (StringUtils.isNoneBlank(germplasmSearchRequestDTO.getPreferredName())) { sqlQuery.setParameter("likeCondition", "%" + germplasmSearchRequestDTO.getPreferredName() + "%"); // } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getAccessionNumbers())) { sqlQuery.setParameterList("accessionNumbers", germplasmSearchRequestDTO.getAccessionNumbers()); } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getCommonCropNames())) { sqlQuery.setParameterList("commonCropNames", germplasmSearchRequestDTO.getCommonCropNames()); } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmDbIds())) { sqlQuery.setParameterList("germplasmDbIds", germplasmSearchRequestDTO.getGermplasmDbIds()); } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmGenus())) { sqlQuery.setParameterList("germplasmGenus", germplasmSearchRequestDTO.getGermplasmGenus()); } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmNames())) { sqlQuery.setParameterList("germplasmNames", germplasmSearchRequestDTO.getGermplasmNames()); } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmPUIs())) { sqlQuery.setParameterList("germplasmPUIs", germplasmSearchRequestDTO.getGermplasmPUIs()); } if (!CollectionUtils.isEmpty(germplasmSearchRequestDTO.getGermplasmSpecies())) { sqlQuery.setParameterList("germplasmSpecies", germplasmSearchRequestDTO.getGermplasmSpecies()); } return ((BigInteger) sqlQuery.uniqueResult()).longValue(); }
From source file:org.generationcp.middleware.dao.GermplasmListDAO.java
License:Open Source License
/** * Get Germplasm List Types//from www .j a v a 2s . c o m * * Return a List of UserDefinedField POJOs representing records from the * udflds table of IBDB which are the types of germplasm lists. * * @return List of germplasm list types */ @SuppressWarnings("rawtypes") public List getGermplasmListTypes() { try { final Session session = this.getSession(); final SQLQuery query = session.createSQLQuery(GermplasmListDAO.GET_GERMPLASM_LIST_TYPES); query.setParameterList(GermplasmListDAO.HIDDEN_LIST_TYPES_PARAM, GermplasmListDAO.HIDDEN_LIST_TYPES_ON_SEARCH); return query.list(); } catch (final HibernateException e) { final String errorMessage = "Error with getGermplasmListTypes() query from GermplasmList: " + e.getMessage(); GermplasmListDAO.LOG.error(errorMessage); throw new MiddlewareQueryException(errorMessage, e); } }
From source file:org.generationcp.middleware.dao.GermplasmListDAO.java
License:Open Source License
/** * Get Germplasm Lists with names like Q or germplasms with name like Q or * gid equal to Q//from w ww .ja v a2s. c o m * * @param q * @param o * - like or equal * @return List of GermplasmLists */ @SuppressWarnings("unchecked") public List<GermplasmList> searchForGermplasmLists(final String searchedString, final String programUUID, final Operation o) { final String q = searchedString.trim(); if ("".equals(q)) { return new ArrayList<>(); } try { final SQLQuery query; if (o.equals(Operation.EQUAL)) { query = this.getSession().createSQLQuery(this.getSearchForGermplasmListsQueryString( GermplasmListDAO.SEARCH_FOR_GERMPLASM_LIST_EQUAL, programUUID)); query.setParameter("gidLength", q.length()); query.setParameter("q", q); query.setParameter("qNoSpaces", q.replace(" ", "")); query.setParameter("qStandardized", GermplasmDataManagerUtil.standardizeName(q)); } else { if (q.contains("%") || q.contains("_")) { query = this.getSession().createSQLQuery(this.getSearchForGermplasmListsQueryString( GermplasmListDAO.SEARCH_FOR_GERMPLASM_LIST_GID_LIKE, programUUID)); query.setParameter("q", q); query.setParameter("qNoSpaces", q.replace(" ", "")); query.setParameter("qStandardized", GermplasmDataManagerUtil.standardizeName(q)); } else { query = this.getSession().createSQLQuery(this.getSearchForGermplasmListsQueryString( GermplasmListDAO.SEARCH_FOR_GERMPLASM_LIST, programUUID)); query.setParameter("gidLength", q.length()); query.setParameter("q", q + "%"); query.setParameter("qNoSpaces", q.replace(" ", "") + "%"); query.setParameter("qStandardized", GermplasmDataManagerUtil.standardizeName(q) + "%"); } } query.setParameter("gid", q); query.setParameterList(GermplasmListDAO.HIDDEN_LIST_TYPES_PARAM, GermplasmListDAO.HIDDEN_LIST_TYPES_ON_SEARCH); if (programUUID != null) { query.setParameter(GermplasmListDAO.PROGRAM_UUID, programUUID); } query.addEntity("listnms", GermplasmList.class); return query.list(); } catch (final Exception e) { final String errorMessage = "Error with searchGermplasmLists(" + q + ") " + e.getMessage(); GermplasmListDAO.LOG.error(errorMessage); throw new MiddlewareQueryException(errorMessage, e); } }
From source file:org.generationcp.middleware.dao.GermplasmListDAO.java
License:Open Source License
public List<Object[]> getAllListMetadata(final List<Integer> listIdsFromGermplasmList) { if (listIdsFromGermplasmList.isEmpty()) { return Collections.emptyList(); }/*from w w w.jav a2 s . c o m*/ final StringBuilder sql = new StringBuilder( "SELECT ln.listid as listId, COUNT(ld.listid) as count, ln.listuid as ownerId ") .append(" FROM listnms ln ").append(" INNER JOIN listdata ld ON ln.listid = ld.listid ") .append(" WHERE ln.listid in (:listids) AND").append(" ln.listtype != 'FOLDER' ") .append(" GROUP BY ln.listid;"); final SQLQuery query = this.getSession().createSQLQuery(sql.toString()); query.addScalar("listId", new IntegerType()); query.addScalar("count", new IntegerType()); query.addScalar("ownerId", new IntegerType()); query.setParameterList("listids", listIdsFromGermplasmList); @SuppressWarnings("unchecked") final List<Object[]> queryResults = query.list(); return queryResults; }