Search in sources :

Example 21 with User

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;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) User(org.cerberus.crud.entity.User) FactoryUser(org.cerberus.crud.factory.impl.FactoryUser) IFactoryUser(org.cerberus.crud.factory.IFactoryUser) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 22 with User

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;
}
Also used : User(org.cerberus.crud.entity.User) FactoryUser(org.cerberus.crud.factory.impl.FactoryUser) IFactoryUser(org.cerberus.crud.factory.IFactoryUser) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 23 with User

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);
}
Also used : User(org.cerberus.crud.entity.User) Test(org.junit.Test)

Example 24 with User

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);
}
Also used : User(org.cerberus.crud.entity.User) Test(org.junit.Test)

Example 25 with 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());
}
Also used : User(org.cerberus.crud.entity.User) Test(org.junit.Test)

Aggregations

User (org.cerberus.crud.entity.User)34 IUserService (org.cerberus.crud.service.IUserService)16 ApplicationContext (org.springframework.context.ApplicationContext)14 JSONObject (org.json.JSONObject)13 IFactoryUser (org.cerberus.crud.factory.IFactoryUser)11 CerberusException (org.cerberus.exception.CerberusException)11 AnswerItem (org.cerberus.util.answer.AnswerItem)9 JSONException (org.json.JSONException)9 Connection (java.sql.Connection)8 PreparedStatement (java.sql.PreparedStatement)8 SQLException (java.sql.SQLException)8 FactoryUser (org.cerberus.crud.factory.impl.FactoryUser)8 ILogEventService (org.cerberus.crud.service.ILogEventService)8 ResultSet (java.sql.ResultSet)7 MessageEvent (org.cerberus.engine.entity.MessageEvent)7 JSONArray (org.json.JSONArray)7 UserGroup (org.cerberus.crud.entity.UserGroup)6 IUserGroupService (org.cerberus.crud.service.IUserGroupService)6 AnswerList (org.cerberus.util.answer.AnswerList)6 Test (org.junit.Test)6