fr.ird.observe.toolkit.eugene.templates.DtoHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eugene-templates Show documentation
Show all versions of eugene-templates Show documentation
ObServe Eugene templates module
The newest version!
package fr.ird.observe.toolkit.eugene.templates;
/*-
* #%L
* ObServe Toolkit :: Eugene Templates
* %%
* Copyright (C) 2008 - 2017 IRD, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
import fr.ird.observe.dto.IdHelper;
import org.nuiton.eugene.TemplateConfiguration;
import org.nuiton.eugene.java.JavaGeneratorUtil;
import org.nuiton.eugene.models.object.ObjectModelAttribute;
import org.nuiton.eugene.models.object.ObjectModelClass;
import org.nuiton.eugene.models.object.xml.ObjectModelAttributeImpl;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
/**
* Created by tchemit on 31/08/17.
*
* @author Tony Chemit - [email protected]
*/
public class DtoHelper {
public static String generateBinderName(int defaultPackageNameLength, String binderPackageName, String fqn, String dtoName, boolean referential) {
String relativeName = fqn.substring(defaultPackageNameLength);
if (referential) {
binderPackageName += ".referential.";
if (relativeName.contains(".longline")) {
return binderPackageName + "longline." + dtoName + "Binder";
} else if (relativeName.contains(".seine")) {
return binderPackageName + "seine." + dtoName + "Binder";
} else {
return binderPackageName + "common." + dtoName + "Binder";
}
} else {
binderPackageName += ".data.";
if (relativeName.contains(".longline")) {
return binderPackageName + "longline." + dtoName + "Binder";
} else {
return binderPackageName + "seine." + dtoName + "Binder";
}
}
}
public static Map getFormProperties(ObjectModelClass input) {
Collection attributes = new LinkedList<>(input.getAttributes());
attributes.addAll(input.getAllOtherAttributes());
Map properties = new TreeMap<>();
for (ObjectModelAttribute attr : attributes) {
if (!attr.isNavigable()) {
continue;
}
String type = attr.getType();
if (!type.endsWith("Reference") || !type.contains("referential")) {
continue;
}
properties.put(attr.getName(), JavaGeneratorUtil.getSimpleName(type));
}
return properties;
}
static void generateMetaInfService(TemplateConfiguration configuration, String outputName, Class type) {
Path outputDirectory = configuration.getProperty(TemplateConfiguration.PROP_RESOURCE_DIRECTORY, File.class).toPath();
Path file = outputDirectory.resolve("META-INF").resolve("services");
try {
if (!Files.exists(file)) {
Files.createDirectories(file);
}
Files.write(file.resolve(type.getName()), outputName.getBytes());
} catch (IOException e) {
throw new IllegalStateException("Could not create file: " + file);
}
}
public static Set getReferenceProperties(DtoTransformerContext context, ObjectModelClass input, Set availableProperties) {
Collection attributes = new LinkedList<>(input.getAttributes());
attributes.addAll(input.getAllOtherAttributes());
Map properties = new TreeMap<>();
for (ObjectModelAttribute attr : attributes) {
if (attr.isNavigable()) {
String attrName = attr.getName();
String type = attr.getType();
if (context.selectedClassesFqn.contains(IdHelper.cleanId(type))) {
// Dto
if (availableProperties.contains(attrName + "Label")) {
ObjectModelAttributeImpl newAttr = new ObjectModelAttributeImpl();
newAttr.setName(attrName + "Label");
newAttr.setType("String");
// only keep navigable attributes
properties.put(newAttr.getName(), newAttr);
}
if (availableProperties.contains(attrName + "Id")) {
ObjectModelAttributeImpl newAttr = new ObjectModelAttributeImpl();
newAttr.setName(attrName + "Id");
newAttr.setType("String");
// only keep navigable attributes
properties.put(newAttr.getName(), newAttr);
}
// Get a ref
if (availableProperties.contains(attrName)) {
ObjectModelAttributeImpl newAttr = new ObjectModelAttributeImpl();
newAttr.setName(attrName);
if (!type.endsWith("Reference")) {
// not a ref, get a ref
type = IdHelper.cleanId(type) + "Reference";
}
newAttr.setType(context.getAttributeType(type));
properties.put(attrName, newAttr);
}
continue;
}
// Simple type
if (availableProperties.contains(attrName)) {
ObjectModelAttributeImpl newAttr = new ObjectModelAttributeImpl();
newAttr.setName(attrName);
newAttr.setType(type);
properties.put(attrName, newAttr);
}
}
}
if (availableProperties.contains("label") && properties.values().stream().noneMatch(p -> p.getName().equals("label"))) {
ObjectModelAttributeImpl attr = new ObjectModelAttributeImpl();
attr.setName("label");
attr.setType("String");
properties.put("label", attr);
}
Set result = new LinkedHashSet<>();
for (String availableProperty : availableProperties) {
ObjectModelAttribute e = properties.get(availableProperty);
if (e == null) {
ObjectModelAttributeImpl ee = new ObjectModelAttributeImpl();
ee.setName(availableProperty);
ee.setType("String");
e = ee;
}
Objects.requireNonNull(e, "Cant' find property " + availableProperty + " on " + input.getQualifiedName());
result.add(e);
}
return result;
}
}