public static void main(String[] args) { EventManager mgr = new EventManager(); if (args[0].equals("store")) { mgr.createAndStoreEvent("My Event", new Date()); } else if (args[0].equals("list")) { List events = mgr.listEvents(); for (int i = 0; i < events.size(); i++) { Event theEvent = (Event) events.get(i); System.out.println("Event: " + theEvent.getTitle() + " Time: " + theEvent.getDate()); } } else if (args[0].equals("addpersontoevent")) { Long eventId = mgr.createAndStoreEvent("My Event", new Date()); Long personId = mgr.createAndStorePerson("Foo", "Bar"); mgr.addPersonToEvent(personId, eventId); System.out.println("Added person " + personId + " to event " + eventId); } else if (args[0].equals("addemailtoperson")) { Long personId = mgr.createAndStorePerson("Foozy", "Beary"); mgr.addEmailToPerson(personId, "foo@bar"); mgr.addEmailToPerson(personId, "bar@foo"); System.out.println( "Added two email addresses (value typed objects) to person entity : " + personId); } HibernateUtil.getSessionFactory().close(); }
public void deleteEvent(Event event) { List<User> list = getUsers(); Collection<Activity> activities = event.getActivities(); Set<Activity> tmpActivities = new HashSet<Activity>(); for (Activity activityEntity : activities) { tmpActivities.add(activityEntity); } for (User user : list) { boolean userUpdated = false; if (user.getEvents().contains(event)) { for (Activity activity : tmpActivities) { user.getActivities().remove(activity); event.getActivities().remove(activity); } user.getEvents().remove(event); userUpdated = true; } if (userUpdated) { updateObject(user); } } deleteObject(event); // for (Activity activity : tmpActivities) { // deleteObject(activity); // } }
public List<Activity> getActivities(Event parent) { List<Activity> result = new ArrayList<Activity>(); Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); Object event = session .createQuery("from Event as event where event.idevent= " + parent.getIdevent()) .uniqueResult(); transaction.commit(); if (event != null) { Event eventEntity = (Event) event; Collection<Activity> activityList = eventEntity.getActivities(); for (Activity activity : activityList) { result.add(activity); } } } finally { if (session.isOpen()) { session.flush(); session.disconnect(); session.close(); } } return result; }
private Long createAndStoreEvent(String title, Date theDate) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Event theEvent = new Event(); theEvent.setTitle(title); theEvent.setDate(theDate); session.save(theEvent); session.getTransaction().commit(); return theEvent.getId(); }
public EventNumberOfPassesRequest getEventPassesRequest(User user, Event event) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List result = session .createQuery( "from EventNumberOfPassesRequest as eventNumberOfPassesRequest " + "where eventNumberOfPassesRequest.idevent=" + event.getIdevent() + " and eventNumberOfPassesRequest.iduser=" + user.getIduser()) .list(); transaction.commit(); if (result != null && result.size() > 0) { return (EventNumberOfPassesRequest) result.get(0); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } return null; }
public Event getEvent(Event event) { Event result = null; Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); Object o = session .createQuery("from Event as event where event.idevent= " + event.getIdevent()) .uniqueResult(); transaction.commit(); if (o != null) { result = (Event) o; } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } return result; }
public List<User> getUserForEvent(Event selectedEvent) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); List<User> result = new ArrayList<User>(); try { Transaction transaction = session.beginTransaction(); String queryString = "select user from User as user where " + selectedEvent.getIdevent() + " in elements (user.events) order by user.firstname"; List users = session.createQuery(queryString).list(); transaction.commit(); for (Object user : users) { result.add((User) user); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } return result; }