use of org.cerberus.crud.entity.Tag in project cerberus-source by cerberustesting.
the class TagDAO method readByVariousByCriteria.
@Override
public AnswerList<Tag> readByVariousByCriteria(String campaign, 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<Tag> objectList = new ArrayList<Tag>();
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 SQL_CALC_FOUND_ROWS * FROM tag tag ");
searchSQL.append(" where 1=1 ");
if (!StringUtil.isNullOrEmpty(searchTerm)) {
searchSQL.append(" and (tag.`id` like ?");
searchSQL.append(" or tag.`tag` like ?");
searchSQL.append(" or tag.`description` like ?");
searchSQL.append(" or tag.`campaign` 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(" )");
}
if (!StringUtil.isNullOrEmpty(campaign)) {
searchSQL.append(" and (`campaign` = ? )");
}
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 + "%");
}
for (String individualColumnSearchValue : individalColumnSearchValues) {
preStat.setString(i++, individualColumnSearchValue);
}
if (!StringUtil.isNullOrEmpty(campaign)) {
preStat.setString(i++, campaign);
}
ResultSet resultSet = preStat.executeQuery();
try {
// gets the data
while (resultSet.next()) {
objectList.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 (objectList.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(objectList, nrTotalRows);
} else if (objectList.size() <= 0) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
response = new AnswerList(objectList, nrTotalRows);
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
response = new AnswerList(objectList, 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(objectList);
return response;
}
use of org.cerberus.crud.entity.Tag in project cerberus-source by cerberustesting.
the class ReadTag method findTagList.
// </editor-fold>
private AnswerItem findTagList(ApplicationContext appContext, boolean userHasPermissions, HttpServletRequest request) throws JSONException {
AnswerItem item = new AnswerItem();
JSONObject object = new JSONObject();
tagService = appContext.getBean(TagService.class);
int startPosition = 0;
if (request.getParameter("iDisplayStartPage") != null) {
startPosition = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayStartPage"), "0"));
startPosition--;
startPosition = startPosition * 30;
} else {
startPosition = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayStart"), "0"));
}
int length = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayLength"), "0"));
/*int sEcho = Integer.valueOf(request.getParameter("sEcho"));*/
String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");
int columnToSortParameter = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortCol_0"), "1"));
String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "id,tag,campaign,description");
String[] columnToSort = sColumns.split(",");
String columnName = columnToSort[columnToSortParameter];
String sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_0"), "desc");
Map<String, List<String>> individualSearch = new HashMap<>();
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(",")));
individualSearch.put(columnToSort[a], search);
}
}
AnswerList resp = tagService.readByCriteria(startPosition, length, columnName, sort, searchParameter, individualSearch);
JSONArray jsonArray = new JSONArray();
if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
// the service was able to perform the query, then we should get all values
for (Tag tagCur : (List<Tag>) resp.getDataList()) {
jsonArray.put(convertTagToJSONObject(tagCur));
}
}
object.put("hasPermissions", userHasPermissions);
object.put("contentTable", jsonArray);
object.put("iTotalRecords", resp.getTotalRows());
object.put("iTotalDisplayRecords", resp.getTotalRows());
item.setItem(object);
item.setResultMessage(resp.getResultMessage());
return item;
}
use of org.cerberus.crud.entity.Tag in project cerberus-source by cerberustesting.
the class ReadTestCaseExecutionByTag method processRequest.
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Calling Servlet Transversal Util.
ServletUtil.servletStart(request);
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
response.setContentType("application/json");
response.setCharacterEncoding("utf8");
String echo = request.getParameter("sEcho");
AnswerItem answer = new AnswerItem(new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));
testCaseExecutionService = appContext.getBean(ITestCaseExecutionService.class);
tagService = appContext.getBean(ITagService.class);
testCaseExecutionInQueueService = appContext.getBean(ITestCaseExecutionQueueService.class);
try {
// Data/Filter Parameters.
String Tag = ParameterParserUtil.parseStringParam(request.getParameter("Tag"), "");
List<String> outputReport = ParameterParserUtil.parseListParamAndDecode(request.getParameterValues("outputReport"), new ArrayList(), "UTF-8");
JSONObject jsonResponse = new JSONObject();
JSONObject statusFilter = getStatusList(request);
JSONObject countryFilter = getCountryList(request, appContext);
// Get Data from database
List<TestCaseExecution> testCaseExecutions = testCaseExecutionService.readLastExecutionAndExecutionInQueueByTag(Tag);
// Table that contain the list of testcases and corresponding executions
if (outputReport.isEmpty() || outputReport.contains("table")) {
jsonResponse.put("table", generateTestCaseExecutionTable(appContext, testCaseExecutions, statusFilter, countryFilter));
}
// Executions per Function (or Test).
if (outputReport.isEmpty() || outputReport.contains("functionChart")) {
jsonResponse.put("functionChart", generateFunctionChart(testCaseExecutions, Tag, statusFilter, countryFilter));
}
// Global executions stats per Status
if (outputReport.isEmpty() || outputReport.contains("statsChart")) {
jsonResponse.put("statsChart", generateStats(request, testCaseExecutions, statusFilter, countryFilter, true));
}
// BugTracker Recap
if (outputReport.isEmpty() || outputReport.contains("bugTrackerStat")) {
jsonResponse.put("bugTrackerStat", generateBugStats(request, testCaseExecutions, statusFilter, countryFilter));
}
// Labels Stats
if (outputReport.isEmpty() || outputReport.contains("labelStat")) {
jsonResponse.put("labelStat", generateLabelStats(appContext, request, testCaseExecutions, statusFilter, countryFilter));
}
if (!outputReport.isEmpty()) {
// currently used to optimize the homePage
if (outputReport.contains("totalStatsCharts") && !outputReport.contains("statsChart")) {
jsonResponse.put("statsChart", generateStats(request, testCaseExecutions, statusFilter, countryFilter, false));
}
// currently used to optimize the homePage
if (outputReport.contains("resendTag")) {
jsonResponse.put("tag", Tag);
}
}
Tag mytag = tagService.convert(tagService.readByKey(Tag));
JSONObject tagJSON = convertTagToJSONObject(mytag);
jsonResponse.put("tagObject", tagJSON);
jsonResponse.put("tagDuration", (mytag.getDateEndQueue().getTime() - mytag.getDateCreated().getTime()) / 60000);
answer.setItem(jsonResponse);
answer.setResultMessage(answer.getResultMessage().resolveDescription("ITEM", "Tag Statistics").resolveDescription("OPERATION", "Read"));
jsonResponse.put("messageType", answer.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", answer.getResultMessage().getDescription());
jsonResponse.put("sEcho", echo);
response.getWriter().print(jsonResponse.toString());
} catch (ParseException ex) {
LOG.error("Error on main call : " + ex);
} catch (CerberusException ex) {
LOG.error("Error on main call : " + ex);
} catch (JSONException ex) {
LOG.error("Error on main call : " + ex);
} catch (Exception ex) {
LOG.error("Error on main call : " + ex);
}
}
use of org.cerberus.crud.entity.Tag in project cerberus-source by cerberustesting.
the class ReadTag method findTagByKey.
private AnswerItem findTagByKey(String tag, ApplicationContext appContext, HttpServletRequest request) throws JSONException, CerberusException {
AnswerItem item = new AnswerItem();
JSONObject object = new JSONObject();
ITagService libService = appContext.getBean(ITagService.class);
// finds the project
AnswerItem answer = libService.readByKey(tag);
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
Tag tagObj = (Tag) answer.getItem();
JSONObject response = convertTagToJSONObject(tagObj);
// response.put("hasPermissionsUpdate", libService.hasPermissionsUpdate(tagObj, request));
// response.put("hasPermissionsDelete", libService.hasPermissionsDelete(tagObj, request));
object.put("contentTable", response);
}
// object.put("hasPermissionsCreate", libService.hasPermissionsCreate(null, request));
item.setItem(object);
item.setResultMessage(answer.getResultMessage());
return item;
}
use of org.cerberus.crud.entity.Tag in project cerberus-source by cerberustesting.
the class TagService method createAuto.
@Override
public Answer createAuto(String tagS, String campaign, String user) {
AnswerItem answerTag;
answerTag = readByKey(tagS);
Tag tag = (Tag) answerTag.getItem();
if (tag == null) {
Answer ans = tagDAO.create(factoryTag.create(0, tagS, "", campaign, null, user, null, user, null));
if (!StringUtil.isNullOrEmpty(campaign)) {
emailService.generateAndSendNotifyStartTagExecution(tagS, campaign);
}
return ans;
// If campaign is not empty, we could notify the Start of campaign execution.
} else {
if ((StringUtil.isNullOrEmpty(tag.getCampaign())) && !StringUtil.isNullOrEmpty(campaign)) {
tag.setCampaign(campaign);
return tagDAO.update(tag.getTag(), tag);
}
return null;
}
}
Aggregations