Beispiel #1
0
 private void findHeirs(List<JEVisClass> all, List<JEVisClass> heirs, JEVisClass jclass)
     throws JEVisException {
   for (JEVisClass heir : all) {
     if (heir.getInheritance().equals(jclass)) {
       heirs.add(heir);
       findHeirs(all, heirs, heir);
     }
   }
 }
  /**
   * 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;
  }
  public JEVisClass getJEVisClass(String name) {
    if (!_cach.containsKey(name)) {
      try {
        JEVisClass newClass = _ds.getClassTable().getObjectClass(name);
        if (newClass == null) {
          return null;
        }
        _cach.put(newClass.getName(), newClass);
      } catch (JEVisException ex) {
        Logger.getLogger(SimpleClassCache.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    return _cach.get(name);
  }
Beispiel #4
0
  public boolean delete(JEVisClass jclass) throws JEVisException {
    String sql = "delete from " + TABLE + " where " + COLUMN_NAME + "=?";
    PreparedStatement ps = null;
    ResultSet rs = null;
    _ds.addQuery("ClassTable.delete");

    try {
      for (JEVisType type : jclass.getTypes()) {
        if (jclass.getInheritance() != null) {
          if (jclass.getInheritance().getType(type.getName()) == null) {
            _ds.getTypeTable().detele(type);
          }
        } else {
          _ds.getTypeTable().detele(type);
        }
      }

      for (JEVisClassRelationship rel : jclass.getRelationships()) {
        _ds.getClassRelationshipTable().delete(rel);
      }

      ps = _connection.prepareStatement(sql);
      ps.setString(1, jclass.getName());

      System.out.println("delete.class: " + ps.toString());
      int count = ps.executeUpdate();

      if (count == 1) {
        SimpleClassCache.getInstance().remove(jclass.getName());

        return true;
      } else {
        return false;
      }

    } catch (Exception ex) {
      throw new JEVisException("Error while deleting Class: " + ex, 2342763);
    } finally {
      if (ps != null) {
        try {
          ps.close();
        } catch (SQLException ex) {
          logger.warn("cound not close Prepared statement: {}", ex);
        }
      }
    }
  }
Beispiel #5
0
  public boolean update(JEVisClass jclass, String oldName) throws JEVisException {

    System.out.println("#######");
    System.out.println("jclass.name: " + jclass);

    System.out.println("ClassTable.update+ " + ((JEVisClassSQL) jclass).getFile());
    try {
      String sql =
          "update "
              + TABLE
              + " set "
              + COLUMN_DESCRIPTION
              + "=?,"
              + COLUMN_NAME
              + "=?,"
              + COLUMN_UNIQUE
              + "=?"; // + COLUMN_ICON + "=?"

      if (((JEVisClassSQL) jclass).getFile() != null || jclass.getIcon() != null) {
        sql += ", " + COLUMN_ICON + "=?";
      }
      sql += " where " + COLUMN_NAME + "=?";

      _ds.addQuery("ClassTable.update");
      PreparedStatement ps = _connection.prepareStatement(sql);

      int i = 1;

      //            if (!oldName.equals(jclass.getName())) { // ->rename
      //                System.out.println("rename");
      ps.setString(i++, jclass.getDescription());
      ps.setString(i++, jclass.getName());
      ps.setBoolean(i++, jclass.isUnique());

      if (((JEVisClassSQL) jclass).getFile() != null) {
        File file = ((JEVisClassSQL) jclass).getFile();
        FileInputStream fis = new FileInputStream(file);
        ps.setBinaryStream(i++, fis, (int) file.length());
      } else if (jclass.getIcon() != null) {
        System.out.println("usinf icon");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(jclass.getIcon(), "gif", os);
        InputStream is = new ByteArrayInputStream(os.toByteArray());

        ps.setBinaryStream(i++, is);
      }

      ps.setString(i++, oldName);

      //            } else {//update with same name
      ////                System.out.println("update");
      //                if (jclass.getDescription() != null) {
      //                    ps.setString(1, jclass.getDescription());
      //                } else {
      //                    ps.setNull(1, Types.VARCHAR);
      //                }
      //
      //                ps.setString(2, jclass.getName());
      //
      ////                if (jclass.getInheritance() != null && jclass.getInheritance().getName()
      // != null) {
      ////                    ps.setString(3, jclass.getInheritance().getName());
      ////                } else {
      ////                    ps.setNull(3, Types.VARCHAR);
      ////                }
      //
      //                //TODO hier warst du, lösche classe vor test (unique=keyword?)
      //                ps.setBoolean(3, jclass.isUnique());
      //
      //
      //
      //                ps.setString(4, jclass.getName());
      //            }
      System.out.println("update class: " + ps);

      int res = ps.executeUpdate();

      // Check if the name changed, if yes we have to change all existing JEVisObjects.....do we
      // want that?
      if (res == 1) {
        if (!oldName.equals(jclass.getName())) {
          // ------------------ Update exintings objects
          System.out.println("update Existing JEVis Objects");
          String updateObject =
              "update "
                  + ObjectTable.TABLE
                  + " set "
                  + ObjectTable.COLUMN_CLASS
                  + "=?"
                  + " where "
                  + ObjectTable.COLUMN_CLASS
                  + "=?";
          PreparedStatement ps2 = _connection.prepareStatement(updateObject);
          ps2.setString(1, jclass.getName());
          ps2.setString(2, oldName);

          int res2 = ps2.executeUpdate();
          if (res2 > 0) {
            //                        System.out.println("updated " + res2 + " JEVisObjects");
          }

          // ------------------- Update existing Valid Parents
          System.out.println("update Existing valid JEVis parents");
          String updateValid =
              "update "
                  + ClassRelationTable.TABLE
                  + " set "
                  + ClassRelationTable.COLUMN_START
                  + "=?"
                  + " where "
                  + ClassRelationTable.COLUMN_START
                  + "=?";
          PreparedStatement psUpdate = _connection.prepareStatement(updateValid);
          psUpdate.setString(1, jclass.getName());
          psUpdate.setString(2, oldName);

          psUpdate.executeUpdate();

          // ------------------- Update existing Valid Parents #2
          //                    System.out.println("update Existing valid JEVis parents");
          String updateValid2 =
              "update "
                  + ClassRelationTable.TABLE
                  + " set "
                  + ClassRelationTable.COLUMN_END
                  + "=?"
                  + " where "
                  + ClassRelationTable.COLUMN_END
                  + "=?";
          PreparedStatement psUpdate2 = _connection.prepareStatement(updateValid2);
          psUpdate2.setString(1, jclass.getName());
          psUpdate2.setString(2, oldName);

          psUpdate2.executeUpdate();
        }

        return true;
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    return false;
  }