All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.engine.importer.ImportManager Maven / Gradle / Ivy

package com.xlrit.gears.engine.importer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.annotations.VisibleForTesting;
import com.xlrit.gears.engine.meta.MetaManager;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@RequiredArgsConstructor
public class ImportManager {
	private final MetaManager metaManager;
	private final EntityManager entityManager;
	private final ObjectMapper objectMapper;

	@Transactional
	public void importRaw(String targetName, String sourceName, Object raw) {
		ImportSource source = createSource(sourceName);
		try (ImportTarget target = createTarget(targetName)) {
			doImport(source, target, raw);
		}
	}

	@VisibleForTesting
	void doImport(ImportSource source, ImportTarget target, Object raw) {
		source.setInput(raw);
		for (SkeletonObject skeleton : source) {
			if (target.getCurrentCollection() == null) {
				target.startCollection(skeleton.classFqn());
			}
			if (!skeleton.classFqn().equals(target.getCurrentCollection())) {
				target.endCollection();
				target.startCollection(skeleton.classFqn());
			}
			target.add(skeleton);
		}
		target.endCollection();
	}

	private ImportSource createSource(String sourceName) {
		return switch (sourceName.toLowerCase()) {
			case "json" -> new JsonImportSource(objectMapper);
			default -> throw new IllegalArgumentException("Unknown import source: " + sourceName);
		};
	}

	private ImportTarget createTarget(String targetName) {
		return switch (targetName.toLowerCase()) {
			case "jpa" -> new JpaImportTarget(metaManager, entityManager);
			default -> throw new IllegalArgumentException("Unknown import target: " + targetName);
		};
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy