/**
 * 
 */
package de.evoila.cf.broker.persistence.repository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import de.evoila.cf.broker.model.ServiceInstance;
import de.evoila.cf.broker.repository.ServiceInstanceRepository;


/**
 * @author Patrick Weber, evoila.
 * @author Marco Di Martino
 *
 */
@Service
public class ServiceInstanceRepositoryImpl implements ServiceInstanceRepository {
	
	@Autowired
	de.evoila.cf.broker.persistence.mongodb.repository.ServiceInstanceRepository serviceInstanceRepository;

	@Override
	public ServiceInstance getServiceInstance(String instanceId) {
		return serviceInstanceRepository.findById(instanceId).get();
	}

	@Override
	public boolean containsServiceInstanceId(String serviceInstanceId) {
		return serviceInstanceRepository.existsById(serviceInstanceId);
	}

	@Override
	public void addServiceInstance(String id, ServiceInstance serviceInstance) {
		if (!id.equals(serviceInstance.getId())) {
			serviceInstance = new ServiceInstance(id, serviceInstance.getServiceDefinitionId(),
					serviceInstance.getPlanId(), serviceInstance.getOrganizationGuid(), serviceInstance.getSpaceGuid(),
					serviceInstance.getParameters(), serviceInstance.getDashboardUrl(),
					serviceInstance.getInternalId());
		}
		serviceInstanceRepository.save(serviceInstance);
	}

	@Override
	public void deleteServiceInstance(String serviceInstanceId) {
		serviceInstanceRepository.deleteById(serviceInstanceId);
	}

	@Override
	public void updateServiceInstance(ServiceInstance serviceInstance){
		serviceInstanceRepository.save(serviceInstance);

	}

}

@@ -296,16 +296,71 @@
 ory;%0D%0A%0D%0A
+import java.util.ArrayList;%0D%0Aimport java.util.List;%0D%0A%0D%0A
 %0D%0A/**%0D%0A 
@@ -787,32 +787,503 @@
 ).get();%0D%0A%09%7D%0D%0A%0D%0A
+%09@Override%0D%0A%09public List%3CServiceInstance%3E getServiceInstancesByServiceDefinitionId(String serviceDefinitionId) %7B%0D%0A%09%09List%3CServiceInstance%3E serviceInstances = new ArrayList%3C%3E();%0D%0A%0D%0A%09%09serviceInstanceRepository.findAll().forEach(serviceInstance -%3E %7B%0D%0A%09%09%09if(serviceInstance.getServiceDefinitionId() != null && serviceInstance.getServiceDefinitionId().equals(serviceDefinitionId)) %7B%0D%0A%09%09%09%09serviceInstances.add(serviceInstance);%0D%0A%09%09%09%7D%0D%0A%09%09%7D);%0D%0A%0D%0A%09%09return serviceInstances;%0D%0A%09%7D%0D%0A%0D%0A
 %09@Override%0D%0A%09pub

add method to get service instances by service definition id