Skip to content

Commit

Permalink
moved listeners registration code to static block
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed Nov 27, 2020
1 parent 76b5389 commit 605b3eb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
31 changes: 14 additions & 17 deletions src/main/java/com/erudika/para/persistence/H2DAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package com.erudika.para.persistence;

import com.erudika.para.AppCreatedListener;
import com.erudika.para.AppDeletedListener;
import com.erudika.para.core.App;
import com.erudika.para.core.ParaObject;
import com.erudika.para.utils.Config;
Expand All @@ -41,29 +39,28 @@ public class H2DAO implements DAO {

private static final Logger logger = LoggerFactory.getLogger(H2DAO.class);

/**
* Default constructor.
*/
public H2DAO() {
if (getClass().getSimpleName().equals(Config.getConfigParam("dao", ""))) {
static {
if (H2DAO.class.getSimpleName().equals(Config.getConfigParam("dao", ""))) {
// set up automatic table creation and deletion
App.addAppCreatedListener(new AppCreatedListener() {
public void onAppCreated(App app) {
if (app != null) {
H2Utils.createTable(app.getAppIdentifier());
}
App.addAppCreatedListener((App app) -> {
if (app != null) {
H2Utils.createTable(app.getAppIdentifier());
}
});
App.addAppDeletedListener(new AppDeletedListener() {
public void onAppDeleted(App app) {
if (app != null) {
H2Utils.deleteTable(app.getAppIdentifier());
}
App.addAppDeletedListener((App app) -> {
if (app != null) {
H2Utils.deleteTable(app.getAppIdentifier());
}
});
}
}

/**
* Default constructor.
*/
public H2DAO() {
}

@Override
public <P extends ParaObject> String create(String appid, P object) {
if (object == null) {
Expand Down
31 changes: 14 additions & 17 deletions src/main/java/com/erudika/para/persistence/SqlDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package com.erudika.para.persistence;

import com.erudika.para.AppCreatedListener;
import com.erudika.para.AppDeletedListener;
import com.erudika.para.core.App;
import com.erudika.para.core.ParaObject;
import com.erudika.para.utils.Config;
Expand All @@ -42,29 +40,28 @@ public class SqlDAO implements DAO {

private static final Logger logger = LoggerFactory.getLogger(SqlDAO.class);

/**
* Default constructor.
*/
public SqlDAO() {
if (getClass().getSimpleName().equals(Config.getConfigParam("dao", ""))) {
static {
if (SqlDAO.class.getSimpleName().equals(Config.getConfigParam("dao", ""))) {
// set up automatic table creation and deletion
App.addAppCreatedListener(new AppCreatedListener() {
public void onAppCreated(App app) {
if (app != null) {
SqlUtils.createTable(app.getAppIdentifier());
}
App.addAppCreatedListener((App app) -> {
if (app != null) {
SqlUtils.createTable(app.getAppIdentifier());
}
});
App.addAppDeletedListener(new AppDeletedListener() {
public void onAppDeleted(App app) {
if (app != null) {
SqlUtils.deleteTable(app.getAppIdentifier());
}
App.addAppDeletedListener((App app) -> {
if (app != null) {
SqlUtils.deleteTable(app.getAppIdentifier());
}
});
}
}

/**
* Default constructor.
*/
public SqlDAO() {
}

@Override
public <P extends ParaObject> String create(String appid, P object) {
if (object == null) {
Expand Down

0 comments on commit 605b3eb

Please sign in to comment.