Skip to content

Commit

Permalink
Merge pull request #8 from agilitytestbed/features/messagerules
Browse files Browse the repository at this point in the history
Features/messagerules
  • Loading branch information
dkooij authored Aug 7, 2018
2 parents c9eb25a + 7ddd12c commit 27d9541
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 15 deletions.
13 changes: 12 additions & 1 deletion database_structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ CREATE TABLE IF NOT EXISTS User_Table(
highest_category_rule_id BIGINT,
highest_saving_goal_id BIGINT,
highest_payment_request_id BIGINT,
highest_user_message_id BIGINT
highest_user_message_id BIGINT,
highest_message_rule_id BIGINT
);

CREATE TABLE IF NOT EXISTS Transaction_Table(
Expand Down Expand Up @@ -99,3 +100,13 @@ CREATE TABLE IF NOT EXISTS User_Message(
PRIMARY KEY(user_id, user_message_id)
);

CREATE TABLE IF NOT EXISTS Message_Rule(
user_id INTEGER,
message_rule_id BIGINT,
category_id BIGINT,
type TEXT,
value float,
FOREIGN KEY(user_id) REFERENCES User_Table(user_id),
PRIMARY KEY(user_id, message_rule_id)
);

29 changes: 27 additions & 2 deletions sql_queries_and_updates.txt
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,31 @@ SET highest_lifetime_balance =
END
WHERE user_id = ?;

increaseHighestMessageRuleID(user_id):
UPDATE User_Table
SET highest_message_rule_id = highest_message_rule_id + 1
WHERE user_id = ?;

getHighestMessageRuleID(user_id):
SELECT highest_message_rule_id
FROM User_Table
WHERE user_id = ?;

createMessageRule(user_id, message_rule_id, category_id, type, value):
INSERT INTO Message_Rule (user_id, message_rule_id, category_id, type, value)
VALUES (?, ?, ?, ?, ?);

getMessageRules(user_id):
SELECT message_rule_id, category_id, type, value
FROM Message_Rule
WHERE user_id = ?;

getMessageRule(user_id, message_rule_id):
SELECT message_rule_id, category_id, type, value
FROM Message_Rule
WHERE user_id = ?
AND message_rule_id = ?;

linkTransactionToCategory(user_id, transaction_id, category_id):
INSERT INTO Transaction_Category (user_id, transaction_id, category_id)
VALUES (?, ?, ?);
Expand Down Expand Up @@ -323,8 +348,8 @@ AND t.user_id = ?
AND t.transaction_id = ?;

createNewUser(session_id):
INSERT INTO User_Table (session_id, highest_lifetime_balance, highest_transaction_id, highest_category_id, highest_category_rule_id, highest_saving_goal_id, highest_payment_request_id, highest_user_message_id)
VALUES (?, 0, 0, 0, 0, 0, 0, 0);
INSERT INTO User_Table (session_id, highest_lifetime_balance, highest_transaction_id, highest_category_id, highest_category_rule_id, highest_saving_goal_id, highest_payment_request_id, highest_user_message_id, highest_message_rule_id)
VALUES (?, 0, 0, 0, 0, 0, 0, 0, 0);

getUserID(session_id):
SELECT user_id
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/nl/utwente/ing/api/MainRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -766,4 +766,31 @@ public ResponseEntity putUserMessageRead(@RequestParam(value = "session_id", def
}
}

/**
* Method used to create a new MessageRule for the user issuing the current request.
*
* @param pSessionID The sessionID specified in the request parameters.
* @param hSessionID The sessionID specified in the HTTP header.
* @param mr The MessageRule object as specified in the json HTTP body.
* @return A ResponseEntity containing a HTTP status code and either a status message or
* the MessageRule created by using this method.
*/
@RequestMapping(method = RequestMethod.POST,
value = RestControllerConstants.URI_PREFIX + "/messageRules")
public ResponseEntity postMessageRule(@RequestParam(value = "session_id", defaultValue = "") String pSessionID,
@RequestHeader(value = "X-session-ID", defaultValue = "") String hSessionID,
@RequestBody MessageRule mr) {
if (mr == null || mr.getCategory_id() <= 0 || mr.getType() == null || mr.getValue() < 0 ||
(!mr.getType().equals("info") && !mr.getType().equals("warning"))) {
return ResponseEntity.status(405).body("Invalid input given");
}
try {
String sessionID = this.getSessionID(pSessionID, hSessionID);
MessageRule messageRule = model.postMessageRule(sessionID, mr);
return ResponseEntity.status(201).body(messageRule);
} catch (InvalidSessionIDException e) {
return ResponseEntity.status(401).body("Session ID is missing or invalid");
}
}

}
10 changes: 10 additions & 0 deletions src/main/java/nl/utwente/ing/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,14 @@ void deleteSavingGoal(String sessionID, long savingGoalID)
*/
void setUserMessageRead(String sessionID, long userMessageID)
throws InvalidSessionIDException, ResourceNotFoundException;

/**
* Method used to create a new MessageRule for a certain user.
*
* @param sessionID The sessionID of the user.
* @param messageRule The MessageRule object to be used to create the new MessageRule.
* @return The MessageRule created by this method.
*/
MessageRule postMessageRule(String sessionID, MessageRule messageRule) throws InvalidSessionIDException;

}
112 changes: 112 additions & 0 deletions src/main/java/nl/utwente/ing/model/bean/MessageRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package nl.utwente.ing.model.bean;

/**
* The MessageRule class.
* Used to store information about a MessageRule.
*
* @author Daan Kooij
*/
public class MessageRule {

private long id;
private long category_id;
private String type;
private float value;

/**
* An empty constructor of MessageRule.
* Used by the Spring framework.
*/
public MessageRule() {

}

/**
* A constructor of MessageRule.
*
* @param id The ID of the to be created MessageRule.
* @param category_id The category_id of the to be created MessageRule.
* @param type The type of the to be created MessageRule.
* @param value The value of the to be created MessageRule.
*/
public MessageRule(long id, long category_id, String type, float value) {
this.id = id;
this.category_id = category_id;
this.type = type;
this.value = value;
}

/**
* Method used to retrieve the ID of MessageRule.
*
* @return The ID of MessageRule.
*/
public long getID() {
return id;
}

/**
* Method used to update the ID of MessageRule.
*
* @param id The new ID of MessageRule.
*/
public void setID(long id) {
this.id = id;
}

/**
* Method used to retrieve the category_id of MessageRule.
*
* @return The category_id of MessageRule.
*/
public long getCategory_id() {
return category_id;
}

/**
* Method used to update the category_id of MessageRule.
*
* @param category_id The new category_id of MessageRule.
*/
public void setCategory_id(long category_id) {
this.category_id = category_id;
}


/**
* Method used to retrieve the type of MessageRule.
*
* @return The type of MessageRule.
*/
public String getType() {
return type;
}

/**
* Method used to update the type of MessageRule.
*
* @param type The new type of MessageRule.
*/
public void setType(String type) {
this.type = type;
}

/**
* Method used to retrieve the value of MessageRule.
*
* @return The value of MessageRule.
*/
public float getValue() {
return value;
}

/**
* Method used to update the value of MessageRule.
*
* @param value The new value of MessageRule.
*/
public void setValue(float value) {
this.value = value;
}

}
4 changes: 2 additions & 2 deletions src/main/java/nl/utwente/ing/model/bean/UserMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class UserMessage {
private String type;

/**
* An empty constructor of PaymentRequest.
* An empty constructor of UserMessage.
* Used by the Spring framework.
*/
public UserMessage() {
Expand Down Expand Up @@ -114,7 +114,7 @@ public void setRead(boolean read) {
/**
* Method used to retrieve the type of UserMessage.
*
* @return The ID of UserMessage.
* @return The type of UserMessage.
*/
public String getType() {
return type;
Expand Down
131 changes: 129 additions & 2 deletions src/main/java/nl/utwente/ing/model/persistentmodel/CustomORM.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,26 @@ public class CustomORM {
" ELSE highest_lifetime_balance \n" +
" END\n" +
"WHERE user_id = ?;";
private static final String INCREASE_HIGHEST_MESSAGE_RULE_ID =
"UPDATE User_Table\n" +
"SET highest_message_rule_id = highest_message_rule_id + 1\n" +
"WHERE user_id = ?;";
private static final String GET_HIGHEST_MESSAGE_RULE_ID =
"SELECT highest_message_rule_id\n" +
"FROM User_Table\n" +
"WHERE user_id = ?;";
private static final String CREATE_MESSAGE_RULE =
"INSERT INTO Message_Rule (user_id, message_rule_id, category_id, type, value)\n" +
"VALUES (?, ?, ?, ?, ?);";
private static final String GET_MESSAGE_RULES =
"SELECT message_rule_id, category_id, type, value\n" +
"FROM Message_Rule\n" +
"WHERE user_id = ?;";
private static final String GET_MESSAGE_RULE =
"SELECT message_rule_id, category_id, type, value\n" +
"FROM Message_Rule\n" +
"WHERE user_id = ?\n" +
"AND message_rule_id = ?;";
private static final String LINK_TRANSACTION_TO_CATEGORY =
"INSERT INTO Transaction_Category (user_id, transaction_id, category_id)\n" +
"VALUES (?, ?, ?);";
Expand All @@ -292,8 +312,8 @@ public class CustomORM {
private static final String CREATE_NEW_USER =
"INSERT INTO User_Table (session_id, highest_lifetime_balance, highest_transaction_id, " +
"highest_category_id, highest_category_rule_id, highest_saving_goal_id, " +
"highest_payment_request_id, highest_user_message_id)\n" +
"VALUES (?, 0, 0, 0, 0, 0, 0, 0);";
"highest_payment_request_id, highest_user_message_id, highest_message_rule_id)\n" +
"VALUES (?, 0, 0, 0, 0, 0, 0, 0, 0);";
private static final String GET_USER_ID =
"SELECT user_id\n" +
"FROM User_Table\n" +
Expand Down Expand Up @@ -1461,6 +1481,113 @@ public void updateHighestLifetimeBalance(int userID, float currentBalance) {
}
}

/**
* Method used to increase the highestMessageRuleID field of a certain user by one in the database.
*
* @param userID The ID of the user whose highestMessageRuleID field should be increased.
*/
public void increaseHighestMessageRuleID(int userID) {
try {
PreparedStatement statement = connection.prepareStatement(INCREASE_HIGHEST_MESSAGE_RULE_ID);
statement.setInt(1, userID);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* Method used to retrieve the highestMessageRuleID field of a certain user from the database.
*
* @param userID The ID of the user whose highestMessageRuleID field should be retrieved.
* @return The value of the highestMessageRuleID field of the user with userID.
*/
public long getHighestMessageRuleID(int userID) {
long highestMessageRuleID = -1;
try {
PreparedStatement statement = connection.prepareStatement(GET_HIGHEST_MESSAGE_RULE_ID);
statement.setInt(1, userID);
ResultSet rs = statement.executeQuery();
highestMessageRuleID = rs.getLong(1);
} catch (SQLException e) {
e.printStackTrace();
}
return highestMessageRuleID;
}

/**
* Method used to insert a MessageRule into the database.
*
* @param userID The ID of the user to which this new MessageRule will belong.
* @param messageRule The MessageRule object to be inserted into the database.
*/
public void createMessageRule(int userID, MessageRule messageRule) {
try {
PreparedStatement statement = connection.prepareStatement(CREATE_MESSAGE_RULE);
statement.setInt(1, userID);
statement.setLong(2, messageRule.getID());
statement.setLong(3, messageRule.getCategory_id());
statement.setString(4, messageRule.getType());
statement.setFloat(5, messageRule.getValue());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* Method used to retrieve a batch of MessageRule objects belonging to a certain user from the database.
*
* @param userID The ID of the user to who the to be retrieved MessageRule objects belong.
* @return An ArrayList of MessageRule objects.
*/
public ArrayList<MessageRule> getMessageRules(int userID) {
ArrayList<MessageRule> messageRules = new ArrayList<>();
try {
PreparedStatement statement = connection.prepareStatement(GET_MESSAGE_RULES);
statement.setInt(1, userID);
ResultSet resultSet = statement.executeQuery();

while (resultSet.next()) {
long messageRuleID = resultSet.getLong(1);
long categoryID = resultSet.getLong(2);
String type = resultSet.getString(3);
float value = resultSet.getFloat(4);
messageRules.add(new MessageRule(messageRuleID, categoryID, type, value));
}
} catch (SQLException e) {
e.printStackTrace();
}
return messageRules;
}

/**
* Method used to retrieve a MessageRule object belonging to a certain user from the database.
*
* @param userID The ID of the user to who the to be retrieved MessageRule object belongs.
* @return An MessageRule object.
*/
public MessageRule getMessageRule(int userID, long messageRuleID) {
MessageRule messageRule = null;
try {
PreparedStatement statement = connection.prepareStatement(GET_MESSAGE_RULE);
statement.setInt(1, userID);
statement.setLong(2, messageRuleID);
ResultSet resultSet = statement.executeQuery();

if (resultSet.next()) {
messageRuleID = resultSet.getLong(1);
long categoryID = resultSet.getLong(2);
String type = resultSet.getString(3);
float value = resultSet.getFloat(4);
messageRule = new MessageRule(messageRuleID, categoryID, type, value);
}
} catch (SQLException e) {
e.printStackTrace();
}
return messageRule;
}

/**
* Method used to link a Transaction to a Category in the database.
*
Expand Down
Loading

0 comments on commit 27d9541

Please sign in to comment.