package org.motechproject.messagecampaign.web.controller;

import com.google.gson.JsonParseException;
import org.apache.commons.io.IOUtils;
import org.motechproject.messagecampaign.service.MessageCampaignService;
import org.motechproject.server.config.SettingsFacade;
import org.motechproject.server.config.service.PlatformSettingsService;
import org.osgi.framework.BundleException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;

@Controller
public class SettingsController {

    @Autowired
    private MessageCampaignService messageCampaignService;

    @Autowired
    private PlatformSettingsService platformSettingsService;

    @Autowired
    @Qualifier("messageCampaignSettings")
    private SettingsFacade settingsFacade;

    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "/settings", method = RequestMethod.POST)
    @PreAuthorize("hasRole('manageCampaigns')")
    public void saveSettings(@ModelAttribute("messageCampaigns") MultipartFile messageCampaigns) throws BundleException, IOException {
        if (messageCampaigns == null || messageCampaigns.isEmpty()) {
            throw new IllegalArgumentException("No file specified");
        }
        String oldConfig = null;
        try {
            oldConfig = IOUtils.toString(platformSettingsService.getRawConfig(settingsFacade.getSymbolicName(),
                    MessageCampaignService.MESSAGE_CAMPAIGNS_JSON_FILENAME));

            platformSettingsService.saveRawConfig(settingsFacade.getSymbolicName(),
                    MessageCampaignService.MESSAGE_CAMPAIGNS_JSON_FILENAME,
                    messageCampaigns.getInputStream());

            messageCampaignService.loadCampaigns();
        } catch (JsonParseException e) {
            //revert to previous config
            platformSettingsService.saveRawConfig(settingsFacade.getSymbolicName(),
                    MessageCampaignService.MESSAGE_CAMPAIGNS_JSON_FILENAME,
                    new ByteArrayInputStream(oldConfig.getBytes()));
            throw new IllegalArgumentException("Invalid JSON file", e);
        }
    }

}


@@ -265,80 +265,8 @@
 de;%0A
-import org.motechproject.server.config.service.PlatformSettingsService;%0A
 impo
@@ -426,24 +426,125 @@
 .Qualifier;%0A
+import org.springframework.core.io.InputStreamResource;%0Aimport org.springframework.core.io.Resource;%0A
 import org.s
@@ -1190,85 +1190,8 @@
 e;%0A%0A
-    @Autowired%0A    private PlatformSettingsService platformSettingsService;%0A%0A
     
@@ -1812,38 +1812,29 @@
 oString(
-platformS
+s
 ettings
-Servic
+Facad
 e.getRaw
@@ -1844,62 +1844,8 @@
 fig(
-settingsFacade.getSymbolicName(),%0A                    
 Mess
@@ -1915,79 +1915,88 @@
     
-platformSettingsService.saveRawConfig(settingsFacade.getSymbolicN
+Resource resource = new InputStreamResource(messageCampaigns.getInputStre
 am
-e
 ()
-,
+);%0A
 %0A   
@@ -2000,32 +2000,53 @@
             
-        
+settingsFacade.saveRawConfig(
 MessageCampa
@@ -2113,41 +2113,16 @@
     
-messageCampaigns.getInputStream()
+resource
 );%0A%0A
@@ -2270,30 +2270,21 @@
     
-platformS
+s
 ettings
-Servic
+Facad
 e.sa
@@ -2299,62 +2299,8 @@
 fig(
-settingsFacade.getSymbolicName(),%0A                    
 Mess
@@ -2375,16 +2375,40 @@
     new 
+InputStreamResource(new 
 ByteArra
@@ -2442,16 +2442,17 @@
 ytes()))
+)
 ;%0A      

Handle Config changes via UI - MessageCampaigns fix