/
HibernateImageGroupRepository.java
279 lines (251 loc) · 10.8 KB
/
HibernateImageGroupRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright (c) 2006 Oliver Stewart. All Rights Reserved.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package com.trailmagic.image.hibernate;
import com.trailmagic.image.Image;
import com.trailmagic.image.ImageFrame;
import com.trailmagic.image.ImageGroup;
import com.trailmagic.image.ImageGroupRepository;
import com.trailmagic.image.NoSuchImageFrameException;
import com.trailmagic.image.NoSuchImageGroupException;
import com.trailmagic.image.ImageGroup.Type;
import com.trailmagic.user.User;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Transactional(readOnly=true)
@SuppressWarnings("unchecked") // for query.list()
@Repository("imageGroupRepository")
public class HibernateImageGroupRepository implements ImageGroupRepository {
private static final String ALBUM_BY_OWNER_AND_NAME_QRY =
"albumByOwnerAndName";
private static final String IMGFRAME_BY_GROUP_NAME_TYPE_AND_IMAGE_ID_QRY =
"imageFrameByGroupNameTypeAndImageId";
private static final String ALBUMS_BY_OWNER_NAME_QRY =
"albumsByOwnerScreenName";
private static final String ROLLS_BY_OWNER_NAME_QRY =
"rollsByOwnerScreenName";
private static final String GROUP_OWNERS_QRY =
"groupOwnersByType";
private static final String GROUPS_BY_OWNER_NAME_QRY =
"groupsByOwnerScreenNameAndType";
private static final String GROUP_BY_OWNER_NAME_TYPE_QRY =
"groupByOwnerGroupNameAndType";
private static final String ROLL_BY_OWNER_AND_NAME_QRY =
"rollByOwnerAndName";
private static final String GROUPS_BY_IMAGE_QRY =
"groupsByImage";
private static final String ROLL_FOR_IMAGE_QRY =
"rollForImage";
private static final String ALL_GROUPS_QUERY =
"allImageGroups";
private SessionFactory sessionFactory;
private HibernateTemplate hibernateTemplate;
@Autowired
public HibernateImageGroupRepository(SessionFactory sessionFactory, HibernateTemplate hibernateTemplate) {
this.sessionFactory = sessionFactory;
this.hibernateTemplate = hibernateTemplate;
}
public ImageGroup getAlbumByOwnerAndName(User owner, String albumName) {
try {
Session session =
SessionFactoryUtils.getSession(sessionFactory, false);
Query qry = session.getNamedQuery(ALBUM_BY_OWNER_AND_NAME_QRY);
qry.setEntity("owner", owner);
qry.setString("albumName", albumName);
qry.setCacheable(true);
return (ImageGroup)qry.uniqueResult();
} catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
}
public ImageGroup getRollByOwnerAndName(User owner, String rollName) {
try {
Session session = SessionFactoryUtils.getSession(sessionFactory, false);
Query qry = session.getNamedQuery(ROLL_BY_OWNER_AND_NAME_QRY);
qry.setEntity("owner", owner);
qry.setString("rollName", rollName);
qry.setCacheable(true);
return (ImageGroup)qry.uniqueResult();
} catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
}
public ImageFrame getImageFrameByGroupNameTypeAndImageId(String groupName, Type groupType, long imageId)
throws NoSuchImageFrameException {
List results =
hibernateTemplate
.findByNamedQueryAndNamedParam(IMGFRAME_BY_GROUP_NAME_TYPE_AND_IMAGE_ID_QRY,
new String[] {"groupName", "groupType", "imageId"},
new Object[] {groupName, groupType, imageId});
if (results.size() == 0) {
throw new NoSuchImageFrameException(groupName, imageId);
} else {
// XXX: handle > 1 results
// XXX: this seems pretty lame
ImageFrame frame = (ImageFrame) results.get(0);
frame.getImageGroup().getFrames().first();
return frame;
}
}
public List<ImageGroup> getAlbumsByOwnerScreenName(String screenName) {
try {
Session session =
SessionFactoryUtils.getSession(sessionFactory, false);
Query qry = session.getNamedQuery(ALBUMS_BY_OWNER_NAME_QRY);
qry.setString("screenName", screenName);
qry.setCacheable(true);
return qry.list();
} catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
}
public List<ImageGroup> getRollsByOwnerScreenName(String screenName) {
try {
Session session =
SessionFactoryUtils.getSession(sessionFactory, false);
Query qry = session.getNamedQuery(ROLLS_BY_OWNER_NAME_QRY);
qry.setCacheable(true);
qry.setString("screenName", screenName);
return qry.list();
} catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
}
public List<User> getOwnersByType(Type groupType) {
try {
Session session =
SessionFactoryUtils.getSession(sessionFactory, false);
Query qry = session.getNamedQuery(GROUP_OWNERS_QRY);
qry.setString("groupType", groupType.toString());
qry.setCacheable(true);
return qry.list();
} catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
}
public List<ImageGroup> getByOwnerScreenNameAndType(String screenName,
Type groupType) {
try {
Session session =
SessionFactoryUtils.getSession(sessionFactory, false);
Query qry = session.getNamedQuery(GROUPS_BY_OWNER_NAME_QRY);
qry.setString("screenName", screenName);
qry.setString("groupType", groupType.toString());
qry.setCacheable(true);
return qry.list();
} catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
}
public ImageGroup getByOwnerNameAndTypeWithFrames(User owner, String groupName,
Type groupType) {
try {
Session session =
SessionFactoryUtils.getSession(sessionFactory, false);
Query qry = session.getNamedQuery(GROUP_BY_OWNER_NAME_TYPE_QRY);
qry.setEntity("owner", owner);
qry.setString("groupName", groupName);
qry.setString("groupType", groupType.toString());
qry.setCacheable(true);
final ImageGroup group = (ImageGroup) qry.uniqueResult();
// join fetch stopped working after enabling caching?
if (group != null && group.getFrames().size() > 0) {
group.getFrames().first();
}
return group;
} catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
}
public List<ImageGroup> getByImage(Image image) {
try {
Session session =
SessionFactoryUtils.getSession(sessionFactory, false);
Query qry = session.getNamedQuery(GROUPS_BY_IMAGE_QRY);
qry.setEntity("image", image);
qry.setCacheable(true);
return qry.list();
} catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
}
public ImageGroup getRollForImage(Image image) {
// images should always only be in one roll
try {
Session session =
SessionFactoryUtils.getSession(sessionFactory, false);
Query qry = session.getNamedQuery(ROLL_FOR_IMAGE_QRY);
qry.setEntity("image", image);
qry.setCacheable(true);
List results = qry.list();
if (results.size() < 1) {
return null;
}
return (ImageGroup) results.get(0);
} catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
}
public ImageGroup getById(long id) {
try {
Session session =
SessionFactoryUtils.getSession(sessionFactory, false);
return (ImageGroup)session.get(ImageGroup.class, id);
} catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
}
public ImageGroup getByIdWithFrames(long id) {
ImageGroup imageGroup = getById(id);
imageGroup.getFrames().first();
return imageGroup;
}
public ImageGroup loadById(long imageGroupId) throws NoSuchImageGroupException {
try {
return hibernateTemplate.load(ImageGroup.class, imageGroupId);
} catch (ObjectRetrievalFailureException e) {
throw new NoSuchImageGroupException(imageGroupId, e);
}
}
public List<ImageGroup> getAll() {
return (List<ImageGroup>)
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) {
Query qry = session.getNamedQuery(ALL_GROUPS_QUERY);
return qry.list();
}
});
}
@Transactional(readOnly=false)
public void saveNewGroup(ImageGroup newGroup) {
hibernateTemplate.save(newGroup);
}
@Transactional(readOnly=false)
public ImageGroup saveGroup(ImageGroup imageGroup) {
return hibernateTemplate.merge(imageGroup);
}
public int getPublicFrameCount(ImageGroup group) {
return (Integer)
hibernateTemplate.findByNamedQuery("publicFrameCount", group.getId()).get(0);
}
}