use of org.cerberus.crud.entity.Invariant in project cerberus-source by cerberustesting.
the class FactoryInvariant method create.
@Override
public Invariant create(String idName, String value, Integer sort, String description, String veryShortDesc, String gp1, String gp2, String gp3, String gp4, String gp5, String gp6, String gp7, String gp8, String gp9) {
Invariant invariant = new Invariant();
invariant.setIdName(idName);
invariant.setSort(sort);
invariant.setValue(value);
invariant.setDescription(description);
invariant.setVeryShortDesc(veryShortDesc);
invariant.setGp1(gp1);
invariant.setGp2(gp2);
invariant.setGp3(gp3);
invariant.setGp4(gp4);
invariant.setGp5(gp5);
invariant.setGp6(gp6);
invariant.setGp7(gp7);
invariant.setGp8(gp8);
invariant.setGp9(gp9);
return invariant;
}
use of org.cerberus.crud.entity.Invariant in project cerberus-source by cerberustesting.
the class ExecutionStartService method startExecution.
@Override
public TestCaseExecution startExecution(TestCaseExecution tCExecution) throws CerberusException {
/**
* Start timestamp.
*/
long executionStart = new Date().getTime();
LOG.debug("Initializing Start Timestamp : " + executionStart);
tCExecution.setStart(executionStart);
/**
* Checking the parameters.
*/
LOG.debug("Checking the parameters.");
Invariant myInvariant;
try {
myInvariant = invariantService.convert(invariantService.readByKey("OUTPUTFORMAT", tCExecution.getOutputFormat()));
} catch (CerberusException ex) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_OUTPUTFORMAT_INVALID);
mes.setDescription(mes.getDescription().replace("%PARAM%", tCExecution.getOutputFormat()));
LOG.debug(mes.getDescription());
throw new CerberusException(mes);
}
try {
myInvariant = invariantService.convert(invariantService.readByKey("VERBOSE", String.valueOf(tCExecution.getVerbose())));
} catch (CerberusException ex) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_VERBOSE_INVALID);
mes.setDescription(mes.getDescription().replace("%PARAM%", String.valueOf(tCExecution.getVerbose())));
LOG.debug(mes.getDescription());
throw new CerberusException(mes);
}
try {
myInvariant = invariantService.convert(invariantService.readByKey("SCREENSHOT", String.valueOf(tCExecution.getScreenshot())));
} catch (CerberusException ex) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_SCREENSHOT_INVALID);
mes.setDescription(mes.getDescription().replace("%PARAM%", String.valueOf(tCExecution.getScreenshot())));
LOG.debug(mes.getDescription());
throw new CerberusException(mes);
}
LOG.debug("Parameters checked.");
/**
* Load TestCase information and set TCase to the TestCaseExecution
* object.
*/
tCExecution.setResultMessage(new MessageGeneral(MessageGeneralEnum.EXECUTION_PE_LOADINGDATA));
LOG.debug("Loading Test Case Information. " + tCExecution.getTest() + "-" + tCExecution.getTestCase());
// Integrate this.loadTestCaseService.loadTestCase(tCExecution); inside with Dependency.
try {
// TestCase tCase = testCaseService.findTestCaseByKey(tCExecution.getTest(), tCExecution.getTestCase());
TestCase tCase = testCaseService.convert(testCaseService.readByKey(tCExecution.getTest(), tCExecution.getTestCase()));
if (tCase != null) {
tCExecution.setTestCaseObj(tCase);
tCExecution.setDescription(tCase.getDescription());
tCExecution.setConditionOper(tCase.getConditionOper());
tCExecution.setConditionVal1(tCase.getConditionVal1());
tCExecution.setConditionVal1Init(tCase.getConditionVal1());
tCExecution.setConditionVal2(tCase.getConditionVal2());
tCExecution.setConditionVal2Init(tCase.getConditionVal2());
tCExecution.setTestCaseVersion(tCase.getTestCaseVersion());
} else {
throw new CerberusException(new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND));
}
/**
* Copy the status of the testcase to the status column of the
* Execution. This is done to know how stable was the testcase at
* the time of the execution.
*/
tCExecution.setStatus(tCase.getStatus());
} catch (CerberusException ex) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_TESTCASE_NOT_FOUND);
mes.setDescription(mes.getDescription().replace("%TEST%", tCExecution.getTest()));
mes.setDescription(mes.getDescription().replace("%TESTCASE%", tCExecution.getTestCase()));
LOG.debug(mes.getDescription());
throw new CerberusException(mes);
}
LOG.debug("Test Case Information Loaded - " + tCExecution.getTest() + "-" + tCExecution.getTestCase());
/**
* Load Test information and Set TestObject to the TestCaseExecution
* object.
*/
LOG.debug("Loading Test Information");
try {
tCExecution.setTestObj(this.testService.convert(this.testService.readByKey(tCExecution.getTest())));
} catch (CerberusException ex) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_TEST_NOT_FOUND);
mes.setDescription(mes.getDescription().replace("%TEST%", tCExecution.getTest()));
LOG.debug(mes.getDescription());
throw new CerberusException(mes);
}
LOG.debug("Test Information Loaded - " + tCExecution.getTest());
/**
* Load Application information and Set Application to the
* TestCaseExecution object.
*/
LOG.debug("Loading Application Information");
try {
tCExecution.setApplication(tCExecution.getTestCaseObj().getApplication());
tCExecution.setApplicationObj(applicationService.convert(this.applicationService.readByKey(tCExecution.getTestCaseObj().getApplication())));
} catch (CerberusException ex) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_APPLICATION_NOT_FOUND);
mes.setDescription(mes.getDescription().replace("%APPLI%", tCExecution.getTestCaseObj().getApplication()));
LOG.debug(mes.getDescription());
throw new CerberusException(mes);
}
LOG.debug("Application Information Loaded - " + tCExecution.getApplicationObj().getApplication() + " - " + tCExecution.getApplicationObj().getDescription());
/**
* Init System from Application.
*/
tCExecution.setSystem(tCExecution.getApplicationObj().getSystem());
/**
* Load Country information and Set it to the TestCaseExecution object.
*/
LOG.debug("Loading Country Information");
try {
tCExecution.setCountryObj(invariantService.convert(invariantService.readByKey("COUNTRY", tCExecution.getCountry())));
} catch (CerberusException ex) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_COUNTRY_NOT_FOUND);
mes.setDescription(mes.getDescription().replace("%COUNTRY%", tCExecution.getCountry()));
LOG.debug(mes.getDescription());
throw new CerberusException(mes);
}
LOG.debug("Country Information Loaded - " + tCExecution.getCountryObj().getValue() + " - " + tCExecution.getCountryObj().getDescription());
/**
* Checking if execution is manual or automaticaly configured. If
* Manual, CountryEnvironmentParameters object is manually created with
* the servlet parameters. If automatic, parameters are build from the
* CountryEnvironmentParameters. table in the database. Environmentdata
* will always be filled with the environment. Environment will be
* forced to MANUAL if execution is manual.
*/
LOG.debug("Checking if connectivity parameters are manual or automatic from the database. '" + tCExecution.isManualURL() + "'");
if (tCExecution.isManualURL()) {
LOG.debug("Execution will be done with manual application connectivity setting.");
if (StringUtil.isNullOrEmpty(tCExecution.getMyHost())) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_MANUALURL_INVALID);
LOG.debug(mes.getDescription());
throw new CerberusException(mes);
} else {
CountryEnvironmentParameters cea;
cea = this.factorycountryEnvironmentParameters.create(tCExecution.getApplicationObj().getSystem(), tCExecution.getCountry(), tCExecution.getEnvironment(), tCExecution.getApplicationObj().getApplication(), tCExecution.getMyHost(), "", tCExecution.getMyContextRoot(), tCExecution.getMyLoginRelativeURL(), "", "", "", "", CountryEnvironmentParameters.DEFAULT_POOLSIZE);
cea.setIp(tCExecution.getMyHost());
cea.setUrl(tCExecution.getMyContextRoot());
tCExecution.setUrl(StringUtil.getURLFromString(cea.getIp(), cea.getUrl(), "", ""));
cea.setUrlLogin(tCExecution.getMyLoginRelativeURL());
tCExecution.setCountryEnvironmentParameters(cea);
LOG.debug(" -> Execution will be done with manual application connectivity setting. IP/URL/LOGIN : " + cea.getIp() + "-" + cea.getUrl() + "-" + cea.getUrlLogin());
}
/**
* If execution is manual, we force the env at 'MANUAL-ENVDATA'
* string. We keep envData information in order to trace the env
* data that has been used.
*/
tCExecution.setEnvironment("MANUAL-" + tCExecution.getEnvironmentData());
} else {
/**
* Automatic application configuration execution.
*/
LOG.debug("Execution will be done with automatic application connectivity setting.");
/**
* Load Country/Environment/Application information and set them to
* the TestCaseExecution object
*/
LOG.debug("Loading Country/Environment/Application Information. " + tCExecution.getCountry() + "-" + tCExecution.getEnvironment() + "-" + tCExecution.getApplicationObj().getApplication());
CountryEnvironmentParameters cea;
try {
cea = this.countryEnvironmentParametersService.convert(this.countryEnvironmentParametersService.readByKey(tCExecution.getApplicationObj().getSystem(), tCExecution.getCountry(), tCExecution.getEnvironment(), tCExecution.getApplicationObj().getApplication()));
if (cea != null) {
tCExecution.setCountryEnvironmentParameters(cea);
// tCExecution.setUrl(cea.getIp()+ cea.getUrl());
tCExecution.setUrl(StringUtil.getURLFromString(cea.getIp(), cea.getUrl(), "", ""));
} else {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_COUNTRYENVAPP_NOT_FOUND);
mes.setDescription(mes.getDescription().replace("%COUNTRY%", tCExecution.getCountry()));
mes.setDescription(mes.getDescription().replace("%ENV%", tCExecution.getEnvironment()));
mes.setDescription(mes.getDescription().replace("%APPLI%", tCExecution.getTestCaseObj().getApplication()));
LOG.error(mes.getDescription());
throw new CerberusException(mes);
}
/**
* Forcing the IP URL and Login config from DevIP, DevURL and
* DevLogin parameter only if DevURL is defined.
*/
} catch (CerberusException ex) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_COUNTRYENVAPP_NOT_FOUND);
mes.setDescription(mes.getDescription().replace("%COUNTRY%", tCExecution.getCountry()));
mes.setDescription(mes.getDescription().replace("%ENV%", tCExecution.getEnvironment()));
mes.setDescription(mes.getDescription().replace("%APPLI%", tCExecution.getTestCaseObj().getApplication()));
LOG.error(mes.getDescription());
throw new CerberusException(mes);
}
LOG.debug("Country/Environment/Application Information Loaded. " + tCExecution.getCountry() + " - " + tCExecution.getEnvironment() + " - " + tCExecution.getApplicationObj().getApplication());
LOG.debug(" -> Execution will be done with automatic application connectivity setting. IP/URL/LOGIN : " + cea.getIp() + "-" + cea.getUrl() + "-" + cea.getUrlLogin());
tCExecution.setEnvironmentData(tCExecution.getEnvironment());
}
/**
* Load Environment object from invariant table.
*/
LOG.debug("Loading Environment Information. " + tCExecution.getEnvironmentData());
try {
tCExecution.setEnvironmentDataObj(invariantService.convert(invariantService.readByKey("ENVIRONMENT", tCExecution.getEnvironmentData())));
} catch (CerberusException ex) {
if (tCExecution.isManualURL()) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_ENVIRONMENT_DOESNOTEXIST_MAN);
mes.setDescription(mes.getDescription().replace("%ENV%", tCExecution.getEnvironmentData()));
LOG.debug(mes.getDescription());
throw new CerberusException(mes);
} else {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_ENVIRONMENT_DOESNOTEXIST);
mes.setDescription(mes.getDescription().replace("%ENV%", tCExecution.getEnvironmentData()));
LOG.debug(mes.getDescription());
throw new CerberusException(mes);
}
}
LOG.debug("Environment Information Loaded");
/**
* Load Country/Environment information and set them to the
* TestCaseExecution object. Environment considered here is the data
* environment.
*/
LOG.debug("Loading Country/Environment Information. " + tCExecution.getCountry() + " - " + tCExecution.getEnvironmentData());
CountryEnvParam countEnvParam;
try {
countEnvParam = this.countryEnvParamService.convert(this.countryEnvParamService.readByKey(tCExecution.getApplicationObj().getSystem(), tCExecution.getCountry(), tCExecution.getEnvironmentData()));
tCExecution.setCountryEnvParam(countEnvParam);
/**
* Copy the Build/Revision of the environment to the Execution. This
* is done to keep track of all execution done on a specific version
* of system
*/
tCExecution.setBuild(countEnvParam.getBuild());
tCExecution.setRevision(countEnvParam.getRevision());
} catch (CerberusException ex) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_COUNTRYENV_NOT_FOUND);
mes.setDescription(mes.getDescription().replace("%SYSTEM%", tCExecution.getApplicationObj().getSystem()));
mes.setDescription(mes.getDescription().replace("%COUNTRY%", tCExecution.getCountry()));
mes.setDescription(mes.getDescription().replace("%ENV%", tCExecution.getEnvironmentData()));
LOG.debug(mes.getDescription());
throw new CerberusException(mes);
}
LOG.debug("Country/Environment Information Loaded. " + tCExecution.getCountry() + " - " + tCExecution.getEnvironmentData());
/**
* If Timeout is defined at the execution level, set action wait default
* to this value, else Get the cerberus_action_wait_default parameter.
* This parameter will be used by tha wait action if no timeout/event is
* defined.
*/
try {
if (!tCExecution.getTimeout().isEmpty()) {
tCExecution.setCerberus_action_wait_default(Integer.valueOf(tCExecution.getTimeout()));
} else {
tCExecution.setCerberus_action_wait_default(parameterService.getParameterIntegerByKey("cerberus_action_wait_default", tCExecution.getApplicationObj().getSystem(), 90000));
}
} catch (NumberFormatException ex) {
LOG.warn("Parameter cerberus_action_wait_default must be an integer, default value set to 90000 milliseconds. " + ex.toString());
tCExecution.setCerberus_action_wait_default(90000);
}
/**
* Check if test can be executed TODO : Replace Message with try/catch
* cerberus exception
*/
tCExecution.setResultMessage(new MessageGeneral(MessageGeneralEnum.EXECUTION_PE_VALIDATIONSTARTING));
LOG.debug("Performing the Checks before starting the execution");
MessageGeneral canExecuteTestCase = this.executionCheckService.checkTestCaseExecution(tCExecution);
tCExecution.setResultMessage(canExecuteTestCase);
/**
* We stop if the result is not OK
*/
if (!(tCExecution.getResultMessage().equals(new MessageGeneral(MessageGeneralEnum.EXECUTION_PE_CHECKINGPARAMETERS)))) {
return tCExecution;
}
LOG.debug("Checks performed -- > OK to continue.");
/**
* For GUI application, check if Browser is supported.
*/
if (!tCExecution.getManualExecution().equals("Y") && tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_GUI)) {
try {
myInvariant = invariantService.convert(invariantService.readByKey("BROWSER", tCExecution.getBrowser()));
} catch (CerberusException ex) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_BROWSER_NOT_SUPPORTED);
mes.setDescription(mes.getDescription().replace("%BROWSER%", tCExecution.getBrowser()));
LOG.debug(mes.getDescription());
throw new CerberusException(mes);
}
}
/**
* Start server if execution is not manual
*/
if (!tCExecution.getManualExecution().equals("Y")) {
tCExecution.setResultMessage(new MessageGeneral(MessageGeneralEnum.EXECUTION_PE_STARTINGROBOTSERVER));
if (tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_GUI) || tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_APK) || tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_IPA) || tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_FAT)) {
if (tCExecution.getIp().equalsIgnoreCase("")) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_SELENIUM_EMPTYORBADIP);
mes.setDescription(mes.getDescription().replace("%IP%", tCExecution.getIp()));
LOG.debug(mes.getDescription());
throw new CerberusException(mes);
}
/**
* Start Selenium server
*/
LOG.debug("Starting Server.");
tCExecution.setResultMessage(new MessageGeneral(MessageGeneralEnum.EXECUTION_PE_CREATINGRUNID));
try {
this.serverService.startServer(tCExecution);
} catch (CerberusException ex) {
LOG.debug(ex.getMessageError().getDescription());
throw new CerberusException(ex.getMessageError());
}
LOG.debug("Server Started.");
}
}
/**
* Register RunID inside database.
*/
tCExecution.setResultMessage(new MessageGeneral(MessageGeneralEnum.EXECUTION_PE_CREATINGRUNID));
LOG.debug("Registering Execution ID on database");
long runID = 0;
try {
runID = this.testCaseExecutionService.registerRunID(tCExecution);
if (runID != 0) {
tCExecution.setId(runID);
executionUUIDObject.setExecutionUUID(tCExecution.getExecutionUUID(), tCExecution);
// Update Queue Execution here if QueueID =! 0.
if (tCExecution.getQueueID() != 0) {
inQueueService.updateToExecuting(tCExecution.getQueueID(), "", runID);
}
} else {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_COULDNOTCREATE_RUNID);
tCExecution.setResultMessage(mes);
LOG.fatal("Could not create RunID, or cannot retreive the generated Key");
throw new CerberusException(mes);
}
} catch (CerberusException ex) {
tCExecution.setResultMessage(new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_COULDNOTCREATE_RUNID));
LOG.warn(ex.getMessageError().getDescription());
throw new CerberusException(ex.getMessageError());
}
LOG.debug("Execution ID registered on database : " + tCExecution.getId());
/**
* Stop the browser if executionID is equal to zero (to prevent database
* instabilities)
*/
if (!tCExecution.getManualExecution().equals("Y")) {
try {
if (tCExecution.getId() == 0) {
LOG.debug("Starting to Stop the Selenium Server.");
this.serverService.stopServer(tCExecution.getSession());
LOG.debug("Selenium Server stopped.");
}
} catch (Exception ex) {
LOG.warn(ex.toString());
}
}
/**
* Feature Flipping. Should be removed when websocket push is fully
* working
*/
tCExecution.setCerberus_featureflipping_activatewebsocketpush(parameterService.getParameterBooleanByKey("cerberus_featureflipping_activatewebsocketpush", "", false));
tCExecution.setCerberus_featureflipping_websocketpushperiod(parameterService.getParameterLongByKey("cerberus_featureflipping_websocketpushperiod", "", 5000));
return tCExecution;
}
use of org.cerberus.crud.entity.Invariant in project cerberus-source by cerberustesting.
the class SeleniumServerService method setCapabilityBrowser.
/**
* Instanciate DesiredCapabilities regarding the browser
*
* @param capabilities
* @param browser
* @param tCExecution
* @return
* @throws CerberusException
*/
private DesiredCapabilities setCapabilityBrowser(DesiredCapabilities capabilities, String browser, TestCaseExecution tCExecution) throws CerberusException {
try {
if (browser.equalsIgnoreCase("firefox")) {
capabilities = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("app.update.enabled", false);
try {
Invariant invariant = invariantService.convert(invariantService.readByKey("COUNTRY", tCExecution.getCountry()));
if (invariant.getGp2() == null) {
LOG.warn("Country selected (" + tCExecution.getCountry() + ") has no value of GP2 in Invariant table, default language set to English (en)");
profile.setPreference("intl.accept_languages", "en");
} else {
profile.setPreference("intl.accept_languages", invariant.getGp2());
}
} catch (CerberusException ex) {
LOG.warn("Country selected (" + tCExecution.getCountry() + ") not in Invariant table, default language set to English (en)");
profile.setPreference("intl.accept_languages", "en");
}
// Set UserAgent if testCaseUserAgent or robotUserAgent is defined
String usedUserAgent = getUserAgentToUse(tCExecution.getTestCaseObj().getUserAgent(), tCExecution.getUserAgent());
if (!StringUtil.isNullOrEmpty(usedUserAgent)) {
profile.setPreference("general.useragent.override", usedUserAgent);
}
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
} else if (browser.equalsIgnoreCase("IE")) {
capabilities = DesiredCapabilities.internetExplorer();
} else if (browser.equalsIgnoreCase("chrome")) {
capabilities = DesiredCapabilities.chrome();
/**
* Add custom capabilities
*/
ChromeOptions options = new ChromeOptions();
// Maximize windows for chrome browser
options.addArguments("--start-fullscreen");
// Set UserAgent if necessary
String usedUserAgent = getUserAgentToUse(tCExecution.getTestCaseObj().getUserAgent(), tCExecution.getUserAgent());
if (!StringUtil.isNullOrEmpty(usedUserAgent)) {
options.addArguments("--user-agent=" + usedUserAgent);
}
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
} else if (browser.contains("android")) {
capabilities = DesiredCapabilities.android();
} else if (browser.contains("ipad")) {
capabilities = DesiredCapabilities.ipad();
} else if (browser.contains("iphone")) {
capabilities = DesiredCapabilities.iphone();
} else if (browser.contains("safari")) {
capabilities = DesiredCapabilities.safari();
} else {
LOG.warn("Not supported Browser : " + browser);
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.EXECUTION_FA_SELENIUM);
mes.setDescription(mes.getDescription().replace("%MES%", "Browser '" + browser + "' is not supported"));
mes.setDescription("Not supported Browser : " + browser);
throw new CerberusException(mes);
}
} catch (CerberusException ex) {
MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.EXECUTION_FA_SELENIUM);
mes.setDescription(mes.getDescription().replace("%MES%", "Failed to set capability on the browser '" + browser + "' due to " + ex.getMessageError().getDescription()));
throw new CerberusException(mes);
}
return capabilities;
}
use of org.cerberus.crud.entity.Invariant in project cerberus-source by cerberustesting.
the class InvariantDAO method readByCriteria.
@Override
public AnswerList readByCriteria(int start, int amount, String column, String dir, String searchTerm, String individualSearch, String PublicPrivateFilter) {
List<Invariant> invariantList = new ArrayList<>();
AnswerList answer = new AnswerList();
MessageEvent msg;
StringBuilder searchSQL = new StringBuilder();
searchSQL.append(" where 1=1 ");
StringBuilder query = new StringBuilder();
query.append("SELECT SQL_CALC_FOUND_ROWS * FROM invariant ");
if (!searchTerm.equals("") && !individualSearch.equals("")) {
searchSQL.append(" and ");
searchSQL.append(getSearchString(searchTerm));
searchSQL.append(" and ");
searchSQL.append(individualSearch);
} else if (!individualSearch.equals("")) {
searchSQL.append(" and `");
searchSQL.append(individualSearch);
searchSQL.append("`");
} else if (!searchTerm.equals("")) {
searchSQL.append(" and ");
searchSQL.append(getSearchString(searchTerm));
}
if (!(PublicPrivateFilter.equalsIgnoreCase(""))) {
searchSQL.append(" and ");
searchSQL.append(PublicPrivateFilter);
}
query.append(searchSQL);
query.append(" order by `");
query.append(column);
query.append("` ");
query.append(dir);
int nrTotalRows = 0;
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);
}
Connection connection = this.databaseSpring.connect();
try {
PreparedStatement preStat = connection.prepareStatement(query.toString());
try {
ResultSet resultSet = preStat.executeQuery();
try {
while (resultSet.next()) {
invariantList.add(this.loadFromResultSet(resultSet));
}
// get the total number of rows
resultSet = preStat.executeQuery("SELECT FOUND_ROWS()");
if (resultSet != null && resultSet.next()) {
nrTotalRows = resultSet.getInt(1);
}
if (invariantList.isEmpty()) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
}
} 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%", exception.toString()));
invariantList.clear();
} finally {
if (resultSet != null) {
resultSet.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%", exception.toString()));
} 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%", exception.toString()));
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
LOG.warn(e.toString());
}
}
answer.setResultMessage(msg);
answer.setTotalRows(nrTotalRows);
answer.setDataList(invariantList);
return answer;
}
use of org.cerberus.crud.entity.Invariant in project cerberus-source by cerberustesting.
the class InvariantDAO method readByCriteria.
@Override
public AnswerList readByCriteria(int start, int amount, String column, String dir, String searchTerm, Map<String, List<String>> individualSearch, String PublicPrivateFilter) {
List<Invariant> invariantList = new ArrayList<>();
AnswerList answer = new AnswerList();
List<String> individalColumnSearchValues = new ArrayList<>();
MessageEvent msg;
StringBuilder searchSQL = new StringBuilder();
searchSQL.append(" where 1=1 ");
StringBuilder query = new StringBuilder();
query.append("SELECT SQL_CALC_FOUND_ROWS * FROM invariant ");
if (!Strings.isNullOrEmpty(searchTerm)) {
searchSQL.append(" and (idname like ? or value like ? or sort like ? or description like ? or VeryShortDesc like ? or gp1 like ? or gp2 like ? or gp3 like ?)");
}
if (!Strings.isNullOrEmpty(PublicPrivateFilter)) {
searchSQL.append(" and ");
searchSQL.append(PublicPrivateFilter);
}
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);
query.append(" order by `");
query.append(column);
query.append("` ");
query.append(dir);
int nrTotalRows = 0;
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);
}
Connection connection = this.databaseSpring.connect();
try {
PreparedStatement preStat = connection.prepareStatement(query.toString());
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 + "%");
}
for (String individualColumnSearchValue : individalColumnSearchValues) {
preStat.setString(i++, individualColumnSearchValue);
}
try {
ResultSet resultSet = preStat.executeQuery();
try {
while (resultSet.next()) {
invariantList.add(this.loadFromResultSet(resultSet));
}
// get the total number of rows
resultSet = preStat.executeQuery("SELECT FOUND_ROWS()");
if (resultSet != null && resultSet.next()) {
nrTotalRows = resultSet.getInt(1);
}
if (invariantList.isEmpty()) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
}
} 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%", exception.toString()));
invariantList.clear();
} finally {
if (resultSet != null) {
resultSet.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%", exception.toString()));
} 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%", exception.toString()));
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
LOG.warn(e.toString());
}
}
answer.setResultMessage(msg);
answer.setTotalRows(nrTotalRows);
answer.setDataList(invariantList);
return answer;
}
Aggregations