package org.sagebionetworks.dashboard.dao.postgres;

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

import javax.annotation.Resource;

import org.sagebionetworks.dashboard.dao.RawAccessRecordDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository("rawAccessRecordDao")
public class RawAccessRecordDaoImpl implements RawAccessRecordDao{

    private final Logger logger = LoggerFactory.getLogger(RawAccessRecordDaoImpl.class);

    @Resource
    private NamedParameterJdbcTemplate dwTemplate;

    private static final String COPY = "COPY raw_access_record FROM ':file_path'" 
            + " CREDENTIALS 'aws_access_key_id=:username;aws_secret_access_key=:password'"
            + " DELIMITER ',' GZIP;";

    @Override
    public void copy(String filePath, String username, String password) {
        Map<String, Object> namedParameters = new HashMap<>();
        namedParameters.put("file_path", filePath);
        namedParameters.put("username", username);
        namedParameters.put("password", password);

        try {
            dwTemplate.update(COPY, namedParameters);
            logger.info("Finish adding " + filePath + " into raw_access_record table.");
        } catch (Throwable e) {
            logger.error("Failed to add " + filePath, e);
        }
    }

}

@@ -669,16 +669,21 @@
 ing COPY
+_HEAD
  = %22COPY
@@ -719,17 +719,16 @@
 e_path'%22
- 
 %0A       
@@ -771,67 +771,127 @@
 _id=
-:username;aws_secret_access_key=:password'%22%0A            +
+%22;%0A    private static final String COPY_CRED = %22;aws_secret_access_key=%22;%0A    private static final String COPY_END =
  %22
+'
  DEL
@@ -1125,102 +1125,78 @@
     
-namedParameters.put(%22username%22, username);%0A        namedParameters.put(%22password%22, password);%0A
+String query = COPY_HEAD + username + COPY_CRED + password + COPY_END;
 %0A   
@@ -1236,20 +1236,21 @@
 .update(
-COPY
+query
 , namedP

add Credentails not as a part of the parameter map since it's not an option in postgres COPY command