package org.intermine.webservice.server;

/*
 * Copyright (C) 2002-2016 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import static org.apache.commons.lang.StringUtils.defaultString;
import static org.apache.commons.lang.StringUtils.lowerCase;
import static org.intermine.metadata.StringUtil.trimSlashes;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.intermine.api.InterMineAPI;
import org.intermine.web.logic.Constants;
import org.intermine.webservice.server.core.JSONService;
import org.intermine.webservice.server.output.HTMLTableFormatter;

/**
 * Service for returning the version of this service.
 * @author Alex Kalderimis
 *
 */
public class VersionService extends JSONService
{
    private String header;
    private String versionType;

    /**
     * Constructor
     * @param im The InterMine configuration object.
     */
    public VersionService(InterMineAPI im) {
        super(im);
    }

    @Override
    protected void execute() throws Exception {
        // Serve the webservice version by default, which provides information about
        // server capabilities, rather than the release version, which
        // provides information about the data available.

        if (versionType.startsWith("release")) {
            addResultValue(webProperties.getProperty("project.releaseVersion"), false);
            header = "Release";
        } else if (versionType.startsWith("intermine")) {
            addResultValue(Constants.INTERMINE_VERSION, false);
            header = "InterMine version";
        } else {
            addResultValue(Constants.WEB_SERVICE_VERSION, false);
            header = "API Version";
        }
    }

    @Override
    protected void initState() {
        super.initState();
        versionType = lowerCase(trimSlashes(defaultString(request.getPathInfo(), "ws")));
    }

    @Override
    protected Map<String, Object> getHeaderAttributes() {
        Map<String, Object> attributes = super.getHeaderAttributes();
        if (Format.HTML == getFormat()) {
            List<String> headers = new ArrayList<String>();
            headers.add(header);
            attributes.put(HTMLTableFormatter.KEY_COLUMN_HEADERS, headers);
        }
        return attributes;
    }

    @Override
    protected Format getDefaultFormat() {
        if (hasCallback()) {
            return Format.JSON;
        } else {
            return Format.TEXT;
        }
    }

    @Override
    protected String getResultsKey() {
        return "version";
    }

    @Override
    protected boolean canServe(Format format) {
        return format == Format.JSON
            || format == Format.HTML
            || format == Format.TEXT;
    }

}

@@ -946,35 +946,8 @@
 e%0A%7B%0A
-    private String header;%0A
     
@@ -974,16 +974,16 @@
 onType;%0A
+
 %0A    /**
@@ -1552,40 +1552,8 @@
 e);%0A
-            header = %22Release%22;%0A
     
@@ -1674,50 +1674,8 @@
 e);%0A
-            header = %22InterMine version%22;%0A
     
@@ -1757,44 +1757,8 @@
 e);%0A
-            header = %22API Version%22;%0A
     
@@ -2121,24 +2121,24 @@
 Format()) %7B%0A
-
             
@@ -2181,24 +2181,330 @@
 %3CString%3E();%0A
+            String header = null;%0A            if (versionType.startsWith(%22release%22)) %7B%0A                header = %22Release%22;%0A            %7D else if (versionType.startsWith(%22intermine%22)) %7B%0A                header = %22InterMine version%22;%0A            %7D else %7B%0A                header = %22API Version%22;%0A            %7D%0A
             

update header