use of com.serotonin.m2m2.db.DatabaseProxy.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.DatabaseProxy.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;
}
use of com.serotonin.m2m2.db.DatabaseProxy.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);
}
Aggregations