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.rest;
020
021import java.util.HashMap;
022import java.util.Iterator;
023import java.util.concurrent.ExecutionException;
024import org.slf4j.LoggerFactory;
025import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml3;
026import org.restlet.resource.Get;
027import org.restlet.resource.ServerResource;
028import pt.ua.dicoogle.plugins.PluginController;
029import pt.ua.dicoogle.sdk.utils.DictionaryAccess;
030import pt.ua.dicoogle.sdk.datastructs.SearchResult;
031import pt.ua.dicoogle.sdk.task.JointQueryTask;
032import pt.ua.dicoogle.sdk.task.Task;
033
034/**
035 *
036 * @author fmvalente
037 */
038
039
040//TODO:add type to file search
041public class RestDumpResource extends ServerResource{
042        
043    @Get
044    public String represent(){
045        
046        
047        
048        String SOPInstanceUID = getRequest().getResourceRef().getQueryAsForm().getValues("uid");
049        if(SOPInstanceUID == null) return null;
050        
051        DictionaryAccess da = DictionaryAccess.getInstance() ; 
052        
053        String query = "SOPInstanceUID:"+SOPInstanceUID;
054        
055        
056        HashMap<String, String> extraFields = new HashMap<String, String>();
057        
058        for (String s : da.getTagList().keySet()) {
059                extraFields.put(s, s);
060        }
061
062        JointQueryTask holder = new JointQueryTask() {
063
064                @Override
065                public void onReceive(Task<Iterable<SearchResult>> e) {
066                    try {
067                        // TODO Auto-generated method stub
068                    System.out.println("onReceive");
069                    for (SearchResult r : e.get())
070                    {
071                        System.out.println(r.getURI());
072                    }
073                    } catch (InterruptedException ex) {
074                        LoggerFactory.getLogger(RestDumpResource.class).error(ex.getMessage(), ex);
075                    } catch (ExecutionException ex) {
076                        LoggerFactory.getLogger(RestDumpResource.class).error(ex.getMessage(), ex);
077                    }
078
079                }
080
081                @Override
082                public void onCompletion() {
083                        // TODO Auto-generated method stub
084                    System.out.println("onComplete");
085
086                }
087        };
088
089        //performs the search
090        Iterable<SearchResult> queryResultList = null;
091        try {
092            System.out.println("Query: + " + query);
093            queryResultList = PluginController.getInstance().queryAll(holder, query, extraFields).get();
094            Thread.sleep(1000);
095        } catch (InterruptedException ex) {
096            LoggerFactory.getLogger(RestDumpResource.class).error(ex.getMessage(), ex);
097        } catch (ExecutionException ex) {
098            LoggerFactory.getLogger(RestDumpResource.class).error(ex.getMessage(), ex);
099        }
100        if (queryResultList==null)
101        {
102            System.err.println("Aborting queryResult is null");
103                return null;
104        }
105        
106        if(!queryResultList.iterator().hasNext()) 
107        {
108            System.err.println("Aborting queryResult does not have next");
109            return null;
110        }//TODO:Throw exception
111        
112        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<tags>\n");
113        
114        Iterator it  = queryResultList.iterator();
115        while (it.hasNext()) {
116                System.out.println("results: +++");
117                SearchResult r = (SearchResult) it.next();
118                for (String s : r.getExtraData().keySet())
119                {
120                    System.out.println(s);
121                    sb.append("\t<tag name=\"").append(s).append("\">").append(escapeHtml3(((String)r.getExtraData().get(s)).trim())).append("</tag>\n");
122                }
123        }
124
125        
126        sb.append("</tags>");
127        
128        //and returns an xml version of our dim search
129        return sb.toString();
130        
131    }
132}