use of org.cerberus.crud.entity.CampaignParameter in project cerberus-source by cerberustesting.
the class CampaignParameterDAO method findCampaignParameterByCriteria.
@Override
public List<CampaignParameter> findCampaignParameterByCriteria(Integer campaignparameterID, String campaign, String parameter, String value) throws CerberusException {
boolean throwEx = false;
final StringBuilder query = new StringBuilder("SELECT * FROM campaignparameter c WHERE 1=1 ");
if (campaignparameterID != null) {
query.append(" AND c.campaignparameterID = ?");
}
if (campaign != null && !"".equals(campaign.trim())) {
query.append(" AND c.campaign LIKE ?");
}
if (parameter != null && !"".equals(parameter.trim())) {
query.append(" AND c.parameter LIKE ?");
}
if (value != null && !"".equals(value.trim())) {
query.append(" AND c.value LIKE ?");
}
// " c.campaignID = ? AND c.campaign LIKE ? AND c.description LIKE ?";
List<CampaignParameter> campaignParametersList = new ArrayList<CampaignParameter>();
try (Connection connection = this.databaseSpring.connect();
PreparedStatement preStat = connection.prepareStatement(query.toString())) {
int index = 1;
if (campaignparameterID != null) {
preStat.setInt(index, campaignparameterID);
index++;
}
if (campaign != null && !"".equals(campaign.trim())) {
preStat.setString(index, "%" + campaign.trim() + "%");
index++;
}
if (parameter != null && !"".equals(parameter.trim())) {
preStat.setString(index, "%" + parameter.trim() + "%");
index++;
}
if (value != null && !"".equals(value.trim())) {
preStat.setString(index, "%" + value.trim() + "%");
index++;
}
try (ResultSet resultSet = preStat.executeQuery()) {
while (resultSet.next()) {
campaignParametersList.add(this.loadFromResultSet(resultSet));
}
} catch (SQLException exception) {
LOG.warn("Unable to execute query : " + exception.toString());
campaignParametersList = null;
}
} catch (SQLException exception) {
LOG.warn("Unable to execute query : " + exception.toString());
campaignParametersList = null;
}
if (throwEx) {
throw new CerberusException(new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND));
}
return campaignParametersList;
}
use of org.cerberus.crud.entity.CampaignParameter in project cerberus-source by cerberustesting.
the class CampaignParameterDAO method readByCampaignByCriteria.
@Override
public AnswerList readByCampaignByCriteria(String campaign, int start, int amount, String column, String dir, String searchTerm, String individualSearch) {
AnswerList response = new AnswerList();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
List<CampaignParameter> campaignParameterList = new ArrayList<CampaignParameter>();
StringBuilder searchSQL = new StringBuilder();
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 campaignparameter ");
searchSQL.append(" where 1=1 ");
if (!StringUtil.isNullOrEmpty(searchTerm)) {
searchSQL.append(" and (`campaignparameterid` like ?");
searchSQL.append(" or `campaign` like ?");
searchSQL.append(" or `parameter` like ?");
searchSQL.append(" or `value` like ?)");
}
if (!StringUtil.isNullOrEmpty(individualSearch)) {
searchSQL.append(" and (`?`)");
}
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());
}
try (Connection connection = this.databaseSpring.connect();
PreparedStatement preStat = connection.prepareStatement(query.toString());
Statement stm = connection.createStatement()) {
int i = 1;
if (!StringUtil.isNullOrEmpty(searchTerm)) {
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
preStat.setString(i++, "%" + searchTerm + "%");
}
if (!StringUtil.isNullOrEmpty(individualSearch)) {
preStat.setString(i++, individualSearch);
}
if (!StringUtil.isNullOrEmpty(campaign)) {
preStat.setString(i++, campaign);
}
try (ResultSet resultSet = preStat.executeQuery();
ResultSet rowSet = stm.executeQuery("SELECT FOUND_ROWS()")) {
// gets the data
while (resultSet.next()) {
campaignParameterList.add(this.loadFromResultSet(resultSet));
}
// get the total number of rows
int nrTotalRows = 0;
if (resultSet != null && resultSet.next()) {
nrTotalRows = resultSet.getInt(1);
}
if (campaignParameterList.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(campaignParameterList, nrTotalRows);
} else if (campaignParameterList.size() <= 0) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
response = new AnswerList(campaignParameterList, nrTotalRows);
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
response = new AnswerList(campaignParameterList, 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()));
}
} 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()));
}
response.setResultMessage(msg);
response.setDataList(campaignParameterList);
return response;
}
use of org.cerberus.crud.entity.CampaignParameter in project cerberus-source by cerberustesting.
the class CampaignParameterService method parseParametersByCampaign.
@Override
public AnswerItem<Map<String, List<String>>> parseParametersByCampaign(final String campaignName) {
final AnswerList<CampaignParameter> campaignParameters = readByCampaign(campaignName);
if (!campaignParameters.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
return new AnswerItem<>(campaignParameters.getResultMessage());
}
final Map<String, List<String>> sortedCampaignParameters = new HashMap<>();
for (final CampaignParameter campaignParameter : campaignParameters.getDataList()) {
if (!sortedCampaignParameters.containsKey(campaignParameter.getParameter())) {
sortedCampaignParameters.put(campaignParameter.getParameter(), new ArrayList<String>());
}
sortedCampaignParameters.get(campaignParameter.getParameter()).add(campaignParameter.getValue());
}
return new AnswerItem<>(sortedCampaignParameters, new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));
}
use of org.cerberus.crud.entity.CampaignParameter in project cerberus-source by cerberustesting.
the class CreateCampaign 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
*/
final void processRequest(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException, CerberusException, JSONException {
JSONObject jsonResponse = new JSONObject();
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
Answer ans = null;
Answer finalAnswer = new Answer();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
response.setContentType("application/json");
response.setCharacterEncoding("utf8");
String charset = request.getCharacterEncoding();
// Parameter that are already controled by GUI (no need to decode) --> We SECURE them
// Parameter that needs to be secured --> We SECURE+DECODE them
String name = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("Campaign"), null, charset);
String notifyStart = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("NotifyStart"), "N", charset);
String notifyEnd = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("NotifyEnd"), "N", charset);
String desc = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("Description"), null, charset);
// Parameter that we cannot secure as we need the html --> We DECODE them
String distribList = ParameterParserUtil.parseStringParam(request.getParameter("DistribList"), "");
// String battery = ParameterParserUtil.parseStringParam(request.getParameter("Batteries"), null);
String parameter = ParameterParserUtil.parseStringParam(request.getParameter("Parameters"), null);
String label = ParameterParserUtil.parseStringParam(request.getParameter("Labels"), null);
if (StringUtil.isNullOrEmpty(name)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Campaign").replace("%OPERATION%", "Create").replace("%REASON%", "Campaign name is missing!"));
finalAnswer.setResultMessage(msg);
} else {
ICampaignService campaignService = appContext.getBean(ICampaignService.class);
IFactoryCampaign factoryCampaign = appContext.getBean(IFactoryCampaign.class);
Campaign camp = factoryCampaign.create(0, name, distribList, notifyStart, notifyEnd, desc);
finalAnswer = campaignService.create(camp);
if (finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/CreateCampaign", "CREATE", "Create Campaign : " + camp.getCampaign(), request);
if (parameter != null) {
JSONArray parameters = new JSONArray(parameter);
ICampaignParameterService campaignParameterService = appContext.getBean(ICampaignParameterService.class);
IFactoryCampaignParameter factoryCampaignParameter = appContext.getBean(IFactoryCampaignParameter.class);
ans = campaignParameterService.deleteByCampaign(name);
int i = 0;
while (i < parameters.length() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
JSONArray bat = parameters.getJSONArray(i);
CampaignParameter co = factoryCampaignParameter.create(0, bat.getString(0), bat.getString(2), bat.getString(3));
ans = campaignParameterService.create(co);
i++;
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Adding Log entry.
*/
logEventService.createForPrivateCalls("/CreateCampaign", "CREATE", "Update Campaign Parameter : " + co.getCampaign() + ", " + co.getValue(), request);
}
}
}
if (label != null) {
JSONArray labels = new JSONArray(label);
ICampaignLabelService campaignLabelService = appContext.getBean(ICampaignLabelService.class);
IFactoryCampaignLabel factoryCampaignLabel = appContext.getBean(IFactoryCampaignLabel.class);
ArrayList<CampaignLabel> arr = new ArrayList<>();
for (int i = 0; i < labels.length(); i++) {
JSONArray bat = labels.getJSONArray(i);
CampaignLabel co = factoryCampaignLabel.create(0, bat.getString(0), Integer.valueOf(bat.getString(2)), request.getRemoteUser(), null, request.getRemoteUser(), null);
arr.add(co);
}
finalAnswer = campaignLabelService.compareListAndUpdateInsertDeleteElements(name, arr);
if (finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Adding Log entry.
*/
logEventService.createForPrivateCalls("/CreateCampaign", "CREATE", "Create Campaign Label : " + camp.getCampaign(), request);
}
}
if (ans != null && !ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
finalAnswer = ans;
}
}
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());
response.getWriter().print(jsonResponse);
response.getWriter().flush();
}
use of org.cerberus.crud.entity.CampaignParameter in project cerberus-source by cerberustesting.
the class UpdateCampaign 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, CerberusException, JSONException {
JSONObject jsonResponse = new JSONObject();
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
Answer ans = new Answer();
Answer finalAnswer = new Answer(new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
ans.setResultMessage(msg);
response.setContentType("application/json");
response.setCharacterEncoding("utf8");
PrintWriter out = response.getWriter();
String charset = request.getCharacterEncoding();
// Parameter that are already controled by GUI (no need to decode) --> We SECURE them
// Parameter that needs to be secured --> We SECURE+DECODE them
int cID = ParameterParserUtil.parseIntegerParamAndDecode(request.getParameter("CampaignID"), 0, charset);
String c = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("Campaign"), null, charset);
String notifystart = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("NotifyStart"), null, charset);
String notifyend = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("NotifyEnd"), null, charset);
String desc = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("Description"), null, charset);
// Parameter that we cannot secure as we need the html --> We DECODE them
String distriblist = ParameterParserUtil.parseStringParam(request.getParameter("DistribList"), "");
if (StringUtil.isNullOrEmpty(c)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Campaign").replace("%OPERATION%", "Update").replace("%REASON%", "Campaign name is missing!"));
finalAnswer.setResultMessage(msg);
} else {
// Parameter that we cannot secure as we need the html --> We DECODE them
// String battery = ParameterParserUtil.parseStringParam(request.getParameter("Batteries"), null);
String parameter = ParameterParserUtil.parseStringParam(request.getParameter("Parameters"), null);
String label = ParameterParserUtil.parseStringParam(request.getParameter("Labels"), null);
ICampaignService campaignService = appContext.getBean(ICampaignService.class);
AnswerItem resp = campaignService.readByKey(c);
if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
/**
* Object could not be found. We stop here and report the error.
*/
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) resp);
} else {
Campaign camp = (Campaign) resp.getItem();
camp.setDistribList(distriblist);
camp.setNotifyStartTagExecution(notifystart);
camp.setNotifyEndTagExecution(notifyend);
camp.setDescription(desc);
finalAnswer = campaignService.update(camp);
if (finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateCampaign", "UPDATE", "Update Campaign : " + c, request);
}
if (parameter != null) {
JSONArray parameters = new JSONArray(parameter);
ICampaignParameterService campaignParameterService = appContext.getBean(ICampaignParameterService.class);
IFactoryCampaignParameter factoryCampaignParameter = appContext.getBean(IFactoryCampaignParameter.class);
ArrayList<CampaignParameter> arr = new ArrayList<>();
for (int i = 0; i < parameters.length(); i++) {
JSONArray bat = parameters.getJSONArray(i);
CampaignParameter co = factoryCampaignParameter.create(0, bat.getString(0), bat.getString(2), bat.getString(3));
arr.add(co);
}
finalAnswer = campaignParameterService.compareListAndUpdateInsertDeleteElements(c, arr);
if (finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateCampaign", "UPDATE", "Update Campaign Parameter : " + camp.getCampaign(), request);
}
}
if (label != null) {
JSONArray labels = new JSONArray(label);
ICampaignLabelService campaignLabelService = appContext.getBean(ICampaignLabelService.class);
IFactoryCampaignLabel factoryCampaignLabel = appContext.getBean(IFactoryCampaignLabel.class);
ArrayList<CampaignLabel> arr = new ArrayList<>();
for (int i = 0; i < labels.length(); i++) {
JSONArray bat = labels.getJSONArray(i);
CampaignLabel co = factoryCampaignLabel.create(0, bat.getString(0), Integer.valueOf(bat.getString(2)), request.getRemoteUser(), null, request.getRemoteUser(), null);
arr.add(co);
}
finalAnswer = campaignLabelService.compareListAndUpdateInsertDeleteElements(c, arr);
if (finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateCampaign", "UPDATE", "Update Campaign Label : " + camp.getCampaign(), request);
}
}
}
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());
response.getWriter().print(jsonResponse);
response.getWriter().flush();
}
Aggregations