Search in sources :

Example 1 with EcoBrowse

use of gov.nih.nci.ctd2.dashboard.util.EcoBrowse in project nci-ctd2-dashboard by CBIIT.

the class EcoController method browse.

@Transactional
@RequestMapping(value = "browse", method = { RequestMethod.GET, RequestMethod.POST }, headers = "Accept=application/json")
public ResponseEntity<String> browse() {
    final HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    final List<EcoBrowse> list = dashboardDao.getEcoBrowse();
    log.debug("number of ECO terms " + list.size());
    Collections.sort(list, new Comparator<EcoBrowse>() {

        @Override
        public int compare(final EcoBrowse o1, final EcoBrowse o2) {
            return o2.getNumberOfSubmissions() - o1.getNumberOfSubmissions();
        }
    });
    final JSONSerializer jsonSerializer = new JSONSerializer().transform(new ImplTransformer(), Class.class).transform(new DateTransformer(), Date.class);
    return new ResponseEntity<String>(jsonSerializer.deepSerialize(list), headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ImplTransformer(gov.nih.nci.ctd2.dashboard.util.ImplTransformer) DateTransformer(gov.nih.nci.ctd2.dashboard.util.DateTransformer) EcoBrowse(gov.nih.nci.ctd2.dashboard.util.EcoBrowse) JSONSerializer(flexjson.JSONSerializer) Transactional(org.springframework.transaction.annotation.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with EcoBrowse

use of gov.nih.nci.ctd2.dashboard.util.EcoBrowse in project nci-ctd2-dashboard by CBIIT.

the class DashboardDaoImpl method getEcoBrowse.

@Override
public List<EcoBrowse> getEcoBrowse() {
    Session session = getSession();
    @SuppressWarnings("unchecked") org.hibernate.query.Query<Object[]> ecocodeQuery = session.createNativeQuery("SELECT ecocode, id, tier, submissionCenter_id FROM observation_template WHERE LENGTH(ecocode)>0");
    List<Object[]> ecocodeResult = ecocodeQuery.list();
    Map<String, EcoBrowse> map = new HashMap<String, EcoBrowse>();
    Map<String, Set<Integer>> mapTier1centers = new HashMap<String, Set<Integer>>();
    Map<String, Set<Integer>> mapTier2centers = new HashMap<String, Set<Integer>>();
    Map<String, Set<Integer>> mapTier3centers = new HashMap<String, Set<Integer>>();
    for (Object[] array : ecocodeResult) {
        String allcodes = (String) array[0];
        Integer templateId = (Integer) array[1];
        Integer tier = (Integer) array[2];
        Integer centerId = (Integer) array[3];
        String countSql = "SELECT COUNT(DISTINCT submission.id), COUNT(DISTINCT observation.id)" + " FROM observation JOIN submission ON observation.submission_id=submission.id" + " JOIN observation_template ON submission.observationTemplate_id=observation_template.id" + " WHERE submission.observationTemplate_id=" + templateId;
        @SuppressWarnings("unchecked") org.hibernate.query.Query<Object[]> query = session.createNativeQuery(countSql);
        Object[] result = query.getSingleResult();
        BigInteger submissionCount = (BigInteger) result[0];
        BigInteger tierCount = (BigInteger) result[1];
        String[] ecocodes = allcodes.split("\\|");
        for (String code : ecocodes) {
            EcoBrowse b = map.get(code);
            if (b == null) {
                // this term not in the map yet
                @SuppressWarnings("unchecked") org.hibernate.query.Query<ECOTerm> ecotermQuery = session.createQuery("FROM ECOTermImpl WHERE code='" + code + "'");
                ECOTerm term = ecotermQuery.getSingleResult();
                b = new EcoBrowse(term.getDisplayName(), term.getStableURL(), 0);
                map.put(code, b);
            }
            b.setNumberOfSubmissions(b.getNumberOfSubmissions() + submissionCount.intValue());
            Set<Integer> centerSet = null;
            switch(tier) {
                case 1:
                    b.setNumberOfTier1Observations(b.getNumberOfTier1Observations() + tierCount.intValue());
                    centerSet = mapTier1centers.get(code);
                    if (centerSet == null) {
                        centerSet = new HashSet<Integer>();
                        mapTier1centers.put(code, centerSet);
                    }
                    break;
                case 2:
                    b.setNumberOfTier2Observations(b.getNumberOfTier2Observations() + tierCount.intValue());
                    centerSet = mapTier2centers.get(code);
                    if (centerSet == null) {
                        centerSet = new HashSet<Integer>();
                        mapTier2centers.put(code, centerSet);
                    }
                    break;
                case 3:
                    b.setNumberOfTier3Observations(b.getNumberOfTier3Observations() + tierCount.intValue());
                    centerSet = mapTier3centers.get(code);
                    if (centerSet == null) {
                        centerSet = new HashSet<Integer>();
                        mapTier3centers.put(code, centerSet);
                    }
                    break;
                default:
                    log.error("unknow tier number " + tier);
            }
            centerSet.add(centerId);
        }
    }
    map.forEach((code, browseItem) -> {
        int tier1 = 0, tier2 = 0, tier3 = 0;
        Set<Integer> set1 = mapTier1centers.get(code);
        Set<Integer> set2 = mapTier2centers.get(code);
        Set<Integer> set3 = mapTier3centers.get(code);
        if (set1 != null)
            tier1 = set1.size();
        if (set2 != null)
            tier2 = set2.size();
        if (set3 != null)
            tier3 = set3.size();
        browseItem.setNumberOfTier1SubmissionCenters(tier1);
        browseItem.setNumberOfTier2SubmissionCenters(tier2);
        browseItem.setNumberOfTier3SubmissionCenters(tier3);
    });
    session.close();
    log.debug("map size " + map.size());
    return new ArrayList<EcoBrowse>(map.values());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EcoBrowse(gov.nih.nci.ctd2.dashboard.util.EcoBrowse) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) ECOTerm(gov.nih.nci.ctd2.dashboard.model.ECOTerm) FullTextSession(org.hibernate.search.FullTextSession) Session(org.hibernate.Session)

Aggregations

EcoBrowse (gov.nih.nci.ctd2.dashboard.util.EcoBrowse)2 JSONSerializer (flexjson.JSONSerializer)1 ECOTerm (gov.nih.nci.ctd2.dashboard.model.ECOTerm)1 DateTransformer (gov.nih.nci.ctd2.dashboard.util.DateTransformer)1 ImplTransformer (gov.nih.nci.ctd2.dashboard.util.ImplTransformer)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Session (org.hibernate.Session)1 FullTextSession (org.hibernate.search.FullTextSession)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 Transactional (org.springframework.transaction.annotation.Transactional)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1