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.controller.DeleteResponse;
import com.dteknoloji.domain.Beacon;
import com.dteknoloji.domain.BeaconGroup;
import com.dteknoloji.domain.Project;
import com.dteknoloji.repository.project.ProjectRepository;
import com.dteknoloji.repository.project.ProjectSpecifications;

/*
 **************************
 * 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 ProjectService {
    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private BeaconGroupService beaconGroupService;

    @Autowired
    private BeaconService beaconService;

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

        return projectRepository.save(project);
    }

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

        List<Project> projectList = new ArrayList<Project>();

        for (Project project : projectRepository.findAll()) {
            projectList.add(project);
        }

        return projectList;
    }

    /**
     * Find projects conforming to specifications
     *
     * @param projectName
     *     The project Name field constraint
     * @param ownerName
     *     The project Owner's Name field constraint
     * @param ownerID
     *     The project Owner's ID field constraint
     *
     * @return The list of projects conforming to given constraints
     */
    public List<Project> findProjectsBySpecs(String projectName, String ownerName, Long ownerID) {
        if (GlobalSettings.DEBUGGING) {
            System.out.println("Finding projects with Project Name = \'" + projectName + "\' Owner Name = \'" + ownerName + "\' ownerID = \'" + ownerID + "\'");
        }

        return projectRepository.findAll(ProjectSpecifications.projectWithSpecification(projectName, ownerName, ownerID));
    }

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

        return projectRepository.findOne(id);
    }

    /**
     * Deletes the project with the given ID and deletes the beacons and beacon groups in
     * the group.
     *
     * @param id
     *     The ID of the project to delete
     *
     * @return Whether the project was deleted or not
     */
    public DeleteResponse delete(Long id) {
        if (GlobalSettings.DEBUGGING) {
            System.out.println("Deleting project with ID = \'" + id + "\'");
        }
        if (projectRepository.exists(id)) {
            // TODO fix cascading removing beacons
            // https://www.google.com/search?q=spring%20hibernate%20prevent%20delete%20cascade&gws_rd=ssl
            // http://outbottle.com/hibernate-manytomany-delete-non-owner-prevent-owner-being-deleted/
            // http://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/
            // http://stackoverflow.com/questions/23829716/spring-roo-and-hibernate-how-to-prevent-deletion
            Project project = projectRepository.findOne(id);
//            deleteBeaconGroups(project);
//            deleteBeacons(project);
            projectRepository.delete(id);
            return DeleteResponse.DELETED;
        } else {
            return DeleteResponse.NOT_FOUND;
        }
    }

    /**
     * Deletes the beacon groups in a project
     *
     * @param project
     *     The project to delete the beacon groups in
     */
    private void deleteBeaconGroups(Project project) {
        for (BeaconGroup beaconGroup : project.getBeaconGroups()) {
            beaconGroupService.delete(beaconGroup.getBeaconGroupId());
        }
    }

    /**
     * Deletes the beacons in a project
     *
     * @param project
     *     The project to delete the beacons in
     */
    private void deleteBeacons(Project project) {
        for (Beacon beacon : project.getBeacons()) {
            beaconService.delete(beacon.getBeaconId());
        }
    }
}

@@ -350,87 +350,8 @@
 se;%0A
-import com.dteknoloji.domain.Beacon;%0Aimport com.dteknoloji.domain.BeaconGroup;%0A
 impo
@@ -3685,1412 +3685,141 @@
     
-// TODO fix cascading removing beacons%0A            // https://www.google.com/search?q=spring%2520hibernate%2520prevent%2520delete%2520cascade&gws_rd=ssl%0A            // http://outbottle.com/hibernate-manytomany-delete-non-owner-prevent-owner-being-deleted/%0A            // http://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/%0A            // http://stackoverflow.com/questions/23829716/spring-roo-and-hibernate-how-to-prevent-deletion%0A            Project project = projectRepository.findOne(id);%0A//            deleteBeaconGroups(project);%0A//            deleteBeacons(project);%0A            projectRepository.delete(id);%0A            return DeleteResponse.DELETED;%0A        %7D else %7B%0A            return DeleteResponse.NOT_FOUND;%0A        %7D%0A    %7D%0A%0A    /**%0A     * Deletes the beacon groups in a project%0A     *%0A     * @param project%0A     *     The project to delete the beacon groups in%0A     */%0A    private void deleteBeaconGroups(Project project) %7B%0A        for (BeaconGroup beaconGroup : project.getBeaconGroups()) %7B%0A            beaconGroupService.delete(beaconGroup.getBeaconGroupId());%0A        %7D%0A    %7D%0A%0A    /**%0A     * Deletes the beacons in a project%0A     *%0A     * @param project%0A     *     The project to delete the beacons in%0A     */%0A    private void deleteBeacons(Project project) %7B%0A        for (Beacon beacon : project.getBeacons()) %7B%0A            beaconService.delete(beacon.getBeaconId())
+projectRepository.delete(id);%0A            return DeleteResponse.DELETED;%0A        %7D else %7B%0A            return DeleteResponse.NOT_FOUND
 ;%0A  

Fix orphan deletion on project delete bug