public List importMappings()

in clns-acuity-admin/acuity-core/src/main/java/com/acuity/visualisations/web/service/wizard/study/StudyMappingsService.java [171:267]


	public List<MappingStatusDTO> importMappings(InputStream inputStream, StudyRule studyRule, boolean replaceExisting) throws Exception {
		CSVReader reader = new CSVReader(new InputStreamReader(inputStream), ',', '"');
		ArrayList<String> columns = new ArrayList<String>();
		for (String s : Arrays.asList(reader.readNext())) {
			columns.add(s.toLowerCase());
		}
		if (replaceExisting) {
			for (FileRule fileRule : new ArrayList<>(studyRule.getFileRules())) {
				studyMappingsServicePartial.deleteFileRule(fileRule, studyRule);
			}
		}
		Map<Long, FileRule> fileRules = new HashMap<>();
		int idIdx = columns.indexOf("id_idx");
		int dataTypeIdx = columns.indexOf("data_type");
		int dataFieldIdx = columns.indexOf("data_field");
		int aggregationFunctionIdx = columns.indexOf("aggregation_function");
		int fileNameIdx = columns.indexOf("file_name");
		int decodingIdx = columns.indexOf("decoding");
		int defaultValueIdx = columns.indexOf("default_value");
		int sourceColumnIdx = columns.indexOf("source_column");
		int fileStandardIdx = columns.indexOf("file_standard");
		if (dataTypeIdx < 0
				|| idIdx < 0
				|| dataFieldIdx < 0
				|| aggregationFunctionIdx < 0
				|| fileNameIdx  < 0
				|| decodingIdx < 0
				|| defaultValueIdx < 0
				|| sourceColumnIdx  < 0
				|| fileStandardIdx < 0) {
			throw new Exception("Not all required columns found in CSV");
		}

		List<MappingRule> mapRulesWithDynamicFields = new ArrayList<>();

		String[] row = reader.readNext();

		while (row != null) {
			final Long id = Long.parseLong(row[idIdx]);
			final Long fileDescriptionId = Long.parseLong(row[dataTypeIdx]);
			final Long fieldDescriptionId = Long.parseLong(row[dataFieldIdx]);
			final Long aggregationFunction = Long.parseLong(row[aggregationFunctionIdx]);
			final String fileName = makeProperFileName(row[fileNameIdx]);
			final String decoding = row[decodingIdx];
			final String defaultValue = row[defaultValueIdx];
			final String sourceColumn = row[sourceColumnIdx];
			String standardFromFile = row[fileStandardIdx];
			final String standard = (standardFromFile == null || "".equals(standardFromFile) ? "AZRAW" : standardFromFile).toUpperCase();

			final boolean dynamic = fieldDescriptionId < 0;

			FileRule fileRule = fileRules.computeIfAbsent(id, p -> {
				FileRuleDTO fileRuleDTO = new FileRuleDTO();
				fileRuleDTO.setTypeId(fileDescriptionId);
				fileRuleDTO.setStudyAcuityEnabled(true);
				fileRuleDTO.setDataSourceLocation(fileName);
				fileRuleDTO.setFileStandard(FileStandard.valueOf(standard));
				return createFileRule(fileRuleDTO, studyRule);
			});

			for (MappingRule mappingRule: fileRule.getMappingRules()) {
				if (!dynamic && mappingRule.getFieldRules().get(0).getDescription().getId().equals(fieldDescriptionId)) {
					MapRuleDTO mapRuleDTO = new MapRuleDTO();
					mapRuleDTO.setAgrFunctions(aggregationFunction);
					mapRuleDTO.setDecodingInfo(decoding);
					mapRuleDTO.setDefaultValue(defaultValue);
					mapRuleDTO.setSourceData(sourceColumn);
					upateMapRule(mapRuleDTO, mappingRule);
				} else if (dynamic) {
					MapRuleDTO mapRuleDTO = new MapRuleDTO();
					mapRuleDTO.setAgrFunctions(aggregationFunction);
					mapRuleDTO.setDecodingInfo(decoding);
					mapRuleDTO.setDefaultValue(defaultValue);
					mapRuleDTO.setSourceData(sourceColumn);
					mapRuleDTO.setDynamic(true);

					MappingRule mapRule = createMapRule(mapRuleDTO, fileRule);
					fileRule.getMappingRules().add(mapRule);
					mapRulesWithDynamicFields.add(mapRule);

					break;
				}
			}
			row = reader.readNext();
		}
		for (FileRule fileRule : fileRules.values()) {
			studyMappingsServicePartial.saveFileRule(fileRule);
			studyMappingsServicePartial.saveMappingRules(fileRule);
		}

		for (MappingRule mappingRule : mapRulesWithDynamicFields) {
			mappingRule.getFieldRules().add(studyMappingsServicePartial
					.saveDynamicFieldRule(mappingRule, mappingRule.getColumnRules().get(0).getName()));
		}

		return validateMappings(studyRule);
	}