use of org.apache.cayenne.gen.ClassGenerationAction in project cayenne by apache.
the class CayenneGeneratorTask method execute.
/**
* Executes the task. It will be called by ant framework.
*/
@Override
public void execute() throws BuildException {
validateAttributes();
Injector injector = DIBootstrap.createInjector(new ToolsModule(LoggerFactory.getLogger(CayenneGeneratorTask.class)));
AntLogger logger = new AntLogger(this);
CayenneGeneratorMapLoaderAction loadAction = new CayenneGeneratorMapLoaderAction(injector);
loadAction.setMainDataMapFile(map);
loadAction.setAdditionalDataMapFiles(additionalMaps);
CayenneGeneratorEntityFilterAction filterAction = new CayenneGeneratorEntityFilterAction();
filterAction.setClient(client);
filterAction.setNameFilter(NamePatternMatcher.build(logger, includeEntitiesPattern, excludeEntitiesPattern));
try {
DataMap dataMap = loadAction.getMainDataMap();
ClassGenerationAction generatorAction = createGeneratorAction();
generatorAction.setLogger(logger);
generatorAction.setTimestamp(map.lastModified());
generatorAction.setDataMap(dataMap);
generatorAction.addEntities(filterAction.getFilteredEntities(dataMap));
generatorAction.addEmbeddables(filterAction.getFilteredEmbeddables(dataMap));
generatorAction.addQueries(dataMap.getQueryDescriptors());
generatorAction.execute();
} catch (Exception e) {
throw new BuildException(e);
}
}
use of org.apache.cayenne.gen.ClassGenerationAction in project cayenne by apache.
the class CgenTask method generate.
@TaskAction
public void generate() {
File dataMapFile = getDataMapFile();
Injector injector = DIBootstrap.createInjector(new ToolsModule(LoggerFactory.getLogger(CgenTask.class)));
CayenneGeneratorMapLoaderAction loaderAction = new CayenneGeneratorMapLoaderAction(injector);
loaderAction.setMainDataMapFile(dataMapFile);
CayenneGeneratorEntityFilterAction filterAction = new CayenneGeneratorEntityFilterAction();
filterAction.setClient(client);
filterAction.setNameFilter(NamePatternMatcher.build(getLogger(), includeEntities, excludeEntities));
try {
loaderAction.setAdditionalDataMapFiles(convertAdditionalDataMaps());
ClassGenerationAction generator = this.createGenerator();
DataMap dataMap = loaderAction.getMainDataMap();
generator.setLogger(getLogger());
if (this.force || getProject().hasProperty("force")) {
generator.setForce(true);
}
generator.setTimestamp(dataMapFile.lastModified());
generator.setDataMap(dataMap);
generator.addEntities(filterAction.getFilteredEntities(dataMap));
generator.addEmbeddables(dataMap.getEmbeddables());
generator.addQueries(dataMap.getQueryDescriptors());
generator.execute();
} catch (Exception exception) {
throw new GradleException("Error generating classes: ", exception);
}
}
use of org.apache.cayenne.gen.ClassGenerationAction in project cayenne by apache.
the class CgenTaskTest method testGeneratorCreation.
@Test
public void testGeneratorCreation() {
ClassGenerationAction action = mock(ClassGenerationAction.class);
CgenTask task = createCgenTaskMock(action);
task.setEmbeddableSuperTemplate("superTemplate");
task.setEmbeddableTemplate("template");
task.setEncoding("UTF-8");
task.setExcludeEntities("entity1");
task.setIncludeEntities("entity2");
task.setMode("entity");
task.setOutputPattern("pattern");
task.setSuperPkg("org.example.model.auto");
task.setSuperTemplate("*.java");
task.setTemplate("*.java");
task.setMakePairs(true);
task.setCreatePropertyNames(true);
task.setOverwrite(true);
task.setUsePkgPath(true);
ClassGenerationAction createdAction = task.createGenerator();
assertSame(action, createdAction);
verify(action).setCreatePropertyNames(true);
verify(action).setMakePairs(true);
verify(action).setOverwrite(true);
verify(action).setUsePkgPath(true);
verify(action).setArtifactsGenerationMode("entity");
verify(action).setEncoding("UTF-8");
verify(action).setEmbeddableSuperTemplate("superTemplate");
verify(action).setEmbeddableTemplate("template");
verify(action).setOutputPattern("pattern");
verify(action).setSuperPkg("org.example.model.auto");
verify(action).setSuperTemplate("*.java");
verify(action).setTemplate("*.java");
}
use of org.apache.cayenne.gen.ClassGenerationAction in project cayenne by apache.
the class CustomModeController method newGenerator.
@Override
protected ClassGenerationAction newGenerator() {
ClassGenerationAction action = new ClassGenerationAction();
getApplication().getInjector().injectMembers(action);
return action;
}
use of org.apache.cayenne.gen.ClassGenerationAction in project cayenne by apache.
the class GeneratorController method createGenerator.
/**
* Creates a class generator for provided selections.
*/
public Collection<ClassGenerationAction> createGenerator() {
File outputDir = getOutputDir();
// no destination folder
if (outputDir == null) {
JOptionPane.showMessageDialog(this.getView(), "Select directory for source files.");
return null;
}
// no such folder
if (!outputDir.exists() && !outputDir.mkdirs()) {
JOptionPane.showMessageDialog(this.getView(), "Can't create directory " + outputDir + ". Select a different one.");
return null;
}
// not a directory
if (!outputDir.isDirectory()) {
JOptionPane.showMessageDialog(this.getView(), outputDir + " is not a valid directory.");
return null;
}
// remove generic entities...
Collection<ObjEntity> selectedEntities = new ArrayList<>(getParentController().getSelectedEntities());
selectedEntities.removeIf(ObjEntity::isGeneric);
Collection<ClassGenerationAction> generators = new ArrayList<>();
Collection<StandardPanelComponent> dataMapLines = ((GeneratorControllerPanel) getView()).getDataMapLines();
for (DataMap map : getParentController().getDataMaps()) {
try {
ClassGenerationAction generator = newGenerator();
generator.setArtifactsGenerationMode(mode);
generator.setDataMap(map);
LinkedList<ObjEntity> objEntities = new LinkedList<>(map.getObjEntities());
objEntities.retainAll(selectedEntities);
generator.addEntities(objEntities);
LinkedList<Embeddable> embeddables = new LinkedList<>(map.getEmbeddables());
embeddables.retainAll(getParentController().getSelectedEmbeddables());
generator.addEmbeddables(embeddables);
generator.addQueries(map.getQueryDescriptors());
Preferences preferences = application.getPreferencesNode(GeneralPreferences.class, "");
if (preferences != null) {
generator.setEncoding(preferences.get(GeneralPreferences.ENCODING_PREFERENCE, null));
}
generator.setDestDir(outputDir);
generator.setMakePairs(true);
generator.setForce(true);
for (StandardPanelComponent dataMapLine : dataMapLines) {
if (dataMapLine.getDataMap() == map && !Util.isEmptyString(dataMapLine.getSuperclassPackage().getText())) {
generator.setSuperPkg(dataMapLine.getSuperclassPackage().getText());
break;
}
}
generators.add(generator);
} catch (CayenneRuntimeException exception) {
JOptionPane.showMessageDialog(this.getView(), exception.getUnlabeledMessage());
return null;
}
}
return generators;
}
Aggregations