package org.sagebionetworks.audit.utils;

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

import org.sagebionetworks.StackConfiguration;
import org.sagebionetworks.aws.utils.s3.BucketDao;
import org.sagebionetworks.aws.utils.s3.BucketDaoImpl;

import com.amazonaws.services.s3.AmazonS3Client;

/**
 * This is a helper class that manages bucketDao and bucketName resources for ObjectRecordDAO.
 * 
 * @author kimyen
 *
 */
public class BucketDaoProvider {

	private Map<String, BucketDao> bucketDaoMap;
	private AmazonS3Client s3Client;
	private String stack;
	private Map<String, String> bucketNameMap;

	public BucketDaoProvider(AmazonS3Client s3Client, String stack) {
		super();
		this.s3Client = s3Client;
		this.stack = stack;
		this.bucketDaoMap = new HashMap<String, BucketDao>();
		this.bucketNameMap = new HashMap<String, String>();
	}
	
	/**
	 * 
	 * @param type
	 * @effect if bucketDaoMap does not contain key type, before this method returns,
	 * it adds a new pair <type, new bucketDao> to bucketDaoMap
	 * @return the bucketDao of the requested type
	 */
	public BucketDao getBucketDao(String type) {
		if (bucketDaoMap.containsKey(type)) {
			return bucketDaoMap.get(type);
		}

		// first time seeing this type		
		String bucketName = getBucketName(type);
		
		// create the bucketDao for this type and save it
		BucketDao bucketDao = new BucketDaoImpl(s3Client, bucketName);
		bucketDaoMap.put(type, bucketDao);
		return bucketDao;
	}
	
	/**
	 * 
	 * @param type
	 * @effect if bucketNameMap does not contain key type, before this method returns,
	 * it tries to create a new bucket and adds a new pair <type, new bucketName> to 
	 * bucketNameMap 
	 * @return the bucketName of the requested type
	 */
	public String getBucketName(String type) {
		if (bucketNameMap.containsKey(type)) {
			return bucketNameMap.get(type);
		}
		
		// first time seeing this type	
		String bucketName = StackConfiguration.getObjectRecordBucketName(stack, type);
		
		// Create the bucket if it does not exist
		s3Client.createBucket(bucketName);
		bucketNameMap.put(type, bucketName);
		return bucketName;
	}
}

@@ -454,54 +454,8 @@
  %7B%0A%0A
-%09private Map%3CString, BucketDao%3E bucketDaoMap;%0A
 %09pri
@@ -684,64 +684,8 @@
 ck;%0A
-%09%09this.bucketDaoMap = new HashMap%3CString, BucketDao%3E();%0A
 %09%09th
@@ -774,481 +774,110 @@
  * @
-effect if bucketDaoMap does not contain key type, before this method returns,%0A%09 * it adds a new pair %3Ctype, new bucketDao%3E to bucketDaoMap%0A%09 * @return the bucketDao of the requested type%0A%09 */%0A%09public BucketDao getBucketDao(String type) %7B%0A%09%09if (bucketDaoMap.containsKey(type)) %7B%0A%09%09%09return bucketDaoMap.get(type);%0A%09%09%7D%0A%0A%09%09// first time seeing this type%09%09%0A%09%09String bucketName = getBucketName(type);%0A%09%09%0A%09%09// create the bucketDao for this type and save it%0A%09%09BucketDao bucketDao =
+return the bucketDao of the requested type%0A%09 */%0A%09public BucketDao getBucketDao(String type) %7B%0A%09%09return
  new
@@ -901,17 +901,20 @@
 Client, 
-b
+getB
 ucketNam
@@ -918,66 +918,15 @@
 Name
-);%0A%09%09bucketDaoMap.put(type, bucketDao);%0A%09%09return bucketDao
+(type))
 ;%0A%09%7D

remove bucketDaoMap