Search in sources :

Example 1 with SubjectWithSummaries

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

the class SubjectScorer method scoreAllRoles.

@Transactional
public void scoreAllRoles() {
    log.info("Removing all role-based scores...");
    List<SubjectWithSummaries> oldEntities = dashboardDao.findEntities(SubjectWithSummaries.class);
    for (SubjectWithSummaries subjectWithSummaries : oldEntities) {
        dashboardDao.delete(subjectWithSummaries);
    }
    log.info("Removed " + oldEntities.size() + " old scores.");
    log.info("Re-scoring all roles...");
    List<SubjectWithSummaries> subjectWithSummariesList = new ArrayList<SubjectWithSummaries>();
    List<SubjectRole> entities = dashboardDao.findEntities(SubjectRole.class);
    for (SubjectRole subjectRole : entities) {
        String keyword = subjectRole.getDisplayName();
        log.info("Scoring subject with role: " + keyword);
        HashMap<Subject, SubjectWithSummaries> subjectToSummaries = new HashMap<Subject, SubjectWithSummaries>();
        HashMap<Subject, HashSet<SubmissionCenter>> subjectToCenters = new HashMap<Subject, HashSet<SubmissionCenter>>();
        HashMap<Subject, HashMap<SubmissionCenter, Integer>> centerBasedScores = new HashMap<Subject, HashMap<SubmissionCenter, Integer>>();
        for (ObservedSubject observedSubject : dashboardDao.findObservedSubjectByRole(keyword)) {
            Subject subject = observedSubject.getSubject();
            SubjectWithSummaries withSummaries = subjectToSummaries.get(subject);
            ObservationTemplate observationTemplate = observedSubject.getObservation().getSubmission().getObservationTemplate();
            SubmissionCenter submissionCenter = observationTemplate.getSubmissionCenter();
            Integer tier = observationTemplate.getTier();
            if (withSummaries == null) {
                withSummaries = new SubjectWithSummaries();
                withSummaries.setRole(keyword);
                withSummaries.setSubject(subject);
                withSummaries.setMaxTier(tier);
                withSummaries.setNumberOfObservations(1);
                HashSet<SubmissionCenter> centers = new HashSet<SubmissionCenter>();
                centers.add(submissionCenter);
                withSummaries.setNumberOfSubmissionCenters(1);
                withSummaries.addSubmission(tier, submissionCenter.getId());
                subjectToCenters.put(subject, centers);
                subjectToSummaries.put(subject, withSummaries);
                HashMap<SubmissionCenter, Integer> cScores = new HashMap<>();
                cScores.put(submissionCenter, tier);
                centerBasedScores.put(subject, cScores);
            } else {
                withSummaries.setMaxTier(Math.max(withSummaries.getMaxTier(), tier));
                withSummaries.setNumberOfObservations(withSummaries.getNumberOfObservations() + 1);
                HashSet<SubmissionCenter> submissionCenters = subjectToCenters.get(subject);
                submissionCenters.add(submissionCenter);
                withSummaries.setNumberOfSubmissionCenters(submissionCenters.size());
                withSummaries.addSubmission(tier, submissionCenter.getId());
                HashMap<SubmissionCenter, Integer> cScores = centerBasedScores.get(subject);
                Integer previousScore = cScores.get(submissionCenter);
                cScores.put(submissionCenter, previousScore == null ? tier : Math.max(tier, previousScore));
            }
        }
        Collection<SubjectWithSummaries> perRole = subjectToSummaries.values();
        for (SubjectWithSummaries subjectWithSummaries : perRole) {
            Integer totalScore = 0;
            for (Integer aScore : centerBasedScores.get(subjectWithSummaries.getSubject()).values()) {
                totalScore += aScore;
            }
            subjectWithSummaries.setScore(totalScore);
        }
        subjectWithSummariesList.addAll(perRole);
        log.info("Done scoring role: " + keyword);
    }
    dashboardDao.batchSave(subjectWithSummariesList, 0);
    log.info("Done scoring all roles...");
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Subject(gov.nih.nci.ctd2.dashboard.model.Subject) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject) SubmissionCenter(gov.nih.nci.ctd2.dashboard.model.SubmissionCenter) SubjectRole(gov.nih.nci.ctd2.dashboard.model.SubjectRole) ObservationTemplate(gov.nih.nci.ctd2.dashboard.model.ObservationTemplate) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with SubjectWithSummaries

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

the class ExploreController method browseByKeyword.

@Transactional
@RequestMapping(value = "{roles}", method = { RequestMethod.GET, RequestMethod.POST }, headers = "Accept=application/json")
public ResponseEntity<String> browseByKeyword(@PathVariable String roles) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    List<SubjectWithSummaries> entities = new ArrayList<SubjectWithSummaries>();
    for (String s : roles.split(",")) {
        String role = s.trim().toLowerCase();
        entities.addAll(getWebServiceUtil().exploreSubjects(role));
    }
    Collections.sort(entities, new Comparator<SubjectWithSummaries>() {

        @Override
        public int compare(SubjectWithSummaries o1, SubjectWithSummaries o2) {
            int i = o2.getScore() - o1.getScore();
            if (i == 0) {
                int j = o2.getMaxTier() - o1.getMaxTier();
                if (j == 0) {
                    return o2.getNumberOfObservations() - o1.getNumberOfObservations();
                } else {
                    return j;
                }
            } else {
                return i;
            }
        }
    });
    JSONSerializer jsonSerializer = new JSONSerializer().transform(new ImplTransformer(), Class.class).transform(new DateTransformer(), Date.class);
    return new ResponseEntity<String>(jsonSerializer.deepSerialize(entities), headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) SubjectWithSummaries(gov.nih.nci.ctd2.dashboard.util.SubjectWithSummaries) ResponseEntity(org.springframework.http.ResponseEntity) ImplTransformer(gov.nih.nci.ctd2.dashboard.util.ImplTransformer) ArrayList(java.util.ArrayList) DateTransformer(gov.nih.nci.ctd2.dashboard.util.DateTransformer) JSONSerializer(flexjson.JSONSerializer) Transactional(org.springframework.transaction.annotation.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ArrayList (java.util.ArrayList)2 Transactional (org.springframework.transaction.annotation.Transactional)2 JSONSerializer (flexjson.JSONSerializer)1 ObservationTemplate (gov.nih.nci.ctd2.dashboard.model.ObservationTemplate)1 ObservedSubject (gov.nih.nci.ctd2.dashboard.model.ObservedSubject)1 Subject (gov.nih.nci.ctd2.dashboard.model.Subject)1 SubjectRole (gov.nih.nci.ctd2.dashboard.model.SubjectRole)1 SubmissionCenter (gov.nih.nci.ctd2.dashboard.model.SubmissionCenter)1 DateTransformer (gov.nih.nci.ctd2.dashboard.util.DateTransformer)1 ImplTransformer (gov.nih.nci.ctd2.dashboard.util.ImplTransformer)1 SubjectWithSummaries (gov.nih.nci.ctd2.dashboard.util.SubjectWithSummaries)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1