package com.dteknoloji.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.dteknoloji.domain.Project;
import com.dteknoloji.service.BeaconGroupService;
import com.dteknoloji.service.BeaconService;
import com.dteknoloji.service.ProjectService;

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

@Controller
@RequestMapping("/Project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @Autowired
    private BeaconService beaconService;

    @Autowired
    private BeaconGroupService beaconGroupService;

    /**
     * Get all project (Optionally, all with matching criteria)
     *
     * @param projectName
     *     (Optional) The name of the project
     * @param ownerName
     *     (Optional) The name of the owner
     * @param ownerID
     *     (Optional) The ID of the owner
     *
     * @return All existing projects (Optionally, all that match the given criteria)
     */
    @RequestMapping(method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<List<Project>> getAllProjects(
        @RequestParam(value = "name", required = false, defaultValue = "") String projectName,
        @RequestParam(value = "ownerName", required = false, defaultValue = "") String ownerName,
        @RequestParam(value = "ownerID", required = false, defaultValue = "") String ownerID) {
        if (projectName.equals("") && ownerName.equals("") && ownerID.equals("")) {
            return new ResponseEntity<List<Project>>(projectService.findAll(), HttpStatus.OK);
        } else {
            return getProjectsWithMatchingCriteria(projectName, ownerName, ownerID);
        }
    }

    /**
     * Returns the list of projects that match a given criteria
     *
     * @param projectName
     *     (Optional) The name of the project
     * @param ownerName
     *     (Optional) The name of the owner
     * @param ownerID
     *     (Optional) The ID of the owner
     *
     * @return The list of projects that match the given criteria
     */
    private ResponseEntity<List<Project>> getProjectsWithMatchingCriteria(String projectName, String ownerName, String ownerID) {
        Long ownerIDAsLong;
        try {
            ownerIDAsLong = Long.valueOf(ownerID);
        } catch (NumberFormatException e) {
            return new ResponseEntity<List<Project>>(HttpStatus.BAD_REQUEST);
        }

        List<Project> projects = projectService.findProjectsBySpecs(projectName, ownerName, ownerIDAsLong);
        if (projects.size() == 0) {
            return new ResponseEntity<List<Project>>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<List<Project>>(projects, HttpStatus.OK);
    }

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

        Project project = projectService.findById(projectIDAsLong);
        if (project == null) {
            return new ResponseEntity<Project>(HttpStatus.NOT_FOUND);
        } else {
            return new ResponseEntity<Project>(project, HttpStatus.OK);
        }
    }
}

@@ -4161,37 +4161,930 @@
 OUND);%0A        %7D
- else
+%0A        return new ResponseEntity%3CProject%3E(project, HttpStatus.OK);%0A    %7D%0A%0A    /**%0A     * Delete the specified project%0A     *%0A     * @param id%0A     *     The ID of the project to delete%0A     *%0A     * @return The deleted project%0A     */%0A    @RequestMapping(method = RequestMethod.DELETE, value = %22/%7Bid%7D%22, produces = %22application/json%22)%0A    public ResponseEntity%3CProject%3E deleteProject(@PathVariable String id) %7B%0A        Long projectIDAsLong;%0A        try %7B%0A            projectIDAsLong = Long.valueOf(id);%0A        %7D catch (NumberFormatException e) %7B%0A            return new ResponseEntity%3CProject%3E(HttpStatus.BAD_REQUEST);%0A        %7D%0A%0A        Project project = projectService.findById(projectIDAsLong);%0A%0A        if (project == null) %7B%0A            return new ResponseEntity%3CProject%3E(HttpStatus.NOT_FOUND);%0A        %7D%0A%0A        boolean deleted = projectService.delete(projectIDAsLong);%0A        if (deleted)
  %7B%0A            r
@@ -5147,21 +5147,88 @@
 );%0A        %7D
+%0A%0A        return new ResponseEntity%3CProject%3E(HttpStatus.FORBIDDEN);
 %0A    %7D%0A%7D%0A

Add project deletion