// Path: subprojects/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/HalResource.java
// @JsonInclude(JsonInclude.Include.NON_NULL)
// public class HalResource<T> extends Resource<T> {
// 
//     private Map<String, Object> embeddedMap;
// 
//     public HalResource(T content, Link... links) {
//         super(content, links);
//     }
// 
//     @SuppressWarnings("unchecked")
//     public void addEmbeddedResource(String relation, Object embeddedResource) {
//         if (embeddedMap == null) {
//             embeddedMap = new HashMap<>();
//         }
// 
//         Object embeddedResources = embeddedMap.get(relation);
//         boolean isPluralRelation = relation.endsWith("s");
// 
// 
// 
//         // if a relation is plural, the content will always be rendered as an array
//         if(isPluralRelation) {
//             if (embeddedResources == null) {
//                 embeddedResources = new ArrayList<>();
//             }
//             ((List<Object>) embeddedResources).add(embeddedResource);
// 
//         // if a relation is singular, it would be a single object if there is only one object available
//         // Otherwise it would be rendered as array
//         } else {
//             if (embeddedResources == null) {
//                 embeddedResources = embeddedResource;
//             } else {
//                 if (embeddedResources instanceof List) {
//                     ((List<Object>) embeddedResources).add(embeddedResource);
//                 } else {
//                     List<Object> embeddedResourcesList = new ArrayList<>();
//                     embeddedResourcesList.add(embeddedResources);
//                     embeddedResourcesList.add(embeddedResource);
//                     embeddedResources = embeddedResourcesList;
//                 }
//             }
//         }
//         embeddedMap.put(relation, embeddedResources);
//     }
// 
//     @JsonProperty("_embedded")
//     public Map<String, Object> getEmbeddedRecources() {
//         return embeddedMap;
//     }
// }


import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
import org.eclipse.sw360.rest.resourceserver.core.HalResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceProcessor;
import org.springframework.hateoas.Resources;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
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.servlet.support.ServletUriComponentsBuilder;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
/*
 * Copyright Siemens AG, 2017. Part of the SW360 Portal Vendor.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.eclipse.sw360.rest.resourceserver.vendor;




@BasePathAwareController
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class VendorController implements ResourceProcessor<RepositoryLinksResource> {
    public static final String VENDORS_URL = "/vendors";

    @NonNull
    private final Sw360VendorService vendorService;

    @RequestMapping(value = VENDORS_URL, method = RequestMethod.GET)
    public ResponseEntity<Resources<Resource<Vendor>>> getVendors(OAuth2Authentication oAuth2Authentication) {
        List<Vendor> vendors = vendorService.getVendors();

        List<Resource<Vendor>> vendorResources = new ArrayList<>();
        for (Vendor vendor : vendors) {
            // TODO Kai Tödter 2017-01-04
            // Find better way to decrease details in list resources,
            // e.g. apply projections or Jackson Mixins
            vendor.setShortname(null);
            vendor.setType(null);
            vendor.setUrl(null);

            Resource<Vendor> vendorResource = new Resource<>(vendor);
            vendorResources.add(vendorResource);
        }
        Resources<Resource<Vendor>> resources = new Resources<>(vendorResources);

        return new ResponseEntity<>(resources, HttpStatus.OK);
    }

    @RequestMapping(VENDORS_URL + "/{id}")
    public ResponseEntity<Resource<Vendor>> getVendor(
            @PathVariable("id") String id, OAuth2Authentication oAuth2Authentication) {
        Vendor sw360Vendor = vendorService.getVendorById(id);
// Path: subprojects/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/HalResource.java
// @JsonInclude(JsonInclude.Include.NON_NULL)
// public class HalResource<T> extends Resource<T> {
// 
//     private Map<String, Object> embeddedMap;
// 
//     public HalResource(T content, Link... links) {
//         super(content, links);
//     }
// 
//     @SuppressWarnings("unchecked")
//     public void addEmbeddedResource(String relation, Object embeddedResource) {
//         if (embeddedMap == null) {
//             embeddedMap = new HashMap<>();
//         }
// 
//         Object embeddedResources = embeddedMap.get(relation);
//         boolean isPluralRelation = relation.endsWith("s");
// 
// 
// 
//         // if a relation is plural, the content will always be rendered as an array
//         if(isPluralRelation) {
//             if (embeddedResources == null) {
//                 embeddedResources = new ArrayList<>();
//             }
//             ((List<Object>) embeddedResources).add(embeddedResource);
// 
//         // if a relation is singular, it would be a single object if there is only one object available
//         // Otherwise it would be rendered as array
//         } else {
//             if (embeddedResources == null) {
//                 embeddedResources = embeddedResource;
//             } else {
//                 if (embeddedResources instanceof List) {
//                     ((List<Object>) embeddedResources).add(embeddedResource);
//                 } else {
//                     List<Object> embeddedResourcesList = new ArrayList<>();
//                     embeddedResourcesList.add(embeddedResources);
//                     embeddedResourcesList.add(embeddedResource);
//                     embeddedResources = embeddedResourcesList;
//                 }
//             }
//         }
//         embeddedMap.put(relation, embeddedResources);
//     }
// 
//     @JsonProperty("_embedded")
//     public Map<String, Object> getEmbeddedRecources() {
//         return embeddedMap;
//     }
// }


// Path: subprojects/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/vendor/VendorController.java
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
import org.eclipse.sw360.rest.resourceserver.core.HalResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceProcessor;
import org.springframework.hateoas.Resources;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
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.servlet.support.ServletUriComponentsBuilder;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;

/*
 * Copyright Siemens AG, 2017. Part of the SW360 Portal Vendor.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.eclipse.sw360.rest.resourceserver.vendor;




@BasePathAwareController
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class VendorController implements ResourceProcessor<RepositoryLinksResource> {
    public static final String VENDORS_URL = "/vendors";

    @NonNull
    private final Sw360VendorService vendorService;

    @RequestMapping(value = VENDORS_URL, method = RequestMethod.GET)
    public ResponseEntity<Resources<Resource<Vendor>>> getVendors(OAuth2Authentication oAuth2Authentication) {
        List<Vendor> vendors = vendorService.getVendors();

        List<Resource<Vendor>> vendorResources = new ArrayList<>();
        for (Vendor vendor : vendors) {
            // TODO Kai Tödter 2017-01-04
            // Find better way to decrease details in list resources,
            // e.g. apply projections or Jackson Mixins
            vendor.setShortname(null);
            vendor.setType(null);
            vendor.setUrl(null);

            Resource<Vendor> vendorResource = new Resource<>(vendor);
            vendorResources.add(vendorResource);
        }
        Resources<Resource<Vendor>> resources = new Resources<>(vendorResources);

        return new ResponseEntity<>(resources, HttpStatus.OK);
    }

    @RequestMapping(VENDORS_URL + "/{id}")
    public ResponseEntity<Resource<Vendor>> getVendor(
            @PathVariable("id") String id, OAuth2Authentication oAuth2Authentication) {
        Vendor sw360Vendor = vendorService.getVendorById(id);
        HalResource<Vendor> userHalResource = createHalVendor(sw360Vendor);