/*
   * (non-Javadoc)
   *
   * @see com.atcloud.model.ModelService#createFEM()
   */
  @Override
  public FEM createFEM(final String name) {

    FEM fem = new FEM();

    fem.setFemID(UUID.randomUUID().toString());
    fem.setName(name);
    fem.setLogicalStrtTimeMSecs(Calendar.getInstance().getTimeInMillis());
    fem.setAutoStart(true);
    fem.setDefDurStrtupPrtclMSecs(10000L);
    fem.setFederationExecutionMSecs(3600000L); // duration of 1 hour
    fem.setJoinFederationMSecs(10000L);
    fem.setRegisterPublicationMSecs(10000L);
    fem.setRegisterSubscriptionMSecs(10000L);
    fem.setRegisterToRunMSecs(10000L);
    fem.setWaitForStartMSecs(10000L);
    fem.setWaitTimeAfterTermMSecs(1L); // recycles within 1 ms

    return fem;
  }
  /*
   * (non-Javadoc)
   *
   * @see com.csc.muthur.server.eventserver.internal.IEventHandler#handle()
   */
  @Override
  public final void handle() throws MuthurException {

    LOG.debug("Handling list federation execution models request...");

    validateServiceReferences();

    /*
     * Initialize the request
     */
    ListFedExecModelsRequest request = new ListFedExecModelsRequest();

    request.initialization(getEvent().serialize());

    try {

      FEM fem = null;

      fem = federationExecutionModelService.getActiveFEMForGroup(request.getGroupName());

      if (fem == null) {
        throw new MuthurException(
            "Unable to find the federation execution "
                + "model for group ["
                + request.getGroupName()
                + "]");
      }

      FederationExecutionModel federationExecutionModel = new FederationExecutionModel();

      /*
       * Copy the FEM to the FederationExecutionModel
       */

      federationExecutionModel.setName(fem.getName());
      federationExecutionModel.setDescription(fem.getDescription());
      federationExecutionModel.setFedExecModelUUID(fem.getFemID());
      federationExecutionModel.setLogicalStartTimeMSecs(fem.getLogicalStrtTimeMSecs());
      federationExecutionModel.setAutoStart(fem.isAutoStart());
      federationExecutionModel.setDefaultDurationWithinStartupProtocolMSecs(
          fem.getDefDurStrtupPrtclMSecs());
      federationExecutionModel.setDurationFederationExecutionMSecs(
          fem.getFederationExecutionMSecs());
      federationExecutionModel.setDurationJoinFederationMSecs(fem.getJoinFederationMSecs());
      federationExecutionModel.setDurationRegisterPublicationMSecs(
          fem.getRegisterPublicationMSecs());
      federationExecutionModel.setDurationRegisterSubscriptionMSecs(
          fem.getRegisterSubscriptionMSecs());
      federationExecutionModel.setDurationRegisterToRunMSecs(fem.getRegisterToRunMSecs());
      federationExecutionModel.setDurationWaitForStartFederationDirectiveMSecs(
          fem.getWaitForStartMSecs());
      federationExecutionModel.setDurationTimeToWaitAfterTerminationMSecs(
          fem.getWaitTimeAfterTermMSecs());

      List<Federate> federates = null;

      /*
       * Get all federates associated with this FEM and add to the
       * FederationExecutionModel
       */

      federates = federationExecutionModelService.getFederatesInFEM(fem.getName());

      if ((federates != null) && (federates.size() > 0)) {
        for (Federate nextFederate : federates) {
          if (nextFederate != null) {
            federationExecutionModel.addRequiredFededrate(nextFederate.getName());
          }
        }
      }

      /*
       * Initialize the response to the event
       */
      ListFedExecModelsResponse rr = new ListFedExecModelsResponse();

      rr.initialization(getEvent().serialize());

      rr.addFEM(federationExecutionModel);

      rr.setStatus("complete");
      rr.setSuccess(true);

      EventMessageResponseDispatcher.sendResponse(rr, getMessage());

    } catch (ATCloudException | MuthurException e) {

      LOG.error("Error listing federation execution models - [" + e.getLocalizedMessage() + "]");

      ListFedExecModelsResponse errorResponse = new ListFedExecModelsResponse();

      errorResponse.initialization(getEvent().serialize());

      errorResponse.setStatus("complete");
      errorResponse.setSuccess(false);
      errorResponse.setErrorDescription(e.getLocalizedMessage());

      EventMessageResponseDispatcher.sendResponse(errorResponse, getMessage());
    }
  }