// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package ws.rest.spring;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
////import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import ws.rest.T1;
import ws.rest.T1Array;
import ws.rest.Test;

////@CrossOrigin(origins="*",methods={RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT,RequestMethod.DELETE,RequestMethod.OPTIONS},allowedHeaders={"Content-Type","Accept"})
@RestController
@RequestMapping(value="/t1")
public class TestController {
    private Test real = new Test();
    @RequestMapping(value="/{f1}",method=RequestMethod.GET,produces={MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE})
    public ResponseEntity<T1> getOne(@PathVariable("f1") int f1) {
        return new ResponseEntity<T1>(real.getOne(f1), HttpStatus.OK);
    }
    // there is no getAll as it is processed as getSome with default query parameters
    @RequestMapping(value="",method=RequestMethod.GET,produces={MediaType.APPLICATION_XML_VALUE})
    public ResponseEntity<T1Array> getSomeXml(@RequestParam(value="start",defaultValue="0") int start, @RequestParam(value="finish",defaultValue="2147483647") int finish) {
        if(start == 0 && finish == 2147483647) {
            return new ResponseEntity<T1Array>(new T1Array(real.getAll()), HttpStatus.OK);
        } else {
            return new ResponseEntity<T1Array>(new T1Array(real.getSome(start, finish)), HttpStatus.OK);
        }
    }
    @RequestMapping(value="",method=RequestMethod.GET,produces={MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<T1[]> getSomeJson(@RequestParam(value="start",defaultValue="0") int start, @RequestParam(value="finish",defaultValue="2147483647") int finish) {
        if(start == 0 && finish == 2147483647) {
            return new ResponseEntity<T1[]>(real.getAll(), HttpStatus.OK);
        } else {
            return new ResponseEntity<T1[]>(real.getSome(start, finish), HttpStatus.OK);
        }
    }
    @RequestMapping(value="",method=RequestMethod.POST,produces={MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE},consumes={MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE})
    public ResponseEntity<T1> save(@RequestBody T1 o) {
        if(real.save(o)) {
            return new ResponseEntity<T1>(o, HttpStatus.OK);
        } else {
        	return new ResponseEntity<T1>(HttpStatus.NOT_FOUND);
        }
    }
    @RequestMapping(value="/{f1}",method=RequestMethod.PUT,consumes={MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE})
    public ResponseEntity<Void> update(@PathVariable("f1") int f1, @RequestBody T1 o) {
        if(o.getF1() == f1 && real.save(o)) {
        	return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
        } else {
        	return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
        }
    }
    @RequestMapping(value="/{f1}",method=RequestMethod.DELETE)
    public ResponseEntity<Void> delete(@PathVariable("f1") int f1) {
        if(real.delete(f1)) {
        	return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
        } else {
        	return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
        }
    }
}
