Search in sources :

Example 1 with ImplTransformer

use of gov.nih.nci.ctd2.dashboard.util.ImplTransformer 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 2 with ImplTransformer

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

the class ObservationsAPI method getObservations.

@Transactional
@RequestMapping(value = "{submissionId}/{indexRanges}", method = { RequestMethod.GET }, headers = "Accept=application/json")
public ResponseEntity<String> getObservations(@PathVariable String submissionId, @PathVariable String indexRanges) {
    /* submissionId is not internal ID as other parts of code. it is the identifier as part of stableURL */
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    Set<Integer> indexes = new HashSet<Integer>();
    try {
        for (String range : indexRanges.split(",")) {
            String[] x = range.split("-");
            if (x.length == 1) {
                int index = Integer.parseInt(x[0]);
                indexes.add(index);
            } else if (x.length == 2) {
                int index1 = Integer.parseInt(x[0]);
                int index2 = Integer.parseInt(x[1]);
                if (index2 <= index1) {
                    log.warn("incorrect ranges syntax:" + indexRanges);
                    return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
                }
                for (int i = index1; i <= index2; i++) {
                    indexes.add(i);
                }
            } else {
                log.warn("incorrect ranges syntax:" + indexRanges);
                return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
            }
        }
    } catch (NumberFormatException ex) {
        log.warn(ex.getMessage());
        return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
    }
    try {
        ObservationItem[] observations = dashboardDao.getObservations(submissionId, indexes);
        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(observations);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<String>(json, headers, HttpStatus.OK);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ImplTransformer(gov.nih.nci.ctd2.dashboard.util.ImplTransformer) FieldNameTransformer(gov.nih.nci.ctd2.dashboard.api.FieldNameTransformer) Date(java.util.Date) ResponseEntity(org.springframework.http.ResponseEntity) SimpleDateTransformer(gov.nih.nci.ctd2.dashboard.api.SimpleDateTransformer) ExcludeTransformer(gov.nih.nci.ctd2.dashboard.api.ExcludeTransformer) HashSet(java.util.HashSet) 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 3 with ImplTransformer

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

the class JSONController method getEntityInJson.

@Transactional
@RequestMapping(value = "{type}/{id}", method = { RequestMethod.GET, RequestMethod.POST }, headers = "Accept=application/json")
public ResponseEntity<String> getEntityInJson(@PathVariable String type, @PathVariable String id) {
    DashboardEntity entityById = null;
    Class<? extends DashboardEntity> clazz = Subject.class;
    if (type.equalsIgnoreCase("subject")) {
        clazz = Subject.class;
    } else if (type.equals("observedsubject")) {
        clazz = ObservedSubject.class;
    }
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    log.debug("JSONController " + type + " " + id);
    if (typesWithStableURL.contains(type)) {
        String stableURL = type + "/" + id;
        if (type.equals("observedevidence"))
            stableURL = "mra/" + id;
        entityById = dashboardDao.getEntityByStableURL(type, stableURL);
    } else {
        entityById = dashboardDao.getEntityById(clazz, Integer.parseInt(id));
    }
    if (entityById == null) {
        return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
    }
    JSONSerializer jsonSerializer = new JSONSerializer().transform(new ImplTransformer(), Class.class).transform(new DateTransformer(), Date.class);
    return new ResponseEntity<String>(jsonSerializer.deepSerialize(entityById), 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) JSONSerializer(flexjson.JSONSerializer) Transactional(org.springframework.transaction.annotation.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ImplTransformer

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

the class ListController method getSearchResultsInJson.

@Transactional
@RequestMapping(value = "{type}", method = { RequestMethod.GET, RequestMethod.POST }, headers = "Accept=application/json")
public ResponseEntity<String> getSearchResultsInJson(@PathVariable String type, @RequestParam(value = "filterBy", required = false) Integer filterBy, @RequestParam(value = "getAll", required = false, defaultValue = "false") Boolean getAll) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    List<? extends DashboardEntity> entities = webServiceUtil.getDashboardEntities(type, filterBy);
    if (!getAll && entities.size() > getMaxNumberOfEntities()) {
        entities = entities.subList(0, getMaxNumberOfEntities());
    }
    JSONSerializer jsonSerializer = new JSONSerializer().transform(new ImplTransformer(), Class.class).transform(new DateTransformer(), Date.class);
    return new ResponseEntity<String>(jsonSerializer.serialize(entities), 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) JSONSerializer(flexjson.JSONSerializer) Transactional(org.springframework.transaction.annotation.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with ImplTransformer

use of gov.nih.nci.ctd2.dashboard.util.ImplTransformer 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)

Aggregations

JSONSerializer (flexjson.JSONSerializer)22 ImplTransformer (gov.nih.nci.ctd2.dashboard.util.ImplTransformer)22 HttpHeaders (org.springframework.http.HttpHeaders)22 ResponseEntity (org.springframework.http.ResponseEntity)22 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)22 Transactional (org.springframework.transaction.annotation.Transactional)20 DateTransformer (gov.nih.nci.ctd2.dashboard.util.DateTransformer)18 Observation (gov.nih.nci.ctd2.dashboard.model.Observation)5 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 ExcludeTransformer (gov.nih.nci.ctd2.dashboard.api.ExcludeTransformer)4 SimpleDateTransformer (gov.nih.nci.ctd2.dashboard.api.SimpleDateTransformer)4 Submission (gov.nih.nci.ctd2.dashboard.model.Submission)4 SearchResults (gov.nih.nci.ctd2.dashboard.util.SearchResults)3 FieldNameTransformer (gov.nih.nci.ctd2.dashboard.api.FieldNameTransformer)2 ObservationItem (gov.nih.nci.ctd2.dashboard.api.ObservationItem)2 DashboardDao (gov.nih.nci.ctd2.dashboard.dao.DashboardDao)2 BigInteger (java.math.BigInteger)2 HashSet (java.util.HashSet)2 List (java.util.List)2