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.io.File;
022import org.restlet.data.Disposition;
023import org.restlet.data.MediaType;
024import org.restlet.representation.FileRepresentation;
025import org.restlet.representation.Representation;
026import org.restlet.representation.StringRepresentation;
027import org.restlet.resource.Get;
028import org.restlet.resource.ServerResource;
029import pt.ua.dicoogle.server.web.rest.elements.ExamTimeCore;
030
031/**
032 *
033 * @author samuelcampos
034 */
035public class ExamTimeResource extends ServerResource {
036
037    @Get
038    public Representation represent() {
039        String action = getRequest().getResourceRef().getQueryAsForm().getValues("action");
040
041        if (action == null) {
042            return new StringRepresentation("", MediaType.TEXT_ALL);
043        }
044
045        ExamTimeCore examTime = ExamTimeCore.getInstance();
046
047
048        if (action.equalsIgnoreCase("getState")) {
049            ExamTimeCore.ExamTimeState state = examTime.getState();
050
051            return new StringRepresentation(state.name(), MediaType.TEXT_ALL);
052        }
053        
054        if (action.equalsIgnoreCase("percentage")) {
055            return new StringRepresentation(examTime.getPercentage() + "", MediaType.TEXT_ALL);
056        }
057
058        if (action.equalsIgnoreCase("startCalc")) {
059            examTime.startThread();
060
061            return new StringRepresentation("OK", MediaType.TEXT_ALL);
062        }
063
064        if (action.equalsIgnoreCase("stopCalc")) {
065            examTime.stopThread();
066
067            return new StringRepresentation("OK", MediaType.TEXT_ALL);
068        }
069
070        if (action.equalsIgnoreCase("download")) {
071            File file = examTime.getFile();
072            
073            if(file == null){
074                return new StringRepresentation("ERROR", MediaType.TEXT_ALL);
075            }
076            
077            FileRepresentation FRepre = new FileRepresentation(file, MediaType.TEXT_CSV);
078            FRepre.getDisposition().setType(Disposition.TYPE_ATTACHMENT);
079            FRepre.getDisposition().setFilename(file.getName());
080
081            return FRepre;
082        }
083
084
085        return new StringRepresentation("UNKNOW_ACTION", MediaType.TEXT_ALL);
086    }
087}