//  Copyright (c) 2014 Couchbase, Inc.
//  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.couchbase.connector.plugin;

import java.util.ArrayList;
import java.util.List;

import com.couchbase.connector.connection.CBConnection;
import com.couchbase.connector.metadata.CBMetadata;
import com.couchbase.connector.metadata.CBRegistrationInfo;
import com.couchbase.connector.read.CBRead;
import com.couchbase.connector.write.CBWrite;
import com.informatica.cloud.api.adapter.common.ILogger;
import com.informatica.cloud.api.adapter.common.OperationContext;
import com.informatica.cloud.api.adapter.connection.IConnection;
import com.informatica.cloud.api.adapter.metadata.Capability;
import com.informatica.cloud.api.adapter.metadata.IMetadata;
import com.informatica.cloud.api.adapter.metadata.IRegistrationInfo;
import com.informatica.cloud.api.adapter.plugin.IExtWrtPlugin;
import com.informatica.cloud.api.adapter.plugin.IPlugin;
import com.informatica.cloud.api.adapter.plugin.PluginVersion;
import com.informatica.cloud.api.adapter.runtime.IRead;
import com.informatica.cloud.api.adapter.runtime.IWrite;
import com.informatica.cloud.api.adapter.runtime.IWrite2;
import com.informatica.cloud.api.adapter.runtime.exception.InitializationException;

public class CBPlugin implements IPlugin, IExtWrtPlugin {

	private IRegistrationInfo csvRegInfo;
	private IConnection csvConnection;
	private IMetadata csvMetadata;
	private ILogger logger;
	private OperationContext context;
	private IRead csvReader;
	private IWrite2 csvWriter;

	@Override
	public List<Capability> getCapabilities() {
		List<Capability> lstCapability = new ArrayList<Capability>();
		lstCapability.add(Capability.SINGLE_OBJECT_READ);
		lstCapability.add(Capability.SUPPORTS_CREATE_RECORD);
		lstCapability.add(Capability.EXTENDED_WRITE);
		return lstCapability;
	}

	@Override
	public IConnection getConnection() {
		if (csvConnection == null) {
			csvConnection = new CBConnection();
		}
		return csvConnection;
	}

	@Override
	public IMetadata getMetadata(IConnection conn)
			throws InitializationException {
		csvMetadata = new CBMetadata(this, (CBConnection) conn);
		return csvMetadata;
	}

	@Override
	public IRead getReader(IConnection conn) throws InitializationException {
		csvReader = new CBRead(this, (CBConnection) conn);
		return csvReader;
	}

	@Override
	public IRegistrationInfo getRegistrationInfo() {
		csvRegInfo = new CBRegistrationInfo();
		return csvRegInfo;
	}

	@Override
	public PluginVersion getVersion() {
		return new PluginVersion(1, 0, 1);
	}

	@Override
	public IWrite getWriter(IConnection conn) throws InitializationException {
		throw new UnsupportedOperationException();
	}

	@Override
	public void setContext(OperationContext context) {
		this.context = context;
	}

	@Override
	public void setLogger(ILogger logger) {
		this.logger = logger;
	}

	public ILogger getLogger() {
		return this.logger;
	}

	@Override
	public IWrite2 getExtendedWriter(IConnection conn) {
		csvWriter = new CBWrite(this, (CBConnection) conn);
		return csvWriter;
	}
}

@@ -1873,18 +1873,17 @@
 onInfo c
-sv
+b
 RegInfo;
@@ -1906,18 +1906,17 @@
 ection c
-sv
+b
 Connecti
@@ -1940,18 +1940,17 @@
 tadata c
-sv
+b
 Metadata
@@ -1982,44 +1982,8 @@
 r;%0D%0A
-%09private OperationContext context;%0D%0A
 %09pri
@@ -1994,18 +1994,17 @@
  IRead c
-sv
+b
 Reader;%0D
@@ -2022,18 +2022,17 @@
 Write2 c
-sv
+b
 Writer;%0D
@@ -2405,18 +2405,17 @@
 %0A%09%09if (c
-sv
+b
 Connecti
@@ -2433,18 +2433,17 @@
  %7B%0D%0A%09%09%09c
-sv
+b
 Connecti
@@ -2483,18 +2483,17 @@
 return c
-sv
+b
 Connecti
@@ -2604,18 +2604,17 @@
 n %7B%0D%0A%09%09c
-sv
+b
 Metadata
@@ -2670,18 +2670,17 @@
 return c
-sv
+b
 Metadata
@@ -2779,18 +2779,17 @@
 n %7B%0D%0A%09%09c
-sv
+b
 Reader =
@@ -2839,18 +2839,17 @@
 return c
-sv
+b
 Reader;%0D
@@ -2921,18 +2921,17 @@
 ) %7B%0D%0A%09%09c
-sv
+b
 RegInfo 
@@ -2969,18 +2969,17 @@
 return c
-sv
+b
 RegInfo;
@@ -3290,33 +3290,8 @@
  %7B%0D%0A
-%09%09this.context = context;
 %0D%0A%09%7D
@@ -3509,18 +3509,17 @@
 ) %7B%0D%0A%09%09c
-sv
+b
 Writer =
@@ -3574,10 +3574,9 @@
 rn c
-sv
+b
 Writ

clean up