package com.b2international.snowowl.snomed.api.impl;

import java.io.File;
import java.io.IOException;

import com.b2international.commons.FileUtils;
import com.b2international.snowowl.core.SnowOwlApplication;
import com.b2international.snowowl.core.exceptions.NotFoundException;
import com.b2international.snowowl.snomed.api.domain.browser.ISnomedBrowserConcept;
import com.b2international.snowowl.snomed.api.domain.exception.SnomedPersistanceException;
import com.b2international.snowowl.snomed.api.impl.domain.browser.SnomedBrowserConcept;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SnomedManualConceptMergeServiceImpl {

	final private ObjectMapper objectMapper;
	final private String storeRoot;
	
	private static String MERGE_REVIEW_STORE = "merge-review-store";
	private static final String SLASH = "/";
	private static final String FILE_TYPE = ".json";
	
	public SnomedManualConceptMergeServiceImpl() {
		objectMapper = new ObjectMapper();
		storeRoot = SnowOwlApplication.INSTANCE.getEnviroment().getDataDirectory().getPath();
	}

	public void storeConceptChanges(String branchPath, String mergeReviewId, ISnomedBrowserConcept conceptUpdate) throws SnomedPersistanceException {
		try {
			File conceptFile = getConceptStorePath(branchPath, mergeReviewId, conceptUpdate.getConceptId());
			conceptFile.getParentFile().mkdirs();
			objectMapper.writeValue(conceptFile, conceptUpdate);
		} catch (IOException e) {
			throw new SnomedPersistanceException("Failed to persist manual concept merge.", e);
		}
	}

	public boolean exists(String branchPath, String mergeReviewId, String conceptId) {
		return getConceptStorePath(branchPath, mergeReviewId, conceptId).isFile();
	}
	
	public ISnomedBrowserConcept retrieve(String branchPath, String mergeReviewId, String conceptId) throws IOException {
		File conceptFile = getConceptStorePath(branchPath, mergeReviewId, conceptId);
		if (!conceptFile.isFile()) {
			throw new NotFoundException("manual concept merge", conceptId);
		}
		return objectMapper.readValue(conceptFile, SnomedBrowserConcept.class);
	}
	
	private File getConceptStorePath(String branchPath, String mergeReviewId, String conceptId) {
		return new File(storeRoot + SLASH + MERGE_REVIEW_STORE + SLASH + branchPath + SLASH + mergeReviewId + SLASH + conceptId + FILE_TYPE); 
	}

	public void deleteAll(String branchPath, String mergeReviewId) {
		
		final File parentFile = getConceptStorePath(branchPath, mergeReviewId, "1").getParentFile();
		FileUtils.deleteDirectory(parentFile);
	}

}

@@ -97,16 +97,90 @@
 ption;%0A%0A
+import javax.annotation.PostConstruct;%0Aimport javax.annotation.Resource;%0A%0A
 import c
@@ -716,22 +716,27 @@
 mpl %7B%0A%0A%09
-final 
+@Resource%0A%09
 private 
@@ -763,22 +763,18 @@
 apper;%0A%09
-final 
+%0A%09
 private 
@@ -958,91 +958,47 @@
 %0A%09%0A%09
-public SnomedManualConceptMergeServiceImpl() %7B%0A%09%09objectMapper = new ObjectMapper();
+@PostConstruct%0A%09protected void init() %7B
 %0A%09%09s
@@ -2413,11 +2413,8 @@
 ) %7B%0A
-%09%09%0A
 %09%09fi
@@ -2548,11 +2548,10 @@
 le);%0A%09%7D%0A
-%0A
 %7D%0A

Use external ObjectMapper when saving manually merged concepts