<commit_before>package com.dteknoloji.controller;

/*
 **************************
 * Copyright (c) 2014     *
 *                        *
 * This code belongs to:  *
 *                        *
 * <NAME>        *
 * S001974                *
 *                        *
 * <EMAIL>    *
 * <EMAIL>   *
 *                        *
 * aemreunal.com          *
 **************************
 */

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;
import com.dteknoloji.domain.BeaconGroup;
import com.dteknoloji.service.BeaconGroupService;
import com.dteknoloji.service.BeaconService;

@Controller
@RequestMapping("/BeaconGroup")
public class BeaconGroupController {
    @Autowired
    private BeaconGroupService beaconGroupService;

    @Autowired
    private BeaconService beaconService;

    /**
     * Get all beacon groups
     *
     * @return All existing beacon groups
     */
    @RequestMapping(method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<List<BeaconGroup>> getAllBeaconGroups() {
        return new ResponseEntity<List<BeaconGroup>>(beaconGroupService.findAll(), HttpStatus.OK);
    }

    /**
     * Get the beacon group with specified ID
     *
     * @param id The ID of the group
     * @return The beacon group
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}", produces = "application/json")
    public ResponseEntity<BeaconGroup> viewBeaconGroup(@PathVariable String id) {
        BeaconGroup beaconGroup = beaconGroupService.findById(Long.valueOf(id));
        if (beaconGroup == null) {
            return new ResponseEntity<BeaconGroup>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<BeaconGroup>(beaconGroup, HttpStatus.OK);
    }

    /**
     * Get beacon objects that belong to a group.
     *
     * @param id The ID of the group
     * @return The list of beacons that belong to the group with the specified ID
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}/beacons", produces = "application/json")
    public ResponseEntity<List<Beacon>> viewBeaconGroupMembers(@PathVariable String id) {
        BeaconGroup beaconGroup = beaconGroupService.findById(Long.valueOf(id));
        if (beaconGroup == null) {
            return new ResponseEntity<List<Beacon>>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<List<Beacon>>(beaconGroup.getBeacons(), HttpStatus.OK);
    }

    /**
     * Create a new beacon group
     *
     * @param restBeaconGroup The beacon group as JSON object
     * @param builder
     * @return The created beacon group
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<BeaconGroup> createBeaconGroup(@RequestBody BeaconGroup restBeaconGroup, UriComponentsBuilder builder) {
        try {
            BeaconGroup newBeaconGroup = beaconGroupService.save(restBeaconGroup);
            if (GlobalSettings.DEBUGGING) {
                System.out.println("Saved beacon group with ID = \'" + newBeaconGroup.getBeaconGroupId() + "\' name = \'" + newBeaconGroup.getName() + "\'");
            }
            HttpHeaders headers = new HttpHeaders();
            headers.setLocation(builder.path("/BeaconGroup/{id}").buildAndExpand(newBeaconGroup.getBeaconGroupId().toString()).toUri());
            return new ResponseEntity<BeaconGroup>(newBeaconGroup, headers, HttpStatus.CREATED);
        } catch (ConstraintViolationException | TransactionSystemException e) {
            if (GlobalSettings.DEBUGGING) {
                System.err.println("Unable to save beacon group! Constraint violation detected!");
            }
            return new ResponseEntity<BeaconGroup>(HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * Add beacon to the specified beacon group.
     * <p/>
     * Can return 409 if beacon already has a group.
     * <p/>
     * Ex: "/BeaconGroup/1/add?beaconId=12"
     *
     * @param beaconGroupId The ID of the beacon group to add the beacon to
     * @param beaconId      The ID of the beacon to add
     * @return The added beacon group
     */
    @RequestMapping(method = RequestMethod.POST, value = "/{beaconGroupId}/add")
    public ResponseEntity<BeaconGroup> addBeaconToGroup(@PathVariable String beaconGroupId, @RequestParam(value = "beaconId", required = true) String beaconId) {
        BeaconGroup beaconGroup = beaconGroupService.findById(Long.valueOf(beaconGroupId));
        if (beaconGroup == null) {
            return new ResponseEntity<BeaconGroup>(HttpStatus.NOT_FOUND);
        } else {
            Beacon beacon = beaconService.findById(Long.valueOf(beaconId));
            if (beacon == null) {
                return new ResponseEntity<BeaconGroup>(HttpStatus.NOT_FOUND);
            } else {
                if (beacon.getGroup() != null) {
                    return new ResponseEntity<BeaconGroup>(HttpStatus.CONFLICT);
                } else {
                    beacon.setGroup(beaconGroup);
                    beaconGroup.getBeacons().add(beacon);
                    beaconService.save(beacon);
                    beaconGroupService.save(beaconGroup);
                    return new ResponseEntity<BeaconGroup>(beaconGroup, HttpStatus.OK);
                }
            }
        }
    }

    /**
     * Delete beacon from the specified beacon group.<commit_msg>Fix possible Long conversion bug<commit_after>package com.dteknoloji.controller;

/*
 **************************
 * Copyright (c) 2014     *
 *                        *
 * This code belongs to:  *
 *                        *
 * <NAME>        *
 * S001974                *
 *                        *
 * <EMAIL>    *
 * <EMAIL>   *
 *                        *
 * aemreunal.com          *
 **************************
 */

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;
import com.dteknoloji.domain.BeaconGroup;
import com.dteknoloji.service.BeaconGroupService;
import com.dteknoloji.service.BeaconService;

@Controller
@RequestMapping("/BeaconGroup")
public class BeaconGroupController {
    @Autowired
    private BeaconGroupService beaconGroupService;

    @Autowired
    private BeaconService beaconService;

    /**
     * Get all beacon groups
     *
     * @return All existing beacon groups
     */
    @RequestMapping(method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<List<BeaconGroup>> getAllBeaconGroups() {
        return new ResponseEntity<List<BeaconGroup>>(beaconGroupService.findAll(), HttpStatus.OK);
    }

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

        BeaconGroup beaconGroup = beaconGroupService.findById(beaconGroupIDAsLong);
        if (beaconGroup == null) {
            return new ResponseEntity<BeaconGroup>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<BeaconGroup>(beaconGroup, HttpStatus.OK);
    }

    /**
     * Get beacon objects that belong to a group.
     *
     * @param id The ID of the group
     * @return The list of beacons that belong to the group with the specified ID
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}/beacons", produces = "application/json")
    public ResponseEntity<List<Beacon>> viewBeaconGroupMembers(@PathVariable String id) {
        Long beaconGroupIDAsLong;
        try {
            beaconGroupIDAsLong = Long.valueOf(id);
        } catch (NumberFormatException e) {
            return new ResponseEntity<List<Beacon>>(HttpStatus.BAD_REQUEST);
        }

        BeaconGroup beaconGroup = beaconGroupService.findById(beaconGroupIDAsLong);
        if (beaconGroup == null) {
            return new ResponseEntity<List<Beacon>>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<List<Beacon>>(beaconGroup.getBeacons(), HttpStatus.OK);
    }

    /**
     * Create a new beacon group
     *
     * @param restBeaconGroup The beacon group as JSON object
     * @param builder
     * @return The created beacon group
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<BeaconGroup> createBeaconGroup(@RequestBody BeaconGroup restBeaconGroup, UriComponentsBuilder builder) {
        try {
            BeaconGroup newBeaconGroup = beaconGroupService.save(restBeaconGroup);
            if (GlobalSettings.DEBUGGING) {
                System.out.println("Saved beacon group with ID = \'" + newBeaconGroup.getBeaconGroupId() + "\' name = \'" + newBeaconGroup.getName() + "\'");
            }
            HttpHeaders headers = new HttpHeaders();
            headers.setLocation(builder.path("/BeaconGroup/{id}").buildAndExpand(newBeaconGroup.getBeaconGroupId().toString()).toUri());
            return new ResponseEntity<BeaconGroup>(newBeaconGroup, headers, HttpStatus.CREATED);
        } catch (ConstraintViolationException | TransactionSystemException e) {
            if (GlobalSettings.DEBUGGING) {
                System.err.println("Unable to save beacon group! Constraint violation detected!");
            }
            return new ResponseEntity<BeaconGroup>(HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * Add beacon to the specified beacon group.
     * <p/>
     * Can return 409 if beacon already has a group.
     * <p/>
     * Ex: "/BeaconGroup/1/add?beaconId=12"
     *
     * @param beaconGroupId The ID of the beacon group to add the beacon to
     * @param beaconId      The ID of the beacon to add
     * @return The added beacon group
     */
    @RequestMapping(method = RequestMethod.POST, value = "/{beaconGroupId}/add")
    public ResponseEntity<BeaconGroup> addBeaconToGroup(@PathVariable String beaconGroupId, @RequestParam(value = "beaconId", required = true) String beaconId) {
        Long beaconGroupIDAsLong;
        try {
            beaconGroupIDAsLong = Long.valueOf(beaconGroupId);
        } catch (NumberFormatException e) {
            return new ResponseEntity<BeaconGroup>(HttpStatus.BAD_REQUEST);
        }
        
        BeaconGroup beaconGroup = beaconGroupService.findById(beaconGroupIDAsLong);
        if (beaconGroup == null) {
            return new ResponseEntity<BeaconGroup>(HttpStatus.NOT_FOUND);
        } else {
            Beacon beacon = beaconService.findById(Long.valueOf(beaconId));
            if (beacon == null) {
                return new ResponseEntity<BeaconGroup>(HttpStatus.NOT_FOUND);
            } else {
                if (beacon.getGroup() != null) {
                    return new ResponseEntity<BeaconGroup>(HttpStatus.CONFLICT);
                } else {
                    beacon.setGroup(beaconGroup);
                    beaconGroup.getBeacons().add(beacon);
                    beaconService.save(beacon);
                    beaconGroupService.save(beaconGroup);
                    return new ResponseEntity<BeaconGroup>(beaconGroup, HttpStatus.OK);
                }
            }
        }
    }

    /**
     * Delete beacon from the specified beacon group.
{'fraction_non_alphanumeric': 0.10502172863351038, 'fraction_numerical': 0.002575245453082247, 'mean_word_length': 2.690822690822691, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3109566', 'n_tokens_mistral': 3436, 'n_tokens_neox': 3302, 'n_words': 728}