/**
 * Licensed to the Austrian Association for Software Tool Integration (AASTI)
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. The AASTI licenses this file to you 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 org.openengsb.core.common;

import java.util.List;

import org.openengsb.core.api.Constants;
import org.openengsb.core.api.InternalServiceRegistrationManager;
import org.openengsb.core.api.ServiceManager;
import org.openengsb.core.api.ServiceValidationFailedException;
import org.openengsb.core.api.model.ConnectorConfiguration;
import org.openengsb.core.api.model.ConnectorDescription;
import org.openengsb.core.api.model.ConnectorId;
import org.openengsb.core.api.persistence.ConfigPersistenceService;
import org.openengsb.core.api.persistence.PersistenceException;

public class ServiceManagerImpl implements ServiceManager {

    private InternalServiceRegistrationManager registrationManager;
    private ConfigPersistenceService configPersistence = OpenEngSBCoreServices
        .getConfigPersistenceService(Constants.CONNECTOR);

    @Override
    public void createService(ConnectorId id, ConnectorDescription connectorDescription)
        throws ServiceValidationFailedException {
        try {
            List<ConnectorConfiguration> list = configPersistence.load(id.toMetaData());
            if (!list.isEmpty()) {
                throw new IllegalArgumentException("connector already exists");
            }
        } catch (PersistenceException e) {
            throw new ServiceValidationFailedException(e);
        }
        registrationManager.createService(id, connectorDescription);
        ConnectorConfiguration configuration = new ConnectorConfiguration(id, connectorDescription);
        try {
            configPersistence.persist(configuration);
        } catch (PersistenceException e) {
            throw new IllegalArgumentException(e);
        }
    }

    @Override
    public void update(ConnectorId id, ConnectorDescription connectorDescpription)
        throws ServiceValidationFailedException {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void delete(ConnectorId id) {
        // TODO delete from config (OPENENGSB-1256)
        registrationManager.delete(id);
    }

    @Override
    public ConnectorDescription getAttributeValues(ConnectorId id) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void assignLocations(ConnectorId serviceId, String... locations) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void removeLocations(ConnectorId serviceId, String... locations) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public void setRegistrationManager(InternalServiceRegistrationManager registrationManager) {
        this.registrationManager = registrationManager;
    }
}

@@ -876,20 +876,101 @@
 va.util.
-List
+Collections;%0Aimport java.util.Dictionary;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Aimpor
@@ -2754,69 +2754,1674 @@
     
-throw new UnsupportedOperationException(%22Not yet implemented%22
+ConnectorDescription old = getOldConfig(id);%0A        registrationManager.update(id, connectorDescpription);%0A        applyConfigChanges(old, connectorDescpription);%0A        try %7B%0A            configPersistence.persist(new ConnectorConfiguration(id, connectorDescpription));%0A        %7D catch (PersistenceException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A    private void applyConfigChanges(ConnectorDescription old, ConnectorDescription diff) %7B%0A        updateAttributes(old.getAttributes(), diff.getAttributes());%0A        updateProperties(old.getProperties(), diff.getProperties());%0A    %7D%0A%0A    private void updateProperties(Dictionary%3CString, Object%3E properties, Dictionary%3CString, Object%3E diff) %7B%0A        for (String key : Collections.list(diff.keys())) %7B%0A            properties.put(key, diff.get(key));%0A        %7D%0A    %7D%0A%0A    private void updateAttributes(Map%3CString, String%3E attributes, Map%3CString, String%3E diff) %7B%0A        for (Map.Entry%3CString, String%3E entry : diff.entrySet()) %7B%0A            if (entry.getValue() == null) %7B%0A                attributes.remove(entry.getKey());%0A            %7D else %7B%0A                attributes.put(entry.getKey(), entry.getValue());%0A            %7D%0A        %7D%0A    %7D%0A%0A    private ConnectorDescription getOldConfig(ConnectorId id) %7B%0A        List%3CConnectorConfiguration%3E list;%0A        try %7B%0A            list = configPersistence.load(id.toMetaData());%0A        %7D catch (PersistenceException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A        if (list.size() != 1) %7B%0A            throw new IllegalStateException(%22multiple connectors with id %22 + id + %22 found%22);%0A        %7D%0A        return list.get(0).getContent(
 );%0A 

implement update-method in connector-persistence