// Path: src/main/java/com/nebhale/buildmonitor/domain/Project.java
// @Entity
// public final class Project {
// 
//     @Id
//     @Size(min = 1, max = 8)
//     private volatile String key;
// 
//     @Size(min = 1, max = 64)
//     private volatile String name;
// 
//     Project() {
//     }
// 
//     /**
//      * Creates a new instance
//      *
//      * @param key  the key of the project
//      * @param name the name of the project
//      */
//     public Project(String key, String name) {
//         this.key = key.toUpperCase();
//         this.name = name;
//     }
// 
//     /**
//      * Returns the key of the project
//      *
//      * @return the key of the project
//      */
//     public String getKey() {
//         return this.key;
//     }
// 
//     /**
//      * Returns the name of the project
//      *
//      * @return the name of the project
//      */
//     public String getName() {
//         return this.name;
//     }
// 
// }
// 
// Path: src/main/java/com/nebhale/buildmonitor/repository/ProjectRepository.java
// public interface ProjectRepository extends JpaRepository<Project, String> {
// 
// }
// 
// Path: src/main/java/com/nebhale/buildmonitor/web/notify/ProjectsChangedNotifier.java
// public interface ProjectsChangedNotifier {
// 
//     /**
//      * Notifies listeners that the collection of projects have changed
//      */
//     void projectsChanged();
// 
// }
// 
// Path: src/main/java/com/nebhale/buildmonitor/web/resource/ProjectResourceAssembler.java
// public interface ProjectResourceAssembler extends ResourceAssembler<Project, Resource<Project>> {
// 
// }


import com.nebhale.buildmonitor.domain.Project;
import com.nebhale.buildmonitor.repository.ProjectRepository;
import com.nebhale.buildmonitor.web.notify.ProjectsChangedNotifier;
import com.nebhale.buildmonitor.web.resource.ProjectResourceAssembler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import java.util.List;
import java.util.stream.Collectors;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
/*
 * Copyright 2013-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nebhale.buildmonitor.web;




/**
 * Controller for accessing {@link Project}s
 */
@Controller
@RequestMapping("/projects")
public final class ProjectController {

    private static final String MEDIA_TYPE = "application/vnd.nebhale.buildmonitor.project+json";

    private final ProjectsChangedNotifier projectsChangedNotifier;

    private final ProjectRepository repository;

    private final ProjectResourceAssembler resourceAssembler;

    @Autowired
    ProjectController(ProjectsChangedNotifier projectsChangedNotifier, ProjectRepository repository,
                      ProjectResourceAssembler resourceAssembler) {
        this.projectsChangedNotifier = projectsChangedNotifier;
        this.repository = repository;
        this.resourceAssembler = resourceAssembler;
    }

    @Transactional
    @RequestMapping(method = RequestMethod.POST, value = "", produces = MEDIA_TYPE)
    ResponseEntity<?> create(@RequestBody Project project) {
        if (this.repository.exists(project.getKey())) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

        this.repository.saveAndFlush(project);

        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(linkTo(ProjectController.class).slash(project.getKey()).toUri());

        this.projectsChangedNotifier.projectsChanged();
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    @Transactional(readOnly = true)
    @RequestMapping(method = RequestMethod.GET, value = "", produces = MEDIA_TYPE)
    ResponseEntity<?> readAll() {
// Path: src/main/java/com/nebhale/buildmonitor/domain/Project.java
// @Entity
// public final class Project {
// 
//     @Id
//     @Size(min = 1, max = 8)
//     private volatile String key;
// 
//     @Size(min = 1, max = 64)
//     private volatile String name;
// 
//     Project() {
//     }
// 
//     /**
//      * Creates a new instance
//      *
//      * @param key  the key of the project
//      * @param name the name of the project
//      */
//     public Project(String key, String name) {
//         this.key = key.toUpperCase();
//         this.name = name;
//     }
// 
//     /**
//      * Returns the key of the project
//      *
//      * @return the key of the project
//      */
//     public String getKey() {
//         return this.key;
//     }
// 
//     /**
//      * Returns the name of the project
//      *
//      * @return the name of the project
//      */
//     public String getName() {
//         return this.name;
//     }
// 
// }
// 
// Path: src/main/java/com/nebhale/buildmonitor/repository/ProjectRepository.java
// public interface ProjectRepository extends JpaRepository<Project, String> {
// 
// }
// 
// Path: src/main/java/com/nebhale/buildmonitor/web/notify/ProjectsChangedNotifier.java
// public interface ProjectsChangedNotifier {
// 
//     /**
//      * Notifies listeners that the collection of projects have changed
//      */
//     void projectsChanged();
// 
// }
// 
// Path: src/main/java/com/nebhale/buildmonitor/web/resource/ProjectResourceAssembler.java
// public interface ProjectResourceAssembler extends ResourceAssembler<Project, Resource<Project>> {
// 
// }


// Path: src/main/java/com/nebhale/buildmonitor/web/ProjectController.java
import com.nebhale.buildmonitor.domain.Project;
import com.nebhale.buildmonitor.repository.ProjectRepository;
import com.nebhale.buildmonitor.web.notify.ProjectsChangedNotifier;
import com.nebhale.buildmonitor.web.resource.ProjectResourceAssembler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import java.util.List;
import java.util.stream.Collectors;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;

/*
 * Copyright 2013-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nebhale.buildmonitor.web;




/**
 * Controller for accessing {@link Project}s
 */
@Controller
@RequestMapping("/projects")
public final class ProjectController {

    private static final String MEDIA_TYPE = "application/vnd.nebhale.buildmonitor.project+json";

    private final ProjectsChangedNotifier projectsChangedNotifier;

    private final ProjectRepository repository;

    private final ProjectResourceAssembler resourceAssembler;

    @Autowired
    ProjectController(ProjectsChangedNotifier projectsChangedNotifier, ProjectRepository repository,
                      ProjectResourceAssembler resourceAssembler) {
        this.projectsChangedNotifier = projectsChangedNotifier;
        this.repository = repository;
        this.resourceAssembler = resourceAssembler;
    }

    @Transactional
    @RequestMapping(method = RequestMethod.POST, value = "", produces = MEDIA_TYPE)
    ResponseEntity<?> create(@RequestBody Project project) {
        if (this.repository.exists(project.getKey())) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

        this.repository.saveAndFlush(project);

        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(linkTo(ProjectController.class).slash(project.getKey()).toUri());

        this.projectsChangedNotifier.projectsChanged();
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    @Transactional(readOnly = true)
    @RequestMapping(method = RequestMethod.GET, value = "", produces = MEDIA_TYPE)
    ResponseEntity<?> readAll() {
        List<Resource<Project>> resources = this.repository.findAll(new Sort("key")).stream()