use of org.cerberus.crud.entity.User in project cerberus-source by cerberustesting.
the class UserDAO method readByCriteria.
@Override
public AnswerList readByCriteria(int start, int amount, String column, String dir, String searchTerm, Map<String, List<String>> individualSearch) {
AnswerList response = new AnswerList();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
List<User> applicationList = new ArrayList<User>();
StringBuilder searchSQL = new StringBuilder();
List<String> individalColumnSearchValues = new ArrayList<String>();
StringBuilder query = new StringBuilder();
// SQL_CALC_FOUND_ROWS allows to retrieve the total number of columns by disrearding the limit clauses that
// were applied -- used for pagination p
query.append("SELECT DISTINCT SQL_CALC_FOUND_ROWS usr.* FROM user usr ");
if (!StringUtil.isNullOrEmpty(searchTerm)) {
query.append("LEFT JOIN usergroup usg ON usg.`Login` = usr.`Login`");
}
searchSQL.append(" where 1=1 ");
if (!StringUtil.isNullOrEmpty(searchTerm)) {
searchSQL.append(" and (usr.`login` like ?");
searchSQL.append(" or usr.`name` like ?");
searchSQL.append(" or usr.`team` like ?");
searchSQL.append(" or usr.`language` like ?");
searchSQL.append(" or usr.`ReportingFavorite` like ?");
searchSQL.append(" or usr.`robotHost` like ?");
searchSQL.append(" or usr.`robotPort` like ?");
searchSQL.append(" or usr.`robotPlatform` like ?");
searchSQL.append(" or usr.`robotBrowser` like ?");
searchSQL.append(" or usr.`robotVersion` like ?");
searchSQL.append(" or usr.`robot` like ?");
searchSQL.append(" or usr.`DefaultSystem` like ?");
searchSQL.append(" or usr.`Email` like ?");
searchSQL.append(" or usg.`GroupName` like ?)");
}
if (individualSearch != null && !individualSearch.isEmpty()) {
searchSQL.append(" and ( 1=1 ");
for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {
searchSQL.append(" and ");
searchSQL.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));
individalColumnSearchValues.addAll(entry.getValue());
}
searchSQL.append(" )");
}
query.append(searchSQL);
if (!StringUtil.isNullOrEmpty(column)) {
query.append(" order by `").append(column).append("` ").append(dir);
}
if ((amount <= 0) || (amount >= MAX_ROW_SELECTED)) {
query.append(" limit ").append(start).append(" , ").append(MAX_ROW_SELECTED);
} else {
query.append(" limit ").append(start).append(" , ").append(amount);
}
// Debug message on SQL.
if (LOG.isDebugEnabled()) {
LOG.debug("SQL : " + query.toString());
}
Connection connection = this.databaseSpring.connect();
try {
PreparedStatement preStat = connection.prepareStatement(query.toString());
try {
int i = 1;
if (!StringUtil.isNullOrEmpty(searchTerm)) {
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
}
for (String individualColumnSearchValue : individalColumnSearchValues) {
preStat.setString(i++, individualColumnSearchValue);
}
ResultSet resultSet = preStat.executeQuery();
try {
// gets the data
while (resultSet.next()) {
applicationList.add(this.loadFromResultSet(resultSet));
}
// get the total number of rows
resultSet = preStat.executeQuery("SELECT FOUND_ROWS()");
int nrTotalRows = 0;
if (resultSet != null && resultSet.next()) {
nrTotalRows = resultSet.getInt(1);
}
if (applicationList.size() >= MAX_ROW_SELECTED) {
// Result of SQl was limited by MAX_ROW_SELECTED constrain. That means that we may miss some lines in the resultList.
LOG.error("Partial Result in the query.");
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));
response = new AnswerList(applicationList, nrTotalRows);
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
response = new AnswerList(applicationList, nrTotalRows);
}
} catch (SQLException exception) {
LOG.error("Unable to execute query : " + exception.toString());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
} finally {
if (resultSet != null) {
resultSet.close();
}
}
} catch (SQLException exception) {
LOG.error("Unable to execute query : " + exception.toString());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
} finally {
if (preStat != null) {
preStat.close();
}
}
} catch (SQLException exception) {
LOG.error("Unable to execute query : " + exception.toString());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
} finally {
try {
if (!this.databaseSpring.isOnTransaction()) {
if (connection != null) {
connection.close();
}
}
} catch (SQLException exception) {
LOG.warn("Unable to close connection : " + exception.toString());
}
}
response.setResultMessage(msg);
response.setDataList(applicationList);
return response;
}
use of org.cerberus.crud.entity.User in project cerberus-source by cerberustesting.
the class UserDAO method updateUserPassword.
@Override
public AnswerItem<User> updateUserPassword(User user, String password, String requestNewPassword) {
AnswerItem<User> answer = new AnswerItem<User>();
MessageEvent msg;
boolean res = false;
final String sql = "UPDATE user SET Password = SHA(?) , Request = ? WHERE Login LIKE ?";
Connection connection = this.databaseSpring.connect();
try {
PreparedStatement preStat = connection.prepareStatement(sql);
try {
preStat.setString(1, password);
preStat.setString(2, requestNewPassword);
preStat.setString(3, user.getLogin());
res = preStat.executeUpdate() > 0;
} catch (SQLException exception) {
LOG.warn("Unable to execute query : " + exception.toString());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Update Password - Unable to execute query"));
} finally {
if (preStat != null) {
preStat.close();
}
}
} catch (SQLException exception) {
LOG.warn("Unable to execute query : " + exception.toString());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Update Password - Unable to execute query"));
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
LOG.warn(e.toString());
}
}
if (res) {
answer.setItem(this.findUserByKey(user.getLogin()));
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", "User").replace("%OPERATION%", "Update password"));
} else {
answer.setItem(user);
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "User").replace("%OPERATION%", "Update Password").replace("%REASON%", "Your password was not updated. " + "Please contact your Cerberus' administrator to learn more information."));
}
answer.setResultMessage(msg);
return answer;
}
use of org.cerberus.crud.entity.User in project cerberus-source by cerberustesting.
the class UserDAOTest method testUpdateUserWhenFailToUpdate.
@Test
public void testUpdateUserWhenFailToUpdate() throws SQLException {
when(databaseSpring.connect()).thenReturn(connection);
when(connection.prepareStatement(anyString())).thenReturn(statement);
when(statement.executeUpdate()).thenReturn(0);
User user = new User();
boolean bool = userDAO.updateUser(user);
Assert.assertEquals(false, bool);
}
use of org.cerberus.crud.entity.User in project cerberus-source by cerberustesting.
the class UserDAOTest method testInsertUserWhenFailToInsert.
@Test
public void testInsertUserWhenFailToInsert() throws SQLException {
when(databaseSpring.connect()).thenReturn(connection);
when(connection.prepareStatement(anyString(), eq(Statement.RETURN_GENERATED_KEYS))).thenReturn(statement);
when(statement.executeUpdate()).thenReturn(0);
when(statement.getGeneratedKeys()).thenReturn(resultSet);
when(resultSet.first()).thenReturn(false);
User user = new User();
boolean bool = userDAO.insertUser(user);
Assert.assertEquals(false, bool);
Assert.assertEquals(new User(), user);
}
use of org.cerberus.crud.entity.User in project cerberus-source by cerberustesting.
the class UserDAOTest method testInsertUser.
@Test
public void testInsertUser() throws SQLException {
int id = 99999;
when(databaseSpring.connect()).thenReturn(connection);
when(connection.prepareStatement(anyString(), eq(Statement.RETURN_GENERATED_KEYS))).thenReturn(statement);
when(statement.executeUpdate()).thenReturn(1);
when(statement.getGeneratedKeys()).thenReturn(resultSet);
when(resultSet.first()).thenReturn(true);
when(resultSet.getInt(1)).thenReturn(id);
User user = new User();
boolean bool = userDAO.insertUser(user);
Assert.assertEquals(true, bool);
Assert.assertEquals(id, user.getUserID());
}
Aggregations