Search in sources :

Example 11 with Observation

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

the class StoriesController method getSearchResultsInJson.

@Transactional
@RequestMapping(method = { RequestMethod.POST, RequestMethod.GET }, headers = "Accept=application/json")
public ResponseEntity<String> getSearchResultsInJson(@RequestParam("limit") Integer limit) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    ArrayList<Observation> entities = new ArrayList<Observation>();
    // If no limit, then show everything
    if (limit == -1)
        limit = Integer.MAX_VALUE;
    for (Submission submission : dashboardDao.findSubmissionByIsStory(true, true)) {
        List<Observation> observationsBySubmission = dashboardDao.findObservationsBySubmission(submission);
        // Story submissions have a single observation in them
        assert observationsBySubmission.size() == 1;
        entities.addAll(observationsBySubmission);
    }
    Collections.sort(entities, new Comparator<Observation>() {

        @Override
        public int compare(Observation o1, Observation o2) {
            Integer rank1 = o1.getSubmission().getObservationTemplate().getSubmissionStoryRank();
            Integer rank2 = o2.getSubmission().getObservationTemplate().getSubmissionStoryRank();
            return rank1 - rank2;
        }
    });
    /*
        entities.sort(new Comparator<Observation>() {
            @Override
            public int compare(Observation o1, Observation o2) {
                Integer rank1 = o1.getSubmission().getObservationTemplate().getSubmissionStoryRank();
                Integer rank2 = o2.getSubmission().getObservationTemplate().getSubmissionStoryRank();
                return rank1-rank2;
            }
        });
        */
    JSONSerializer jsonSerializer = new JSONSerializer().transform(new ImplTransformer(), Class.class).transform(new DateTransformer(), Date.class);
    return new ResponseEntity<String>(jsonSerializer.serialize(entities.subList(0, Math.min(limit, entities.size()))), headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Submission(gov.nih.nci.ctd2.dashboard.model.Submission) ImplTransformer(gov.nih.nci.ctd2.dashboard.util.ImplTransformer) ArrayList(java.util.ArrayList) DateTransformer(gov.nih.nci.ctd2.dashboard.util.DateTransformer) ResponseEntity(org.springframework.http.ResponseEntity) Observation(gov.nih.nci.ctd2.dashboard.model.Observation) JSONSerializer(flexjson.JSONSerializer) Transactional(org.springframework.transaction.annotation.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with Observation

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

the class RssController method generateFeed.

private String generateFeed(List<? extends DashboardEntity> dashboardEntities, String rssTitlePostfix, String rssDescription, String rssLink) {
    String dashboardUrl = context.getScheme() + "://" + context.getServerName() + context.getContextPath() + "/";
    // Set the stage and define metadata on the RSS feed
    SyndFeed feed = new SyndFeedImpl();
    feed.setFeedType("rss_2.0");
    feed.setTitle("CTD^2 Dashboard - " + rssTitlePostfix);
    feed.setDescription(rssDescription);
    feed.setLink(rssLink);
    // We will have two major categories: Subject and Submission
    // and will have subcategories of Subject for each particular subject
    List<SyndCategory> categories = new ArrayList<SyndCategory>();
    SyndCategory subjectCategory = new SyndCategoryImpl();
    subjectCategory.setName("Subject");
    categories.add(subjectCategory);
    SyndCategory submissionCategory = new SyndCategoryImpl();
    submissionCategory.setName("Submission");
    categories.add(submissionCategory);
    feed.setCategories(categories);
    feed.setLanguage("en");
    feed.setManagingEditor("ocg@mail.nih.gov (CTD^2 Network)");
    SyndImage feedImg = new SyndImageImpl();
    feedImg.setTitle("CTD^2 Logo");
    feedImg.setUrl(dashboardUrl + "img/logos/ctd2_overall.png");
    feedImg.setLink(dashboardUrl);
    feed.setImage(feedImg);
    // And prepare the items to be put into the RSS
    List<SyndEntry> rssItems = new ArrayList<SyndEntry>();
    for (DashboardEntity entity : dashboardEntities) {
        if (entity instanceof Subject) {
            Subject subject = (Subject) entity;
            // Collect all role & submission pairs for this particular subject
            Map<Submission, Set<String>> roleMap = new HashMap<Submission, Set<String>>();
            Map<Submission, Set<ObservedSubject>> osMap = new HashMap<Submission, Set<ObservedSubject>>();
            List<ObservedSubject> sObservations = dashboardDao.findObservedSubjectBySubject((Subject) entity);
            for (ObservedSubject sObservation : sObservations) {
                String role = sObservation.getObservedSubjectRole().getSubjectRole().getDisplayName();
                Submission submission = sObservation.getObservation().getSubmission();
                Set<String> roles = roleMap.get(submission);
                if (roles == null) {
                    roles = new HashSet<String>();
                    roleMap.put(submission, roles);
                }
                roles.add(role);
                Set<ObservedSubject> oses = osMap.get(submission);
                if (oses == null) {
                    oses = new HashSet<ObservedSubject>();
                    osMap.put(submission, oses);
                }
                oses.add(sObservation);
            }
            // These will always belong to "Subject"
            List<SyndCategory> sCategories = new ArrayList<SyndCategory>();
            sCategories.add(subjectCategory);
            // and "Subject/<name>" categories
            SyndCategory sCategory = new SyndCategoryImpl();
            sCategory.setName(subjectCategory.getName() + "/" + entity.getDisplayName());
            categories.add(sCategory);
            // Also add this specific category to the global list
            sCategories.add(sCategory);
            // And now combine this information into individual items around submissions/subject
            for (Submission submission : roleMap.keySet()) {
                Set<String> roles = roleMap.get(submission);
                assert roles != null;
                Set<ObservedSubject> oses = osMap.get(submission);
                assert oses != null;
                SyndEntry item = new SyndEntryImpl();
                item.setCategories(sCategories);
                // Construct title
                ObservationTemplate observationTemplate = submission.getObservationTemplate();
                String description = observationTemplate.getDescription();
                Integer tier = observationTemplate.getTier();
                String combinedRoles = StringUtils.join(roles, ", ");
                String name = subject.getDisplayName();
                int noOfObservations = oses.size();
                StringBuilder title = new StringBuilder();
                title.append("Role '").append(combinedRoles).append("'").append(": ").append(description).append(" ").append("(").append("Tier ").append(tier).append(" - ").append(noOfObservations).append(" ").append(noOfObservations > 1 ? "observations" : "observation").append(" on ").append(name).append(")");
                item.setTitle(title.toString());
                String centerName = observationTemplate.getSubmissionCenter().getDisplayName();
                item.setAuthor(centerName);
                item.setPublishedDate(submission.getSubmissionDate());
                String submissionDescription = observationTemplate.getSubmissionDescription();
                StringBuilder itemDescStr = new StringBuilder();
                itemDescStr.append("<b>Project</b>: ").append(observationTemplate.getProject()).append("<br />");
                if (submissionDescription != null && !submissionDescription.isEmpty()) {
                    itemDescStr.append("<b>Summary</b>: ").append(submissionDescription);
                }
                SyndContent itemDesc = new SyndContentImpl();
                itemDesc.setType("text/html");
                itemDesc.setValue(itemDescStr.toString());
                item.setDescription(itemDesc);
                // Create a link to the subject page with filters enabled
                item.setLink(dashboardUrl + "#subject/" + subject.getId() + "/" + combinedRoles + "/" + tier);
                item.setUri(String.format("#ctd2#subject#%s#%d", name, title.hashCode() & Integer.MAX_VALUE));
                rssItems.add(item);
            }
        // End of subject-centered items
        } else if (entity instanceof Submission) {
            Submission submission = (Submission) entity;
            SyndEntry item = new SyndEntryImpl();
            ObservationTemplate observationTemplate = submission.getObservationTemplate();
            int noOfObservations = dashboardDao.findObservationsBySubmission(submission).size();
            StringBuilder title = new StringBuilder();
            Boolean isStory = observationTemplate.getIsSubmissionStory();
            title.append(isStory ? "Story" : "Submission").append(": ");
            title.append(observationTemplate.getDescription().replaceAll("\\.$", "")).append(" (").append("Tier ").append(observationTemplate.getTier()).append(" - ").append(noOfObservations).append(" ").append(noOfObservations > 1 ? "observations" : "observation").append(")");
            item.setTitle(title.toString());
            String submissionDescription = observationTemplate.getSubmissionDescription();
            StringBuilder itemDescStr = new StringBuilder();
            itemDescStr.append("<b>Project</b>: ").append(observationTemplate.getProject()).append("<br />");
            if (submissionDescription != null && !submissionDescription.isEmpty()) {
                itemDescStr.append("<b>Summary</b>: ").append(submissionDescription);
            }
            SyndContent itemDesc = new SyndContentImpl();
            itemDesc.setType("text/html");
            itemDesc.setValue(itemDescStr.toString());
            item.setDescription(itemDesc);
            item.setPublishedDate(submission.getSubmissionDate());
            String centerName = observationTemplate.getSubmissionCenter().getDisplayName();
            item.setAuthor(centerName);
            item.setLink(dashboardUrl + "#submission/" + submission.getId());
            item.setUri(String.format("ctd2#submission#%d", title.hashCode() & Integer.MAX_VALUE));
            rssItems.add(item);
        }
    // End of submission item(s)
    }
    // Add all items into the feed
    feed.setEntries(rssItems);
    // And print the XML/RSS version out
    SyndFeedOutput feedOutput = new SyndFeedOutput();
    String feedStr = null;
    try {
        feedStr = feedOutput.outputString(feed, true);
    } catch (FeedException e) {
        e.printStackTrace();
    }
    return feedStr;
}
Also used : SyndCategory(com.rometools.rome.feed.synd.SyndCategory) SyndImageImpl(com.rometools.rome.feed.synd.SyndImageImpl) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) ArrayList(java.util.ArrayList) SyndFeedOutput(com.rometools.rome.io.SyndFeedOutput) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) DashboardEntity(gov.nih.nci.ctd2.dashboard.model.DashboardEntity) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) SyndImage(com.rometools.rome.feed.synd.SyndImage) ObservationTemplate(gov.nih.nci.ctd2.dashboard.model.ObservationTemplate) SyndFeedImpl(com.rometools.rome.feed.synd.SyndFeedImpl) Submission(gov.nih.nci.ctd2.dashboard.model.Submission) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) FeedException(com.rometools.rome.io.FeedException) Subject(gov.nih.nci.ctd2.dashboard.model.Subject) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject) SyndCategoryImpl(com.rometools.rome.feed.synd.SyndCategoryImpl) SyndContent(com.rometools.rome.feed.synd.SyndContent) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject)

Example 13 with Observation

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

the class ObservationAPI method getObservation.

@Transactional
@RequestMapping(value = "{id}", method = { RequestMethod.GET }, headers = "Accept=application/json")
public ResponseEntity<String> getObservation(@PathVariable String id) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    ObservationItem observation = dashboardDao.getObservationInfo("observation/" + id);
    if (observation == null) {
        return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
    }
    log.debug("ready to serialize");
    JSONSerializer jsonSerializer = new JSONSerializer().transform(new ImplTransformer(), Class.class).transform(new SimpleDateTransformer(), Date.class).transform(new FieldNameTransformer("class"), "clazz").transform(new FieldNameTransformer("class"), "subject_list.clazz").transform(new FieldNameTransformer("class"), "evidence_list.clazz").transform(new FieldNameTransformer("subject_uri"), "subject_list.uri").transform(new ExcludeTransformer(), void.class).exclude("class").exclude("evidence_list.evidenceName").exclude("evidence_list.columnName").exclude("subject_list.columnName").exclude("subject_list.synonyms").exclude("subject_list.xref").exclude("uri").exclude("id");
    String json = "{}";
    try {
        json = jsonSerializer.deepSerialize(observation);
    } catch (Exception e) {
        e.printStackTrace();
        return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<String>(json, headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ImplTransformer(gov.nih.nci.ctd2.dashboard.util.ImplTransformer) SimpleDateTransformer(gov.nih.nci.ctd2.dashboard.api.SimpleDateTransformer) FieldNameTransformer(gov.nih.nci.ctd2.dashboard.api.FieldNameTransformer) ExcludeTransformer(gov.nih.nci.ctd2.dashboard.api.ExcludeTransformer) Date(java.util.Date) ObservationItem(gov.nih.nci.ctd2.dashboard.api.ObservationItem) JSONSerializer(flexjson.JSONSerializer) Transactional(org.springframework.transaction.annotation.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with Observation

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

the class ObservationController method getObservationsBySubmissionIdAndEcoTerm.

@Transactional
@RequestMapping(value = "bySubmissionAndEcoTerm", method = { RequestMethod.GET, RequestMethod.POST }, headers = "Accept=application/json")
public ResponseEntity<String> getObservationsBySubmissionIdAndEcoTerm(@RequestParam("submissionId") Integer submissionId, @RequestParam("ecocode") String ecocode) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    String summaryTemplate = dashboardDao.getEntityById(Submission.class, submissionId).getObservationTemplate().getObservationSummary();
    List<Observation> observations = dashboardDao.getObservationsForSubmissionAndEcoCode(submissionId, ecocode);
    List<ObservationWithSummary> list = new ArrayList<ObservationWithSummary>();
    for (Observation observation : observations) {
        String expanded = dashboardDao.expandSummary(observation.getId(), summaryTemplate) + " (<a class='button-link' href='#" + observation.getStableURL() + "'>details &raquo;</a>)";
        list.add(new ObservationWithSummary(observation, expanded));
    }
    JSONSerializer jsonSerializer = new JSONSerializer().transform(new ImplTransformer(), Class.class).transform(new DateTransformer(), Date.class);
    return new ResponseEntity<String>(jsonSerializer.serialize(list), headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ImplTransformer(gov.nih.nci.ctd2.dashboard.util.ImplTransformer) Observation(gov.nih.nci.ctd2.dashboard.model.Observation) 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)

Example 15 with Observation

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

the class ObservationController method getBySubjectId.

private List<Observation> getBySubjectId(Integer subjectId, String role, Integer tier) {
    Subject subject = dashboardDao.getEntityById(Subject.class, subjectId);
    if (subject != null) {
        Set<Observation> observations = new HashSet<Observation>();
        for (ObservedSubject observedSubject : dashboardDao.findObservedSubjectBySubject(subject)) {
            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)) {
                observations.add(observedSubject.getObservation());
            }
        }
        List<Observation> list = new ArrayList<Observation>(observations);
        Collections.sort(list, new Comparator<Observation>() {

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

Aggregations

BigInteger (java.math.BigInteger)18 ArrayList (java.util.ArrayList)16 Observation (gov.nih.nci.ctd2.dashboard.model.Observation)14 Session (org.hibernate.Session)13 FullTextSession (org.hibernate.search.FullTextSession)13 Submission (gov.nih.nci.ctd2.dashboard.model.Submission)8 HashMap (java.util.HashMap)8 ObservedSubject (gov.nih.nci.ctd2.dashboard.model.ObservedSubject)7 Subject (gov.nih.nci.ctd2.dashboard.model.Subject)7 HashSet (java.util.HashSet)7 Transactional (org.springframework.transaction.annotation.Transactional)7 JSONSerializer (flexjson.JSONSerializer)6 ObservationTemplate (gov.nih.nci.ctd2.dashboard.model.ObservationTemplate)6 ImplTransformer (gov.nih.nci.ctd2.dashboard.util.ImplTransformer)6 Summary (gov.nih.nci.ctd2.dashboard.util.Summary)6 HttpHeaders (org.springframework.http.HttpHeaders)6 ResponseEntity (org.springframework.http.ResponseEntity)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 ObservedSubjectRole (gov.nih.nci.ctd2.dashboard.model.ObservedSubjectRole)5 DateTransformer (gov.nih.nci.ctd2.dashboard.util.DateTransformer)5