]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.hibernate/src/main/java/org/argeo/slc/hibernate/test/tree/TreeTestResultCollectionDaoHibernate.java
Finalize JMS serialization
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.hibernate / src / main / java / org / argeo / slc / hibernate / test / tree / TreeTestResultCollectionDaoHibernate.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.hibernate.test.tree;
18
19 import java.sql.SQLException;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.SortedSet;
23 import java.util.TreeSet;
24
25 import org.argeo.slc.SlcException;
26 import org.argeo.slc.core.test.tree.ResultAttributes;
27 import org.argeo.slc.core.test.tree.TreeTestResult;
28 import org.argeo.slc.core.test.tree.TreeTestResultCollection;
29 import org.argeo.slc.dao.test.tree.TreeTestResultCollectionDao;
30 import org.hibernate.HibernateException;
31 import org.hibernate.LockMode;
32 import org.hibernate.Session;
33 import org.springframework.orm.hibernate3.HibernateCallback;
34 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
35
36 /** Hibernate implementation of collections DAO. */
37 public class TreeTestResultCollectionDaoHibernate extends HibernateDaoSupport
38 implements TreeTestResultCollectionDao {
39
40 public void create(TreeTestResultCollection ttrCollection) {
41 getHibernateTemplate().save(ttrCollection);
42 }
43
44 public TreeTestResultCollection getTestResultCollection(String id) {
45 return (TreeTestResultCollection) getHibernateTemplate().get(
46 TreeTestResultCollection.class, id);
47 }
48
49 public void update(TreeTestResultCollection ttrCollection) {
50 getHibernateTemplate().update(ttrCollection);
51 }
52
53 public void delete(TreeTestResultCollection ttrCollection) {
54 getHibernateTemplate().delete(ttrCollection);
55 }
56
57 @SuppressWarnings("unchecked")
58 public SortedSet<TreeTestResultCollection> listCollections() {
59 return new TreeSet<TreeTestResultCollection>(getHibernateTemplate()
60 .find("from TreeTestResultCollection"));
61 }
62
63 public void addResultToCollection(final TreeTestResultCollection ttrc,
64 final String resultUuid) {
65 getHibernateTemplate().execute(new HibernateCallback() {
66 public Object doInHibernate(Session session)
67 throws HibernateException, SQLException {
68 session.lock(ttrc, LockMode.NONE);
69 TreeTestResult ttr = (TreeTestResult) session.get(
70 TreeTestResult.class, resultUuid);
71 ttrc.getResults().add(ttr);
72 session.update(ttrc);
73 return ttrc;
74 }
75 });
76 }
77
78 public void removeResultFromCollection(final TreeTestResultCollection ttrc,
79 final String resultUuid) {
80 getHibernateTemplate().execute(new HibernateCallback() {
81 public Object doInHibernate(Session session)
82 throws HibernateException, SQLException {
83 session.lock(ttrc, LockMode.NONE);
84 TreeTestResult ttr = (TreeTestResult) session.get(
85 TreeTestResult.class, resultUuid);
86 if (ttrc.getResults().remove(ttr))
87 session.update(ttrc);
88 return ttrc;
89 }
90 });
91 }
92
93 @SuppressWarnings("unchecked")
94 public List<ResultAttributes> listResultAttributes(String collectionId) {
95 List<ResultAttributes> list;
96 if (collectionId == null)
97 list = getHibernateTemplate().find(
98 "select new org.argeo.slc.core.test.tree.ResultAttributes(ttr)"
99 + " from TreeTestResult ttr");
100 else
101 list = getHibernateTemplate()
102 .find(
103 "select new org.argeo.slc.core.test.tree.ResultAttributes(ttr) "
104 + " from TreeTestResult ttr, TreeTestResultCollection ttrc "
105 + " where ttr in elements(ttrc.results) and ttrc.id=?",
106 collectionId);
107
108 return list;
109 }
110
111 @SuppressWarnings("unchecked")
112 public List<TreeTestResult> listResults(String collectionId,
113 Map<String, String> attributes) {
114 List<TreeTestResult> list;
115
116 if (collectionId == null) {
117 if (attributes == null || attributes.size() == 0)
118 list = getHibernateTemplate().find("from TreeTestResult");
119 else if (attributes.size() == 1) {
120 Map.Entry<String, String> entry = attributes.entrySet()
121 .iterator().next();
122 Object[] args = { entry.getKey(), entry.getValue() };
123 list = getHibernateTemplate().find(
124 "select ttr from TreeTestResult ttr"
125 + " where attributes[?]=?", args);
126 } else {
127 throw new SlcException(
128 "Multiple attributes filter are currently not supported.");
129 }
130 } else {
131 if (attributes == null || attributes.size() == 0)
132 list = getHibernateTemplate()
133 .find(
134 "select ttr "
135 + " from TreeTestResult ttr, TreeTestResultCollection ttrc "
136 + " where ttr in elements(ttrc.results) and ttrc.id=?",
137 collectionId);
138 else if (attributes.size() == 1) {
139 Map.Entry<String, String> entry = attributes.entrySet()
140 .iterator().next();
141 Object[] args = { collectionId, entry.getKey(),
142 entry.getValue() };
143 list = getHibernateTemplate()
144 .find(
145 "select ttr from TreeTestResult ttr, TreeTestResultCollection ttrc "
146 + " where ttr in elements(ttrc.results) and ttrc.id=?"
147 + " and attributes[?]=?", args);
148 } else {
149 throw new SlcException(
150 "Multiple attributes filter are currently not supported.");
151 }
152 }
153 return list;
154 }
155 }