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

com.xlrit.gears.plugin.jasper.ExampleTemplateWriter Maven / Gradle / Ivy

There is a newer version: 1.17.4
Show newest version
package com.xlrit.gears.plugin.jasper;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.xlrit.gears.engine.meta.*;

import lombok.Data;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
class ExampleTemplateWriter {
	private final MetaManager metaManager;
	private final List fields = new ArrayList<>();
	private final List subDatasets = new ArrayList<>();

	public void processParameters(Object parameters) {
		TypeInfo typeInfo = metaManager.requireTypeInfo(parameters);
		processRootType(typeInfo);
	}

	public void print() {
		for (Dataset subDataset : subDatasets) {
			System.out.println(subDataset.toString());
		}
		for (Field field : fields) {
			System.out.println(field.toString());
		}
	}

	private void processRootType(TypeInfo typeInfo) {
		if (typeInfo instanceof MultipleInfo) {
			MultipleInfo multipleInfo = (MultipleInfo) typeInfo;
			processRootType(multipleInfo.getElementType());
		}
		else if (typeInfo instanceof ObjectInfo) {
			ObjectInfo objectInfo = (ObjectInfo) typeInfo;
			fields.addAll(extractDirectFields(objectInfo));
			processNestedTypes(objectInfo);
		}
		else {
			throw new UnsupportedOperationException("Expected an object or collection of objects");
		}
	}

	private void processNestedTypes(ObjectInfo objectInfo) {
		objectInfo.getFields().stream()
			.filter(BaseField::isEntityReference)
			.forEach(f -> processNestedType(f.getLabel(), f.getTypeInfo()));
	}

	private void processNestedType(String name, TypeInfo typeInfo) {
		if (typeInfo instanceof MultipleInfo multipleInfo) {
			processNestedType(name, multipleInfo.getElementType());
		}
		else if (typeInfo instanceof ObjectInfo) {
			ObjectInfo objectInfo = (ObjectInfo) typeInfo;
			subDatasets.add(new Dataset(name, extractDirectFields(objectInfo)));
			processNestedTypes(objectInfo);
		}
		else {
			throw new UnsupportedOperationException("Expected an object or collection of objects");
		}
	}

	private List extractDirectFields(ObjectInfo objectInfo) {
		return objectInfo.getFields().stream()
			.filter(field -> field.getTypeInfo() instanceof BasicTypeInfo)
			.map(this::convertField)
			.collect(Collectors.toList());
	}

	private Field convertField(BaseField field) {
		String fieldName = field.getName();
		String className = field.getTypeInfo().getJavaType().getName();
		return new Field(fieldName, className);
	}

	@Data
	static class Field {
		private final String fieldName;
		private final String className;

		@Override
		public String toString() {
			return "\t";
		}
	}

	@Data
	static class Dataset {
		private final String name;
		private final List fields;

		@Override
		public String toString() {
			StringBuilder sb = new StringBuilder();
			sb.append("\t\n");
			for (Field field : fields) {
				sb.append("\t").append(field.toString()).append("\n");
			}
			sb.append("\t");
			return sb.toString();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy