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.servlets;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import javax.servlet.ServletException;
028import javax.servlet.http.HttpServlet;
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpServletResponse;
031
032import net.sf.json.JSONArray;
033import net.sf.json.JSONException;
034import net.sf.json.JSONObject;
035import pt.ua.dicoogle.core.query.ExportToCSVQueryTask;
036import pt.ua.dicoogle.plugins.PluginController;
037
038/**
039 * @author fredericosilva@ua.pt
040 */
041public class ExportToCSVServlet extends HttpServlet {
042
043        private static final long serialVersionUID = 1L;
044
045        @Override
046        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
047                        throws ServletException, IOException {
048                resp.setHeader("Content-disposition","attachment; filename=QueryResultsExport.csv");
049                String queryString = req.getParameter("query");
050                String[] fields = req.getParameterValues("fields");
051                String[] providers = req.getParameterValues("providers");
052                
053                if(queryString == null)
054                        resp.sendError(401, "Query Parameters not found");
055                
056                if(fields == null || fields.length==0)
057                        resp.sendError(402, "Fields Parameters not found");
058                                                        
059            List<String> fieldList = new ArrayList<>(fields.length);
060            Map<String, String> fieldsMap = new HashMap<>();
061            for(String f : fields){
062                fieldList.add(f);
063                fieldsMap.put(f, f);
064            }
065                            
066        ExportToCSVQueryTask task = new ExportToCSVQueryTask( fieldList,
067                                resp.getOutputStream());
068
069        if(providers == null || providers.length == 0) {
070                        PluginController.getInstance().queryAll(task, queryString, fieldsMap);
071                } else {
072                        List<String> providersList = new ArrayList<>();
073                        for (String f : providers) {
074                                providersList.add(f);
075                        }
076                        PluginController.getInstance().query(task, providersList, queryString,
077                                        fieldsMap);
078                }
079
080                task.await();
081        }
082        
083        @Override
084        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
085                        throws ServletException, IOException {
086                // TODO Auto-generated method stub
087                String dataString = req.getParameter("JSON-DATA");
088                if (dataString == null) {
089                        resp.sendError(401,
090                                        "No data suplied: Please fill the field \"JSON-DATA\"");
091                        return;
092                }
093
094                List<String> orderedFields = new ArrayList<>();
095                Map<String, String> fields = new HashMap<>();
096                String queryString = null;
097                JSONArray arr;          
098                
099                try {
100                        JSONObject data = JSONObject.fromObject(dataString);
101                        queryString = data.getString("queryString");
102                        if (queryString == null) {
103                                resp.sendError(
104                                                402,
105                                                "QueryString no suplied: Please fill the field \"queryString\" in \"JSON-DATA\"");
106                                return;
107                        }
108
109                        arr = data.getJSONArray("extraFields");
110                        if (arr.isEmpty()) {
111                                resp.sendError(403,
112                                                "No fields no suplied: Please fill the field \"extraFiekds\" in \"JSON-DATA\"");
113                                return;
114                        }
115
116                        for (Object f : arr) {
117                                fields.put(f.toString(), f.toString());
118                                orderedFields.add(f.toString());
119                        }
120
121                        arr = data.getJSONArray("providers");
122                } catch (JSONException ex) {
123                        resp.sendError(400,
124                                        "Error parsing the JSON String: " + ex.toString());
125                        return;
126                }
127
128                ExportToCSVQueryTask task = new ExportToCSVQueryTask(orderedFields,
129                                resp.getOutputStream());
130
131                if (arr.isEmpty()) {
132                        PluginController.getInstance().queryAll(task, queryString, fields);
133                } else {
134                        List<String> providers = new ArrayList<>();
135                        for (Object f : arr) {
136                                providers.add(f.toString());
137                        }
138                        PluginController.getInstance().query(task, providers, queryString,
139                                        fields);
140                }
141
142                task.await();
143                
144        }
145
146}