package com.aemreunal.controller;

import java.util.List;
import javax.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionSystemException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.aemreunal.config.GlobalSettings;
import com.aemreunal.domain.Beacon;
import com.aemreunal.service.BeaconService;

/*
 **************************
 * Copyright (c) 2014     *
 *                        *
 * This code belongs to:  *
 *                        *
 * Ahmet Emre Ünal        *
 * S001974                *
 *                        *
 * aemreunal@gmail.com    *
 * emre.unal@ozu.edu.tr   *
 *                        *
 * aemreunal.com          *
 **************************
 */

@Controller
@RequestMapping("/Beacon")
public class BeaconController {
    @Autowired
    private BeaconService beaconService;

    /**
     * Get the beacon with the specified ID
     *
     * @param id
     *     The ID of the beacon
     *
     * @return The beacon
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}", produces = "application/json")
    public ResponseEntity<Beacon> viewBeacon(@PathVariable String id) {
        Long beaconIDAsLong;
        try {
            beaconIDAsLong = Long.valueOf(id);
        } catch (NumberFormatException e) {
            return new ResponseEntity<Beacon>(HttpStatus.BAD_REQUEST);
        }

        Beacon beacon = beaconService.findById(beaconIDAsLong);
        if (beacon == null) {
            return new ResponseEntity<Beacon>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Beacon>(beacon, HttpStatus.OK);
    }

    /**
     * Create a multiple new beacons
     *
     * @param restBeacons
     *     The beacon list as JSON object
     *
     * @return The created beacons
     */
    @RequestMapping(method = RequestMethod.POST, value = "/import")
    public ResponseEntity<List<Beacon>> createMultipleBeacon(@RequestBody List<Beacon> restBeacons) {
        for (Beacon restBeacon : restBeacons) {
            try {
                Beacon newBeacon = beaconService.save(restBeacon);
                if (GlobalSettings.DEBUGGING) {
                    System.out.println("Saved beacon with UUID = \'" + newBeacon.getUuid() + "\' major = \'" + newBeacon.getMajor() + "\' minor = \'" + newBeacon.getMinor() + "\'");
                }
            } catch (ConstraintViolationException | TransactionSystemException e) {
                if (GlobalSettings.DEBUGGING) {
                    System.err.println("Unable to save beacon! Constraint violation detected!");
                }
                return new ResponseEntity<List<Beacon>>(HttpStatus.BAD_REQUEST);
            }
        }
        return new ResponseEntity<List<Beacon>>(restBeacons, HttpStatus.CREATED);
    }

    /**
     * Delete the specified beacon
     *
     * @param id
     *     The ID of beacon to delete
     *
     * @return The deleted beacon
     */
    @Transactional
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}", produces = "application/json")
    public ResponseEntity<Beacon> deleteBeacon(@PathVariable String id) {
        Long beaconIDAsLong;
        try {
            beaconIDAsLong = Long.valueOf(id);
        } catch (NumberFormatException e) {
            return new ResponseEntity<Beacon>(HttpStatus.BAD_REQUEST);
        }

        DeleteResponse response = beaconService.delete(beaconIDAsLong);

        switch (response) {
            case DELETED:
                return new ResponseEntity<Beacon>(HttpStatus.OK);
            case FORBIDDEN:
                return new ResponseEntity<Beacon>(HttpStatus.FORBIDDEN);
            case NOT_FOUND:
                return new ResponseEntity<Beacon>(HttpStatus.NOT_FOUND);
            case NOT_DELETED:
                return new ResponseEntity<Beacon>(HttpStatus.NOT_ACCEPTABLE);
            default:
                return new ResponseEntity<Beacon>(HttpStatus.I_AM_A_TEAPOT);
        }
    }

}

@@ -32,85 +32,8 @@
 r;%0A%0A
-import java.util.List;%0Aimport javax.validation.ConstraintViolationException;%0A
 impo
@@ -237,75 +237,8 @@
 er;%0A
-import org.springframework.transaction.TransactionSystemException;%0A
 impo
@@ -349,53 +349,145 @@
 ion.
-*;%0Aimport com.aemreunal.config.GlobalSettings
+PathVariable;%0Aimport org.springframework.web.bind.annotation.RequestMapping;%0Aimport org.springframework.web.bind.annotation.RequestMethod
 ;%0Aim
@@ -1839,33 +1839,37 @@
 .OK);%0A    %7D%0A
-%0A
+//%0A//
     /**%0A
      * Creat
@@ -1852,24 +1852,26 @@
 /%0A//    /**%0A
+//
      * Creat
@@ -1895,23 +1895,27 @@
 beacons%0A
+//
      *%0A
+//
      * @
@@ -1928,24 +1928,26 @@
 restBeacons%0A
+//
      *     T
@@ -1976,23 +1976,27 @@
  object%0A
+//
      *%0A
+//
      * @
@@ -2022,24 +2022,26 @@
 beacons%0A
+//
      */%0A
     @Req
@@ -2024,32 +2024,34 @@
 acons%0A//     */%0A
+//
     @RequestMapp
@@ -2102,16 +2102,18 @@
 mport%22)%0A
+//
     publ
@@ -2198,32 +2198,34 @@
  restBeacons) %7B%0A
+//
         for (Bea
@@ -2252,24 +2252,26 @@
 tBeacons) %7B%0A
+//
             
@@ -2268,32 +2268,34 @@
           try %7B%0A
+//
                 
@@ -2345,16 +2345,18 @@
 eacon);%0A
+//
         
@@ -2387,32 +2387,34 @@
 gs.DEBUGGING) %7B%0A
+//
                 
@@ -2575,24 +2575,26 @@
 () + %22%5C'%22);%0A
+//
             
@@ -2591,32 +2591,34 @@
               %7D%0A
+//
             %7D ca
@@ -2677,32 +2677,34 @@
 mException e) %7B%0A
+//
                 
@@ -2731,24 +2731,26 @@
 EBUGGING) %7B%0A
+//
             
@@ -2834,16 +2834,18 @@
 ted!%22);%0A
+//
         
@@ -2846,32 +2846,34 @@
               %7D%0A
+//
                 
@@ -2929,32 +2929,34 @@
 s.BAD_REQUEST);%0A
+//
             %7D%0A  
@@ -2945,32 +2945,34 @@
 //            %7D%0A
+//
         %7D%0A      
@@ -2957,32 +2957,34 @@
   %7D%0A//        %7D%0A
+//
         return n
@@ -3045,24 +3045,26 @@
 s.CREATED);%0A
+//
     %7D%0A%0A    /

Disable importing beacons