Example usage for org.hibernate ScrollableResults beforeFirst

List of usage examples for org.hibernate ScrollableResults beforeFirst

Introduction

In this page you can find the example usage for org.hibernate ScrollableResults beforeFirst.

Prototype

void beforeFirst();

Source Link

Document

Go to a location just before first result, This is the location of the cursor on a newly returned scrollable result.

Usage

From source file:com.multimedia.service.wallpaper.CmsWallpaperService.java

License:Apache License

@Override
public long renewResolution(StatusBean sb) {
    File f;/*from w w w .  jav a2  s .  com*/
    BufferedImage bi;
    sb.setTotal((Long) wallpaper_service.getSinglePropertyU("count(*)"));
    ScrollableResults sr = wallpaper_service.getScrollableResults("id, name", null, null, null, null);
    File img_dir = new File(wallpaper_service.getStorePath(), "full");

    sr.beforeFirst();
    while (sr.next()) {
        f = new File(img_dir, sr.getString(1));
        try {
            bi = ImageUtils.readImage(f).getImage();
            wallpaper_service.updateObjectArrayShortByProperty(WALLPAPER_RESOLUTION,
                    new Object[] { bi.getWidth(), bi.getHeight() }, "id", new Object[] { sr.getLong(0) });
            sb.increaseDone(1);
        } catch (Exception ex) {
            logger.error("while trying to read wallpaper's resolution id = " + sr.getLong(0), ex);
        }
    }
    return sb.getDone();
}

From source file:de.tudarmstadt.ukp.csniper.webapp.evaluation.EvaluationRepository.java

License:Apache License

@Transactional
public int[][] listCachedParsesPages(String aCollectionId, int aPageSize) {
    List<int[]> pages = new ArrayList<int[]>();
    ScrollableResults results = null;
    try {//from w w  w  .j  a v a  2s  .c o  m
        String queryString = "SELECT id FROM CachedParse WHERE collectionId = :collectionId";
        org.hibernate.Query query = ((HibernateQuery) entityManager.createQuery(queryString))
                .getHibernateQuery();
        query.setParameter("collectionId", aCollectionId);
        results = query.scroll();
        results.beforeFirst();
        int row = 0;
        int[] curPage = new int[] { -1, -1 };
        boolean hasNext = results.next();
        while (hasNext) {
            int id = results.getLong(0).intValue();
            // Record start of page
            if ((row % aPageSize) == 0) {
                curPage[0] = id;
            }

            // Step ahead
            hasNext = results.next();
            row++;

            // Record end of page when end of page or end of results is reached
            if (((row % aPageSize) == (aPageSize - 1)) || !hasNext) {
                curPage[1] = id;
                pages.add(curPage);
                curPage = new int[] { -1, -1 };
            }
        }
    } finally {
        if (results != null) {
            results.close();
        }
    }
    return pages.toArray(new int[pages.size()][2]);
}

From source file:edu.vt.vbi.patric.dao.DBDisease.java

License:Apache License

public List<ResultType> getVFDBList(Map<String, String> key, Map<String, String> sort, int start, int end) {

    String sql = "";

    sql += getVFDBSQL(key, "function");

    sql += " GROUP BY vfg_id, gene_name, gene_product, vf_id, vf_name, vf_fullname, function ";

    if (sort != null && sort.containsKey("field") && sort.get("field") != null && sort.containsKey("direction")
            && sort.get("direction") != null) {

        sql += " ORDER BY " + sort.get("field") + " " + sort.get("direction");

    } else {/* w w w.ja v  a  2  s.c o  m*/

        sql += " ORDER BY b.vfg_id, b.gene_name";
    }

    Session session = factory.getCurrentSession();
    session.beginTransaction();
    SQLQuery q = session.createSQLQuery(sql);
    q = bindVFDBSQLValues(q, key);

    if (end > 0) {
        q.setMaxResults(end);
    }

    ScrollableResults scr = q.scroll();
    List<ResultType> results = new ArrayList<>();

    if (start > 1) {
        scr.setRowNumber(start - 1);
    } else {
        scr.beforeFirst();
    }

    for (int i = start; (end > 0 && i < end && scr.next()) || (end == -1 && scr.next()); i++) {

        Object[] obj = scr.get();
        ResultType row = new ResultType();
        row.put("vfg_id", obj[0]);
        row.put("gene_name", obj[1]);
        row.put("gene_product", obj[2]);
        row.put("vf_id", obj[3]);
        row.put("vf_name", obj[4]);
        row.put("vf_fullname", obj[5]);
        row.put("function", obj[6]);
        row.put("feature_count", obj[7]);

        results.add(row);
    }

    session.getTransaction().commit();

    return results;

}

From source file:edu.vt.vbi.patric.dao.DBDisease.java

License:Apache License

public List<ResultType> getVFDBFeatureList(Map<String, String> key, Map<String, String> sort, int start,
        int end) {

    String sql = "";

    sql += getVFDBFeatureSQL(key, "function");

    if (sort != null && sort.containsKey("field") && sort.get("field") != null && sort.containsKey("direction")
            && sort.get("direction") != null) {

        sql += " ORDER BY " + sort.get("field") + " " + sort.get("direction");

    } else {//from   w ww.ja v a  2s  .c  o  m

        sql += " ORDER BY na_feature_id";
    }

    Session session = factory.getCurrentSession();
    session.beginTransaction();
    SQLQuery q = session.createSQLQuery(sql);
    q = bindVFDBFeatureSQLValues(q, key);

    if (end > 0) {
        q.setMaxResults(end);
    }

    ScrollableResults scr = q.scroll();
    List<ResultType> results = new ArrayList<>();

    if (start > 1) {
        scr.setRowNumber(start - 1);
    } else {
        scr.beforeFirst();
    }

    for (int i = start; (end > 0 && i < end && scr.next()) || (end == -1 && scr.next()); i++) {
        Object[] obj = scr.get();
        ResultType row = new ResultType();
        row.put("vf_id", obj[0]);
        row.put("vf_name", obj[1]);
        row.put("vfg_id", obj[2]);
        row.put("gene_name", obj[3]);
        row.put("na_feature_id", obj[4]);
        row.put("genome_info_id", obj[5]);
        row.put("genome_name", obj[6]);
        row.put("accession", obj[7]);
        row.put("locus_tag", obj[8]);
        row.put("product", obj[9]);
        row.put("genome_id", obj[10]);
        row.put("feature_id", obj[11]);
        row.put("patric_id", obj[12]);
        row.put("refseq_locus_tag", obj[13]);
        row.put("alt_locus_tag", obj[14]);

        results.add(row);
    }

    session.getTransaction().commit();

    return results;

}

From source file:edu.vt.vbi.patric.dao.DBDisease.java

License:Apache License

public List<ResultType> getCTDList(Map<String, String> key, Map<String, String> sort, int start, int end) {

    String sql = "";

    sql += getCTDSQL(key, "function");

    if (sort != null && sort.containsKey("field") && sort.get("field") != null && sort.containsKey("direction")
            && sort.get("direction") != null) {

        sql += " ORDER BY " + sort.get("field") + " " + sort.get("direction");

    } else {/*from  w w  w.j  a  v  a 2  s. c om*/

        sql += " ORDER BY cs.gene_sym, cs.disease_name";
    }

    Session session = factory.getCurrentSession();
    session.beginTransaction();
    SQLQuery q = session.createSQLQuery(sql);
    q = bindSQLValues(q, key);

    if (end > 0) {
        q.setMaxResults(end);
    }

    ScrollableResults scr = q.scroll();
    List<ResultType> results = new ArrayList<>();

    if (start > 1) {
        scr.setRowNumber(start - 1);
    } else {
        scr.beforeFirst();
    }

    for (int i = start; (end > 0 && i < end && scr.next()) || (end == -1 && scr.next()); i++) {

        Object[] obj = scr.get();
        ResultType row = new ResultType();
        row.put("gene_sym", obj[0]);
        row.put("gene_id", obj[1]);
        row.put("gene_disease_rel", obj[2]);
        row.put("disease_name", obj[3]);
        row.put("pubmed_id", obj[4]);
        row.put("disease_id", obj[5]);
        row.put("gd_app_name", obj[6]);

        results.add(row);
    }

    session.getTransaction().commit();

    return results;

}

From source file:edu.vt.vbi.patric.dao.DBDisease.java

License:Apache License

public List<ResultType> getCTDGraphList(Map<String, String> key, Map<String, String> sort, int start, int end) {

    String sql = "";

    sql += getCTDSQL(key, "graphlist");

    if (sort != null && sort.containsKey("field") && sort.get("field") != null && sort.containsKey("direction")
            && sort.get("direction") != null) {

        sql += " ORDER BY " + sort.get("field") + " " + sort.get("direction");

    } else {//from   w w w.j  a  v a2s. c o  m

        sql += " ORDER BY cs.gene_sym, cs.disease_name";
    }

    Session session = factory.getCurrentSession();
    session.beginTransaction();
    SQLQuery q = session.createSQLQuery(sql);
    q = bindGraphCTDSQLValues(q, key);

    if (end > 0) {
        q.setMaxResults(end);
    }

    ScrollableResults scr = q.scroll();
    List<ResultType> results = new ArrayList<>();

    if (start > 1) {
        scr.setRowNumber(start - 1);
    } else {
        scr.beforeFirst();
    }

    for (int i = start; (end > 0 && i < end && scr.next()) || (end == -1 && scr.next()); i++) {

        Object[] obj = scr.get();
        ResultType row = new ResultType();
        row.put("gene_sym", obj[0]);
        row.put("gene_id", obj[1]);
        row.put("gene_disease_rel", obj[2]);
        row.put("disease_name", obj[3]);
        row.put("pubmed_id", obj[4]);
        row.put("disease_id", obj[5]);
        row.put("gd_app_name", obj[6]);

        results.add(row);
    }

    session.getTransaction().commit();

    return results;

}

From source file:edu.vt.vbi.patric.dao.DBDisease.java

License:Apache License

public List<ResultType> getGADGraphList(Map<String, String> key, Map<String, String> sort, int start, int end) {

    String sql = "";

    sql += getGADSQL(key, "graphlist");

    if (sort != null && sort.containsKey("field") && sort.get("field") != null && sort.containsKey("direction")
            && sort.get("direction") != null) {

        sql += " ORDER BY " + sort.get("field") + " " + sort.get("direction");

    } else {//from w  w w. j a  va  2 s  .  co m

        sql += " ORDER BY gs.gene_sym";
    }

    Session session = factory.getCurrentSession();
    session.beginTransaction();
    SQLQuery q = session.createSQLQuery(sql);
    q = bindGraphGADSQLValues(q, key);

    if (end > 0) {
        q.setMaxResults(end);
    }

    ScrollableResults scr = q.scroll();
    List<ResultType> results = new ArrayList<>();

    if (start > 1) {
        scr.setRowNumber(start - 1);
    } else {
        scr.beforeFirst();
    }

    for (int i = start; (end > 0 && i < end && scr.next()) || (end == -1 && scr.next()); i++) {

        Object[] obj = scr.get();
        ResultType row = new ResultType();
        row.put("gene_sym", obj[0]);
        row.put("gene_id", obj[1]);
        row.put("association", obj[2]);
        row.put("mesh_disease_terms", obj[3]);
        row.put("broad_phenotype", obj[4]);
        row.put("pubmed_id", obj[5]);
        row.put("conclusion", obj[6]);
        row.put("gd_app_name", obj[7]);

        results.add(row);
    }

    session.getTransaction().commit();

    return results;
}

From source file:edu.vt.vbi.patric.dao.DBDisease.java

License:Apache License

public List<ResultType> getGADList(Map<String, String> key, Map<String, String> sort, int start, int end) {

    String sql = "";

    sql += getGADSQL(key, "function");

    if (sort != null && sort.containsKey("field") && sort.get("field") != null && sort.containsKey("direction")
            && sort.get("direction") != null) {

        sql += " ORDER BY " + sort.get("field") + " " + sort.get("direction");

    } else {/*from w  ww .j a v a  2  s . co  m*/

        sql += " ORDER BY gs.gene_sym";
    }

    Session session = factory.getCurrentSession();
    session.beginTransaction();
    SQLQuery q = session.createSQLQuery(sql);
    q = bindSQLValues(q, key);

    if (end > 0) {
        q.setMaxResults(end);
    }

    ScrollableResults scr = q.scroll();
    List<ResultType> results = new ArrayList<>();

    if (start > 1) {
        scr.setRowNumber(start - 1);
    } else {
        scr.beforeFirst();
    }

    for (int i = start; (end > 0 && i < end && scr.next()) || (end == -1 && scr.next()); i++) {

        Object[] obj = scr.get();
        ResultType row = new ResultType();
        row.put("gene_sym", obj[0]);
        row.put("gene_id", obj[1]);
        row.put("association", obj[2]);
        row.put("mesh_disease_terms", obj[3]);
        row.put("broad_phenotype", obj[4]);
        row.put("pubmed_id", obj[5]);
        row.put("conclusion", obj[6]);
        row.put("gd_app_name", obj[7]);

        results.add(row);
    }

    session.getTransaction().commit();

    return results;
}

From source file:edu.vt.vbi.patric.dao.DBPRC.java

License:Apache License

public ArrayList<ResultType> getPRCData(String taxonId, String filter, int start, int end, String sort,
        String dir) {//from  w ww .j av a2  s . c om

    String sql = "select distinct experiment_id, description, speciesname, processing_type, summary, pubmed_id, count(distinct sample_id) from app.post_genomic";
    if (filter.equals("MS"))
        sql += " where processing_type = 'Mass spectrometry'";
    else if (filter.equals("MA"))
        sql += " where processing_type = 'Microarray'";
    else
        sql += " where processing_type = 'Protein interaction'";
    sql += " and taxon_id in (" + DBSummary.getTaxonIdsInTaxonSQL(":taxonId") + ")";
    sql += " group by experiment_id, description, speciesname, processing_type, summary, pubmed_id";
    sql += " order by " + sort + " " + dir;

    Session session = factory.getCurrentSession();
    session.beginTransaction();
    SQLQuery q = session.createSQLQuery(sql);

    q.setTimeout(SQL_TIMEOUT);
    q.setString("taxonId", taxonId);

    ScrollableResults scr = null;

    try {
        scr = q.scroll();
    } catch (Exception ex) {
        System.out.println("[SQL error]" + taxonId);
        ex.printStackTrace();
        return null;
    }

    ArrayList<ResultType> results = new ArrayList<ResultType>();
    Object[] obj = null;
    if (start > 1) {
        scr.setRowNumber(start - 1);
    } else {
        scr.beforeFirst();
    }

    for (int i = start; (end > 0 && i < end && scr.next() == true) || (end == -1 && scr.next() == true); i++) {
        obj = scr.get();
        ResultType row = new ResultType();

        row.put("experiment_id", obj[0]);
        row.put("description", obj[1]);
        row.put("speciesname", obj[2]);
        row.put("experimenttype", obj[3]);
        row.put("summary", obj[4]);
        row.put("pubmed_id", obj[5]);
        row.put("samples", obj[6]);
        results.add(row);
    }
    session.getTransaction().commit();
    return results;
}

From source file:gallery.service.rss.RssServiceImpl.java

License:Apache License

@Override
@Transactional(readOnly = true)/*from  w  w  w . j av  a2s.co  m*/
public void create() {
    if (generating) {
        logger.info("xml is allready generating ...");
        return;
    }
    try {
        generating = true;
        logger.info("start generate xml");
        long time = System.currentTimeMillis();

        File img_dir = new File(wallpaper_service.getStorePath(), Config.ENCLOSURE_IMG_SUBDIR);

        //get main wallpaper page
        Channel chan;
        List<Pages> temp = pages_service.getByPropertiesValueOrdered(null, MAIN_PSEUDONYMES, MAIN_VALUES, null,
                null);
        if (temp.isEmpty()) {
            chan = new Channel(gallery.web.Config.SITE_NAME, gallery.web.Config.SITE_NAME,
                    gallery.web.Config.SITE_NAME);
        } else {
            //TODO localize it !!!
            IAutoreplaceService.IReplacement repl = autoreplace_service.getAllReplacements("ru");
            String title = repl.replaceAll(temp.get(0).getTitle());
            String description = repl.replaceAll(temp.get(0).getDescription());
            chan = new Channel(title, gallery.web.Config.SITE_NAME, description);
        }

        RSS rss = new RSS();

        chan.setImage(new Channel.Image(gallery.web.Config.SITE_NAME + Config.LOGO_WEBDIR, chan.getTitle(),
                chan.getLink(), 0, 0, null));
        chan.setLastBuildDate(new java.util.Date());
        rss.addChannel(chan);

        ScrollableResults sr = wallpaper_service.getScrollableResults(
                "id, id_pages, description, title, date_upload, name", "active", Boolean.TRUE,
                new String[] { "date_upload" }, new String[] { "DESC" });
        int max_elements = 100;
        sr.beforeFirst();
        while (sr.next() && (max_elements-- > 0)) {
            try {
                Item item = new Item(sr.getString(2), gallery.web.Config.SITE_NAME + "index.htm?id_pages_nav="
                        + sr.getLong(1) + "&id_photo_nav=" + sr.getLong(0), sr.getString(3));
                item.setPubDate(sr.getDate(4));
                long fileLen = (new File(img_dir, sr.getString(5))).length();
                if (fileLen > 0) {
                    item.setEnclosure(new Item.Enclosure(
                            gallery.web.Config.SITE_NAME + Config.ENCLOSURE_IMG_WEBDIR + sr.getString(5),
                            fileLen, "image/jpeg"));
                }
                //item.addCategory(new Item.Category("test"));
                chan.addItem(item);
            } finally {
                //TODO: mb add some logging here
            }
        }
        sr.close();
        try {
            new RSSFeedGeneratorImpl().generateToFile(rss, new File(path, Config.RSS_FILE_NAME), "UTF-8");
        } catch (Exception e) {
            logger.error("error while saving rss to file", e);
        }
        time = System.currentTimeMillis() - time;
        logger.info("end generate xml. generated in: " + time);
    } finally {
        generating = false;
    }
}