package gov.va.med.srcalc.web.controller.admin;

import gov.va.med.srcalc.domain.model.RiskModel;
import gov.va.med.srcalc.service.AdminService;
import gov.va.med.srcalc.service.InvalidIdentifierException;
import gov.va.med.srcalc.web.view.Views;
import gov.va.med.srcalc.web.view.admin.EditRiskModel;
import gov.va.med.srcalc.web.view.admin.EditRiskModelValidator;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/admin/models/{riskModelId}")
public class EditRiskModelController {

    private static final String ATTRIBUTE_RISK_MODEL = "riskModel";
    
    private static final Logger fLogger = LoggerFactory.getLogger(EditRiskModelController.class);
    
    private final AdminService fAdminService;
    
    /**
     * Constructs an instance.
     * @param adminService the AdminService to use for operations
     */
    @Inject
    public EditRiskModelController(final AdminService adminService )
    {
        fAdminService = adminService;
    }
    
    @ModelAttribute(ATTRIBUTE_RISK_MODEL)
    public EditRiskModel createEditRiskModel(
            @PathVariable final int riskModelId )
            throws InvalidIdentifierException
    {    	
    	RiskModel rm = fAdminService.getRiskModelForId( riskModelId ); 

    	if( rm == null ) {
    		throw new InvalidIdentifierException( "Unable to find RiskModel with ID "+riskModelId );        		
    	}
    	EditRiskModel editModel = EditRiskModel.fromRiskModel( rm );

        return editModel;    		
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView displayForm( )
            throws InvalidIdentifierException
    {
        final ModelAndView mv = new ModelAndView( Views.EDIT_RISK_MODEL );        
        return mv;
    }
    
    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView saveRiskModel(
            @ModelAttribute(ATTRIBUTE_RISK_MODEL) final EditRiskModel saveModel,
            final BindingResult bindingResult)
                    throws InvalidIdentifierException
    {
        // Spring has already bound the user input to saveModel; now validate
        //
    	EditRiskModelValidator validator = new EditRiskModelValidator();
    	validator.validate( saveModel, bindingResult);

        if( bindingResult.hasErrors() )
        {
            fLogger.debug( "EditRiskModel has errors: {}", bindingResult);
            return displayForm( );
        }
        
        // Apply the changes to the persistent model.
        fAdminService.saveRiskModel( saveModel.applyChanges() );

        // Using the POST-redirect-GET pattern.
        return new ModelAndView("redirect:/admin");
    }

}

@@ -947,19 +947,18 @@
 ntroller
- %7B
 %0A
+%7B
 %0A    pri
@@ -1619,19 +1619,17 @@
    %7B
+%0A
     
-%09%0A
     
-%09
 Risk
@@ -1671,17 +1671,16 @@
 elForId(
- 
 riskMode
@@ -1686,23 +1686,32 @@
 elId
- 
 );
- %0A%0A
+%0A        %0A    
     
-%09
 if
-(
  
+(
 rm =
@@ -1720,19 +1720,32 @@
 null
- ) %7B%0A    %09%09
+)%0A        %7B%0A            
 thro
@@ -1777,17 +1777,16 @@
 ception(
- 
 %22Unable 
@@ -1816,9 +1816,11 @@
 ID %22
-+
+ + 
 risk
@@ -1826,27 +1826,27 @@
 kModelId
- 
 );
+%0A
         
 %09%09%0A    %09
@@ -1841,23 +1841,18 @@
     
-%09%09
+%7D
 %0A    
-%09%7D%0A
     
-%09
 Edit
@@ -1901,20 +1901,18 @@
 skModel(
- 
 rm
- 
 );%0A%0A    
@@ -1936,14 +1936,8 @@
 del;
-    %09%09
 %0A   
@@ -2561,17 +2561,20 @@
  //%0A    
-%09
+    
 EditRisk
@@ -2638,9 +2638,12 @@
     
-%09
+    
 vali
@@ -2657,17 +2657,16 @@
 alidate(
- 
 saveMode

Fix formatting.