<commit_before>package us.mn.state.health.lims.datasubmission;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import us.mn.state.health.lims.common.log.LogEvent;
import us.mn.state.health.lims.datasubmission.valueholder.DataIndicator;
import us.mn.state.health.lims.datasubmission.valueholder.DataResource;
import us.mn.state.health.lims.datasubmission.valueholder.DataValue;
import us.mn.state.health.lims.siteinformation.dao.SiteInformationDAO;
import us.mn.state.health.lims.siteinformation.daoimpl.SiteInformationDAOImpl;

public class DataSubmitter {
	
	public static boolean sendDataIndicator(DataIndicator indicator) throws IOException, ParseException {
		boolean success = true;
		for (DataResource resource : indicator.getResources()) {
			String result;
			JSONObject jsonResult;
			
			//expand levels if level is ALL
			List<String> levels = new ArrayList<String>();
			if (resource.getLevel().equals(DataResource.ALL)) {
				levels = DataResource.getAllNamedLevels();
			} else {
				levels.add(resource.getLevel());
			}
			
			List<DataValue> columnValues = resource.getColumnValues();
			List<DataValue> commonValues = new ArrayList<DataValue>();
			//put in extra data that is often used
			commonValues.add(new DataValue("month", Integer.toString(indicator.getMonth()), false));
			commonValues.add(new DataValue("year", Integer.toString(indicator.getYear()), false));
			commonValues.add(new DataValue("facility", indicator.getFacilityCode(), false));
			columnValues.addAll(commonValues);
			//trust OE side has correct information
			Map<String,String> idsByLevel = resource.getLevelIdMap();
			//check for resource on server instead of trusting info is correct on OE side
			//Map<String,String> idsByLevel = getIdsBySearchKeys(resource, columnValuePairs);			
			try {
				//resource doesn't exist in any capacity on server. Send as normal
				if (idsByLevel.isEmpty()) {
					result = sendJSONPost(resource);
					jsonResult = (JSONObject) (new JSONParser()).parse(result);
					if (jsonResult.get("error") != null) {
						success = false;
					}
					for (String level : levels) {
						if (jsonResult.containsKey(level)) {
							String id = Long.toString((long) ((JSONObject) jsonResult.get(level)).get("id"));
							resource.getLevelIdMap().put(level, id);
						}
					}
				//resource fully, or partially exists on server, send as sub resources
				} else {
					resource.setLevelIdMap(idsByLevel);
					for (String level : levels) {
						if (idsByLevel.containsKey(level)) {
							result = sendJSONPut(resource, level);
							jsonResult = (JSONObject) (new JSONParser()).parse(result);
							if (jsonResult.get("error") != null) {
								success = false;
							}
						} else {
							result = sendJSONPost(resource, level);
							jsonResult = (JSONObject) (new JSONParser()).parse(result);
							if (jsonResult.get("error") != null) {
								success = false;
							}
							if (jsonResult.containsKey(level)) {
								String id = Long.toString((long) ((JSONObject) jsonResult.get(level.toString().toLowerCase())).get("id"));
								resource.getLevelIdMap().put(level, id);
							}
						}
					}
				}
			} catch (Exception e) {
				success = false;
				LogEvent.logError("DataSubmitter", "performAction()", e.toString());
				e.printStackTrace();
			} finally {
				//remove extra information as it does not need to be saved to database<commit_msg>datasubmission conversion fix<commit_after>package us.mn.state.health.lims.datasubmission;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import us.mn.state.health.lims.common.log.LogEvent;
import us.mn.state.health.lims.datasubmission.valueholder.DataIndicator;
import us.mn.state.health.lims.datasubmission.valueholder.DataResource;
import us.mn.state.health.lims.datasubmission.valueholder.DataValue;
import us.mn.state.health.lims.siteinformation.dao.SiteInformationDAO;
import us.mn.state.health.lims.siteinformation.daoimpl.SiteInformationDAOImpl;

public class DataSubmitter {
	
	public static boolean sendDataIndicator(DataIndicator indicator) throws IOException, ParseException {
		boolean success = true;
		for (DataResource resource : indicator.getResources()) {
			String result;
			JSONObject jsonResult;
			
			//expand levels if level is ALL
			List<String> levels = new ArrayList<String>();
			if (resource.getLevel().equals(DataResource.ALL)) {
				levels = DataResource.getAllNamedLevels();
			} else {
				levels.add(resource.getLevel());
			}
			
			List<DataValue> columnValues = resource.getColumnValues();
			List<DataValue> commonValues = new ArrayList<DataValue>();
			//put in extra data that is often used
			commonValues.add(new DataValue("month", Integer.toString(indicator.getMonth()), false));
			commonValues.add(new DataValue("year", Integer.toString(indicator.getYear()), false));
			commonValues.add(new DataValue("facility", indicator.getFacilityCode(), false));
			columnValues.addAll(commonValues);
			//trust OE side has correct information
			Map<String,String> idsByLevel = resource.getLevelIdMap();
			//check for resource on server instead of trusting info is correct on OE side
			//Map<String,String> idsByLevel = getIdsBySearchKeys(resource, columnValuePairs);			
			try {
				//resource doesn't exist in any capacity on server. Send as normal
				if (idsByLevel.isEmpty()) {
					result = sendJSONPost(resource);
					jsonResult = (JSONObject) (new JSONParser()).parse(result);
					if (jsonResult.get("error") != null) {
						success = false;
					}
					for (String level : levels) {
						if (jsonResult.containsKey(level)) {
							String id = Long.toString((Long) ((JSONObject) jsonResult.get(level)).get("id"));
							resource.getLevelIdMap().put(level, id);
						}
					}
				//resource fully, or partially exists on server, send as sub resources
				} else {
					resource.setLevelIdMap(idsByLevel);
					for (String level : levels) {
						if (idsByLevel.containsKey(level)) {
							result = sendJSONPut(resource, level);
							jsonResult = (JSONObject) (new JSONParser()).parse(result);
							if (jsonResult.get("error") != null) {
								success = false;
							}
						} else {
							result = sendJSONPost(resource, level);
							jsonResult = (JSONObject) (new JSONParser()).parse(result);
							if (jsonResult.get("error") != null) {
								success = false;
							}
							if (jsonResult.containsKey(level)) {
								String id = Long.toString((Long) ((JSONObject) jsonResult.get(level.toString().toLowerCase())).get("id"));
								resource.getLevelIdMap().put(level, id);
							}
						}
					}
				}
			} catch (Exception e) {
				success = false;
				LogEvent.logError("DataSubmitter", "performAction()", e.toString());
				e.printStackTrace();
			} finally {
				//remove extra information as it does not need to be saved to database
{'fraction_non_alphanumeric': 0.12509344629952654, 'fraction_numerical': 0.0, 'mean_word_length': 3.970278637770898, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5451341', 'n_tokens_mistral': 2844, 'n_tokens_neox': 2336, 'n_words': 511}