Search in sources :

Example 1 with LogEvent

use of org.cerberus.crud.entity.LogEvent in project cerberus-source by cerberustesting.

the class LogEventDAO method readByCriteria.

@Override
public AnswerList readByCriteria(int start, int amount, String colName, 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<LogEvent> logEventList = new ArrayList<LogEvent>();
    StringBuilder searchSQL = new StringBuilder();
    List<String> individalColumnSearchValues = new ArrayList<String>();
    final 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 SQL_CALC_FOUND_ROWS * FROM logevent ");
    searchSQL.append(" where 1=1 ");
    if (!StringUtil.isNullOrEmpty(searchTerm)) {
        searchSQL.append(" and (`time` like ?");
        searchSQL.append(" or `login` like ?");
        searchSQL.append(" or `page` like ?");
        searchSQL.append(" or `action` like ?");
        searchSQL.append(" or `log` 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(colName)) {
        query.append("order by `").append(colName).append("` ").append(dir);
    } else {
        query.append("order by `logEventID` desc");
    }
    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 + "%");
            }
            for (String individualColumnSearchValue : individalColumnSearchValues) {
                preStat.setString(i++, individualColumnSearchValue);
            }
            ResultSet resultSet = preStat.executeQuery();
            try {
                // gets the data
                while (resultSet.next()) {
                    logEventList.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 (logEventList.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(logEventList, nrTotalRows);
                } else if (logEventList.size() <= 0) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                    response = new AnswerList(logEventList, nrTotalRows);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    response = new AnswerList(logEventList, 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(logEventList);
    return response;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) LogEvent(org.cerberus.crud.entity.LogEvent) IFactoryLogEvent(org.cerberus.crud.factory.IFactoryLogEvent) 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 2 with LogEvent

use of org.cerberus.crud.entity.LogEvent in project cerberus-source by cerberustesting.

the class ReadLogEvent method findLogEventByID.

private AnswerItem findLogEventByID(ApplicationContext appContext, long id) throws JSONException, CerberusException {
    AnswerItem item = new AnswerItem();
    JSONObject object = new JSONObject();
    ILogEventService libService = appContext.getBean(ILogEventService.class);
    AnswerItem answer = libService.readByKey(id);
    if (answer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // if the service returns an OK message then we can get the item and convert it to JSONformat
        LogEvent lib = (LogEvent) answer.getItem();
        JSONObject response = convertLogEventToJSONObject(lib);
        object.put("contentTable", response);
    }
    item.setItem(object);
    item.setResultMessage(answer.getResultMessage());
    return item;
}
Also used : JSONObject(org.json.JSONObject) LogEvent(org.cerberus.crud.entity.LogEvent) ILogEventService(org.cerberus.crud.service.ILogEventService) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 3 with LogEvent

use of org.cerberus.crud.entity.LogEvent in project cerberus-source by cerberustesting.

the class FactoryLogEvent method create.

@Override
public LogEvent create(long logEventID, long userID, String login, Timestamp time, String page, String action, String log, String remoteIP, String localIP) {
    LogEvent newLogEvent = new LogEvent();
    newLogEvent.setLogEventID(logEventID);
    newLogEvent.setUserID(userID);
    newLogEvent.setLogin(login);
    newLogEvent.setTime(time);
    newLogEvent.setPage(page);
    newLogEvent.setAction(action);
    newLogEvent.setLog(log);
    newLogEvent.setremoteIP(remoteIP);
    newLogEvent.setLocalIP(localIP);
    return newLogEvent;
}
Also used : LogEvent(org.cerberus.crud.entity.LogEvent) IFactoryLogEvent(org.cerberus.crud.factory.IFactoryLogEvent)

Example 4 with LogEvent

use of org.cerberus.crud.entity.LogEvent in project cerberus-source by cerberustesting.

the class LogEventDAO method readByKey.

@Override
public AnswerItem readByKey(long logEventID) {
    AnswerItem ans = new AnswerItem();
    LogEvent result = null;
    final String query = "SELECT * FROM logevent WHERE `logEventID` = ?";
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setLong(1, logEventID);
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = loadFromResultSet(resultSet);
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    ans.setItem(result);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                }
            } 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 {
                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 {
            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 (connection != null) {
                connection.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    // sets the message
    ans.setResultMessage(msg);
    return ans;
}
Also used : LogEvent(org.cerberus.crud.entity.LogEvent) IFactoryLogEvent(org.cerberus.crud.factory.IFactoryLogEvent) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 5 with LogEvent

use of org.cerberus.crud.entity.LogEvent in project cerberus-source by cerberustesting.

the class ReadLogEvent method findLogEventList.

// </editor-fold>
private AnswerItem findLogEventList(ApplicationContext appContext, HttpServletRequest request) throws CerberusException, JSONException {
    AnswerItem item = new AnswerItem();
    JSONObject jsonResponse = new JSONObject();
    logEventService = appContext.getBean(LogEventService.class);
    int startPosition = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayStart"), "0"));
    int length = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayLength"), "10000"));
    String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");
    int columnToSortParameter = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortCol_0"), "0"));
    String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "Time,login,Page,Action,log");
    String[] columnToSort = sColumns.split(",");
    String columnName = columnToSort[columnToSortParameter];
    String sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_0"), "desc");
    List<String> individualLike = new ArrayList(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").split(",")));
    Map<String, List<String>> individualSearch = new HashMap<String, List<String>>();
    for (int a = 0; a < columnToSort.length; a++) {
        if (null != request.getParameter("sSearch_" + a) && !request.getParameter("sSearch_" + a).isEmpty()) {
            List<String> search = new ArrayList(Arrays.asList(request.getParameter("sSearch_" + a).split(",")));
            if (individualLike.contains(columnToSort[a])) {
                individualSearch.put(columnToSort[a] + ":like", search);
            } else {
                individualSearch.put(columnToSort[a], search);
            }
        }
    }
    AnswerList resp = logEventService.readByCriteria(startPosition, length, columnName, sort, searchParameter, individualSearch);
    JSONArray jsonArray = new JSONArray();
    boolean userHasPermissions = false;
    if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        for (LogEvent myLogEvent : (List<LogEvent>) resp.getDataList()) {
            jsonArray.put(convertLogEventToJSONObject(myLogEvent));
        }
    }
    jsonResponse.put("hasPermissions", userHasPermissions);
    jsonResponse.put("contentTable", jsonArray);
    jsonResponse.put("iTotalRecords", resp.getTotalRows());
    jsonResponse.put("iTotalDisplayRecords", resp.getTotalRows());
    item.setItem(jsonResponse);
    item.setResultMessage(resp.getResultMessage());
    return item;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) HashMap(java.util.HashMap) LogEvent(org.cerberus.crud.entity.LogEvent) ILogEventService(org.cerberus.crud.service.ILogEventService) LogEventService(org.cerberus.crud.service.impl.LogEventService) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) AnswerItem(org.cerberus.util.answer.AnswerItem) JSONObject(org.json.JSONObject) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

LogEvent (org.cerberus.crud.entity.LogEvent)5 IFactoryLogEvent (org.cerberus.crud.factory.IFactoryLogEvent)3 AnswerItem (org.cerberus.util.answer.AnswerItem)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ILogEventService (org.cerberus.crud.service.ILogEventService)2 MessageEvent (org.cerberus.engine.entity.MessageEvent)2 AnswerList (org.cerberus.util.answer.AnswerList)2 JSONObject (org.json.JSONObject)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 LogEventService (org.cerberus.crud.service.impl.LogEventService)1 JSONArray (org.json.JSONArray)1