/* (non-Javadoc)
   * @see org.apache.oozie.command.XCommand#execute()
   */
  @Override
  protected String submit() throws CommandException {
    String jobId = null;
    LOG.info("STARTED Coordinator Submit");
    InstrumentUtils.incrJobCounter(getName(), 1, getInstrumentation());

    boolean exceptionOccured = false;
    try {
      mergeDefaultConfig();

      String appXml = readAndValidateXml();
      coordJob.setOrigJobXml(appXml);
      LOG.debug("jobXml after initial validation " + XmlUtils.prettyPrint(appXml).toString());

      String appNamespace = readAppNamespace(appXml);
      coordJob.setAppNamespace(appNamespace);

      appXml = XmlUtils.removeComments(appXml);
      initEvaluators();
      Element eJob = basicResolveAndIncludeDS(appXml, conf, coordJob);
      LOG.debug("jobXml after all validation " + XmlUtils.prettyPrint(eJob).toString());

      jobId = storeToDB(eJob, coordJob);
      // log job info for coordinator job
      LogUtils.setLogInfo(coordJob, logInfo);
      LOG = XLog.resetPrefix(LOG);

      if (!dryrun) {
        // submit a command to materialize jobs for the next 1 hour (3600 secs)
        // so we don't wait 10 mins for the Service to run.
        queue(new CoordMaterializeTransitionXCommand(jobId, 3600), 100);
      } else {
        Date startTime = coordJob.getStartTime();
        long startTimeMilli = startTime.getTime();
        long endTimeMilli = startTimeMilli + (3600 * 1000);
        Date jobEndTime = coordJob.getEndTime();
        Date endTime = new Date(endTimeMilli);
        if (endTime.compareTo(jobEndTime) > 0) {
          endTime = jobEndTime;
        }
        jobId = coordJob.getId();
        LOG.info("[" + jobId + "]: Update status to RUNNING");
        coordJob.setStatus(Job.Status.RUNNING);
        coordJob.setPending();
        CoordActionMaterializeCommand coordActionMatCom =
            new CoordActionMaterializeCommand(jobId, startTime, endTime);
        Configuration jobConf = null;
        try {
          jobConf = new XConfiguration(new StringReader(coordJob.getConf()));
        } catch (IOException e1) {
          LOG.warn("Configuration parse error. read from DB :" + coordJob.getConf(), e1);
        }
        String action = coordActionMatCom.materializeJobs(true, coordJob, jobConf, null);
        String output =
            coordJob.getJobXml()
                + System.getProperty("line.separator")
                + "***actions for instance***"
                + action;
        return output;
      }
    } catch (CoordinatorJobException cex) {
      exceptionOccured = true;
      LOG.warn("ERROR:  ", cex);
      throw new CommandException(cex);
    } catch (IllegalArgumentException iex) {
      exceptionOccured = true;
      LOG.warn("ERROR:  ", iex);
      throw new CommandException(ErrorCode.E1003, iex);
    } catch (Exception ex) {
      exceptionOccured = true;
      LOG.warn("ERROR:  ", ex);
      throw new CommandException(ErrorCode.E0803, ex);
    } finally {
      if (exceptionOccured) {
        if (coordJob.getId() == null || coordJob.getId().equalsIgnoreCase("")) {
          coordJob.setStatus(CoordinatorJob.Status.FAILED);
          coordJob.resetPending();
        }
      }
    }

    LOG.info("ENDED Coordinator Submit jobId=" + jobId);
    return jobId;
  }