package com.dteknoloji.controller.beacon;

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

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

    /**
     * Get all beacons (Optionally, all with matching criteria)
     *
     * @param uuid (Optional) The UUID of the beacon
     * @param major (Optional) The major of the beacon
     * @param minor (Optional) The minor of the beacon
     * @return All existing beacons (Optionally, all that match the given criteria)
     */
    @RequestMapping(method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<List<Beacon>> getAllBeacons(
            @RequestParam(value = "uuid", required = false, defaultValue = "") String uuid,
            @RequestParam(value = "major", required = false, defaultValue = "") String major,
            @RequestParam(value = "minor", required = false, defaultValue = "") String minor) {
        if (uuid.equals("") && major.equals("") && minor.equals("")) {
            return new ResponseEntity<List<Beacon>>(service.findAll(), HttpStatus.OK);
        } else {
            return getBeaconsWithMatchingCriteria(uuid, major, minor);
        }
    }

    /**
     * Returns the list of beacons that match a given criteria
     *
     * @param uuid (Optional) The UUID of the beacon
     * @param major (Optional) The major of the beacon
     * @param minor (Optional) The minor of the beacon
     * @return The list of beacons that match the given criteria
     */
    private ResponseEntity<List<Beacon>> getBeaconsWithMatchingCriteria(String uuid, String major, String minor) {
        List<Beacon> beacons = service.findBeaconsBySpecs(uuid, major, minor);

        if (beacons.size() == 0) {
            return new ResponseEntity<List<Beacon>>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<List<Beacon>>(beacons, HttpStatus.OK);
    }

    /**
     * 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) {
        Beacon beacon = service.findById(Long.valueOf(id));
        if (beacon == null) {
            return new ResponseEntity<Beacon>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Beacon>(beacon, HttpStatus.OK);
    }

    /**
     * Create a new beacon
     *
     * @param restBeacon The beacon as JSON object
     * @param builder
     * @return The created beacon
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Beacon> createBeacon(@RequestBody Beacon restBeacon, UriComponentsBuilder builder) {
        try {
            Beacon newBeacon = service.save(restBeacon);
            if (GlobalSettings.DEBUGGING) {
                System.out.println("Saved beacon with UUID = \'" + newBeacon.getUuid() + "\' major = \'" + newBeacon.getMajor() + "\' minor = \'" + newBeacon.getMinor() + "\'");
            }
            HttpHeaders headers = new HttpHeaders();
            headers.setLocation(builder.path("/Beacon/{id}").buildAndExpand(newBeacon.getBeaconId().toString()).toUri());
            return new ResponseEntity<Beacon>(newBeacon, headers, HttpStatus.CREATED);
        } catch (ConstraintViolationException | TransactionSystemException e) {
            if (GlobalSettings.DEBUGGING) {
                System.err.println("Unable to save beacon! Constraint violation detected!");
            }
            return new ResponseEntity<Beacon>(HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * Delete the specified beacon
     *
     * @param id The ID of beacon to delete
     * @return The deleted beacon
     */
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}", produces = "application/json")
    public ResponseEntity<Beacon> deleteBeacon(@PathVariable String id) {

        Beacon beacon = service.findById(Long.valueOf(id));
        if (beacon == null) {
            return new ResponseEntity<Beacon>(HttpStatus.NOT_FOUND);
        }

        boolean deleted = service.delete(Long.valueOf(id));
        if (deleted) {
            return new ResponseEntity<Beacon>(beacon, HttpStatus.OK);
        }

        return new ResponseEntity<Beacon>(HttpStatus.FORBIDDEN);
    }

}

@@ -4262,32 +4262,1222 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Create a multiple new beacons%0A     *%0A     * @param restBeacons The beacon list as JSON object%0A     * @param builder%0A     * @return The created beacons%0A     */%0A    @RequestMapping(method = RequestMethod.POST, value = %22/import%22)%0A    public ResponseEntity%3CList%3CBeacon%3E%3E createMultipleBeacon(@RequestBody List%3CBeacon%3E restBeacons, UriComponentsBuilder builder) %7B%0A        for (Beacon restBeacon : restBeacons) %7B%0A            try %7B%0A                Beacon newBeacon = service.save(restBeacon);%0A                if (GlobalSettings.DEBUGGING) %7B%0A                    System.out.println(%22Saved beacon with UUID = %5C'%22 + newBeacon.getUuid() + %22%5C' major = %5C'%22 + newBeacon.getMajor() + %22%5C' minor = %5C'%22 + newBeacon.getMinor() + %22%5C'%22);%0A                %7D%0A            %7D catch (ConstraintViolationException %7C TransactionSystemException e) %7B%0A                if (GlobalSettings.DEBUGGING) %7B%0A                    System.err.println(%22Unable to save beacon! Constraint violation detected!%22);%0A                %7D%0A                return new ResponseEntity%3CList%3CBeacon%3E%3E(HttpStatus.BAD_REQUEST);%0A            %7D%0A        %7D%0A        return new ResponseEntity%3CList%3CBeacon%3E%3E(restBeacons, HttpStatus.CREATED);%0A    %7D%0A%0A
     /**%0A     * D

Add batch importing beacons