package org.sagebionetworks.repo.model.dbo.schema;

import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.TABLE_VALIDATION_JSON_SCHEMA_INDEX;
import org.sagebionetworks.repo.model.dbo.DBOBasicDao;
import org.sagebionetworks.repo.model.dbo.SinglePrimaryKeySqlParameterSource;
import org.sagebionetworks.repo.model.schema.JsonSchema;
import org.sagebionetworks.repo.transactions.WriteTransaction;
import org.sagebionetworks.repo.web.NotFoundException;
import org.sagebionetworks.schema.adapter.JSONObjectAdapterException;
import org.sagebionetworks.schema.adapter.org.json.EntityFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class ValidationJsonSchemaIndexDaoImpl implements ValidationJsonSchemaIndexDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private DBOBasicDao basicDao;

	@WriteTransaction
	@Override
	public JsonSchema getValidationSchema(String versionId) {
		try {
			DBOValidationJsonSchemaIndex dbo = basicDao.getObjectByPrimaryKey(DBOValidationJsonSchemaIndex.class, 
					new SinglePrimaryKeySqlParameterSource(versionId));
			JsonSchema dto = convertFromDBOtoDTO(dbo);
			return dto;
		} catch (NotFoundException e) {
			throw new NotFoundException();
		}
	}
	
	@WriteTransaction
	@Override
	public void createOrUpdate(String versionId, JsonSchema schema) {
		DBOValidationJsonSchemaIndex dbo = convertFromDTOtoDBO(versionId, schema);
		basicDao.createOrUpdate(dbo);
	}
	
	@Override
	@WriteTransaction
	public void delete(String versionId) {
		basicDao.deleteObjectByPrimaryKey(DBOValidationJsonSchemaIndex.class, 
				new SinglePrimaryKeySqlParameterSource(versionId));
	}

	@Override
	@WriteTransaction
	public void truncateAll() {
		jdbcTemplate.update("DELETE FROM " + TABLE_VALIDATION_JSON_SCHEMA_INDEX);
	}
	
	private DBOValidationJsonSchemaIndex convertFromDTOtoDBO(String versionId, JsonSchema schema) {
		DBOValidationJsonSchemaIndex dbo = new DBOValidationJsonSchemaIndex();
		dbo.setVersionId(versionId);
		try {
			dbo.setValidationSchema(EntityFactory.createJSONStringForEntity(schema));
		} catch (JSONObjectAdapterException e) {
			throw new RuntimeException(e);
		}
		return dbo;
	}
	
	private JsonSchema convertFromDBOtoDTO(DBOValidationJsonSchemaIndex dbo) {
		String schemaString = dbo.getValidationSchema();
		try {
			JsonSchema dto = EntityFactory.createEntityFromJSONString(schemaString, JsonSchema.class);
			return dto;
		} catch (JSONObjectAdapterException e) {
			throw new RuntimeException(e);
		}
	}
}

@@ -952,35 +952,16 @@
 icDao;%0A%0A
-%09@WriteTransaction%0A
 %09@Overri

Remove WriteTransaction annotation