Search in sources :

Example 6 with ClassGenerationAction

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);
    }
}
Also used : Injector(org.apache.cayenne.di.Injector) BuildException(org.apache.tools.ant.BuildException) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) BuildException(org.apache.tools.ant.BuildException) DataMap(org.apache.cayenne.map.DataMap) ClientClassGenerationAction(org.apache.cayenne.gen.ClientClassGenerationAction) ClassGenerationAction(org.apache.cayenne.gen.ClassGenerationAction)

Example 7 with ClassGenerationAction

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);
    }
}
Also used : Injector(org.apache.cayenne.di.Injector) GradleException(org.gradle.api.GradleException) File(java.io.File) InputFile(org.gradle.api.tasks.InputFile) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) GradleException(org.gradle.api.GradleException) InvalidUserDataException(org.gradle.api.InvalidUserDataException) ClientClassGenerationAction(org.apache.cayenne.gen.ClientClassGenerationAction) ClassGenerationAction(org.apache.cayenne.gen.ClassGenerationAction) DataMap(org.apache.cayenne.map.DataMap) TaskAction(org.gradle.api.tasks.TaskAction)

Example 8 with ClassGenerationAction

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");
}
Also used : ClassGenerationAction(org.apache.cayenne.gen.ClassGenerationAction) Test(org.junit.Test)

Example 9 with ClassGenerationAction

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;
}
Also used : ClassGenerationAction(org.apache.cayenne.gen.ClassGenerationAction)

Example 10 with ClassGenerationAction

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;
}
Also used : ArrayList(java.util.ArrayList) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) LinkedList(java.util.LinkedList) DataMap(org.apache.cayenne.map.DataMap) Embeddable(org.apache.cayenne.map.Embeddable) ObjEntity(org.apache.cayenne.map.ObjEntity) GeneralPreferences(org.apache.cayenne.modeler.dialog.pref.GeneralPreferences) Preferences(java.util.prefs.Preferences) File(java.io.File) ClassGenerationAction(org.apache.cayenne.gen.ClassGenerationAction)

Aggregations

ClassGenerationAction (org.apache.cayenne.gen.ClassGenerationAction)12 ClientClassGenerationAction (org.apache.cayenne.gen.ClientClassGenerationAction)6 DataMap (org.apache.cayenne.map.DataMap)4 ToolsModule (org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)3 File (java.io.File)2 Injector (org.apache.cayenne.di.Injector)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Preferences (java.util.prefs.Preferences)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 Embeddable (org.apache.cayenne.map.Embeddable)1 ObjEntity (org.apache.cayenne.map.ObjEntity)1 GeneralPreferences (org.apache.cayenne.modeler.dialog.pref.GeneralPreferences)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 BuildException (org.apache.tools.ant.BuildException)1 GradleException (org.gradle.api.GradleException)1 InvalidUserDataException (org.gradle.api.InvalidUserDataException)1 InputFile (org.gradle.api.tasks.InputFile)1 TaskAction (org.gradle.api.tasks.TaskAction)1