use of com.serotonin.m2m2.db.DatabaseType in project ma-modules-public by infiniteautomation.
the class M2MReportImportDwr method generateJson.
@DwrPermission(admin = true)
public ProcessResult generateJson(String driverClassname, String connectionUrl, String username, String password) {
ProcessResult result = new ProcessResult();
// Validate the connection
try {
DriverManager.registerDriver((Driver) Class.forName(driverClassname).newInstance());
Connection connection = DriverManager.getConnection(connectionUrl, username, password);
connection.setAutoCommit(false);
// Test the connection.
DatabaseMetaData md = connection.getMetaData();
String productName = md.getDatabaseProductName();
DatabaseType type = DatabaseType.DERBY;
if (productName.equalsIgnoreCase("mysql")) {
type = DatabaseType.MYSQL;
} else if (productName.equalsIgnoreCase("Apache Derby")) {
type = DatabaseType.DERBY;
} else if (productName.contains("Microsoft")) {
type = DatabaseType.MSSQL;
} else if (productName.equalsIgnoreCase("h2")) {
type = DatabaseType.H2;
} else if (productName.equalsIgnoreCase("postgressql")) {
type = DatabaseType.MYSQL;
}
// Get the reports
M2MReportDao dao = new M2MReportDao(connection, type);
List<M2MReportVO> legacyReports = dao.getReports();
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
jsonWriter.setPrettyIndent(3);
jsonWriter.setPrettyOutput(true);
int cnt = 0;
jsonWriter.append("{");
jsonWriter.indent();
jsonWriter.quote("reports");
jsonWriter.append(": [");
// Convert the reports to our VOs
for (M2MReportVO legacyReport : legacyReports) {
legacyReport.jsonWrite(jsonWriter, dao);
cnt++;
if (cnt < legacyReports.size())
jsonWriter.append(",");
}
jsonWriter.append(']');
jsonWriter.append("}");
jsonWriter.flush();
result.addData("reports", stringWriter.toString());
} catch (Exception e) {
result.addContextualMessage("connectionUrl", "common.default", e.getMessage());
return result;
}
return result;
}
use of com.serotonin.m2m2.db.DatabaseType in project ma-core-public by infiniteautomation.
the class AbstractDatabaseProxy method initialize.
@PostConstruct
@Override
public void initialize() {
initializeImpl();
DataSource dataSource = getDataSource();
this.jdbcTemplate = new ExtendedJdbcTemplate(dataSource);
this.context = DSL.using(getConfig());
this.transactionManager = new DataSourceTransactionManager(dataSource);
SystemSettingsAccessor systemSettingsAccessor = () -> context;
DatabaseSchemaUpgrader upgrader = new DatabaseSchemaUpgrader(this, systemSettingsAccessor, classLoader);
// VO objects stored in blobs
try {
systemSettingsAccessor.getSystemSetting(SystemSettingsDao.LANGUAGE).ifPresent(Common::setSystemLanguage);
} catch (DataAccessException e) {
// that's ok, table probably doesn't exist yet
}
// First confirm that if we are MySQL we have JSON Support
if (getType().name().equals(DatabaseType.MYSQL.name())) {
try {
runScript(new String[] { "CREATE TABLE mangoUpgrade28 (test JSON)engine=InnoDB;", "DROP TABLE mangoUpgrade28;" }, null);
} catch (BadSqlGrammarException e) {
String version = "?";
try {
DatabaseMetaData dmd = getDataSource().getConnection().getMetaData();
version = dmd.getDatabaseProductVersion();
} catch (Exception ex) {
log.error("Failed to create test table for JSON compatibility" + ex);
}
throw new ShouldNeverHappenException("Unable to start Mango, MySQL version must be at least 5.7.8 to support JSON columns. Your version is " + version);
}
}
try {
boolean newDatabase = false;
String convertTypeStr = env.getProperty(propertyPrefix + "convert.db.type");
boolean willConvert = false;
if (!databaseExists()) {
if (!restoreTables()) {
createTables();
// Check if we should convert from another database.
if (!StringUtils.isBlank(convertTypeStr)) {
willConvert = true;
} else {
newDatabase = true;
}
}
}
if (newDatabase) {
doInTransaction(txStatus -> {
initializeCoreDatabase(context);
});
} else if (!willConvert) {
// Make sure the core schema version matches the application version. If we are running a conversion
// then the responsibility is on the User to be converting from a compatible version
upgrader.checkCoreUpgrade();
}
// Ensure the modules are installed after the core schema is updated
for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) {
try {
def.setDatabaseProxy(this);
def.newInstallationCheck();
} catch (Exception e) {
log.error("Module " + def.getModule().getName() + " new installation check failed", e);
}
}
if (!willConvert) {
// Allow modules to upgrade their schemas
for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) {
upgrader.checkModuleUpgrade(def);
}
}
if (willConvert) {
// Found a database type from which to convert.
DatabaseType convertType = DatabaseType.valueOf(convertTypeStr.toUpperCase());
// TODO check that the convert source has the current DB version, or upgrade it if not.
AbstractDatabaseProxy sourceProxy = (AbstractDatabaseProxy) factory.createDatabaseProxy(convertType, propertyPrefix + "convert.");
try {
sourceProxy.initialize();
DBConvert convert = new DBConvert();
convert.setSource(sourceProxy);
convert.setTarget(this);
try {
convert.execute();
} catch (SQLException e) {
throw new ShouldNeverHappenException(e);
}
} finally {
sourceProxy.terminate();
}
}
listeners.forEach(l -> l.onInitialize(this));
} catch (CannotGetJdbcConnectionException e) {
log.error("Unable to connect to database of type " + getType().name(), e);
throw e;
} catch (IOException e) {
log.error("Exception initializing database proxy: " + e.getMessage(), e);
throw new UncheckedIOException(e);
} catch (Exception e) {
log.error("Exception initializing database proxy: " + e.getMessage(), e);
throw e;
}
}
use of com.serotonin.m2m2.db.DatabaseType in project ma-core-public by infiniteautomation.
the class AbstractDatabaseProxy method initialize.
/* (non-Javadoc)
* @see com.serotonin.m2m2.db.DatabaseProxy#initialize(java.lang.ClassLoader)
*/
@Override
public void initialize(ClassLoader classLoader) {
initializeImpl("");
useMetrics = Common.envProps.getBoolean("db.useMetrics", false);
ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate();
ejt.setDataSource(getDataSource());
transactionManager = new DataSourceTransactionManager(getDataSource());
try {
if (newDatabaseCheck(ejt)) {
// Check if we should convert from another database.
String convertTypeStr = null;
try {
convertTypeStr = Common.envProps.getString("convert.db.type");
} catch (MissingResourceException e) {
convertTypeStr = "";
}
if (!StringUtils.isBlank(convertTypeStr)) {
// Found a database type from which to convert.
DatabaseType convertType = DatabaseType.valueOf(convertTypeStr.toUpperCase());
if (convertType == null)
throw new IllegalArgumentException("Unknown convert database type: " + convertType);
// TODO check that the convert source has the current DB version, or upgrade it if not.
AbstractDatabaseProxy sourceProxy = convertType.getImpl();
sourceProxy.initializeImpl("convert.");
DBConvert convert = new DBConvert();
convert.setSource(sourceProxy);
convert.setTarget(this);
try {
convert.execute();
} catch (SQLException e) {
throw new ShouldNeverHappenException(e);
}
sourceProxy.terminate(false);
} else {
// Record the current version.
SystemSettingsDao.instance.setValue(SystemSettingsDao.DATABASE_SCHEMA_VERSION, Integer.toString(Common.getDatabaseSchemaVersion()));
// Add the settings flag that this is a new instance. This flag is removed when an administrator
// logs in.
SystemSettingsDao.instance.setBooleanValue(SystemSettingsDao.NEW_INSTANCE, true);
/**
* Add a startup task to run after the Audit system is ready
*/
Providers.get(IMangoLifecycle.class).addStartupTask(new Runnable() {
@Override
public void run() {
// New database. Create a default user.
User user = new User();
user.setId(Common.NEW_ID);
user.setName("Administrator");
user.setUsername("admin");
user.setPassword(Common.encrypt("admin"));
user.setEmail("admin@yourMangoDomain.com");
user.setPhone("");
user.setPermissions(SuperadminPermissionDefinition.GROUP_NAME);
user.setDisabled(false);
user.setHomeUrl("/ui/administration/home");
UserDao.instance.saveUser(user);
DefaultDataPointPropertiesTemplateFactory factory = new DefaultDataPointPropertiesTemplateFactory();
factory.saveDefaultTemplates();
}
});
}
} else
// The database exists, so let's make its schema version matches the application version.
DBUpgrade.checkUpgrade();
// Check if we are using NoSQL
if (NoSQLProxyFactory.instance.getProxy() != null) {
noSQLProxy = NoSQLProxyFactory.instance.getProxy();
noSQLProxy.initialize();
}
} catch (CannotGetJdbcConnectionException e) {
log.fatal("Unable to connect to database of type " + getType().name(), e);
throw e;
} catch (Exception e) {
log.fatal("Exception initializing database proxy: " + e.getMessage(), e);
throw e;
}
// Allow modules to upgrade themselves
for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) DBUpgrade.checkUpgrade(def, classLoader);
postInitialize(ejt);
}
use of com.serotonin.m2m2.db.DatabaseType in project ma-modules-public by infiniteautomation.
the class M2MReportImportDwr method migrate.
@DwrPermission(admin = true)
public ProcessResult migrate(String driverClassname, String connectionUrl, String username, String password) {
ProcessResult result = new ProcessResult();
// Validate the connection
try {
DriverManager.registerDriver((Driver) Class.forName(driverClassname).newInstance());
Connection connection = DriverManager.getConnection(connectionUrl, username, password);
connection.setAutoCommit(false);
// Test the connection.
DatabaseMetaData md = connection.getMetaData();
String productName = md.getDatabaseProductName();
DatabaseType type = DatabaseType.DERBY;
if (productName.equalsIgnoreCase("mysql")) {
type = DatabaseType.MYSQL;
} else if (productName.equalsIgnoreCase("Apache Derby")) {
type = DatabaseType.DERBY;
} else if (productName.contains("Microsoft")) {
type = DatabaseType.MSSQL;
} else if (productName.equalsIgnoreCase("h2")) {
type = DatabaseType.H2;
} else if (productName.equalsIgnoreCase("postgressql")) {
type = DatabaseType.MYSQL;
}
// Get the reports
M2MReportDao dao = new M2MReportDao(connection, type);
List<M2MReportVO> legacyReports = dao.getReports();
// Convert the reports to our VOs
List<ReportVO> reports = new ArrayList<ReportVO>();
for (M2MReportVO legacyReport : legacyReports) {
try {
ReportVO report = legacyReport.convert(dao);
report.validate(result);
reports.add(report);
} catch (Exception e) {
result.addGenericMessage("common.default", e.getMessage());
}
}
if (!result.getHasMessages()) {
ReportDao reportDao = ReportDao.instance;
for (ReportVO vo : reports) {
vo.validate(result);
reportDao.saveReport(vo);
}
}
result.addData("reports", reports);
} catch (Exception e) {
result.addContextualMessage("connectionUrl", "common.default", e.getMessage());
return result;
}
return result;
}
Aggregations