Skip to content

Commit

Permalink
add support for isDirty and isNewRecord functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SupahNickie committed Dec 13, 2016
1 parent 48f8990 commit 6486271
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
Binary file renamed caffeineORM_v4_0_1.jar → caffeineORM_v4_1_0.jar
Binary file not shown.
27 changes: 26 additions & 1 deletion src/supahnickie/caffeine/CaffeineObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@
public class CaffeineObject {
static List<String> ignoredFields = new ArrayList<String>();
protected String validationErrors = "";
Map<String, Object> attrsOnInit = new HashMap<String, Object>();
boolean isNewRecord = false;

static {
ignoredFields.add("tableName");
ignoredFields.add("validationErrors");
ignoredFields.add("caffeineAssociations");
ignoredFields.add("attrsOnInit");
ignoredFields.add("isNewRecord");
}

protected CaffeineObject() throws Exception {
this.isNewRecord = true;
this.captureCurrentStateOfAttrs();
}

/* Internal meta-utilities */
Expand Down Expand Up @@ -56,6 +65,8 @@ public static final CaffeineObject find(Class klass, int i) throws Exception {
while (rs.next()) {
newInstance.setAttrs(rs);
}
newInstance.captureCurrentStateOfAttrs();
newInstance.setIsNewRecord(false);
CaffeineConnection.teardown(rs, ps);
return newInstance;
}
Expand Down Expand Up @@ -197,9 +208,17 @@ private final List<CaffeineObject> getBelongsTo(Class associated, String foreign

/* Helper methods */

public boolean isNewRecord() { return this.isNewRecord; }

public boolean isDirty() throws Exception {
if (this.attrsOnInit.equals(this.buildArgsFromCurrentInstance())) { return false; }
return true;
}

private final Map<String, Object> buildArgsFromCurrentInstance() throws Exception {
Map<String, Object> args = new HashMap<String, Object>();
Field[] fields = CaffeineConnection.getQueryClass().getDeclaredFields();
setQueryClass(this.getClass());
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
String[] nameSplit = field.toString().split("\\.");
String simpleName = nameSplit[nameSplit.length - 1];
Expand All @@ -211,6 +230,10 @@ private final Map<String, Object> buildArgsFromCurrentInstance() throws Exceptio
return args;
}

void captureCurrentStateOfAttrs() throws Exception {
this.attrsOnInit = this.buildArgsFromCurrentInstance();
}

private final static String insertInsertPlaceholders(Map<String, Object> args, List<Object> argKeys) throws Exception {
String tableName = (String) getFieldValue("tableName");
String sql = "insert into " + tableName + " (";
Expand Down Expand Up @@ -321,4 +344,6 @@ final void setAttr(String column, Object value) throws Exception {
// Do nothing
}
}

void setIsNewRecord(boolean b) { this.isNewRecord = b; }
}
4 changes: 4 additions & 0 deletions src/supahnickie/caffeine/CaffeineSQLRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ static final CaffeineObject executeUpdate(Connection c, PreparedStatement ps, Ca
ps.executeUpdate();
c.commit();
instance.setAttrsFromSqlReturn(ps.getGeneratedKeys());
instance.setIsNewRecord(false);
instance.captureCurrentStateOfAttrs();
c.close();
ps.close();
CaffeineConnection.teardown();
Expand Down Expand Up @@ -118,9 +120,11 @@ private static final List<CaffeineObject> createListFromQueryReturn(List<HashMap
List<CaffeineObject> ret = new ArrayList<CaffeineObject>();
for (HashMap<String, Object> row : table) {
CaffeineObject newInstance = (CaffeineObject) CaffeineConnection.getQueryClass().newInstance();
newInstance.setIsNewRecord(false);
for (String column: row.keySet()) {
newInstance.setAttr(column, row.get(column));
}
newInstance.captureCurrentStateOfAttrs();
ret.add(newInstance);
}
return ret;
Expand Down
36 changes: 36 additions & 0 deletions src/supahnickie/caffeineTester/CaffeineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,42 @@ public void updateValidations() throws Exception {
assertEquals("getValiationErrors should report back the reason", "* last_name of another illegal name is not allowed for a User *", user.getValidationErrors());
}

@Test
public void isDirty() throws Exception {
User user = new User();
assertEquals("dirty on a new record should not be true", false, user.isDirty());
user.setFirstName("Grawr");
assertEquals("dirty on a changed record should be true", true, user.isDirty());
user.setFirstName(null);
assertEquals("dirty on a should be set back to false if the record has the same attrs", false, user.isDirty());
User user2 = (User) CaffeineObject.find(User.class, 3);
assertEquals("dirty on a looked-up record should not be true", false, user2.isDirty());
user2.setFirstName("ohhh yeaaaah, snap into a Slim Jim");
assertEquals("dirty on an existing record with different attrs than it started should be true", true, user2.isDirty());
user2.update();
assertEquals("record should not be considered dirty anymore after updating in the DB", false, user2.isDirty());
List<CaffeineObject> users = CaffeineConnection.objectQuery("select * from users");
assertArrayEquals("all returned records should not be dirty", new boolean[] {false, false, false}, new boolean[] {users.get(0).isDirty(), users.get(2).isDirty(), users.get(2).isDirty()});
}

@Test
public void isNewRecord() throws Exception {
User user = new User();
assertEquals("newRecord on a new record should be true", true, user.isNewRecord());
user.setFirstName("Graaw!");
assertEquals("changing attrs should not affect whether the record was new or not", true, user.isNewRecord());
user.create();
assertEquals("once persisted in the DB, the record should not be new anymore", false, user.isNewRecord());
User user2 = (User) CaffeineObject.find(User.class, 3);
assertEquals("newRecord on a looked-up record should not be true", false, user2.isNewRecord());
user2.setFirstName("ohhh yeaaaah, snap into a Slim Jim");
assertEquals("changing attrs on an existing record should not affect the state of newRecord", false, user2.isNewRecord());
user2.update();
assertEquals("existing record should also not change newRecord state after persisting", false, user2.isNewRecord());
List<CaffeineObject> users = CaffeineConnection.objectQuery("select * from users");
assertArrayEquals("all returned records should not be flagged as new", new boolean[] {false, false, false}, new boolean[] {users.get(0).isNewRecord(), users.get(2).isNewRecord(), users.get(2).isNewRecord()});
}

// Test helper methods

@Before
Expand Down
4 changes: 3 additions & 1 deletion src/supahnickie/testClasses/Download.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public class Download extends CaffeineObject {
public String file_file_name;
public int user_id;

public Download() {}
public Download() throws Exception {
super();
}

/* Validations */

Expand Down
4 changes: 3 additions & 1 deletion src/supahnickie/testClasses/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public class User extends CaffeineObject {
private int sign_in_count;
private String role;

public User() {}
public User() throws Exception {
super();
}

/* Validations */

Expand Down

0 comments on commit 6486271

Please sign in to comment.