use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.
the class ProjectDAO method create.
@Override
public Answer create(Project project) {
MessageEvent msg = null;
StringBuilder query = new StringBuilder();
query.append("INSERT INTO project (`idproject`, `VCCode`, `Description`, `active` ) ");
query.append("VALUES (?,?,?,?)");
// 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 {
preStat.setString(1, project.getIdProject());
preStat.setString(2, project.getCode());
preStat.setString(3, project.getDescription());
preStat.setString(4, project.getActive());
preStat.executeUpdate();
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT"));
} catch (SQLException exception) {
LOG.error("Unable to execute query : " + exception.toString());
if (exception.getSQLState().equals(SQL_DUPLICATED_CODE)) {
// 23000 is the sql state for duplicate entries
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_DUPLICATE);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT").replace("%REASON%", exception.toString()));
} else {
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());
}
}
return new Answer(msg);
}
use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.
the class RobotCapabilityDAO method create.
@Override
public Answer create(RobotCapability capability) {
Answer ans = new Answer();
MessageEvent msg = null;
LOG.debug(Query.CREATE);
try (Connection connection = databaseSpring.connect();
PreparedStatement preStat = connection.prepareStatement(Query.CREATE)) {
// Prepare and execute query
preStat.setString(1, capability.getRobot());
preStat.setString(2, capability.getCapability());
preStat.setString(3, capability.getValue());
preStat.executeUpdate();
// Set the final message
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK).resolveDescription("ITEM", OBJECT_NAME).resolveDescription("OPERATION", "CREATE");
} catch (Exception e) {
LOG.warn("Unable to create robot capability: " + e.getMessage());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
} finally {
ans.setResultMessage(msg);
}
return ans;
}
use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.
the class RobotCapabilityDAO method update.
@Override
public Answer update(RobotCapability capability) {
Answer ans = new Answer();
MessageEvent msg = null;
LOG.debug(Query.UPDATE);
try (Connection connection = databaseSpring.connect();
PreparedStatement preStat = connection.prepareStatement(Query.UPDATE)) {
// Prepare and execute query
preStat.setString(1, capability.getValue());
preStat.setString(2, capability.getRobot());
preStat.setString(3, capability.getCapability());
preStat.executeUpdate();
// Set the final message
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK).resolveDescription("ITEM", OBJECT_NAME).resolveDescription("OPERATION", "UPDATE");
} catch (Exception e) {
LOG.warn("Unable to update robot capability: " + e.getMessage());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
} finally {
ans.setResultMessage(msg);
}
return ans;
}
use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.
the class RobotDAO method delete.
@Override
public Answer delete(Robot application) {
MessageEvent msg = null;
final String query = "DELETE FROM robot WHERE robotID = ? ";
// Debug message on SQL.
if (LOG.isDebugEnabled()) {
LOG.debug("SQL : " + query);
}
Connection connection = this.databaseSpring.connect();
try {
PreparedStatement preStat = connection.prepareStatement(query);
try {
preStat.setInt(1, application.getRobotID());
preStat.executeUpdate();
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "DELETE"));
} 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());
}
}
return new Answer(msg);
}
use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.
the class RobotDAO method update.
@Override
public Answer update(Robot robot) {
MessageEvent msg = null;
StringBuilder query = new StringBuilder();
query.append("UPDATE robot SET robot= ? , host = ? , port = ? ,");
query.append("platform = ?, browser = ? , version = ?, active=?, description = ?, useragent = ?, screensize = ?, robotdecli = ?");
if (robot.getHostUser() != null) {
query.append(", host_user = ?");
}
if (robot.getHostPassword() != null) {
query.append(", host_password = ?");
}
query.append("WHERE robotID = ?");
// 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 cpt = 1;
preStat.setString(cpt++, robot.getRobot());
preStat.setString(cpt++, robot.getHost());
preStat.setString(cpt++, robot.getPort());
preStat.setString(cpt++, robot.getPlatform());
preStat.setString(cpt++, robot.getBrowser());
preStat.setString(cpt++, robot.getVersion());
preStat.setString(cpt++, robot.getActive());
preStat.setString(cpt++, robot.getDescription());
preStat.setString(cpt++, robot.getUserAgent());
preStat.setString(cpt++, robot.getScreenSize());
preStat.setString(cpt++, robot.getRobotDecli());
if (robot.getHostUser() != null) {
preStat.setString(cpt++, robot.getHostUser());
}
if (robot.getHostPassword() != null) {
preStat.setString(cpt++, robot.getHostPassword());
}
preStat.setInt(cpt++, robot.getRobotID());
preStat.executeUpdate();
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "UPDATE"));
} 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());
}
}
return new Answer(msg);
}
Aggregations