Search in sources :

Example 1 with SchemaAwareStoreManager

use of org.datanucleus.store.schema.SchemaAwareStoreManager in project datanucleus-core by datanucleus.

the class SchemaTool method main.

/**
 * Entry method when invoked from the command line.
 * @param args List of options for processing by the available methods in this class.
 * @throws Exception If an error occurs in operation
 */
public static void main(String[] args) throws Exception {
    SchemaTool tool = new SchemaTool();
    CommandLine cmd = new CommandLine();
    cmd.addOption(OPTION_CREATE_DATABASE, OPTION_CREATE_DATABASE, null, Localiser.msg("014024"));
    cmd.addOption(OPTION_DELETE_DATABASE, OPTION_DELETE_DATABASE, null, Localiser.msg("014025"));
    cmd.addOption(OPTION_CREATE_TABLES_FOR_CLASSES, OPTION_CREATE_TABLES_FOR_CLASSES, null, Localiser.msg("014026"));
    cmd.addOption(OPTION_DELETE_TABLES_FOR_CLASSES, OPTION_DELETE_TABLES_FOR_CLASSES, null, Localiser.msg("014027"));
    cmd.addOption(OPTION_DELETE_CREATE_TABLES_FOR_CLASSES, OPTION_DELETE_CREATE_TABLES_FOR_CLASSES, null, Localiser.msg("014044"));
    cmd.addOption(OPTION_VALIDATE_TABLES_FOR_CLASSES, OPTION_VALIDATE_TABLES_FOR_CLASSES, null, Localiser.msg("014028"));
    cmd.addOption(OPTION_DBINFO, OPTION_DBINFO, null, Localiser.msg("014029"));
    cmd.addOption(OPTION_SCHEMAINFO, OPTION_SCHEMAINFO, null, Localiser.msg("014030"));
    cmd.addOption("help", "help", null, Localiser.msg("014033"));
    cmd.addOption(OPTION_DDL_FILE, OPTION_DDL_FILE, "ddlFile", Localiser.msg("014031"));
    cmd.addOption(OPTION_COMPLETE_DDL, OPTION_COMPLETE_DDL, null, Localiser.msg("014032"));
    cmd.addOption(OPTION_INCLUDE_AUTO_START, OPTION_INCLUDE_AUTO_START, null, "Include Auto-Start Mechanisms");
    cmd.addOption(OPTION_API, OPTION_API, "api", "API Adapter (JDO, JPA, etc)");
    cmd.addOption(OPTION_CATALOG_NAME, OPTION_CATALOG_NAME, "catalog", "CatalogName");
    cmd.addOption(OPTION_SCHEMA_NAME, OPTION_SCHEMA_NAME, "schema", "SchemaName");
    cmd.addOption("v", "verbose", null, "verbose output");
    cmd.addOption("pu", "persistenceUnit", "<persistence-unit>", "name of the persistence unit to handle the schema for");
    cmd.addOption("props", "properties", "props", "path to a properties file");
    cmd.addOption("ignoreMetaDataForMissingClasses", "ignoreMetaDataForMissingClasses", null, "Ignore metadata for classes that are missing?");
    cmd.parse(args);
    // Remaining command line args are filenames (class files, metadata files)
    String[] filenames = cmd.getDefaultArgs();
    if (cmd.hasOption("api")) {
        tool.setApi(cmd.getOptionArg("api"));
    }
    if (cmd.hasOption(OPTION_CATALOG_NAME)) {
        tool.setCatalogName(cmd.getOptionArg(OPTION_CATALOG_NAME));
    }
    if (cmd.hasOption(OPTION_SCHEMA_NAME)) {
        NucleusLogger.GENERAL.info(">> sch input = " + cmd.getOptionArg(OPTION_SCHEMA_NAME));
        tool.setSchemaName(cmd.getOptionArg(OPTION_SCHEMA_NAME));
    }
    // Determine the mode of operation required
    String msg = null;
    Mode mode = Mode.CREATE;
    if (cmd.hasOption(OPTION_CREATE_TABLES_FOR_CLASSES)) {
        mode = Mode.CREATE;
        msg = Localiser.msg("014000");
    } else if (cmd.hasOption(OPTION_DELETE_TABLES_FOR_CLASSES)) {
        mode = Mode.DELETE;
        msg = Localiser.msg("014001");
    } else if (cmd.hasOption(OPTION_DELETE_CREATE_TABLES_FOR_CLASSES)) {
        mode = Mode.DELETE_CREATE;
        msg = Localiser.msg("014045");
    } else if (cmd.hasOption(OPTION_VALIDATE_TABLES_FOR_CLASSES)) {
        mode = Mode.VALIDATE;
        msg = Localiser.msg("014002");
    } else if (cmd.hasOption(OPTION_CREATE_DATABASE)) {
        mode = Mode.CREATE_DATABASE;
        msg = Localiser.msg("014034", tool.getCatalogName(), tool.getSchemaName());
    } else if (cmd.hasOption(OPTION_DELETE_DATABASE)) {
        mode = Mode.DELETE_DATABASE;
        msg = Localiser.msg("014035", tool.getCatalogName(), tool.getSchemaName());
    } else if (cmd.hasOption(OPTION_DBINFO)) {
        mode = Mode.DATABASE_INFO;
        msg = Localiser.msg("014003");
    } else if (cmd.hasOption(OPTION_SCHEMAINFO)) {
        mode = Mode.SCHEMA_INFO;
        msg = Localiser.msg("014004");
    } else if (cmd.hasOption("help")) {
        System.out.println(Localiser.msg("014023", cmd.toString()));
        System.exit(0);
    }
    LOGGER.info(msg);
    System.out.println(msg);
    // Extract the selected options
    String propsFileName = null;
    String persistenceUnitName = null;
    if (cmd.hasOption(OPTION_DDL_FILE)) {
        tool.setDdlFile(cmd.getOptionArg(OPTION_DDL_FILE));
    }
    if (cmd.hasOption(OPTION_COMPLETE_DDL)) {
        tool.setCompleteDdl(true);
    }
    if (cmd.hasOption(OPTION_INCLUDE_AUTO_START)) {
        tool.setIncludeAutoStart(true);
    }
    if (cmd.hasOption("v")) {
        tool.setVerbose(true);
    }
    boolean ignoreMetaDataForMissingClasses = false;
    if (cmd.hasOption("ignoreMetaDataForMissingClasses")) {
        ignoreMetaDataForMissingClasses = true;
    }
    if (cmd.hasOption("pu")) {
        persistenceUnitName = cmd.getOptionArg("pu");
    }
    if (cmd.hasOption("props")) {
        propsFileName = cmd.getOptionArg("props");
    }
    // Classpath
    msg = Localiser.msg("014005");
    LOGGER.info(msg);
    if (tool.isVerbose()) {
        System.out.println(msg);
    }
    StringTokenizer tokeniser = new StringTokenizer(System.getProperty("java.class.path"), File.pathSeparator);
    while (tokeniser.hasMoreTokens()) {
        msg = Localiser.msg("014006", tokeniser.nextToken());
        LOGGER.info(msg);
        if (tool.isVerbose()) {
            System.out.println(msg);
        }
    }
    if (tool.isVerbose()) {
        System.out.println();
    }
    // DDL file
    String ddlFilename = tool.getDdlFile();
    if (ddlFilename != null) {
        msg = Localiser.msg(tool.getCompleteDdl() ? "014018" : "014019", ddlFilename);
        LOGGER.info(msg);
        if (tool.isVerbose()) {
            System.out.println(msg);
            System.out.println();
        }
    }
    // Create a NucleusContext for use with this mode
    StoreNucleusContext nucleusCtx = null;
    try {
        Properties props = (propsFileName != null) ? PersistenceUtils.setPropertiesUsingFile(propsFileName) : null;
        nucleusCtx = getNucleusContextForMode(mode, tool.getApi(), props, persistenceUnitName, ddlFilename, tool.isVerbose(), ignoreMetaDataForMissingClasses);
    } catch (Exception e) {
        // Unable to create a NucleusContext so likely input errors
        LOGGER.error("Error creating NucleusContext", e);
        System.out.println(Localiser.msg("014008", e.getMessage()));
        System.exit(1);
        return;
    }
    Set<String> classNames = null;
    if (mode != Mode.SCHEMA_INFO && mode != Mode.DATABASE_INFO) {
        // This will load up all MetaData for the specified input and throw exceptions where errors are found
        try {
            MetaDataManager metaDataMgr = nucleusCtx.getMetaDataManager();
            ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
            if (filenames == null && persistenceUnitName == null) {
                msg = Localiser.msg("014007");
                LOGGER.error(msg);
                System.out.println(msg);
                throw new NucleusUserException(msg);
            }
            FileMetaData[] filemds = null;
            if (persistenceUnitName != null) {
                // Schema management via "persistence-unit"
                msg = Localiser.msg("014015", persistenceUnitName);
                LOGGER.info(msg);
                if (tool.isVerbose()) {
                    System.out.println(msg);
                    System.out.println();
                }
                // The NucleusContext will have initialised the MetaDataManager with the persistence-unit
                filemds = metaDataMgr.getFileMetaData();
            } else {
                // Schema management via "Input Files" (metadata/class)
                msg = Localiser.msg("014009");
                LOGGER.info(msg);
                if (tool.isVerbose()) {
                    System.out.println(msg);
                }
                for (int i = 0; i < filenames.length; i++) {
                    String entry = Localiser.msg("014010", filenames[i]);
                    LOGGER.info(entry);
                    if (tool.isVerbose()) {
                        System.out.println(entry);
                    }
                }
                if (tool.isVerbose()) {
                    System.out.println();
                }
                LOGGER.debug(Localiser.msg("014011", "" + filenames.length));
                filemds = MetaDataUtils.getFileMetaDataForInputFiles(metaDataMgr, clr, filenames);
                LOGGER.debug(Localiser.msg("014012", "" + filenames.length));
            }
            classNames = new TreeSet<String>();
            if (filemds == null) {
                msg = Localiser.msg("014021");
                LOGGER.error(msg);
                System.out.println(msg);
                System.exit(2);
                return;
            }
            for (int i = 0; i < filemds.length; i++) {
                for (int j = 0; j < filemds[i].getNoOfPackages(); j++) {
                    for (int k = 0; k < filemds[i].getPackage(j).getNoOfClasses(); k++) {
                        String className = filemds[i].getPackage(j).getClass(k).getFullClassName();
                        if (!classNames.contains(className)) {
                            classNames.add(className);
                        }
                    }
                }
            }
        } catch (Exception e) {
            // Exception will have been logged and sent to System.out in "getFileMetaDataForInput()"
            System.exit(2);
            return;
        }
    }
    // Run SchemaTool
    StoreManager storeMgr = nucleusCtx.getStoreManager();
    if (!(storeMgr instanceof SchemaAwareStoreManager)) {
        LOGGER.error("StoreManager of type " + storeMgr.getClass().getName() + " is not schema-aware so cannot be used with SchemaTool");
        System.exit(2);
        return;
    }
    SchemaAwareStoreManager schemaStoreMgr = (SchemaAwareStoreManager) storeMgr;
    try {
        if (mode == Mode.CREATE_DATABASE) {
            tool.createDatabase(schemaStoreMgr, tool.getCatalogName(), tool.getSchemaName());
        } else if (mode == Mode.DELETE_DATABASE) {
            tool.deleteDatabase(schemaStoreMgr, tool.getCatalogName(), tool.getSchemaName());
        } else if (mode == Mode.CREATE) {
            tool.createSchemaForClasses(schemaStoreMgr, classNames);
        } else if (mode == Mode.DELETE) {
            tool.deleteSchemaForClasses(schemaStoreMgr, classNames);
        } else if (mode == Mode.DELETE_CREATE) {
            tool.deleteSchemaForClasses(schemaStoreMgr, classNames);
            tool.createSchemaForClasses(schemaStoreMgr, classNames);
        } else if (mode == Mode.VALIDATE) {
            tool.validateSchemaForClasses(schemaStoreMgr, classNames);
        } else if (mode == Mode.DATABASE_INFO) {
            storeMgr.printInformation("DATASTORE", System.out);
        } else if (mode == Mode.SCHEMA_INFO) {
            storeMgr.printInformation("SCHEMA", System.out);
        }
        msg = Localiser.msg("014043");
        LOGGER.info(msg);
        System.out.println(msg);
    } catch (Exception e) {
        msg = Localiser.msg("014037", e.getMessage());
        System.out.println(msg);
        LOGGER.error(msg, e);
        System.exit(2);
        return;
    } finally {
        storeMgr.close();
    }
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) Properties(java.util.Properties) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NucleusException(org.datanucleus.exceptions.NucleusException) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager) StoreManager(org.datanucleus.store.StoreManager) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager) CommandLine(org.datanucleus.util.CommandLine) StringTokenizer(java.util.StringTokenizer) StoreNucleusContext(org.datanucleus.StoreNucleusContext) FileMetaData(org.datanucleus.metadata.FileMetaData)

Example 2 with SchemaAwareStoreManager

use of org.datanucleus.store.schema.SchemaAwareStoreManager in project motech by motech.

the class SchemaGenerator method generateSchema.

public void generateSchema() throws IOException {
    LOGGER.debug("Starting to generate entity schema.");
    Set<String> classNames = classNames();
    if (!classNames.isEmpty()) {
        SchemaAwareStoreManager storeManager = getStoreManager();
        storeManager.createSchemaForClasses(classNames, new Properties());
    }
    LOGGER.info("Entity schema generation completed.");
}
Also used : Properties(java.util.Properties) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager)

Example 3 with SchemaAwareStoreManager

use of org.datanucleus.store.schema.SchemaAwareStoreManager in project tests by datanucleus.

the class DynamicEnhanceSchemaToolTest method schemaCreate.

public void schemaCreate(DynamicEnhanceSchemaToolClassLoader runtimeCL) throws Exception {
    Map props = getPropertiesForDatastore(runtimeCL);
    JDOPersistenceManagerFactory pmf = (JDOPersistenceManagerFactory) JDOHelper.getPersistenceManagerFactory(props);
    try {
        JDOMetadata filemd = pmf.newMetadata();
        createMetadata(filemd);
        pmf.registerMetadata(filemd);
        Set<String> classNames = new HashSet<>();
        classNames.add("test.Client");
        PersistenceNucleusContext nucCtx = pmf.getNucleusContext();
        StoreManager storeMgr = nucCtx.getStoreManager();
        if (!(storeMgr instanceof SchemaAwareStoreManager)) {
            // Can't create schema with this datastore
            return;
        }
        try {
            SchemaTool schematool = new SchemaTool();
            schematool.setDdlFile("target/schema.ddl");
            schematool.setCompleteDdl(true);
            SchemaAwareStoreManager schemaStoreMgr = (SchemaAwareStoreManager) nucCtx.getStoreManager();
            schematool.createSchemaForClasses(schemaStoreMgr, classNames);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } finally {
        pmf.close();
    }
}
Also used : JDOMetadata(javax.jdo.metadata.JDOMetadata) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) SchemaTool(org.datanucleus.store.schema.SchemaTool) HashMap(java.util.HashMap) Map(java.util.Map) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager) HashSet(java.util.HashSet) StoreManager(org.datanucleus.store.StoreManager) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager)

Example 4 with SchemaAwareStoreManager

use of org.datanucleus.store.schema.SchemaAwareStoreManager in project motech by motech.

the class SchemaGeneratorTest method shouldGenerateSchema.

@Test
public void shouldGenerateSchema() throws IOException {
    when(pmf.getNucleusContext()).thenReturn(nucleusContext);
    when(nucleusContext.getStoreManager()).thenReturn(storeManager);
    schemaGenerator.generateSchema();
    ArgumentCaptor<Set> captor = ArgumentCaptor.forClass(Set.class);
    verify((SchemaAwareStoreManager) storeManager).createSchemaForClasses(captor.capture(), eq(new Properties()));
    Set set = captor.getValue();
    assertNotNull(set);
    assertEquals(8, set.size());
    assertTrue(set.contains("org.motechproject.test.EntityClass"));
    assertTrue(set.contains("org.motechproject.test.AnotherClass"));
    assertTrue(set.contains("org.motechproject.test.EntityWithoutHistoryClass"));
    assertTrue(set.contains(ClassName.getHistoryClassName("org.motechproject.test.EntityClass")));
    assertTrue(set.contains(ClassName.getHistoryClassName("org.motechproject.test.AnotherClass")));
    assertFalse(set.contains(ClassName.getHistoryClassName("org.motechproject.test.EntityWithoutHistoryClass")));
    assertTrue(set.contains(ClassName.getTrashClassName("org.motechproject.test.EntityClass")));
    assertTrue(set.contains(ClassName.getTrashClassName("org.motechproject.test.AnotherClass")));
    assertTrue(set.contains(ClassName.getTrashClassName("org.motechproject.test.EntityWithoutHistoryClass")));
}
Also used : Set(java.util.Set) Properties(java.util.Properties) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager) Test(org.junit.Test)

Example 5 with SchemaAwareStoreManager

use of org.datanucleus.store.schema.SchemaAwareStoreManager in project datanucleus-core by datanucleus.

the class PersistenceNucleusContextImpl method initialiseSchema.

/**
 * Method to handle generation (create, drop, drop+create) of a schema at initialisation.
 * Will generate the schema for all classes that have had their metadata loaded at this point, which
 * typically means the persistence-unit.
 * @param generateModeStr Generate "mode"
 * @param generateScripts Whether to generate
 */
protected void initialiseSchema(String generateModeStr, boolean generateScripts) {
    Mode mode = null;
    if (generateModeStr.equalsIgnoreCase("create")) {
        mode = Mode.CREATE;
    } else if (generateModeStr.equalsIgnoreCase("drop")) {
        mode = Mode.DELETE;
    } else if (generateModeStr.equalsIgnoreCase("drop-and-create")) {
        mode = Mode.DELETE_CREATE;
    }
    if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
        if (mode == Mode.CREATE) {
            NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014000"));
        } else if (mode == Mode.DELETE) {
            NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014001"));
        } else if (mode == Mode.DELETE_CREATE) {
            NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014045"));
        }
    }
    // Extract the classes that have metadata loaded (e.g persistence-unit)
    Set<String> schemaClassNames = null;
    MetaDataManager metaDataMgr = getMetaDataManager();
    FileMetaData[] filemds = metaDataMgr.getFileMetaData();
    schemaClassNames = new TreeSet<String>();
    if (filemds == null) {
        throw new NucleusUserException("No classes to process in generateSchema");
    }
    for (int i = 0; i < filemds.length; i++) {
        for (int j = 0; j < filemds[i].getNoOfPackages(); j++) {
            for (int k = 0; k < filemds[i].getPackage(j).getNoOfClasses(); k++) {
                String className = filemds[i].getPackage(j).getClass(k).getFullClassName();
                if (!schemaClassNames.contains(className)) {
                    schemaClassNames.add(className);
                }
            }
        }
    }
    StoreManager storeMgr = getStoreManager();
    if (storeMgr instanceof SchemaAwareStoreManager) {
        SchemaAwareStoreManager schemaStoreMgr = (SchemaAwareStoreManager) storeMgr;
        SchemaTool schemaTool = new SchemaTool();
        if (mode == Mode.CREATE) {
            if (generateScripts) {
                // Generate the required script
                schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_TARGET));
                schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
            } else {
                // Process the required metadata/script
                String createOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_CREATE_ORDER);
                String createScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_SOURCE);
                if (StringUtils.isWhitespace(createScript)) {
                    createScript = null;
                }
                if (StringUtils.isWhitespace(createOrder)) {
                    createOrder = (createScript != null) ? "script" : "metadata";
                } else {
                    if (createOrder.equals("script") || createOrder.equals("metadata-then-script") || createOrder.equals("script-the-metadata")) {
                        if (createScript == null) {
                            NucleusLogger.DATASTORE_SCHEMA.warn("create order set to " + createOrder + " but no script defined, so using metadata instead");
                            createOrder = "metadata";
                        }
                    }
                }
                if (createOrder.equals("script")) {
                    processDatastoreScript(createScript);
                } else if (createOrder.equals("script-then-metadata")) {
                    processDatastoreScript(createScript);
                    schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
                } else if (createOrder.equals("metadata-then-script")) {
                    schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
                    processDatastoreScript(createScript);
                } else {
                    schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
                }
            }
        } else if (mode == Mode.DELETE) {
            if (generateScripts) {
                // Generate the required script
                schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_TARGET));
                schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
            } else {
                // Process the required metadata/script
                String dropOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_DROP_ORDER);
                String dropScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_SOURCE);
                if (StringUtils.isWhitespace(dropScript)) {
                    dropScript = null;
                }
                if (StringUtils.isWhitespace(dropOrder)) {
                    dropOrder = (dropScript != null) ? "script" : "metadata";
                } else {
                    if (dropOrder.equals("script") || dropOrder.equals("metadata-then-script") || dropOrder.equals("script-the-metadata")) {
                        if (dropScript == null) {
                            NucleusLogger.DATASTORE_SCHEMA.warn("drop order set to " + dropOrder + " but no script defined, so using metadata instead");
                            dropOrder = "metadata";
                        }
                    }
                }
                if (dropOrder.equals("script")) {
                    processDatastoreScript(dropScript);
                } else if (dropOrder.equals("script-then-metadata")) {
                    processDatastoreScript(dropScript);
                    schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                } else if (dropOrder.equals("metadata-then-script")) {
                    schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                    processDatastoreScript(dropScript);
                } else {
                    schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                }
            }
        } else if (mode == Mode.DELETE_CREATE) {
            if (generateScripts) {
                // Generate the required scripts
                schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_TARGET));
                schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_TARGET));
                schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
            } else {
                // Process the required metadata/scripts
                String dropOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_DROP_ORDER);
                String dropScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_SOURCE);
                if (StringUtils.isWhitespace(dropScript)) {
                    dropScript = null;
                }
                if (StringUtils.isWhitespace(dropOrder)) {
                    dropOrder = (dropScript != null) ? "script" : "metadata";
                } else {
                    if (dropOrder.equals("script") || dropOrder.equals("metadata-then-script") || dropOrder.equals("script-the-metadata")) {
                        if (dropScript == null) {
                            NucleusLogger.DATASTORE_SCHEMA.warn("drop order set to " + dropOrder + " but no script defined, so using metadata instead");
                            dropOrder = "metadata";
                        }
                    }
                }
                if (dropOrder.equals("script")) {
                    processDatastoreScript(dropScript);
                } else if (dropOrder.equals("script-then-metadata")) {
                    processDatastoreScript(dropScript);
                    schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                } else if (dropOrder.equals("metadata-then-script")) {
                    schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                    processDatastoreScript(dropScript);
                } else {
                    schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                }
                String createOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_CREATE_ORDER);
                String createScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_SOURCE);
                if (StringUtils.isWhitespace(createScript)) {
                    createScript = null;
                }
                if (StringUtils.isWhitespace(createOrder)) {
                    createOrder = (createScript != null) ? "script" : "metadata";
                } else {
                    if (createOrder.equals("script") || createOrder.equals("metadata-then-script") || createOrder.equals("script-the-metadata")) {
                        if (createScript == null) {
                            NucleusLogger.DATASTORE_SCHEMA.warn("create order set to " + createOrder + " but no script defined, so using metadata instead");
                            createOrder = "metadata";
                        }
                    }
                }
                if (createOrder.equals("script")) {
                    processDatastoreScript(createScript);
                } else if (createOrder.equals("script-then-metadata")) {
                    processDatastoreScript(createScript);
                    schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
                } else if (createOrder.equals("metadata-then-script")) {
                    schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
                    processDatastoreScript(createScript);
                } else {
                    schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
                }
            }
        }
        String loadScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_LOAD_SOURCE);
        if (!StringUtils.isWhitespace(loadScript)) {
            String scriptContent = getDatastoreScriptForResourceName(loadScript);
            if (storeMgr instanceof SchemaScriptAwareStoreManager && !StringUtils.isWhitespace(scriptContent)) {
                ((SchemaScriptAwareStoreManager) storeMgr).executeScript(scriptContent);
            }
        }
    } else {
        if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
            NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("008016", StringUtils.toJVMIDString(storeMgr)));
        }
    }
    if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
        NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014043"));
    }
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Mode(org.datanucleus.store.schema.SchemaTool.Mode) MetaDataManager(org.datanucleus.metadata.MetaDataManager) SchemaTool(org.datanucleus.store.schema.SchemaTool) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager) FederatedStoreManager(org.datanucleus.store.federation.FederatedStoreManager) StoreManager(org.datanucleus.store.StoreManager) SchemaScriptAwareStoreManager(org.datanucleus.store.schema.SchemaScriptAwareStoreManager) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager) SchemaScriptAwareStoreManager(org.datanucleus.store.schema.SchemaScriptAwareStoreManager) FileMetaData(org.datanucleus.metadata.FileMetaData)

Aggregations

SchemaAwareStoreManager (org.datanucleus.store.schema.SchemaAwareStoreManager)5 Properties (java.util.Properties)3 StoreManager (org.datanucleus.store.StoreManager)3 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 FileMetaData (org.datanucleus.metadata.FileMetaData)2 MetaDataManager (org.datanucleus.metadata.MetaDataManager)2 SchemaTool (org.datanucleus.store.schema.SchemaTool)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 StringTokenizer (java.util.StringTokenizer)1 JDOMetadata (javax.jdo.metadata.JDOMetadata)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 PersistenceNucleusContext (org.datanucleus.PersistenceNucleusContext)1 StoreNucleusContext (org.datanucleus.StoreNucleusContext)1 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 FederatedStoreManager (org.datanucleus.store.federation.FederatedStoreManager)1 SchemaScriptAwareStoreManager (org.datanucleus.store.schema.SchemaScriptAwareStoreManager)1