use of org.cerberus.servlet.manualtestcase.DeleteTestCaseExecutionFile in project cerberus-source by cerberustesting.
the class DeleteTestCaseExecutionFile 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, JSONException, CerberusException {
JSONObject jsonResponse = new JSONObject();
Answer ans = new Answer();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
ans.setResultMessage(msg);
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
response.setContentType("application/json");
// Calling Servlet Transversal Util.
ServletUtil.servletStart(request);
/**
* Parsing and securing all required parameters.
*/
Long fileId = ParameterParserUtil.parseLongParam(request.getParameter("fileID"), 0);
/**
* Checking all constrains before calling the services.
*/
if (fileId == null) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseExecutionFile").replace("%OPERATION%", "Delete").replace("%REASON%", "field fileID is missing!"));
ans.setResultMessage(msg);
} else {
/**
* All data seems cleans so we can call the services.
*/
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
ITestCaseExecutionFileService testCaseExecutionFileService = appContext.getBean(ITestCaseExecutionFileService.class);
IParameterService parameterService = appContext.getBean(IParameterService.class);
IRecorderService recorderService = appContext.getBean(IRecorderService.class);
AnswerItem resp = testCaseExecutionFileService.readByKey(fileId);
if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
/**
* Object could not be found. We stop here and report the error.
*/
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseExecutionFile").replace("%OPERATION%", "Delete").replace("%REASON%", "TestCaseExecutionFile with this ID does not exist."));
ans.setResultMessage(msg);
} else {
/**
* The service was able to perform the query and confirm the
* object exist, then we can delete it.
*/
TestCaseExecutionFile testCaseExecutionFile = (TestCaseExecutionFile) resp.getItem();
String rootFolder = parameterService.getParameterStringByKey("cerberus_exemanualmedia_path", "", "");
testCaseExecutionFileService.deleteFile(rootFolder, testCaseExecutionFile.getFileName());
ans = testCaseExecutionFileService.delete(testCaseExecutionFile);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Delete was successful. Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/DeleteTestCaseExecutionFile", "DELETE", "Delete TestCase Execution File : ['" + testCaseExecutionFile + "']", request);
}
}
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", ans.getResultMessage().getDescription());
response.getWriter().print(jsonResponse.toString());
response.getWriter().flush();
}
Aggregations