public void create(EntityManager em, ObservingPlan elem) throws Exception {

    boolean localEm = false;

    try {

      if (em == null) {
        localEm = true;
        em = DBUtil.getEntityManager();
      }

      if (localEm) DBUtil.beginTransaction(em);

      em.persist(elem);

      if (localEm) DBUtil.commit(em);

    } catch (Exception ex) {
      ex.printStackTrace();
      LogUtil.severe(this, "Error creating an observing plan: " + ex.getMessage());
      if (localEm) DBUtil.rollback(em);
      throw ex;
    } finally {
      if (localEm) DBUtil.close(em);
    }
  }
  public long getCountByScheduleDate(EntityManager em, Date ini, Date end) throws Exception {

    boolean localEm = false;

    try {

      if (em == null) {
        localEm = true;
        em = DBUtil.getEntityManager();
      }

      DBUtil.beginTransaction(em);

      Query query =
          em.createQuery(
              "SELECT COUNT(op.id) FROM ObservingPlan op where scheduleDateIni>=?1 AND scheduleDateEnd<=?2");
      query.setParameter(1, ini);
      query.setParameter(2, end);
      Number countResult = (Number) query.getSingleResult();

      DBUtil.commit(em);

      return countResult.longValue();

    } catch (Exception ex) {
      ex.printStackTrace();
      LogUtil.severe(this, "Error recovering the CountByScheduleDate : " + ex.getMessage());
      DBUtil.rollback(em);
      throw ex;
    } finally {
      if (localEm) DBUtil.close(em);
    }
  }
  /**
   * {@inheritDoc}
   *
   * <p>RETRIEVER PARAMS: -DEV_ID (String): Device identifier.
   *
   * <p>RETURNS: DeviceType
   */
  @Override
  public Object retrieve(Map<String, Object> params) throws RTException {

    DeviceType devType = null;
    String devId = (String) params.get("DEV_ID");

    String factoryClassName = Config.getProperty("rt_config", "device.discoverer.provider");

    if (factoryClassName.equals("eu.gloria.rts2.rtd.RTS2DeviceDiscovery")) {
      // Auto

      LogUtil.info(this, ">>>>>>>>>>>>>>Resolving device type. BEGIN");
      Rts2Cmd cmd = Rts2Cmd.getNewCmd(Rts2CmdType.deviceinfo);
      cmd.getParameters().put("d", devId);
      String jsonContent = cmd.execute();
      LogUtil.info(this, ">>>>>>>>>>>>>>Resolving device type. END");

      try {
        ObjectMapper mapper = new ObjectMapper();
        HashMap<String, Object> data =
            (HashMap<String, Object>) mapper.readValue(jsonContent, Object.class);
        // HashMap data = (HashMap)JsonObj.fromJson(jsonContent);
        devType = Rts2GatewayTools.resolveDeviceType((Integer) data.get("type"));

      } catch (Exception ex) {
        throw new Rts2Exception("Error recovering the device list. " + ex.getMessage());
      }
    } else {
      // XML file

      LogUtil.info(this, ">>>>>>>>>>>>>>Resolving device type. BEGIN");

      try {

        configDeviceManager = new ConfigDeviceManager();

      } catch (Exception ex) {
        LogUtil.severe(
            null,
            "Rts2GatewayDeviceTypeCacheRetriever. static initialization. Error loading the device list XML."
                + ex.getMessage());
        ex.printStackTrace();
      }

      if (configDeviceManager.getDeviceList() != null
          && configDeviceManager.getDeviceList().getDevice() != null) {

        for (eu.gloria.rt.entity.environment.config.device.Device dev :
            configDeviceManager.getDeviceList().getDevice()) {
          if (dev.getShortName().equals(devId)) {
            devType = DeviceType.fromValue(dev.getType().toString());
            break;
          }
        }
      }

      LogUtil.info(this, ">>>>>>>>>>>>>>Resolving device type. END");
    }

    return devType;
  }