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.net.URI;
022import java.net.URISyntaxException;
023import java.util.ArrayList;
024import java.util.List;
025import java.util.concurrent.ExecutionException;
026
027import org.restlet.data.Form;
028import org.restlet.data.MediaType;
029import org.restlet.representation.Representation;
030import org.restlet.representation.StringRepresentation;
031import org.restlet.resource.Get;
032import org.restlet.resource.ServerResource;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036import pt.ua.dicoogle.plugins.PluginController;
037import pt.ua.dicoogle.sdk.datastructs.Report;
038import pt.ua.dicoogle.sdk.task.Task;
039
040public class ForceIndexing extends ServerResource{
041    
042        private static final Logger log = LoggerFactory.getLogger(ForceIndexing.class); 
043        
044    @Get
045    public Representation represent(){
046        
047        Form queryForm = getRequest().getResourceRef().getQueryAsForm();
048        //System.out.println("Fetching Data");
049        String[] uris = queryForm.getValuesArray("uri");
050        String pluginName = queryForm.getValues("plugin");
051        
052        PluginController pc = PluginController.getInstance();
053        //System.out.println("Generating Tasks");
054        List<Task<Report>> reports  = new ArrayList<>(uris.length);
055        for(String uriString : uris) {
056            URI u = null;
057            try {
058                u = new URI(uriString.replaceAll(" ","%20"));
059            } catch (URISyntaxException ex) {
060                log.error("Could not create URI", ex);
061                ex.printStackTrace();
062            } 
063            if(u != null){
064                log.info("Sent Index Request: {}, {}",pluginName, u.toString());
065                if(pluginName == null)
066                        reports.addAll(pc.index(u));
067                else
068                        reports.addAll(pc.index(pluginName,u));
069            }
070        }
071        
072        //System.out.println("Waiting for Results");
073        List<Report> done = new ArrayList<>(reports.size());
074        StringBuilder builder = new StringBuilder();
075        for(Task<Report> t : reports){
076            try {
077                Report r = t.get();
078                done.add(r);
079                builder.append(r).append("\n");
080            }catch(InterruptedException | ExecutionException ex){
081                log.error("UNKNOW ERROR", ex);
082                ex.printStackTrace();
083            }
084        }
085        //System.out.println("Exporting Results");
086        
087        log.info("Finished forced indexing procedure: {}", reports.size());
088        
089        return new StringRepresentation(builder.toString(), MediaType.TEXT_PLAIN);
090    }
091}