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.domain.Project;
import com.dteknoloji.repository.project.ProjectRepository;

/*
 **************************
 * 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;
    }

    /**
     * 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 boolean delete(Long id) {
        if (GlobalSettings.DEBUGGING) {
            System.out.println("Deleting project with ID = \'" + id + "\'");
        }
        if (projectRepository.exists(id)) {
            Project project = projectRepository.findOne(id);
            deleteBeaconGroups(project);
            deleteBeacons(project);
            projectRepository.delete(id);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 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());
        }
    }
}

@@ -473,16 +473,80 @@
 ository;
+%0Aimport com.dteknoloji.repository.project.ProjectSpecifications;
 %0A%0A/*%0A **
@@ -1233,36 +1233,32 @@
 ject%0A     *     
-    
 The project to s
@@ -2018,32 +2018,842 @@
 ectList;%0A    %7D%0A%0A
+    /**%0A     * Find projects conforming to specifications%0A     *%0A     * @param projectName%0A     *     The project Name field constraint%0A     * @param ownerName%0A     *     The project Owner's Name field constraint%0A     * @param ownerID%0A     *     The project Owner's ID field constraint%0A     *%0A     * @return The list of projects conforming to given constraints%0A     */%0A    public List%3CProject%3E findProjectsBySpecs(String projectName, String ownerName, Long ownerID) %7B%0A        if (GlobalSettings.DEBUGGING) %7B%0A            System.out.println(%22Finding projects with Project Name = %5C'%22 + projectName + %22%5C' Owner Name = %5C'%22 + ownerName + %22%5C' ownerID = %5C'%22 + ownerID + %22%5C'%22);%0A        %7D%0A%0A        return projectRepository.findAll(ProjectSpecifications.projectWithSpecification(projectName, ownerName, ownerID));%0A    %7D%0A%0A
     /**%0A     * F
@@ -2914,36 +2914,32 @@
 m id%0A     *     
-    
 The ID of the pr
@@ -3379,28 +3379,24 @@
 %0A     *     
-    
 The ID of th
@@ -4043,36 +4043,32 @@
 ject%0A     *     
-    
 The project to d
@@ -4390,26 +4390,22 @@
 project%0A
+
      *
-    
      The

Add Project by specs searching