Search in sources :

Example 26 with Subject

use of gov.nih.nci.ctd2.dashboard.model.Subject in project nci-ctd2-dashboard by CBIIT.

the class DashboardDaoImpl method findSubjectsBySynonym.

@Override
public List<Subject> findSubjectsBySynonym(String synonym, boolean exact) {
    Set<Subject> subjects = new HashSet<Subject>();
    // First grab the synonyms
    String query = "from SynonymImpl where displayName " + (exact ? " = :synonym" : "like concat('%', :synonym, '%')");
    List<Synonym> synonymList = queryWithClass(query, "synonym", synonym);
    for (Synonym o : synonymList) {
        // Second: find subjects with the synonym
        List<Subject> subjectList = queryWithClass("select o from SubjectImpl as o where :synonyms member of o.synonyms", "synonyms", o);
        for (Subject o2 : subjectList) {
            subjects.add(o2);
        }
    }
    return new ArrayList<Subject>(subjects);
}
Also used : ArrayList(java.util.ArrayList) Synonym(gov.nih.nci.ctd2.dashboard.model.Synonym) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject) Subject(gov.nih.nci.ctd2.dashboard.model.Subject) HashSet(java.util.HashSet)

Example 27 with Subject

use of gov.nih.nci.ctd2.dashboard.model.Subject in project nci-ctd2-dashboard by CBIIT.

the class DashboardDaoImpl method getSubjectCounts.

@Override
public WordCloudEntry[] getSubjectCounts(Integer associatedSubject) {
    String sqlForObservations = "SELECT DISTINCT observation_id FROM observed_subject WHERE subject_id=" + associatedSubject;
    Session session = getSession();
    @SuppressWarnings("unchecked") org.hibernate.query.Query<Integer> query3 = session.createNativeQuery(sqlForObservations);
    List<Integer> observationIds = query3.getResultList();
    // ID=0 is not any object
    StringBuffer idList = new StringBuffer("0");
    for (Integer observationId : observationIds) {
        idList.append("," + observationId);
    }
    List<WordCloudEntry> list = new ArrayList<WordCloudEntry>();
    String sql = "SELECT displayName, count(*) AS x, stableURL FROM observed_subject" + " JOIN dashboard_entity ON observed_subject.subject_id=dashboard_entity.id" + " JOIN subject ON observed_subject.subject_id=subject.id" + " WHERE subject.id!=" + associatedSubject + " AND observation_id IN (" + idList + ") GROUP BY subject.id ORDER BY x DESC LIMIT 250";
    log.debug(sql);
    @SuppressWarnings("unchecked") org.hibernate.query.Query<Object[]> query = session.createNativeQuery(sql);
    for (Object[] obj : query.getResultList()) {
        String subject = (String) obj[0];
        String fullname = null;
        if (subject.length() > ABBREVIATION_LENGTH_LIMIT) {
            fullname = subject;
            subject = shorternSubjectName(subject);
        }
        Integer count = ((BigInteger) obj[1]).intValue();
        String url = (String) obj[2];
        list.add(new WordCloudEntry(subject, count, url, fullname));
    }
    session.close();
    return list.toArray(new WordCloudEntry[0]);
}
Also used : ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) FullTextSession(org.hibernate.search.FullTextSession) Session(org.hibernate.Session) WordCloudEntry(gov.nih.nci.ctd2.dashboard.util.WordCloudEntry)

Example 28 with Subject

use of gov.nih.nci.ctd2.dashboard.model.Subject in project nci-ctd2-dashboard by CBIIT.

the class DashboardDaoImpl method summarizePerSubject.

private Summary summarizePerSubject(Class<? extends Subject> subjectClass, String label) {
    String tableName = "";
    Class<?> implClass = subjectClass.isInterface() ? dashboardFactory.getImplClass(subjectClass) : subjectClass;
    java.lang.annotation.Annotation[] annotation = implClass.getAnnotationsByType(javax.persistence.Table.class);
    if (annotation.length == 1) {
        javax.persistence.Table table = (javax.persistence.Table) annotation[0];
        tableName = table.name();
    } else {
        return new Summary("exception", 0, 0, 0, 0);
    }
    Session session = getSession();
    @SuppressWarnings("unchecked") org.hibernate.query.Query<BigInteger> query = session.createNativeQuery("SELECT count(DISTINCT submission_id) FROM " + tableName + " JOIN observed_subject ON observed_subject.subject_id=" + tableName + ".id" + " JOIN observation ON observation.id=observed_subject.observation_id");
    BigInteger submissions = query.getSingleResult();
    String tierQuery = "SELECT tier, COUNT(DISTINCT observation_id) FROM " + tableName + " JOIN observed_subject ON observed_subject.subject_id=" + tableName + ".id" + " JOIN observation ON observation.id=observed_subject.observation_id" + " JOIN submission ON submission.id=observation.submission_id" + " JOIN observation_template ON observation_template.id=submission.observationTemplate_id" + " GROUP BY tier";
    @SuppressWarnings("unchecked") org.hibernate.query.Query<Object[]> query2 = session.createNativeQuery(tierQuery);
    int[] tierCount = new int[3];
    List<Object[]> result = query2.list();
    for (Object[] obj : result) {
        Integer tier = (Integer) obj[0];
        BigInteger count = (BigInteger) obj[1];
        tierCount[tier - 1] = count.intValue();
    }
    session.close();
    return new Summary(label, submissions.intValue(), tierCount[0], tierCount[1], tierCount[2]);
}
Also used : Annotation(gov.nih.nci.ctd2.dashboard.model.Annotation) BigInteger(java.math.BigInteger) Summary(gov.nih.nci.ctd2.dashboard.util.Summary) BigInteger(java.math.BigInteger) FullTextSession(org.hibernate.search.FullTextSession) Session(org.hibernate.Session)

Example 29 with Subject

use of gov.nih.nci.ctd2.dashboard.model.Subject in project nci-ctd2-dashboard by CBIIT.

the class DashboardDaoImpl method prepareAPIData.

/*
     * This is still necessary for API 2.0 because we need to access roles from
     * subject.
     */
@SuppressWarnings("unchecked")
@Override
public void prepareAPIData() {
    Session session = getSession();
    session.beginTransaction();
    session.createQuery("DELETE FROM EvidenceItem").executeUpdate();
    session.createQuery("DELETE FROM SubjectItem").executeUpdate();
    session.createQuery("DELETE FROM ObservationItem").executeUpdate();
    org.hibernate.query.Query<Object[]> query0 = session.createNativeQuery("SELECT submission.id, observationSummary FROM observation_template JOIN  submission ON observation_template.id=submission.observationTemplate_id");
    List<Object[]> submissions = query0.list();
    for (Object[] objs : submissions) {
        Integer submission_id = (Integer) (objs[0]);
        String observationSummary = (String) (objs[1]);
        org.hibernate.query.Query<Object[]> query = session.createNativeQuery("SELECT id, stableURL FROM observation WHERE submission_id=" + submission_id);
        List<Object[]> result = query.list();
        for (Object[] obj : result) {
            Integer id = (Integer) obj[0];
            String uri = (String) obj[1];
            List<EvidenceItem> evidences = createObservedEvidenceInfo(id);
            List<SubjectItem> subjects = createObservedSubjectInfo(id);
            ObservationItem obsv = new ObservationItem();
            obsv.setId(id);
            obsv.setSubmission_id(submission_id);
            obsv.observation_summary = replaceValues(observationSummary, subjects, evidences);
            obsv.evidence_list = evidences.toArray(new EvidenceItem[0]);
            obsv.subject_list = subjects.toArray(new SubjectItem[0]);
            session.save(obsv);
            obsv.uri = uri;
        }
    }
    session.getTransaction().commit();
    session.close();
}
Also used : EvidenceItem(gov.nih.nci.ctd2.dashboard.api.EvidenceItem) SubjectItem(gov.nih.nci.ctd2.dashboard.api.SubjectItem) BigInteger(java.math.BigInteger) FullTextSession(org.hibernate.search.FullTextSession) Session(org.hibernate.Session) ObservationItem(gov.nih.nci.ctd2.dashboard.api.ObservationItem)

Example 30 with Subject

use of gov.nih.nci.ctd2.dashboard.model.Subject in project nci-ctd2-dashboard by CBIIT.

the class ObservationController method onePerSubmissionBySubjectId.

private List<ObservationWithCount> onePerSubmissionBySubjectId(Integer subjectId, String role, Integer tier) {
    Subject subject = dashboardDao.getEntityById(Subject.class, subjectId);
    if (subject != null) {
        Map<Integer, Integer> submissionIds = new HashMap<Integer, Integer>();
        Map<Integer, Observation> observations = new HashMap<Integer, Observation>();
        for (ObservedSubject observedSubject : dashboardDao.findObservedSubjectBySubject(subject)) {
            Observation observation = observedSubject.getObservation();
            Submission submission = observation.getSubmission();
            Integer submissionId = submission.getId();
            ObservedSubjectRole observedSubjectRole = observedSubject.getObservedSubjectRole();
            String subjectRole = observedSubjectRole.getSubjectRole().getDisplayName();
            Integer observationTier = observedSubject.getObservation().getSubmission().getObservationTemplate().getTier();
            if ((role.equals("") || role.equals(subjectRole)) && (tier == 0 || tier == observationTier)) {
                if (!submissionIds.containsKey(submissionId)) {
                    submissionIds.put(submissionId, 1);
                    observations.put(submissionId, observation);
                } else {
                    submissionIds.put(submissionId, submissionIds.get(submissionId) + 1);
                }
            }
        }
        List<ObservationWithCount> list = new ArrayList<ObservationWithCount>();
        for (Integer submissionId : observations.keySet()) {
            Observation obs = observations.get(submissionId);
            Integer count = submissionIds.get(submissionId);
            list.add(new ObservationWithCount(obs, count));
        }
        Collections.sort(list, new Comparator<ObservationWithCount>() {

            @Override
            public int compare(ObservationWithCount o1, ObservationWithCount o2) {
                Integer tier2 = o2.observation.getSubmission().getObservationTemplate().getTier();
                Integer tier1 = o1.observation.getSubmission().getObservationTemplate().getTier();
                return tier2 - tier1;
            }
        });
        return list;
    } else {
        return new ArrayList<ObservationWithCount>();
    }
}
Also used : Submission(gov.nih.nci.ctd2.dashboard.model.Submission) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Subject(gov.nih.nci.ctd2.dashboard.model.Subject) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject) BigInteger(java.math.BigInteger) Observation(gov.nih.nci.ctd2.dashboard.model.Observation) ObservedSubjectRole(gov.nih.nci.ctd2.dashboard.model.ObservedSubjectRole) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject)

Aggregations

Subject (gov.nih.nci.ctd2.dashboard.model.Subject)15 ArrayList (java.util.ArrayList)14 ObservedSubject (gov.nih.nci.ctd2.dashboard.model.ObservedSubject)12 BigInteger (java.math.BigInteger)12 FullTextSession (org.hibernate.search.FullTextSession)11 Session (org.hibernate.Session)10 HashSet (java.util.HashSet)9 JSONSerializer (flexjson.JSONSerializer)8 ObservationTemplate (gov.nih.nci.ctd2.dashboard.model.ObservationTemplate)8 HashMap (java.util.HashMap)8 HttpHeaders (org.springframework.http.HttpHeaders)8 ResponseEntity (org.springframework.http.ResponseEntity)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 Xref (gov.nih.nci.ctd2.dashboard.model.Xref)7 Transactional (org.springframework.transaction.annotation.Transactional)7 Submission (gov.nih.nci.ctd2.dashboard.model.Submission)6 WordCloudEntry (gov.nih.nci.ctd2.dashboard.util.WordCloudEntry)6 DashboardEntity (gov.nih.nci.ctd2.dashboard.model.DashboardEntity)5 Observation (gov.nih.nci.ctd2.dashboard.model.Observation)5 ObservedSubjectRole (gov.nih.nci.ctd2.dashboard.model.ObservedSubjectRole)5