Search in sources :

Example 81 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class SQLTemplateIT method testSQLTemplateForDataMap.

@Test
public void testSQLTemplateForDataMap() {
    DataMap testDataMap = context.getEntityResolver().getDataMap("testmap");
    SQLTemplate q1 = new SQLTemplate(testDataMap, "SELECT * FROM ARTIST", true);
    List<DataRow> result = context.performQuery(q1);
    assertEquals(0, result.size());
}
Also used : DataRow(org.apache.cayenne.DataRow) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 82 with DataMap

use of org.apache.cayenne.map.DataMap 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 83 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class DbGeneratorTask method execute.

@Override
public void execute() {
    Logger logger = new AntLogger(this);
    log(String.format("connection settings - [driver: %s, url: %s, username: %s]", driver, url, userName), Project.MSG_VERBOSE);
    log(String.format("generator options - [dropTables: %s, dropPK: %s, createTables: %s, createPK: %s, createFK: %s]", dropTables, dropPK, createTables, createPK, createFK), Project.MSG_VERBOSE);
    validateAttributes();
    ClassLoader loader = null;
    Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger));
    try {
        loader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(DbGeneratorTask.class.getClassLoader());
        // Load the data map and run the db generator.
        DataMap dataMap = loadDataMap(injector);
        // load driver taking custom CLASSPATH into account...
        DriverDataSource dataSource = new DriverDataSource((Driver) Class.forName(driver).newInstance(), url, userName, password);
        DbAdapter adapter = getAdapter(injector, dataSource);
        DbGenerator generator = new DbGenerator(adapter, dataMap, Collections.<DbEntity>emptyList(), null, NoopJdbcEventLogger.getInstance());
        generator.setShouldCreateFKConstraints(createFK);
        generator.setShouldCreatePKSupport(createPK);
        generator.setShouldCreateTables(createTables);
        generator.setShouldDropPKSupport(dropPK);
        generator.setShouldDropTables(dropTables);
        generator.runGenerator(dataSource);
    } catch (Exception ex) {
        Throwable th = Util.unwindException(ex);
        String message = "Error generating database";
        if (th.getLocalizedMessage() != null) {
            message += ": " + th.getLocalizedMessage();
        }
        log(message, Project.MSG_ERR);
        throw new BuildException(message, th);
    } finally {
        Thread.currentThread().setContextClassLoader(loader);
        injector.shutdown();
    }
}
Also used : DriverDataSource(org.apache.cayenne.datasource.DriverDataSource) Logger(org.slf4j.Logger) NoopJdbcEventLogger(org.apache.cayenne.log.NoopJdbcEventLogger) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) BuildException(org.apache.tools.ant.BuildException) DataMap(org.apache.cayenne.map.DataMap) DbAdapter(org.apache.cayenne.dba.DbAdapter) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) BuildException(org.apache.tools.ant.BuildException) DbGenerator(org.apache.cayenne.access.DbGenerator)

Example 84 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class DataChannelDescriptor method encodeAsXML.

@Override
public void encodeAsXML(XMLEncoder encoder, ConfigurationNodeVisitor delegate) {
    encoder.start("domain").attribute("xmlns", SCHEMA_XSD).attribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance", true).attribute("xsi:schemaLocation", SCHEMA_XSD + " " + SCHEMA_XSD + ".xsd", true).projectVersion();
    if (!properties.isEmpty()) {
        List<String> keys = new ArrayList<>(properties.keySet());
        Collections.sort(keys);
        for (String key : keys) {
            encoder.property(key, properties.get(key));
        }
    }
    if (!dataMaps.isEmpty()) {
        List<DataMap> maps = new ArrayList<>(this.dataMaps);
        Collections.sort(maps);
        for (DataMap dataMap : maps) {
            encoder.start("map").attribute("name", dataMap.getName().trim()).end();
        }
    }
    if (!nodeDescriptors.isEmpty()) {
        List<DataNodeDescriptor> nodes = new ArrayList<>(nodeDescriptors);
        Collections.sort(nodes);
        encoder.nested(nodes, delegate);
    }
    delegate.visitDataChannelDescriptor(this);
    encoder.end();
}
Also used : ArrayList(java.util.ArrayList) DataMap(org.apache.cayenne.map.DataMap)

Example 85 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class DefaultDataChannelDescriptorMerger method merge.

public DataChannelDescriptor merge(DataChannelDescriptor... descriptors) {
    if (descriptors == null || descriptors.length == 0) {
        throw new IllegalArgumentException("Null or empty descriptors");
    }
    if (descriptors.length == 1) {
        return descriptors[0];
    }
    int len = descriptors.length;
    // merge into a new descriptor; do not alter source descriptors
    DataChannelDescriptor merged = new DataChannelDescriptor();
    merged.setName(descriptors[len - 1].getName());
    merged.getProperties().putAll(descriptors[len - 1].getProperties());
    // iterate in reverse order to reduce add/remove operations
    for (int i = len - 1; i >= 0; i--) {
        DataChannelDescriptor descriptor = descriptors[i];
        // TODO: they still have a link to the unmerged descriptor, is it bad?
        for (DataMap map : descriptor.getDataMaps()) {
            // report conflicting DataMap and leave the existing copy
            DataMap existing = merged.getDataMap(map.getName());
            if (existing != null) {
                logger.info("Discarding overridden DataMap '" + map.getName() + "' from descriptor '" + descriptor.getName() + "'");
            } else {
                logger.info("Using DataMap '" + map.getName() + "' from descriptor '" + descriptor.getName() + "' in merged descriptor");
                merged.getDataMaps().add(map);
            }
        }
        // DataNodes are merged by copy as we may modify them (changing map linking)
        for (DataNodeDescriptor node : descriptor.getNodeDescriptors()) {
            DataNodeDescriptor existing = merged.getNodeDescriptor(node.getName());
            if (existing != null) {
                logger.info("Discarding overridden DataNode '" + node.getName() + "' from descriptor '" + descriptor.getName() + "'");
                for (String mapName : node.getDataMapNames()) {
                    if (!existing.getDataMapNames().contains(mapName)) {
                        existing.getDataMapNames().add(mapName);
                    }
                }
            } else {
                logger.info("Using DataNode '" + node.getName() + "' from descriptor '" + descriptor.getName() + "' in merged descriptor");
                merged.getNodeDescriptors().add(cloneDataNodeDescriptor(node, merged));
            }
        }
    }
    return merged;
}
Also used : DataMap(org.apache.cayenne.map.DataMap)

Aggregations

DataMap (org.apache.cayenne.map.DataMap)233 Test (org.junit.Test)88 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)48 DbEntity (org.apache.cayenne.map.DbEntity)48 ObjEntity (org.apache.cayenne.map.ObjEntity)45 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)22 URL (java.net.URL)21 URLResource (org.apache.cayenne.resource.URLResource)20 ArrayList (java.util.ArrayList)19 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)19 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)17 QueryDescriptor (org.apache.cayenne.map.QueryDescriptor)16 DataMapEvent (org.apache.cayenne.configuration.event.DataMapEvent)15 File (java.io.File)14 Procedure (org.apache.cayenne.map.Procedure)14 Embeddable (org.apache.cayenne.map.Embeddable)13 Injector (org.apache.cayenne.di.Injector)12 DbAttribute (org.apache.cayenne.map.DbAttribute)11 EntityResolver (org.apache.cayenne.map.EntityResolver)11 Entity (org.apache.cayenne.map.Entity)10