<commit_before>/**
 * Copyright (C) 2015 Orange
 * 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.orange.clara.cloud.cf.servicebroker.dbaas.service;

import com.orange.clara.cloud.cf.servicebroker.dbaas.domain.DBaasService;
import com.orange.clara.cloud.cf.servicebroker.dbaas.domain.DatabaseCredentials;
import com.orange.clara.cloud.cf.servicebroker.dbaas.domain.DbaasServiceInstance;
import com.orange.clara.cloud.cf.servicebroker.dbaas.domain.DbaasServiceInstanceBinding;
import com.orange.clara.cloud.cf.servicebroker.dbaas.repository.DBaasServiceInstanceBindingRepository;
import com.orange.clara.cloud.cf.servicebroker.dbaas.repository.DBaasServiceInstanceRepository;
import org.cloudfoundry.community.servicebroker.exception.ServiceBrokerException;
import org.cloudfoundry.community.servicebroker.exception.ServiceInstanceBindingExistsException;
import org.cloudfoundry.community.servicebroker.model.CreateServiceInstanceBindingRequest;
import org.cloudfoundry.community.servicebroker.model.DeleteServiceInstanceBindingRequest;
import org.cloudfoundry.community.servicebroker.model.ServiceInstanceBinding;
import org.cloudfoundry.community.servicebroker.service.ServiceInstanceBindingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sbortolussi on 20/04/2015.
 */
@Service
public class DbaasServiceInstanceBindingService implements ServiceInstanceBindingService {

    private DBaasServiceInstanceBindingRepository dBaasServiceInstanceBindingRepository;
    private DBaasServiceInstanceRepository dBaasServiceInstanceRepository;
    private DBaasService dBaasService;

    private static final Logger LOGGER = LoggerFactory.getLogger(DbaasServiceInstanceBindingService.class);


    @Autowired
    public DbaasServiceInstanceBindingService(DBaasServiceInstanceBindingRepository dBaasServiceInstanceBindingRepository, DBaasServiceInstanceRepository dBaasServiceInstanceRepository, DBaasService dBaasService) {
        this.dBaasServiceInstanceBindingRepository = dBaasServiceInstanceBindingRepository;
        this.dBaasServiceInstanceRepository = dBaasServiceInstanceRepository;
        this.dBaasService = dBaasService;
    }


    public ServiceInstanceBinding createServiceInstanceBinding(CreateServiceInstanceBindingRequest request) throws ServiceInstanceBindingExistsException, ServiceBrokerException {
        if (dBaasServiceInstanceBindingRepository.findOne(request.getBindingId()) != null) {
            //TODO generate ServiceInstanceBinding from DbaasServiceInstanceBinding
            throw new ServiceInstanceBindingExistsException(new ServiceInstanceBinding(request.getBindingId(), request.getServiceInstanceId(), null, null, request.getPlanId()));
        }

        final DbaasServiceInstance database = dBaasServiceInstanceRepository.findOne(request.getServiceInstanceId());
        final DatabaseCredentials databaseCredentials = dBaasService.getDatabaseCredentials(database.getDatabaseUid());
        //dbaas service instance binding user will be dbaas instance service default owner
        final DbaasServiceInstanceBinding binding = new DbaasServiceInstanceBinding(request.getBindingId(), database.getDatabaseUid(), database.getOwner());
        dBaasServiceInstanceBindingRepository.save(binding);

        Map<String, Object> credentials = new HashMap<String, Object>();
        credentials.put("hostname", databaseCredentials.getHostname());
        credentials.put("name", databaseCredentials.getDatabaseName());
        credentials.put("port", databaseCredentials.getPort());
        credentials.put("password", <PASSWORD>.getOwner().<PASSWORD>());
        credentials.put("username", database.getOwner().getName());
        credentials.put("uri", databaseCredentials.getUri(database.getOwner()));
        return new ServiceInstanceBinding(request.getBindingId(), request.getServiceInstanceId(), credentials, null, request.getAppGuid());
    }

    public ServiceInstanceBinding deleteServiceInstanceBinding(DeleteServiceInstanceBindingRequest request) throws ServiceBrokerException {
        DbaasServiceInstanceBinding binding = dBaasServiceInstanceBindingRepository.findOne(request.getBindingId());
        if (binding == null) {
            LOGGER.warn("Will not delete dbaas broker service instance binding<{}>. Dbaas broker service instance binding is not registered.", request.getBindingId());
            return null;
        }
        dBaasService.deleteUserFromDatabase(binding.getDatabaseId(), binding.getUser());
        dBaasServiceInstanceBindingRepository.delete(request.getBindingId());
        return new ServiceInstanceBinding(request.getBindingId(), request.getInstance().getServiceInstanceId(), null, null, null);
    }

}
<commit_msg>Do not remove user from dbaas (we have only one) need a talk with the owner<commit_after>/**
 * Copyright (C) 2015 Orange
 * 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.orange.clara.cloud.cf.servicebroker.dbaas.service;

import com.orange.clara.cloud.cf.servicebroker.dbaas.domain.DBaasService;
import com.orange.clara.cloud.cf.servicebroker.dbaas.domain.DatabaseCredentials;
import com.orange.clara.cloud.cf.servicebroker.dbaas.domain.DbaasServiceInstance;
import com.orange.clara.cloud.cf.servicebroker.dbaas.domain.DbaasServiceInstanceBinding;
import com.orange.clara.cloud.cf.servicebroker.dbaas.repository.DBaasServiceInstanceBindingRepository;
import com.orange.clara.cloud.cf.servicebroker.dbaas.repository.DBaasServiceInstanceRepository;
import org.cloudfoundry.community.servicebroker.exception.ServiceBrokerException;
import org.cloudfoundry.community.servicebroker.exception.ServiceInstanceBindingExistsException;
import org.cloudfoundry.community.servicebroker.model.CreateServiceInstanceBindingRequest;
import org.cloudfoundry.community.servicebroker.model.DeleteServiceInstanceBindingRequest;
import org.cloudfoundry.community.servicebroker.model.ServiceInstanceBinding;
import org.cloudfoundry.community.servicebroker.service.ServiceInstanceBindingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sbortolussi on 20/04/2015.
 */
@Service
public class DbaasServiceInstanceBindingService implements ServiceInstanceBindingService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DbaasServiceInstanceBindingService.class);
    private DBaasServiceInstanceBindingRepository dBaasServiceInstanceBindingRepository;
    private DBaasServiceInstanceRepository dBaasServiceInstanceRepository;
    private DBaasService dBaasService;


    @Autowired
    public DbaasServiceInstanceBindingService(DBaasServiceInstanceBindingRepository dBaasServiceInstanceBindingRepository, DBaasServiceInstanceRepository dBaasServiceInstanceRepository, DBaasService dBaasService) {
        this.dBaasServiceInstanceBindingRepository = dBaasServiceInstanceBindingRepository;
        this.dBaasServiceInstanceRepository = dBaasServiceInstanceRepository;
        this.dBaasService = dBaasService;
    }


    public ServiceInstanceBinding createServiceInstanceBinding(CreateServiceInstanceBindingRequest request) throws ServiceInstanceBindingExistsException, ServiceBrokerException {
        if (dBaasServiceInstanceBindingRepository.findOne(request.getBindingId()) != null) {
            //TODO generate ServiceInstanceBinding from DbaasServiceInstanceBinding
            throw new ServiceInstanceBindingExistsException(new ServiceInstanceBinding(request.getBindingId(), request.getServiceInstanceId(), null, null, request.getPlanId()));
        }

        final DbaasServiceInstance database = dBaasServiceInstanceRepository.findOne(request.getServiceInstanceId());
        final DatabaseCredentials databaseCredentials = dBaasService.getDatabaseCredentials(database.getDatabaseUid());
        //dbaas service instance binding user will be dbaas instance service default owner
        final DbaasServiceInstanceBinding binding = new DbaasServiceInstanceBinding(request.getBindingId(), database.getDatabaseUid(), database.getOwner());
        dBaasServiceInstanceBindingRepository.save(binding);

        Map<String, Object> credentials = new HashMap<String, Object>();
        credentials.put("hostname", databaseCredentials.getHostname());
        credentials.put("name", databaseCredentials.getDatabaseName());
        credentials.put("port", databaseCredentials.getPort());
        credentials.put("password", <PASSWORD>.getOwner().<PASSWORD>());
        credentials.put("username", database.getOwner().getName());
        credentials.put("uri", databaseCredentials.getUri(database.getOwner()));
        return new ServiceInstanceBinding(request.getBindingId(), request.getServiceInstanceId(), credentials, null, request.getAppGuid());
    }

    public ServiceInstanceBinding deleteServiceInstanceBinding(DeleteServiceInstanceBindingRequest request) throws ServiceBrokerException {
        DbaasServiceInstanceBinding binding = dBaasServiceInstanceBindingRepository.findOne(request.getBindingId());
        if (binding == null) {
            LOGGER.warn("Will not delete dbaas broker service instance binding<{}>. Dbaas broker service instance binding is not registered.", request.getBindingId());
            return null;
        }
        //dBaasService.deleteUserFromDatabase(binding.getDatabaseId(), binding.getUser()); removed cause it delete the only user in database (create service instance binding never create a user)
        dBaasServiceInstanceBindingRepository.delete(request.getBindingId());
        return new ServiceInstanceBinding(request.getBindingId(), request.getInstance().getServiceInstanceId(), null, null, null);
    }

}

{'fraction_non_alphanumeric': 0.08139428779998174, 'fraction_numerical': 0.0032849712565015056, 'mean_word_length': 5.715686274509804, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2776570', 'n_tokens_mistral': 2779, 'n_tokens_neox': 2717, 'n_words': 588}