@Override
  public void saveConference(
      String title,
      Date start,
      Date end,
      String room,
      String website,
      List<Stakeholder> stakeholders) {
    if (stakeholders == null) stakeholders = new ArrayList<>();
    Conference c = new Conference();
    List<Key> keys = new ArrayList<>();
    try {

      for (Stakeholder s : stakeholders) {
        keys.add(s.getId());
      }

      c = new Conference(title, start, end, room, website, keys);
      em.persist(c);
    } finally {
      em.close();
    }

    try {
      em = EMF.get().createEntityManager();

      for (Key k : keys) {
        Stakeholder s = em.find(Stakeholder.class, k);
        s.addConference(c.getId());
      }
    } finally {
      em.close();
    }
  }
  @Override
  public void saveStakeholder(
      String name, String type, String email, String website, List<Conference> conferences) {
    if (conferences == null) conferences = new ArrayList<>();
    Stakeholder s = new Stakeholder();
    List<Key> keys = new ArrayList<>();
    try {

      for (Conference c : conferences) {
        keys.add(c.getId());
      }

      s = new Stakeholder(name, type, email, website, keys);
      em.persist(s);
    } finally {
      em.close();
    }

    try {
      em = EMF.get().createEntityManager();

      for (Key k : keys) {
        Conference c = em.find(Conference.class, k);
        c.addStakeholder(s.getId());
      }
    } finally {
      em.close();
    }
  }
Example #3
0
 public static synchronized EntityManager getLocalEm() {
   EntityManager em = LocalEm.get();
   if (em == null) {
     em = EMF.get().createEntityManager();
     LocalEm.set(em);
   }
   return em;
 }
 @Override
 public void updateStakeholder(
     Long id,
     String type,
     String name,
     String website,
     String email,
     List<Conference> conferences) {
   // remove then reaadd stakeholder because real update is too complicated in many to many
   this.deleteStakeholder(id);
   em = EMF.get().createEntityManager();
   this.saveStakeholder(name, type, email, website, conferences);
 }
public class ContactFormController {

  private static DatastoreService ds = EMF.getDs();
  private Transaction txn = null;

  public void create(ContactForm cf) {

    Entity e = Util.contactFormToEntity(cf);
    txn = ds.beginTransaction();
    ds.put(e);
    txn.commit();
  }
}
  // theDateAdjustment: can be negative or positive. Never results in an ActiveThruGmtDate before
  // before 24 hours from now.
  public static void upateUserActiveThruGmtDate(User theUser, Integer theDateAdjustment) {
    EntityManager emMessageThread = EMF.get().createEntityManager();

    try {
      List<MessageThread> messageThreads =
          (List<MessageThread>)
              emMessageThread
                  .createNamedQuery("MessageThread.getBySenderUserIdAndStatus")
                  .setParameter("senderUserId", KeyFactory.keyToString(theUser.getKey()))
                  .setParameter("status", MessageThread.ACTIVE_STATUS)
                  .getResultList();

      for (MessageThread mt : messageThreads) {
        emMessageThread.getTransaction().begin();
        Date activeThruGmtDate = mt.getActiveThruGmtDate();
        if (activeThruGmtDate == null) {
          activeThruGmtDate = new Date();
        }

        // the following line could subtract days in adjustment is negative
        activeThruGmtDate = GMT.addDaysToDate(activeThruGmtDate, theDateAdjustment);
        if (theDateAdjustment < 0) {
          activeThruGmtDate = GMT.setToFutureDate(activeThruGmtDate);
        }
        mt.setActiveThruGmtDate(activeThruGmtDate);
        emMessageThread.getTransaction().commit();
      }
      log.info("ActiveThruGmtDate adjusted: messageThread count = " + messageThreads.size());

    } catch (Exception e) {
      log.severe("Query MessageThread.getBySenderUserIdAndStatus failed");
    } finally {
      emMessageThread.close();
    }
    return;
  }
@Stateless
public class AipuBean implements Aipu, Serializable {
  private static final long serialVersionUID = 1L;
  EntityManager em = EMF.get().createEntityManager();

  @Override
  public News getNews(Long newsId) {
    News n = em.find(News.class, newsId);
    return n;
  }

  @Override
  public List<News> getAllNews() {
    try {
      Query query = em.createQuery("SELECT n FROM News n");
      List<News> results = (List<News>) query.getResultList();
      Collections.sort(results);
      return results;
    } finally {
      em.close();
    }
  }

  @Override
  public void saveNews(String title, String text) {
    try {
      Date date = swissTimeZone();
      News n = new News(title, text, date);
      em.persist(n);
    } finally {
      em.close();
    }
  }

  private Date swissTimeZone() {
    Date date = new Date();
    date.setTime(date.getTime() + 600000 * 6);
    return date;
  }

  @Override
  public void deleteNews(Long newsId) {
    try {
      News n = em.find(News.class, newsId);
      em.getTransaction().begin();
      em.remove(n);
      em.getTransaction().commit();
    } finally {
      em.close();
    }
  }

  @Override
  public void updateNews(Long id, String title, String text) {
    try {
      News n = em.find(News.class, id);
      n.setTitle(title);
      n.setText(text);
      n.setPublished(swissTimeZone());
    } finally {
      em.close();
    }
  }

  @Override
  public Conference getConference(long conferenceId) {
    Conference c = new Conference();
    try {
      c = em.find(Conference.class, conferenceId);
    } finally {
      em.close();
    }

    return c;
  }

  @Override
  public List<Conference> getAllConferences() {
    try {
      Query query = em.createQuery("SELECT c FROM Conference c ");
      List<Conference> results = (List<Conference>) query.getResultList();
      Collections.sort(results);

      return results;
    } finally {
      em.close();
    }
  }

  @Override
  public void saveConference(
      String title,
      Date start,
      Date end,
      String room,
      String website,
      List<Stakeholder> stakeholders) {
    if (stakeholders == null) stakeholders = new ArrayList<>();
    Conference c = new Conference();
    List<Key> keys = new ArrayList<>();
    try {

      for (Stakeholder s : stakeholders) {
        keys.add(s.getId());
      }

      c = new Conference(title, start, end, room, website, keys);
      em.persist(c);
    } finally {
      em.close();
    }

    try {
      em = EMF.get().createEntityManager();

      for (Key k : keys) {
        Stakeholder s = em.find(Stakeholder.class, k);
        s.addConference(c.getId());
      }
    } finally {
      em.close();
    }
  }

  @Override
  public void deleteConference(long conferenceId) {
    try {
      Conference c = em.find(Conference.class, conferenceId);
      em.getTransaction().begin();
      // first remove related relation ...
      for (Key Skey : c.getStakeholders()) {
        Stakeholder s = em.find(Stakeholder.class, Skey.getId());
        s.getConferences().remove(c.getId());
        em.persist(s);
      }
      // then remove the entity
      em.remove(c);
      em.getTransaction().commit();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      em.close();
    }
  }

  @Override
  public Stakeholder getStakeholder(long stakeholderId) {
    Stakeholder s = new Stakeholder();
    try {
      s = em.find(Stakeholder.class, stakeholderId);
    } finally {
      em.close();
    }
    return s;
  }

  @Override
  public List<Stakeholder> getAllStakeholder() {
    try {
      Query query = em.createQuery("SELECT s FROM Stakeholder s");
      List<Stakeholder> results = (List<Stakeholder>) query.getResultList();
      Collections.sort(results);
      return results;
    } finally {
      em.close();
    }
  }

  @Override
  public void saveStakeholder(
      String name, String type, String email, String website, List<Conference> conferences) {
    if (conferences == null) conferences = new ArrayList<>();
    Stakeholder s = new Stakeholder();
    List<Key> keys = new ArrayList<>();
    try {

      for (Conference c : conferences) {
        keys.add(c.getId());
      }

      s = new Stakeholder(name, type, email, website, keys);
      em.persist(s);
    } finally {
      em.close();
    }

    try {
      em = EMF.get().createEntityManager();

      for (Key k : keys) {
        Conference c = em.find(Conference.class, k);
        c.addStakeholder(s.getId());
      }
    } finally {
      em.close();
    }
  }

  @Override
  public void deleteStakeholder(long stakeholderId) {

    try {
      Stakeholder s = em.find(Stakeholder.class, stakeholderId);
      em.getTransaction().begin();
      // first remove related relation ...
      for (Key Ckey : s.getConferences()) {
        Conference c = em.find(Conference.class, Ckey.getId());
        c.getStakeholders().remove(s.getId());
        em.persist(c);
      }
      // then remove the entity
      em.remove(s);
      em.getTransaction().commit();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      em.close();
    }
  }

  @Override
  public void updateStakeholder(
      Long id,
      String type,
      String name,
      String website,
      String email,
      List<Conference> conferences) {
    // remove then reaadd stakeholder because real update is too complicated in many to many
    this.deleteStakeholder(id);
    em = EMF.get().createEntityManager();
    this.saveStakeholder(name, type, email, website, conferences);
  }
}
public class EventTemplateController {

  private static DatastoreService ds = EMF.getDs();
  private static EntityManagerFactory emf = EMF.get();
  private Transaction txn = null;

  public EntityManager getEntityManager() {
    return emf.createEntityManager();
  }

  public void create(EventTemplate eventTemplate) throws RollbackFailureException, Exception {
    EntityManager em = null;
    try {
      txn = ds.beginTransaction();
      em = getEntityManager();
      em.persist(eventTemplate);
      txn.commit();
    } catch (Exception ex) {
      try {
        txn.rollback();
      } catch (Exception re) {
        throw new RollbackFailureException(
            "An error occurred attempting to roll back the transaction.", re);
      }

      throw ex;
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public void edit(EventTemplate eventTemplate) throws RollbackFailureException, Exception {
    create(eventTemplate);
  }

  public void destroy(Key key) throws RollbackFailureException, Exception {
    EntityManager em = null;
    try {
      txn = ds.beginTransaction();
      em = getEntityManager();
      EventTemplate eventTemplate;
      eventTemplate = em.getReference(EventTemplate.class, key);
      em.remove(eventTemplate);
      txn.commit();
    } catch (Exception ex) {
      try {
        txn.rollback();
      } catch (Exception re) {
        throw new RollbackFailureException(
            "An error occurred attempting to roll back the transaction.", re);
      }
      throw ex;
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public EventTemplate findEventTemplate(Key key) {
    EntityManager em = getEntityManager();
    try {
      EventTemplate a = em.find(EventTemplate.class, key);
      return a;
    } finally {
      em.close();
    }
  }

  public List<EventTemplate> getAllEventTemplatesKeys() {
    Query q = new Query("EventTemplate");
    q.addSort("name");
    PreparedQuery pq = ds.prepare(q);

    List<EventTemplate> temps = new ArrayList<EventTemplate>();
    String name = null;
    String id = null;
    for (Entity result : pq.asIterable()) {
      name = (String) result.getProperty("name");
      id = result.getKey().getName();
      EventTemplate et = new EventTemplate(id);
      et.setName(name);
      temps.add(et);
      et = null;
    }
    return temps;
  }
}
Example #9
0
 public TaskDao() {
   em = EMF.get().createEntityManager();
 }
Example #10
0
 private static EntityManager getEntityManager() {
   return EMF.get().createEntityManager();
 }