001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle. 005 * 006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package pt.ua.dicoogle.server.web.utils; 020 021import java.net.URI; 022import java.util.Iterator; 023 024import net.sf.json.JSONArray; 025import net.sf.json.JSONObject; 026 027import org.apache.commons.lang3.StringUtils; 028 029import pt.ua.dicoogle.core.dim.DIMGeneric; 030import pt.ua.dicoogle.core.dim.Patient; 031import pt.ua.dicoogle.core.dim.Serie; 032import pt.ua.dicoogle.core.dim.Study; 033 034/** 035 * Helper Class to tranlate DIMGeneric objects into JSON. 036 * Usefull for web environments 037 * 038 * TODO: Insert proper documentation. 039 * 040 * @author Tiago Marques Godinho. 041 * 042 */ 043public class DIM2JSONConverter { 044 045 /** 046 * Converts a DIMGeneric object to JSON. 047 * 048 * @param dim DIM Object 049 * @return The resulting JSON Objects, or null if dim is null. 050 */ 051 public static JSONArray convertToJSON(DIMGeneric dim){ 052 JSONArray arr = new JSONArray(); 053 054 Iterator<Patient> it = dim.getPatients().iterator(); 055 while(it.hasNext()){ 056 Patient p = it.next(); 057 058 arr.add(convertToJSON(p)); 059 } 060 061 return arr; 062 } 063 064 /** 065 * Converts a Patient Object to JSON. 066 * 067 * @param patient Patient to be Converted 068 * @return The resulting JSON Object, or null if patient is null. 069 */ 070 public static JSONObject convertToJSON(Patient patient){ 071 072 JSONObject obj = new JSONObject(); 073 074 obj.put("id", StringUtils.trimToNull(patient.getPatientID())); 075 obj.put("name", StringUtils.trimToNull(patient.getPatientName())); 076 obj.put("sex", StringUtils.trimToNull(patient.getPatientSex())); 077 078 JSONArray studies = new JSONArray(); 079 for(Study s : patient.getStudies()){ 080 studies.add(convertToJSON(s)); 081 } 082 obj.put("studies", studies); 083 084 return obj; 085 } 086 087 088 /** 089 * Converts a Study Object to JSON. 090 * 091 * @param study Study to be converted. 092 * @return The resulting JSON Object, or null if study is null. 093 */ 094 private static JSONObject convertToJSON(Study study) { 095 096 JSONObject obj = new JSONObject(); 097 098 obj.put("id",StringUtils.trimToNull( study.getStudyID())); 099 obj.put("uid", StringUtils.trimToNull(study.getStudyInstanceUID())); 100 obj.put("data", StringUtils.trimToNull(study.getStudyData())); 101 obj.put("time", StringUtils.trimToNull(study.getStudyTime())); 102 obj.put("descr", StringUtils.trimToNull(study.getStudyDescription())); 103 obj.put("iname", StringUtils.trimToNull(study.getInstitutuionName())); 104 105 JSONArray series = new JSONArray(); 106 for(Serie serie : study.getSeries()){ 107 series.add(convertToJSON(serie)); 108 } 109 obj.put("series", series); 110 111 return obj; 112 } 113 114 /** 115 * Converts a Series Model to JSON. 116 * 117 * @param serie The DICOM Series model 118 * @return The resulting JSON object, or null if serie is null. 119 */ 120 private static JSONObject convertToJSON(Serie serie) { 121 122 JSONObject obj = new JSONObject(); 123 124 obj.put("uid", StringUtils.trimToNull(serie.getSerieInstanceUID())); 125 obj.put("mod", StringUtils.trimToNull(serie.getModality())); 126 obj.put("number", serie.getSerieNumber()); 127 obj.put("descr", StringUtils.trimToNull(serie.getSeriesDescription())); 128 129 JSONArray images = new JSONArray(); 130 Iterator<String> itUID = serie.getSOPInstanceUIDList().iterator(); 131 Iterator<URI> itURI = serie.getImageList().iterator(); 132 while(itUID.hasNext() && itURI.hasNext()){ 133 String uid = itUID.next(); 134 URI uri = itURI.next(); 135 136 JSONObject imgObj = new JSONObject(); 137 imgObj.put("uid", uid); 138 imgObj.put("uri", uri.toString()); 139 140 images.add(imgObj); 141 } 142 obj.put("images", images); 143 144 return obj; 145 } 146}