fr.ird.observe.maven.plugins.toolbox.GenerateI18nValidatorFieldsMojo Maven / Gradle / Ivy
Show all versions of toolbox-maven-plugin Show documentation
package fr.ird.observe.maven.plugins.toolbox;
/*-
* #%L
* ObServe Toolkit :: Maven plugin
* %%
* Copyright (C) 2017 - 2020 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 com.google.common.collect.ImmutableMap;
import fr.ird.observe.dto.data.DataDto;
import fr.ird.observe.dto.referential.I18nReferentialDto;
import fr.ird.observe.dto.referential.ReferentialDto;
import fr.ird.observe.spi.DtoModelHelper;
import io.ultreia.java4all.application.context.ApplicationContext;
import io.ultreia.java4all.i18n.spi.I18nResourceInitializationException;
import io.ultreia.java4all.i18n.spi.bean.BeanPropertyI18nKeyProducer;
import io.ultreia.java4all.i18n.spi.bean.BeanPropertyI18nKeyProducerProvider;
import io.ultreia.java4all.i18n.spi.builder.I18nKeySet;
import io.ultreia.java4all.i18n.spi.builder.I18nModule;
import io.ultreia.java4all.i18n.spi.builder.I18nModuleNotInitializedException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
/**
* Pour générer les clefs i18n des champs utilisés dans les validateurs.
*
* Created on 31/08/16.
*
* @author Tony Chemit - [email protected]
* @since 5.0
*/
@Mojo(name = "generate-i18n-validator-fields", threadSafe = true, defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE)
class GenerateI18nValidatorFieldsMojo extends GenerateValidatorMojoSupport {
/**
* The root directory where to generated.
*/
@Parameter(property = "generateI18nValidatorFields.outputDirectory", defaultValue = "${basedir}/target/generated-sources/java", required = true)
private File outputDirectory;
/**
* Un flag pour activer le mode verbeux.
*
* @since 1.0.0
*/
@Parameter(property = "generateI18nValidatorFields.verbose", defaultValue = "${maven.verbose}")
private boolean verbose;
/**
* A flag to skip the goal.
*
* @since 1.0.0
*/
@Parameter(property = "generateI18nValidatorFields.skip", defaultValue = "false")
private boolean skip;
private Map, Class>> classMapping;
@Override
protected Path createOutputFile() {
return null;
}
@Override
protected boolean isSkip() {
return skip;
}
@Override
protected void loadValidators() throws IOException {
if (ApplicationContext.isInit()) {
ApplicationContext.get().close();
}
try {
new ApplicationContext();
ImmutableMap.Builder, Class>> classMappingBuilder = ImmutableMap.builder();
DtoModelHelper.getDtoToMainDtoClassMapping().getMapping().keySet().forEach(t -> {
getLog().info("Add dto: " + t.getName());
classMappingBuilder.put(t.getSuperclass(), t);
classMappingBuilder.put(t, t);
});
classMappingBuilder.put(ReferentialDto.class, ReferentialDto.class);
classMappingBuilder.put(I18nReferentialDto.class, I18nReferentialDto.class);
classMappingBuilder.put(DataDto.class, DataDto.class);
this.classMapping = classMappingBuilder.build();
} finally {
if (ApplicationContext.isInit()) {
ApplicationContext.get().close();
}
}
super.loadValidators();
}
@Override
public void doAction() throws I18nModuleNotInitializedException, I18nResourceInitializationException, IOException {
Log log = getLog();
if (isVerbose()) {
log.info("project = " + getProject());
}
log.info("Generate to " + getOutputFile());
I18nModule i18nModule = I18nModule.forGetter(getProject().getProperties());
I18nKeySet getterFile = i18nModule.getModuleKeySet("validation-fields");
List compileSourceRoots = getProject().getTestCompileSourceRoots();
if (!compileSourceRoots.contains(outputDirectory.getAbsolutePath())) {
log.info("Add to test compile source root: " + outputDirectory);
getProject().addTestCompileSourceRoot(outputDirectory.getAbsolutePath());
}
BeanPropertyI18nKeyProducer labelsBuilder = BeanPropertyI18nKeyProducerProvider.get().getDefaultLabelsBuilder();
for (ValidatorsCache.ValidatorInfo validator : getValidators()) {
Class> type = validator.getType();
for (String field : validator.getFields()) {
String key = labelsBuilder.getI18nPropertyKey(type, field);
getterFile.addKey(key);
}
}
log.info(String.format("%d i18n keys(s) detected.", getterFile.size()));
i18nModule.storeModuleKeySet(getterFile);
}
@Override
public boolean isVerbose() {
return verbose;
}
@Override
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
@Override
public Map, Class>> getExtraClassMapping() {
return classMapping;
}
}