/**
   * Returns a collection of bounds for reactions included in the given's model metabolism. Note
   * that it has to be a sum of bounds of the parent model (if the given model has a parent) and the
   * bounds directly associated with current model.
   *
   * @param model
   * @return collection of bounds for the given model
   */
  Collection<EBounds> getBoundsForModel(EModel model, EntityManager em) {

    if (model.getParent() == null) {
      return model.getEBoundsCollection();
    } else {
      /*
       * There are two sources of bounds for each model.
       * First, we retrieve bounds that have been defined for this model
       */
      List<EBounds> res1 =
          em.createQuery("SELECT e FROM EBounds e WHERE e.model = :model")
              .setParameter("model", model)
              .getResultList();

      /*
       * The remaining bounds are taken from the parent model
       */
      List<EBounds> res2 =
          em.createQuery(
                  " SELECT b FROM EModel m, EBounds b WHERE "
                      + " m.id = :modelID AND m.parent IS NOT NULL AND "
                      + " b.model = m.parent AND NOT EXISTS (SELECT c "
                      + " FROM EBounds c WHERE c.model = :model AND "
                      + " b.reaction = c.reaction )")
              .setParameter("modelID", model.getId())
              .setParameter("model", model)
              .getResultList();
      res1.addAll(res2);
      return res1;
    }
  }
 /**
  * @param parentList list of parent models
  * @return list of children models
  */
 public LinkedList<EModel> getChildrenByModelList(List<EModel> parentList) {
   LinkedList<EModel> children = new LinkedList<EModel>();
   for (EModel model : parentList) {
     //            children.addAll(getChildrenByModel(model));
     children.addAll(model.getEModelCollection());
   }
   return children;
 }
 public List<ESpecies> getSpecies(int modelId) {
   EntityManager em = getEntityManager();
   try {
     em.getTransaction().begin();
     EModel model = em.find(EModel.class, modelId);
     List<ESpecies> species =
         em.createQuery(
                 "SELECT s FROM ESpecies s, EModel m WHERE s.compartment.metabolism.id = m.metabolism.id AND m.id = :modelId")
             .setParameter("modelId", model.getId())
             .getResultList();
     em.getTransaction().commit();
     return species;
   } finally {
     em.close();
   }
 }
  public void addModel(
      EMetabolism metabolism,
      EModel model,
      Map<String, ECompartment> compartment_map,
      Map<String, ESpecies> species_map,
      List<EReaction> reaction_list,
      List<EReactant> reactant_list,
      List<EProduct> product_list,
      List<EBounds> bounds_list) {
    EntityManager em = getEntityManager();
    try {
      em.getTransaction().begin();

      em.persist(metabolism);
      em.persist(model);

      model.getMetabolism().getEModelCollection().add(model);

      for (ECompartment c : compartment_map.values()) {
        em.persist(c);
        c.getMetabolism().getECompartmentCollection().add(c);
      }

      for (ESpecies sp : species_map.values()) {
        em.persist(sp);
        sp.getCompartment().getESpeciesCollection().add(sp);
      }

      for (EReaction r : reaction_list) {
        em.persist(r);
        r.getMetabolism().getEReactionCollection().add(r);
      }

      for (EReactant r : reactant_list) {
        em.persist(r);
        r.getSpecies().getEReactantCollection().add(r);
        /* r.getReaction().getEReactantCollection().add(r); */
      }

      for (EProduct p : product_list) {
        em.persist(p);
        p.getSpecies().getEProductCollection().add(p);
        /* p.getReaction().getEProductCollection().add(p); */
      }

      for (EBounds b : bounds_list) {
        em.persist(b);
        b.getModel().getEBoundsCollection().add(b);
        /* b.getReaction().getEBoundsCollection().add(b); */
      }

      em.merge(metabolism);

      em.getTransaction().commit();
    } finally {
      em.close();
    }
  }
 /**
  * @param modelId
  * @return creactions
  */
 public Collection<EReaction> getDetachedReactions(int modelId) {
   ArrayList<EReaction> detachedReactions = new ArrayList<EReaction>(0);
   EModel model = getModel(modelId);
   EntityManager em = getEntityManager();
   try {
     em.getTransaction().begin();
     Collection<EBounds> eboundColl = model.getEBoundsCollection();
     for (EBounds bounds : eboundColl) {
       if (bounds.getLowerBound() == 0 && bounds.getUpperBound() == 0) {
         detachedReactions.add(bounds.getReaction());
       }
     }
     em.getTransaction().commit();
   } catch (NoResultException ex) {
     em.getTransaction().rollback();
   } finally {
     em.close();
   }
   return detachedReactions;
 }
 /** Removes @bounds. */
 public void removeBounds(EModel model) {
   EntityManager em = getEntityManager();
   try {
     em.getTransaction().begin();
     for (EBounds bounds : model.getEBoundsCollection()) {
       EBounds boundsx = em.find(EBounds.class, bounds.getId());
       em.remove(boundsx);
     }
     em.getTransaction().commit();
   } finally {
     em.close();
   }
 }
  /**
   * Returns list of all models from database that are ancestors of a given model (transitive
   * closure version).
   *
   * @return - list of all ancestors of a given model (including that model).
   */
  public List<EModel> getAncestorsModels(EModel start) {
    EntityManager em = getEntityManager();
    try {
      em.getTransaction().begin();
      // List<EModel> allModels = (List<EModel>) em.createQuery("select m from EModel as
      // m").getResultList();
      EModel model = start;
      List<EModel> result = new LinkedList<EModel>();

      // assume that the start model is really a model from the database
      result.add(model);

      // transitive closure
      while (model.getParent() != null) {
        result.add(model.getParent());
        model = model.getParent();
      }

      em.getTransaction().commit();
      return result;
    } finally {
      em.close();
    }
  }
 /** Removes @model from database. */
 public void removeModel(EModel model) {
   EntityManager em = getEntityManager();
   try {
     em.getTransaction().begin();
     EModel modelx = em.find(EModel.class, model.getId());
     removeModel(em, modelx);
     // !!1tu jest błąd
     // Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Obiekt DELETE w
     // tabeli 'ETASK' spowodował naruszenie reguły ograniczającej klucz obcy 'ECOMMONRESULTSTASK'
     // dla klucza (1). Instrukcja została wycofana.
     em.getTransaction().commit();
   } finally {
     em.close();
   }
 }
 /**
  * Finds and returns @model.
  *
  * @return - @model
  */
 public EModel getModel(EModel model) {
   EntityManager em = getEntityManager();
   try {
     em.getTransaction().begin();
     EModel modelx =
         (EModel)
             em.createNamedQuery("EModel.findById")
                 .setParameter("id", model.getId())
                 .setHint("eclipselink.refresh", true)
                 .getSingleResult();
     em.getTransaction().commit();
     return modelx;
   } finally {
     em.close();
   }
 }
  private void removeModel(EntityManager em, EModel model) {
    /* delete model's Model */
    if (model.getTask() != null) {
      em.remove(model.getTask());
    }

    /* delete children */
    for (EModel child : model.getEModelCollection()) {
      removeModel(em, child);
    }

    /* update Metabolism */
    model.getMetabolism().getEModelCollection().remove(model);

    if (model.getMetabolism().getEModelCollection().size() == 0) {
      /* delete Metabolism */
      em.remove(model.getMetabolism());
    }

    em.remove(model);
  }