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

import java.util.List;

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

import de.evoila.cf.broker.exception.ServiceBrokerException;
import de.evoila.cf.broker.exception.ServiceDefinitionDoesNotExistException;
import de.evoila.cf.broker.model.Catalog;
import de.evoila.cf.broker.model.Plan;
import de.evoila.cf.broker.model.ServiceDefinition;
import de.evoila.cf.broker.repository.ServiceDefinitionRepository;

/**
 * @author Christian Brinker, evoila.
 *
 */
@Repository
public class ServiceDefinitionRepositoryImpl implements ServiceDefinitionRepository {

	@Autowired
	private Catalog catalog;

	// Depl
	/*
	 * (non-Javadoc)
	 * 
	 * @see de.evoila.cf.broker.service.impl.ServiceDefinitionRepository#
	 * getServiceDefinition()
	 */
	@Override
	public List<ServiceDefinition> getServiceDefinition() {
		return catalog.getServices();
	}

	// public Map<String, ServiceInstance> getServiceInstances() {
	// return serviceInstances;
	// }

	// Depl
	/*
	 * (non-Javadoc)
	 * 
	 * @see de.evoila.cf.broker.service.impl.ServiceDefinitionRepository#
	 * validateServiceId(java.lang.String)
	 */
	@Override
	public void validateServiceId(String serviceDefinitionId) throws ServiceDefinitionDoesNotExistException {
		for(ServiceDefinition serviceDefinition : catalog.getServices()) {
			if (!serviceDefinitionId.equals(serviceDefinition.getId())) {
				throw new ServiceDefinitionDoesNotExistException(serviceDefinitionId);
			}
		}
	}

	// Depl + Bind
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.evoila.cf.broker.service.impl.ServiceDefinitionRepository#getPlan(java
	 * .lang.String)
	 */
	@Override
	public Plan getPlan(String planId) throws ServiceBrokerException {
		for(ServiceDefinition serviceDefinition : catalog.getServices()) {
			for (Plan currentPlan : serviceDefinition.getPlans()) {
				if (currentPlan.getId().equals(planId)) {
					return currentPlan;
				}
			}
		}
		throw new ServiceBrokerException("Missing plan for id: " + planId);
	}

}

@@ -1468,9 +1468,8 @@
 if (
-!
 serv
@@ -1524,16 +1524,40 @@
 )) %7B%0D%0A%09%09
+%09%09return;%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%0D%0A
 %09%09throw 
@@ -1622,27 +1622,16 @@
 onId);%0D%0A
-%09%09%09%7D%0D%0A%09%09%7D%0D%0A
 %09%7D%0D%0A%0D%0A%09/

fix a bug that caused the application to crash when there are more than one services in the catalog