package com.imcode.controllers.html;

import com.imcode.entities.SchemaVersion;
import com.imcode.services.SchemaVersionService;
import com.imcode.utils.DatabaseWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import org.apache.commons.dbcp2.BasicDataSource;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * Created by ruslan on 09.06.16.
 */
@Controller
@RequestMapping("/schema_versions")
public class SchemaVersionController {

    @Autowired
    @Qualifier("dataSource")
    private BasicDataSource dataSource;

    @Autowired
    private SchemaVersionService schemeVersionService;

    @Autowired
    private ServletContext servletContext;

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView migrate(@RequestParam("file") CommonsMultipartFile multipartFile,
                                @RequestParam(value = "description", required = false) String description,
                                WebRequest webRequest) {

        SchemaVersion currentVersion = createCurrentVersion(multipartFile.getOriginalFilename(), description);

        DatabaseWorker databaseWorker = new DatabaseWorker(dataSource, currentVersion, servletContext);

        if (!databaseWorker.runScript(multipartFile)) {
            schemeVersionService.delete(currentVersion.getId());
        } else {
            databaseWorker.createVersionDump();
        }

        return new ModelAndView("redirect:/test.html");
    }

    @RequestMapping(value = "/current", method = RequestMethod.GET)
    public @ResponseBody SchemaVersion getCurrentSchemaVersion(WebRequest webRequest) {
        return schemeVersionService.findCurrentVersion();
    }

    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody List<SchemaVersion> getAllSchemaVersion(WebRequest webRequest) {
        return schemeVersionService.findAll();
    }

    @RequestMapping(value = "/current/{version}", method = RequestMethod.POST)
    public @ResponseBody SchemaVersion migrateToVersion(@PathVariable("version") String version, WebRequest webRequest) {
        SchemaVersion schemaVersion = schemeVersionService.findVersion(version);
        if (schemaVersion.getCurrent()) {
            return schemaVersion;
        }

        SchemaVersion currentVersion = schemeVersionService.findCurrentVersion();
        currentVersion.setCurrent(false);
        schemeVersionService.save(currentVersion);

        schemaVersion.setCurrent(true);
        schemeVersionService.save(schemaVersion);

        List<SchemaVersion> schemaVersionList = schemeVersionService.findAll();

        DatabaseWorker databaseWorker = new DatabaseWorker(dataSource, schemaVersion, servletContext);

        databaseWorker.migrateToVersion();

        schemeVersionService.findAll().stream()
                .forEach(schema -> schemeVersionService.delete(schema.getId()));

        schemeVersionService.save(schemaVersionList);

        return schemaVersion;
    }

    @RequestMapping(value = "/dump/{version}", method = RequestMethod.GET)
    public void getVersionDump(@PathVariable("version") String version,
                               HttpServletResponse httpServletResponse,
                               WebRequest webRequest) {
        SchemaVersion schemaVersion = schemeVersionService.findVersion(version);

        DatabaseWorker databaseWorker = new DatabaseWorker(dataSource, schemaVersion, servletContext);

        databaseWorker.saveVersionDumpInResponse(httpServletResponse);

//        return new ModelAndView("redirect:/");
    }

    @RequestMapping(value = "/dump", method = RequestMethod.GET)
    public void getVersionDump(HttpServletResponse httpServletResponse,
                                       WebRequest webRequest) {
        DatabaseWorker databaseWorker = new DatabaseWorker(dataSource, null, servletContext);

        databaseWorker.saveVersionDumpInResponse(httpServletResponse);

//        return new ModelAndView("redirect:/test.html");
    }

    @RequestMapping(value = "/delete/{version}", method = RequestMethod.POST)
    public ModelAndView deleteSchemaVersion(@PathVariable("version") String version,
                                            WebRequest webRequest) {

        SchemaVersion schemaVersion = schemeVersionService.findVersion(version);
        if (schemaVersion.getCurrent()) {
            return new ModelAndView("redirect:/test.html");
        }

        schemeVersionService.delete(schemaVersion.getId());

        DatabaseWorker databaseWorker = new DatabaseWorker(dataSource, schemaVersion, servletContext);
        databaseWorker.deleteVersion();

        return new ModelAndView("redirect:/test.html");
    }

    private SchemaVersion createCurrentVersion(String versionName, String description) {
        SchemaVersion currentVersion = schemeVersionService.findCurrentVersion();
        if (currentVersion != null) {
            currentVersion.setCurrent(false);
            schemeVersionService.save(currentVersion);
        }

        SchemaVersion schemaVersion = new SchemaVersion();
        schemaVersion.setCurrent(true);
        schemaVersion.setVersion(versionName.substring(0, versionName.indexOf(".sql")));
        schemaVersion.setTimestamp(new Date());
        if (description != null) {
            schemaVersion.setDescription(description);
        }

        return schemeVersionService.save(schemaVersion);
    }

}

@@ -825,20 +825,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0A/**%0A 
@@ -2411,32 +2411,443 @@
 ndAll();%0A    %7D%0A%0A
+    @RequestMapping(value = %22/access%22, method = RequestMethod.GET)%0A    public @ResponseBody Map%3CString, Object%3E getDataAccess(WebRequest webRequest) %7B%0A        Map%3CString, Object%3E objectMap = new HashMap%3C%3E();%0A        objectMap.put(%22URL%22, dataSource.getUrl());%0A        objectMap.put(%22LOGIN%22, dataSource.getUsername());%0A        objectMap.put(%22PASSWORD%22, dataSource.getPassword());%0A        return objectMap;%0A    %7D%0A%0A
     @RequestMapp

Fix creating dump, deploy changes