2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.hibernate
.test
.tree
;
19 import java
.sql
.SQLException
;
20 import java
.util
.List
;
22 import java
.util
.SortedSet
;
23 import java
.util
.TreeSet
;
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
;
36 /** Hibernate implementation of collections DAO. */
37 public class TreeTestResultCollectionDaoHibernate
extends HibernateDaoSupport
38 implements TreeTestResultCollectionDao
{
40 public void create(TreeTestResultCollection ttrCollection
) {
41 getHibernateTemplate().save(ttrCollection
);
44 public TreeTestResultCollection
getTestResultCollection(String id
) {
45 return (TreeTestResultCollection
) getHibernateTemplate().get(
46 TreeTestResultCollection
.class, id
);
49 public void update(TreeTestResultCollection ttrCollection
) {
50 getHibernateTemplate().update(ttrCollection
);
53 public void delete(TreeTestResultCollection ttrCollection
) {
54 getHibernateTemplate().delete(ttrCollection
);
57 @SuppressWarnings("unchecked")
58 public SortedSet
<TreeTestResultCollection
> listCollections() {
59 return new TreeSet
<TreeTestResultCollection
>(getHibernateTemplate()
60 .find("from TreeTestResultCollection"));
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
);
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
))
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");
101 list
= getHibernateTemplate()
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=?",
111 @SuppressWarnings("unchecked")
112 public List
<TreeTestResult
> listResults(String collectionId
,
113 Map
<String
, String
> attributes
) {
114 List
<TreeTestResult
> list
;
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()
122 Object
[] args
= { entry
.getKey(), entry
.getValue() };
123 list
= getHibernateTemplate().find(
124 "select ttr from TreeTestResult ttr"
125 + " where attributes[?]=?", args
);
127 throw new SlcException(
128 "Multiple attributes filter are currently not supported.");
131 if (attributes
== null || attributes
.size() == 0)
132 list
= getHibernateTemplate()
135 + " from TreeTestResult ttr, TreeTestResultCollection ttrc "
136 + " where ttr in elements(ttrc.results) and ttrc.id=?",
138 else if (attributes
.size() == 1) {
139 Map
.Entry
<String
, String
> entry
= attributes
.entrySet()
141 Object
[] args
= { collectionId
, entry
.getKey(),
143 list
= getHibernateTemplate()
145 "select ttr from TreeTestResult ttr, TreeTestResultCollection ttrc "
146 + " where ttr in elements(ttrc.results) and ttrc.id=?"
147 + " and attributes[?]=?", args
);
149 throw new SlcException(
150 "Multiple attributes filter are currently not supported.");