package com.dteknoloji.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dteknoloji.config.GlobalSettings;
import com.dteknoloji.domain.Beacon;
import com.dteknoloji.domain.BeaconGroup;
import com.dteknoloji.repository.beaconGroup.BeaconGroupRepository;

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

@Transactional
@Service
public class BeaconGroupService {

    @Autowired
    private BeaconGroupRepository repository;

    @Autowired
    private BeaconService beaconService;

    /**
     * Saves/updates the given beacon group
     *
     * @param beaconGroup The beacon group to save/update
     * @return The saved/updated beacon group
     */
    public BeaconGroup save(BeaconGroup beaconGroup) {
        if (GlobalSettings.DEBUGGING) {
            System.out.println("Saving beacon group with ID = \'" + beaconGroup.getBeaconGroupId() + "\'");
        }

        return repository.save(beaconGroup);
    }

    /**
     * Returns the list of all the beacon groups
     *
     * @return A list of all beacon groups
     */
    public List<BeaconGroup> findAll() {
        if (GlobalSettings.DEBUGGING) {
            System.out.println("Finding all beacon groups");
        }

        List<BeaconGroup> beaconGroupList = new ArrayList<BeaconGroup>();

        for (BeaconGroup beaconGroup : repository.findAll()) {
            beaconGroupList.add(beaconGroup);
        }

        return beaconGroupList;
    }

    /**
     * Finds the beacon group with the given ID
     *
     * @param id The ID of the beacon group to search for
     * @return The beacon group with that ID
     */
    public BeaconGroup findById(Long id) {
        if (GlobalSettings.DEBUGGING) {
            System.out.println("Finding beacon group with ID = \'" + id + "\'");
        }

        return repository.findOne(id);
    }

    /**
     * Deletes the beacon group with the given ID and updates the beacons
     * in the group.
     *
     * @param id The ID of the beacon group to delete
     * @return Whether the beacon group was deleted or not
     */
    public boolean delete(Long id) {
        if (GlobalSettings.DEBUGGING) {
            System.out.println("Deleting beacon group with ID = \'" + id + "\'");
        }
        if(repository.exists(id)) {
            for (Beacon beacon : repository.findOne(id).getBeacons()) {
                beacon.setGroup(null);
                beaconService.save(beacon);
            }
            repository.delete(id);
            return true;
        } else {
            return false;
        }
    }
}

@@ -1079,16 +1079,31 @@
 conGroup
+%0A     *        
  The bea
@@ -1127,16 +1127,23 @@
 /update%0A
+     *%0A
      * @
@@ -2027,32 +2027,47 @@
      * @param id
+%0A     *        
  The ID of the b
@@ -2092,16 +2092,23 @@
 rch for%0A
+     *%0A
      * @
@@ -2318,33 +2318,32 @@
 %5C'%22);%0A        %7D%0A
-%0A
         return r
@@ -2453,23 +2453,16 @@
  beacons
-%0A     *
  in the 
@@ -2491,16 +2491,31 @@
 param id
+%0A     *        
  The ID 
@@ -2544,16 +2544,23 @@
  delete%0A
+     *%0A
      * @
@@ -2797,16 +2797,17 @@
       if
+ 
 (reposit
@@ -2840,105 +2840,135 @@
     
-for (Beacon beacon : repository.findOne(id).getBeacons()) %7B%0A                beacon.setGroup(null)
+updateBeaconsInGroup(id);%0A            repository.delete(id);%0A            return true;%0A        %7D else %7B%0A            return false
 ;%0A  
@@ -2977,92 +2977,216 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-beaconService.save(beacon);
+/**%0A     * Removes the association between a group and the beacons in the group
 %0A     
+*%0A
      
-  %7D%0A            repository.delete(id);
+* @param id%0A     *         The ID of the group%0A     */%0A    private void updateBeaconsInGroup(Long id) %7B
 %0A   
@@ -3194,66 +3194,141 @@
     
-    return true;%0A        %7D else %7B%0A            return false
+for (Beacon beacon : repository.findOne(id).getBeacons()) %7B%0A            beacon.setGroup(null);%0A            beaconService.save(beacon)
 ;%0A  

Reformat JavaDoc