fr.ird.observe.toolkit.maven.plugin.fixtures.FixturesTemplate Maven / Gradle / Ivy
package fr.ird.observe.toolkit.maven.plugin.fixtures;
/*-
* #%L
* ObServe Toolkit :: Maven plugin
* %%
* Copyright (C) 2017 - 2022 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 com.google.gson.Gson;
import fr.ird.observe.dto.BusinessDto;
import fr.ird.observe.dto.ToolkitId;
import fr.ird.observe.dto.data.DataDto;
import fr.ird.observe.dto.referential.ReferentialDto;
import fr.ird.observe.dto.referential.ReferentialDtoReferenceWithCodeAware;
import fr.ird.observe.dto.referential.ReferentialDtoReferenceWithNoCodeAware;
import fr.ird.observe.navigation.tree.io.ToolkitTreeNodeStates;
import fr.ird.observe.navigation.tree.io.request.ToolkitRequestConfig;
import fr.ird.observe.services.ObserveServiceInitializer;
import fr.ird.observe.services.ObserveServiceMainFactory;
import fr.ird.observe.services.service.api.DataEntityService;
import fr.ird.observe.services.service.api.ReferentialEntityService;
import fr.ird.observe.spi.json.java.util.DateAdapter;
import fr.ird.observe.toolkit.maven.plugin.server.TemplateHelper;
import fr.ird.observe.toolkit.maven.plugin.server.html.model.DocModule;
import fr.ird.observe.toolkit.maven.plugin.server.html.model.DocPackage;
import fr.ird.observe.toolkit.maven.plugin.server.html.model.DocProject;
import fr.ird.observe.toolkit.maven.plugin.server.html.model.DocRequest;
import fr.ird.observe.toolkit.maven.plugin.server.html.model.DocType;
import fr.ird.observe.toolkit.maven.plugin.server.html.model.Requests;
import io.ultreia.java4all.util.SortedProperties;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
/**
* Created on 22/11/2021.
*
* @author Tony Chemit - [email protected]
* @since 5.0.55
*/
public class FixturesTemplate {
public static final String PAYLOAD = "payload";
private final FixturesModel model;
private final Gson gson;
private final ReferentialEntityService referentialEntityService;
private final DataEntityService dataEntityService;
private final ToolkitRequestConfig config;
private final ToolkitRequestConfig fullConfig;
private final DateAdapter dataAdapter;
public FixturesTemplate(FixturesModel model, Gson gson, ObserveServiceMainFactory serviceMainFactory, ObserveServiceInitializer serviceInitializer) {
this.model = model;
this.gson = gson;
this.referentialEntityService = serviceMainFactory.newService(serviceInitializer, ReferentialEntityService.class);
this.dataEntityService = serviceMainFactory.newService(serviceInitializer, DataEntityService.class);
this.config = new ToolkitRequestConfig(false, true, true, false, false);
this.fullConfig = new ToolkitRequestConfig(false, true, true, true, false);
dataAdapter = new DateAdapter();
}
public void generate() throws IOException {
generateIds();
generateInit();
this.generateContent(
model.getReferentialPaths(),
(type, id) -> referentialEntityService.getOne(type, config, id),
(type, id) -> referentialEntityService.getOne(type, fullConfig, id));
this.generateContent(
model.getDataPaths(),
(type, id) -> dataEntityService.getOne(type, config, id),
(type, id) -> dataEntityService.getOne(type, fullConfig, id));
generateByProject();
generateByModule();
generateByPackage();
}
private void generateIds() throws IOException {
Path variablesPath = model.getVariablesPath();
SortedProperties variables = model.getVariables();
Files.deleteIfExists(variablesPath);
List result = new ArrayList<>(variables.size());
for (String variableId : variables.stringPropertyNames()) {
result.add(String.format("%s=%s", variableId, variables.get(variableId)));
}
Files.write(variablesPath, result);
}
private void generateInit() {
DocProject project = new DocProject("init");
for (Map.Entry entry : model.getInitPaths().entrySet()) {
String queryType = entry.getKey();
Path path = entry.getValue();
Requests requests = Requests.valueOf(queryType);
Map parameters = createParameters();
String result = null;
String modelVersion = model.getModelVersion().getVersion();
switch (requests) {
case Ping:
parameters.clear();
result = "{\n" +
" \"modelVersion\": \"" + modelVersion + "\",\n" +
" \"serverVersion\": \"" + model.getApplicationVersion().getVersion() + "\"\n" +
"}";
break;
case Open:
parameters.clear();
addParameter(parameters, "config.modelVersion", modelVersion);
addParameter(parameters, "config.login", "aLogin");
addParameter(parameters, "config.password", "aPassword");
addParameter(parameters, "config.databaseName", "9-tck");
addParameter(parameters, "referentialLocale", "FR");
result = "{\n" +
" \"url\": \"http://localhost:8080/observeweb\",\n" +
" \"authenticationToken\": \"XXX\",\n" +
" \"dataSourceInformation\": {\n" +
" \"permission\": \"ALL\",\n" +
" \"minimumVersion\": \"3.0\",\n" +
" \"version\": \"" + modelVersion + "\",\n" +
" \"migrations\": [],\n" +
" \"owner\": true,\n" +
" \"superUser\": true\n" +
" },\n" +
" \"connectMode\": \"SERVER\"\n" +
"}";
break;
case Close:
result = "null";
break;
case Information:
result = "{\n" +
" \"url\": \"http://localhost:8080/observeweb\",\n" +
" \"login\": \"aLogin\",\n" +
" \"database\": \"9-tck\",\n" +
" \"apiAccess\": \"ALL\",\n" +
" \"validationMode\": \"STRONG\",\n" +
" \"authenticationToken\": \"XXX\",\n" +
" \"modelVersion\": \"" + modelVersion + "\",\n" +
" \"credentials\": \"ALL\"\n" +
"}";
break;
}
consumeInit(path, project, requests, parameters, result);
}
}
private void consumeInit(Path path, DocProject project, Requests queryType, Map parameters, String responseContent) {
DocRequest request = new DocRequest(project, queryType);
String requestContent = toJson(request, parameters);
write(request, path, requestContent, responseContent);
}
private void generateByProject() {
ToolkitTreeNodeStates all = referentialEntityService.getByModule(null, config);
consumeGetAll(Requests.GetAllForProject, model.getFixturesPath().resolve("referential"), all);
}
private void generateByModule() {
for (Map.Entry entry : model.getGetByModule().entrySet()) {
String moduleName = entry.getKey();
Path path = entry.getValue();
ToolkitTreeNodeStates all = referentialEntityService.getByModule(moduleName, config);
consumeGetAll(Requests.GetAllForModule, path, all);
}
}
private void generateByPackage() {
for (Map.Entry entry : model.getGetByPackage().entrySet()) {
String packageName = entry.getKey();
Path path = entry.getValue();
ToolkitTreeNodeStates all = referentialEntityService.getByPackage(packageName, config);
consumeGetAll(Requests.GetAllForPackage, path, all);
}
}
private void generateContent(Map pathMap,
BiFunction, String, ToolkitTreeNodeStates> getConsumer,
BiFunction, String, ToolkitTreeNodeStates> fullConsumer) {
Map> types = model.getTypes();
SortedProperties variables = model.getVariables();
for (Map.Entry entry : pathMap.entrySet()) {
String idVariable = entry.getKey();
Path path = entry.getValue();
@SuppressWarnings("unchecked") Class dtoType = (Class) types.get(idVariable);
String id = variables.getProperty(idVariable);
if (id == null || id.isEmpty()) {
continue;
}
ToolkitTreeNodeStates toolkitTreeNodeStates = getConsumer.apply(dtoType, id);
ToolkitTreeNodeStates fullToolkitTreeNodeStates = fullConsumer.apply(dtoType, id);
Path packagePath = path.getParent();
Path modulePath = packagePath.getParent();
Path projectPath = modulePath.getParent();
DocProject project = new DocProject(projectPath.toFile().getName());
DocModule module = new DocModule(project, modulePath.toFile().getName());
DocPackage docPackage = new DocPackage(module, packagePath.toFile().getName());
DocType docType = new DocType(docPackage, path.toFile().getName());
Map result = new LinkedHashMap<>();
if (ReferentialDto.class.isAssignableFrom(dtoType)) {
@SuppressWarnings("unchecked") Class extends ReferentialDto> referentialType = (Class extends ReferentialDto>) dtoType;
ToolkitTreeNodeStates all = referentialEntityService.getAll(referentialType, config);
consumeGetAll(Requests.GetAll, path, all);
}
consumeGet(docType, path, dtoType, fullToolkitTreeNodeStates, id, result);
Map object = consumeUpdate(docType, path, toolkitTreeNodeStates, id, result);
consumeCreate(docType, path, object, result);
consumeDelete(docType, path, id);
}
}
protected void consumeGet(DocType docType, Path path, Class dtoType, ToolkitTreeNodeStates toolkitTreeNodeStates, String id, Map result) {
DocRequest requestOne = new DocRequest(docType, Requests.GetOne);
Map requestOneParameters = addConfigParameters(createParameters());
String requestPrototypeContent = toJson(requestOne, null);
requestOne.setIdInPath(id);
String requestOneContent = toJson(requestOne, requestOneParameters);
String json = toolkitTreeNodeStates.getState("content");
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy