001: package com.tctest.domain;
002:
003: import org.hibernate.Session;
004:
005: import java.util.ArrayList;
006: import java.util.Date;
007: import java.util.Iterator;
008: import java.util.List;
009:
010: public class EventManager {
011:
012: public List listEmailsOfEvent(Long eventId) {
013: Session session = HibernateUtil.getSessionFactory()
014: .getCurrentSession();
015: session.beginTransaction();
016:
017: List emailList = new ArrayList();
018: Event event = (Event) session.load(Event.class, eventId);
019: for (Iterator it = event.getParticipants().iterator(); it
020: .hasNext();) {
021: Person person = (Person) it.next();
022: emailList.addAll(person.getEmailAddresses());
023: }
024:
025: session.getTransaction().commit();
026: return emailList;
027: }
028:
029: public Long createAndStoreEvent(String title, Date theDate) {
030:
031: Session session = HibernateUtil.getSessionFactory()
032: .getCurrentSession();
033:
034: session.beginTransaction();
035:
036: Event theEvent = new Event();
037: theEvent.setTitle(title);
038: theEvent.setDate(theDate);
039:
040: Long eventId = (Long) session.save(theEvent);
041:
042: session.getTransaction().commit();
043: return eventId;
044: }
045:
046: public Long createAndStorePerson(String firstName, String lastName) {
047:
048: Session session = HibernateUtil.getSessionFactory()
049: .getCurrentSession();
050:
051: session.beginTransaction();
052:
053: Person person = new Person();
054: person.setFirstname(firstName);
055: person.setLastname(lastName);
056:
057: Long personId = (Long) session.save(person);
058:
059: session.getTransaction().commit();
060: return personId;
061: }
062:
063: public List listEvents() {
064:
065: Session session = HibernateUtil.getSessionFactory()
066: .getCurrentSession();
067:
068: session.beginTransaction();
069:
070: List result = session.createQuery("from Event").setCacheable(
071: true).list();
072:
073: session.getTransaction().commit();
074:
075: return result;
076: }
077:
078: public void addPersonToEvent(Long personId, Long eventId) {
079:
080: Session session = HibernateUtil.getSessionFactory()
081: .getCurrentSession();
082: session.beginTransaction();
083:
084: Person aPerson = (Person) session.load(Person.class, personId);
085: Event anEvent = (Event) session.load(Event.class, eventId);
086:
087: aPerson.getEvents().add(anEvent);
088:
089: session.getTransaction().commit();
090: }
091:
092: public Long addPersonToAccount(Long personId, Account account) {
093: Session session = HibernateUtil.getSessionFactory()
094: .getCurrentSession();
095: session.beginTransaction();
096:
097: Person aPerson = (Person) session.load(Person.class, personId);
098: account.setPerson(aPerson);
099:
100: Long accountId = (Long) session.save(account);
101:
102: session.getTransaction().commit();
103: return accountId;
104: }
105:
106: public Account getAccount(Long accountId) {
107: Session session = HibernateUtil.getSessionFactory()
108: .getCurrentSession();
109: session.beginTransaction();
110:
111: Account account = (Account) session.load(Account.class,
112: accountId);
113:
114: session.getTransaction().commit();
115: return account;
116: }
117:
118: public void addEmailToPerson(Long personId, String emailAddress) {
119:
120: Session session = HibernateUtil.getSessionFactory()
121: .getCurrentSession();
122: session.beginTransaction();
123:
124: Person aPerson = (Person) session.load(Person.class, personId);
125:
126: // The getEmailAddresses() might trigger a lazy load of the collection
127: aPerson.getEmailAddresses().add(emailAddress);
128:
129: session.getTransaction().commit();
130: }
131:
132: public void addPhoneNumberToPerson(Long personId, PhoneNumber pN) {
133:
134: Session session = HibernateUtil.getSessionFactory()
135: .getCurrentSession();
136: session.beginTransaction();
137:
138: Person aPerson = (Person) session.load(Person.class, personId);
139: pN.setPersonId(personId.longValue());
140: aPerson.getPhoneNumbers().add(pN);
141:
142: session.getTransaction().commit();
143: }
144: }
|