/**
   * set a node attribute
   *
   * @param objectID unique ID of the JEVisObject on the Server.
   * @param attributeName unique name of the Attribute under this Object
   * @param value and its value
   */
  public static void writeToJEVis(long objectID, String attributeName, Object value) {
    try {
      // Check if the connection is still alive. An JEVisException will be
      // thrown if you use one of the functions and the connection is lost
      if (jevis.isConnectionAlive()) {

        // Get the JEVisObject with the given ID. You can get the uniqe
        // ID with the help of JEConfig.
        if (jevis.getObject(objectID) != null) {
          JEVisObject myObject = jevis.getObject(objectID);
          Logger.getLogger(WiotechStructureCreator.class.getName())
              .log(Level.INFO, "JEVisObject: " + myObject);

          // Get the JEVisAttribute by its unique identifier.
          if (myObject.getAttribute(attributeName) != null) {
            JEVisAttribute attribute = myObject.getAttribute(attributeName);
            Logger.getLogger(WiotechStructureCreator.class.getName())
                .log(Level.INFO, "JEVisAttribute: " + attribute);

            DateTime timestamp = DateTime.now();

            // Now we let the Attribute creates an JEVisSample,an JEVisSample allways need an
            // Timestamp and an value.
            JEVisSample newSample =
                attribute.buildSample(timestamp, value, "This is an note, imported via SysReader");
            // Until now we created the sample only localy and we have to commit it to the JEVis
            // Server.
            newSample.commit();

            // TODO: we need an example for attribute.addSamples(listOfSamples); function. This
            // function allows to commit a bunch of sample at once
          } else {
            Logger.getLogger(WiotechStructureCreator.class.getName())
                .log(Level.SEVERE, "Could not found the Attribute with the name:" + attributeName);
          }
        } else {
          Logger.getLogger(WiotechStructureCreator.class.getName())
              .log(Level.SEVERE, "Could not found the Object with the id:" + objectID);
        }
      } else {
        Logger.getLogger(WiotechStructureCreator.class.getName())
            .log(Level.SEVERE, "Connection to the JEVisServer is not alive");
        // TODO: the programm could now retry to connect,
        // We dont have to do the isConnectionAlive() but use the JEVisException to handle this
        // problem.
      }
    } catch (JEVisException ex) {
      Logger.getLogger(WiotechStructureCreator.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
  /**
   * Create an new JEVisObject on the JEVis Server.
   *
   * @param parentObjectID unique ID of the parent object where the new object will be created under
   * @param newObjectClass The JEVisClass of the new JEVisObject
   * @param newObjectName The name of the new JEVisObject
   */
  private static JEVisObject createObject(
      long parentObjectID, String newObjectClass, String newObjectName) {
    JEVisObject newObject = null;
    try {
      // Check if the connection is still alive. An JEVisException will be
      // thrown if you use one of the functions and the connection is lost
      if (jevis.isConnectionAlive()) {

        // Get the ParentObject from the JEVis system
        if (jevis.getObject(parentObjectID) != null) {

          JEVisObject parentObject = jevis.getObject(parentObjectID);
          JEVisClass parentClass = parentObject.getJEVisClass();

          // Get the JEVisClass we want our new JEVisObject to have
          if (jevis.getJEVisClass(newObjectClass) != null) {
            JEVisClass newClass = jevis.getJEVisClass(newObjectClass);

            // Check if the JEVisObject with this class is allowed under a parent of the other Class
            // it will also check if the JEVisClass is unique and if another object of the Class
            // exist.
            if (newClass.isAllowedUnder(parentClass)) {
              newObject = parentObject.buildObject(newObjectName, newClass);
              newObject.commit();
              Logger.getLogger(WiotechStructureCreator.class.getName())
                  .log(Level.INFO, "New ID: " + newObject.getID());
            } else {
              Logger.getLogger(WiotechStructureCreator.class.getName())
                  .log(
                      Level.SEVERE,
                      "Cannot create Object because the parent JEVisClass does not allow the child");
            }
          }

        } else {
          Logger.getLogger(WiotechStructureCreator.class.getName())
              .log(Level.SEVERE, "Cannot create Object because the parent is not accessible");
        }

      } else {
        Logger.getLogger(WiotechStructureCreator.class.getName())
            .log(Level.SEVERE, "Connection to the JEVisServer is not alive");
      }

    } catch (JEVisException ex) {
      Logger.getLogger(WiotechStructureCreator.class.getName()).log(Level.SEVERE, null, ex);
    }
    return newObject;
  }
  /**
   * Connect to JEVis
   *
   * @param sqlServer Address of the MySQL Server
   * @param port Port of the MySQL Server, Default is 3306
   * @param sqlSchema Database schema of the JEVis database
   * @param sqlUser MySQl user for the connection
   * @param sqlPW MySQL password for the connection
   * @param jevisUser Username of the JEVis user
   * @param jevisPW Password of the JEVis user
   */
  public void connectToJEVis(
      String sqlServer,
      String port,
      String sqlSchema,
      String sqlUser,
      String sqlPW,
      String jevisUser,
      String jevisPW) {

    try {
      // Create an new JEVisDataSource from the MySQL implementation
      // JEAPI-SQl. This connection needs an vaild user on the MySQl Server.
      // Later it will also be possible to use the JEAPI-WS and by this
      // using the JEVis webservice (REST) as an endpoint which is much
      // saver than using a public SQL-port.
      jevis = new JEVisDataSourceSQL(sqlServer, port, sqlSchema, sqlUser, sqlPW);

      // authentificate the JEVis user.
      if (jevis.connect(jevisUser, jevisPW)) {
        Logger.getLogger(WiotechStructureCreator.class.getName())
            .log(Level.INFO, "Connection was successful");
      } else {
        Logger.getLogger(WiotechStructureCreator.class.getName())
            .log(Level.INFO, "Connection was not successful, exiting app");
        System.exit(1);
      }

    } catch (JEVisException ex) {
      Logger.getLogger(WiotechStructureCreator.class.getName())
          .log(Level.SEVERE, "There was an error while connecting to the JEVis Server");
      Logger.getLogger(WiotechStructureCreator.class.getName()).log(Level.SEVERE, null, ex);
      System.exit(1);
    }
  }
  private static ObjectAndBoolean createObjectCheckNameExistance(
      long parentObjectID, String newObjectClass, String newObjectName) {

    try {
      // Check if the connection is still alive. An JEVisException will be
      // thrown if you use one of the functions and the connection is lost
      if (jevis.isConnectionAlive()) {

        // Get the ParentObject from the JEVis system
        if (jevis.getObject(parentObjectID) != null) {

          JEVisObject parentObject = jevis.getObject(parentObjectID);

          List<JEVisObject> children = parentObject.getChildren();

          for (JEVisObject child : children) {

            try {
              String mac = child.getAttribute("MAC").getLatestSample().getValueAsString();
              if (mac.equals(newObjectName)) {
                return new ObjectAndBoolean(child, false);
              }
            } catch (NullPointerException ex) {
              if (child.getName().equals(newObjectName)) {
                return new ObjectAndBoolean(child, false);
              }
            }
          }

        } else {
          Logger.getLogger(WiotechStructureCreator.class.getName())
              .log(Level.SEVERE, "Cannot create Object because the parent is not accessible");
        }

      } else {
        Logger.getLogger(WiotechStructureCreator.class.getName())
            .log(Level.SEVERE, "Connection to the JEVisServer is not alive");
      }

    } catch (JEVisException ex) {
      Logger.getLogger(WiotechStructureCreator.class.getName()).log(Level.SEVERE, null, ex);
    }
    return new ObjectAndBoolean(createObject(parentObjectID, newObjectClass, newObjectName), true);
  }