1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.ehcache.hibernate.domain;
17
18 import java.util.ArrayList;
19 import java.util.Date;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.ListIterator;
23
24 import org.hibernate.Query;
25 import org.hibernate.Session;
26 import org.hibernate.SessionFactory;
27
28 public class EventManager {
29
30 private final SessionFactory sessionFactory;
31
32 public EventManager(SessionFactory sessionFactory) {
33 this.sessionFactory = sessionFactory;
34 }
35
36 public List listEmailsOfEvent(Long eventId) {
37 Session session = sessionFactory.getCurrentSession();
38 session.beginTransaction();
39
40 List emailList = new ArrayList();
41 Event event = (Event) session.load(Event.class, eventId);
42 for (Iterator it = event.getParticipants().iterator(); it.hasNext();) {
43 Person person = (Person) it.next();
44 emailList.addAll(person.getEmailAddresses());
45 }
46
47 session.getTransaction().commit();
48 return emailList;
49 }
50
51 public Long createAndStoreEvent(String title, Person organizer, Date theDate) {
52
53 Session session = sessionFactory.getCurrentSession();
54
55 session.beginTransaction();
56
57 Event theEvent = new Event();
58 theEvent.setTitle(title);
59 theEvent.setDate(theDate);
60 theEvent.setOrganizer(organizer);
61
62 Long eventId = (Long) session.save(theEvent);
63
64 session.getTransaction().commit();
65 return eventId;
66 }
67
68 public Long createAndStorePerson(String firstName, String lastName) {
69
70 Session session = sessionFactory.getCurrentSession();
71
72 session.beginTransaction();
73
74 Person person = new Person();
75 person.setFirstname(firstName);
76 person.setLastname(lastName);
77
78 Long personId = (Long) session.save(person);
79
80 session.getTransaction().commit();
81 return personId;
82 }
83
84 public Long createAndStorePerson(Person person) {
85
86 Session session = sessionFactory.getCurrentSession();
87
88 session.beginTransaction();
89
90 Long personId = (Long) session.save(person);
91
92 session.getTransaction().commit();
93 return personId;
94 }
95
96 public List listEvents() {
97
98 Session session = sessionFactory.getCurrentSession();
99
100 session.beginTransaction();
101
102 List result = session.createQuery("from Event").setCacheable(true).list();
103
104 session.getTransaction().commit();
105
106 return result;
107 }
108
109 /***
110 * Call setEntity() on a cacheable query - see FORGE-265
111 */
112 public List listEventsOfOrganizer(Person organizer) {
113
114 Session session = sessionFactory.getCurrentSession();
115
116 session.beginTransaction();
117
118 Query query = session.createQuery("from Event ev where ev.organizer = :organizer");
119
120 query.setCacheable(true);
121 query.setEntity("organizer", organizer);
122 List result = query.list();
123
124 session.getTransaction().commit();
125
126 return result;
127 }
128
129 /***
130 * Use a Criteria query - see FORGE-247
131 */
132 public List listEventsWithCriteria() {
133 Session session = sessionFactory.getCurrentSession();
134
135 session.beginTransaction();
136
137 List result = session.createCriteria(Event.class)
138 .setCacheable(true)
139 .list();
140
141 session.getTransaction().commit();
142
143 return result;
144 }
145
146 public void addPersonToEvent(Long personId, Long eventId) {
147
148 Session session = sessionFactory.getCurrentSession();
149 session.beginTransaction();
150
151 Person aPerson = (Person) session.load(Person.class, personId);
152 Event anEvent = (Event) session.load(Event.class, eventId);
153
154 aPerson.getEvents().add(anEvent);
155
156 session.getTransaction().commit();
157 }
158
159 public Long addPersonToAccount(Long personId, Account account) {
160 Session session = sessionFactory.getCurrentSession();
161 session.beginTransaction();
162
163 Person aPerson = (Person) session.load(Person.class, personId);
164 account.setPerson(aPerson);
165
166 Long accountId = (Long) session.save(account);
167
168 session.getTransaction().commit();
169 return accountId;
170 }
171
172 public Account getAccount(Long accountId) {
173 Session session = sessionFactory.getCurrentSession();
174 session.beginTransaction();
175
176 Account account = (Account) session.load(Account.class, accountId);
177
178 session.getTransaction().commit();
179 return account;
180 }
181
182 public void addEmailToPerson(Long personId, String emailAddress) {
183
184 Session session = sessionFactory.getCurrentSession();
185 session.beginTransaction();
186
187 Person aPerson = (Person) session.load(Person.class, personId);
188
189
190 aPerson.getEmailAddresses().add(emailAddress);
191
192 session.getTransaction().commit();
193 }
194
195 public void addPhoneNumberToPerson(Long personId, PhoneNumber pN) {
196
197 Session session = sessionFactory.getCurrentSession();
198 session.beginTransaction();
199
200 Person aPerson = (Person) session.load(Person.class, personId);
201 pN.setPersonId(personId.longValue());
202 aPerson.getPhoneNumbers().add(pN);
203
204 session.getTransaction().commit();
205 }
206
207 public void addTalismanToPerson(Long personId, String talisman) {
208
209 Session session = sessionFactory.getCurrentSession();
210 session.beginTransaction();
211
212 Person aPerson = (Person) session.load(Person.class, personId);
213 aPerson.addTalisman(talisman);
214
215 session.getTransaction().commit();
216 }
217
218 public Long createHolidayCalendar() {
219
220 Session session = sessionFactory.getCurrentSession();
221 session.beginTransaction();
222
223
224 List calendars = session.createQuery("from HolidayCalendar").setCacheable(true).list();
225 for (ListIterator li = calendars.listIterator(); li.hasNext();) {
226 session.delete(li.next());
227 }
228
229 HolidayCalendar calendar = new HolidayCalendar();
230 calendar.init();
231
232 Long calendarId = (Long) session.save(calendar);
233
234 session.getTransaction().commit();
235 return calendarId;
236 }
237
238 public HolidayCalendar getHolidayCalendar() {
239 Session session = sessionFactory.getCurrentSession();
240
241 session.beginTransaction();
242
243 List calendars = session.createQuery("from HolidayCalendar").setCacheable(true).list();
244
245 session.getTransaction().commit();
246
247 return calendars.isEmpty() ? null : (HolidayCalendar) calendars.get(0);
248 }
249 }