/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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 io.gravitee.repositories.mongodb;

import com.mongodb.*;
import com.mongodb.MongoClientOptions.Builder;
import io.gravitee.repositories.mongodb.mapper.GraviteeDozerMapper;
import io.gravitee.repositories.mongodb.mapper.GraviteeMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import java.util.Arrays;
import java.util.List;

@Configuration
@ComponentScan
@EnableMongoRepositories
public class RepositoryConfiguration extends AbstractMongoConfiguration {

	@Value("${repository.mongodb.dbname:gravitee}")
	private String databaseName;

	@Value("${repository.mongodb.host:localhost}")
	private String host;

	@Value("${repository.mongodb.port:27017}")
	private Integer port;

	@Value("${repository.mongodb.username:#{null}}")
	private String userName;

	@Value("${repository.mongodb.password:#{null}}")
	private String password;

	@Bean
	@Override
	public Mongo mongo() throws Exception {
		List<MongoCredential> credentials = null;
		if (userName != null || password != null) {
			credentials = Arrays.asList(MongoCredential.createMongoCRCredential(
					userName, databaseName, password.toCharArray()));
		}
		String sHost = host == null ? "localhost" : host;
		int iPort = port == null ? 27017 : port;

		MongoClientOptions options = builder().build();

		if (credentials == null) {
			return new MongoClient(Arrays.asList(new ServerAddress(sHost, iPort)), options);
		}

		return new MongoClient(Arrays.asList(new ServerAddress(sHost, iPort)),
				credentials, options);
	}

	private Builder builder() {
		Builder builder = MongoClientOptions.builder();

		builder.writeConcern(WriteConcern.SAFE);

		//TODO: we have to provide more configuration options for these properties
		/*
			builder.alwaysUseMBeans(options.isAlwaysUseMBeans());
			builder.connectionsPerHost(options.getConnectionsPerHost());
			builder.connectTimeout(options.getConnectTimeout());
			builder.cursorFinalizerEnabled(options.isCursorFinalizerEnabled());
			builder.dbDecoderFactory(options.getDbDecoderFactory());
			builder.dbEncoderFactory(options.getDbEncoderFactory());
			builder.description(options.getDescription());
			builder.maxWaitTime(options.getMaxWaitTime());
			builder.readPreference(options.getReadPreference());
			builder.socketFactory(options.getSocketFactory());
			builder.socketKeepAlive(options.isSocketKeepAlive());
			builder.socketTimeout(options.getSocketTimeout());
			builder.threadsAllowedToBlockForConnectionMultiplier(options.getThreadsAllowedToBlockForConnectionMultiplier());
		*/
		return builder;
	}

	@Override
	protected String getMappingBasePackage() {
		return getClass().getPackage().getName();
	}

	@Override
	protected String getDatabaseName() {
		return databaseName;
	}
	
	@Bean
	public GraviteeMapper graviteeMapper(){
		return new GraviteeDozerMapper();
	}
}

@@ -957,31 +957,186 @@
 ork.
-context.annotation.Bean
+beans.factory.config.BeanDefinition;%0Aimport org.springframework.context.annotation.Bean;%0Aimport org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider
 ;%0Aim
@@ -1247,32 +1247,153 @@
 .Configuration;%0A
+import org.springframework.core.type.filter.AnnotationTypeFilter;%0Aimport org.springframework.data.annotation.Persistent;%0A
 import org.sprin
@@ -1443,32 +1443,95 @@
 oConfiguration;%0A
+import org.springframework.data.mongodb.core.mapping.Document;%0A
 import org.sprin
@@ -1601,54 +1601,191 @@
 es;%0A
-%0Aimport java.util.Arrays;%0Aimport java.util.Lis
+import org.springframework.util.ClassUtils;%0Aimport org.springframework.util.StringUtils;%0A%0Aimport java.util.Arrays;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A
@@ -4241,10 +4241,786 @@
 r();%0A%09%7D%0A
+%0A%0A%09protected Set%3CClass%3C?%3E%3E getInitialEntitySet() throws ClassNotFoundException %7B%0A%0A%09%09String basePackage = getMappingBasePackage();%0A%09%09Set%3CClass%3C?%3E%3E initialEntitySet = new HashSet%3CClass%3C?%3E%3E();%0A%0A%09%09if (StringUtils.hasText(basePackage)) %7B%0A%09%09%09ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider(%0A%09%09%09%09%09false);%0A%09%09%09componentProvider.addIncludeFilter(new AnnotationTypeFilter(Document.class));%0A%09%09%09componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class));%0A%0A%09%09%09for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) %7B%0A%09%09%09%09initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(),%0A%09%09%09%09%09%09this.getClass().getClassLoader()));%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return initialEntitySet;%0A%09%7D%0A
 %7D%0A

Resolve classloader issue when two different classloaders are used for Spring and own classes