// Disable Heartbeat
try {
if (sysInfo.isEnableHeartbeat() != null && sysInfo.isEnableHeartbeat()) {
// Sending beat
ProcessBundle beat = new ProcessBundle(HB_Process_ID, vars).init(this);
new ProcessRunner(beat).execute(this);
}
// Deactivating the process at SystemInfo
sysInfo.setEnableHeartbeat(false);
sysInfo.setTestHeartbeat("N");
OBDal.getInstance().save(sysInfo);
// Un-scheduling the process
final OBCriteria<ProcessRequest> prCriteria = OBDal.getInstance().createCriteria(
ProcessRequest.class);
prCriteria.add(Expression.eq(ProcessRequest.PROPERTY_PROCESS, HBProcess));
prCriteria
.add(Expression.eq(ProcessRequest.PROPERTY_CHANNEL, Channel.SCHEDULED.toString()));
final List<ProcessRequest> requestList = prCriteria.list();
if (requestList.size() != 0) {
final ProcessRequest pr = requestList.get(0);
OBDal.getInstance().save(pr);
OBDal.getInstance().commitAndClose();
final ProcessBundle bundle = ProcessBundle.request(pr.getId(), vars, this);
OBScheduler.getInstance().unschedule(pr.getId(), bundle.getContext());
}
String msg = Utility.messageBD(this, "HB_SUCCESS", vars.getLanguage());
advisePopUpRefresh(request, response, "SUCCESS", "Heartbeat Configuration", msg);
} catch (Exception e) {
log4j.error(e.getMessage(), e);
advisePopUpRefresh(request, response, "ERROR", "Heartbeat Configuration", e.getMessage());
}
} else { // Enable Heartbeat
try {
HBProcess.setActive(true);
OBDal.getInstance().save(HBProcess);
OBDal.getInstance().commitAndClose();
// Sending beat
ProcessBundle bundle = new ProcessBundle(HB_Process_ID, vars).init(this);
final String beatExecutionId = new ProcessRunner(bundle).execute(this);
// Getting beat result
final ProcessRun result = OBDal.getInstance().get(ProcessRun.class, beatExecutionId);
if (result.getStatus().equals("ERR")) {
// Restoring not active state
sysInfo.setEnableHeartbeat(false);
sysInfo.setTestHeartbeat("N");
OBDal.getInstance().save(sysInfo);
OBDal.getInstance().commitAndClose();
String msg = Utility.messageBD(this, "HB_INTERNAL_ERROR", vars.getLanguage());
// Extracting the last line from the log
String log = result.getLog().substring(0, result.getLog().lastIndexOf("\n"));
log = log.substring(log.lastIndexOf("\n"));
msg += "\n" + log;
msg = Utility.formatMessageBDToHtml(msg);
if (vars.commandIn("CONFIGURE", "CONFIGURE_MODULE")) {
OBError err = new OBError();
err.setType("Error");
err.setMessage(msg);
vars.setMessage(HB_tabId, err);
printPageRedirect(response, vars);
} else {
advisePopUpRefresh(request, response, "ERROR", "Heartbeat Configuration", msg);
}
return;
}
// Scheduling the process
final OBCriteria<ProcessRequest> prCriteria = OBDal.getInstance().createCriteria(
ProcessRequest.class);
prCriteria.add(Expression.eq(ProcessRequest.PROPERTY_PROCESS, HBProcess));
prCriteria
.add(Expression.eq(ProcessRequest.PROPERTY_CHANNEL, Channel.SCHEDULED.toString()));
final List<ProcessRequest> requestList = prCriteria.list();
ProcessRequest pr = null;
if (requestList.size() == 0) { // Creating a process request
pr = OBProvider.getInstance().get(ProcessRequest.class);
pr.setProcess(HBProcess);
pr.setActive(true);
final ProcessContext context = new ProcessContext(vars);
pr.setOpenbravoContext(context.toString());
} else {
pr = requestList.get(0);
}
// Schedule the next beat in 7 days
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.DATE, 7);
pr.setStartDate(c1.getTime());
// At today's same time
pr.setStartTime(new Timestamp(Calendar.getInstance().getTimeInMillis()));
pr.setSecurityBasedOnRole(true);
pr.setDailyOption(EVERY_N_DAYS);
pr.setDailyInterval(Long.parseLong("7"));
pr.setTiming(SCHEDULE);
OBDal.getInstance().save(pr);
// SQLC uses a different connection
OBDal.getInstance().commitAndClose();
final ProcessBundle bundle2 = ProcessBundle.request(pr.getId(), vars, this);
if (requestList.size() == 0) {
OBScheduler.getInstance().schedule(pr.getId(), bundle2);
} else {
OBScheduler.getInstance().reschedule(pr.getId(), bundle2);
}