blob: 4aa7cd4d259fe9ef1f3e15e7b1a90a60bfaaa9eb [file] [log] [blame]
margaretha38d530e2017-07-11 19:06:50 +02001package de.ids_mannheim.korap.dao;
2
margarethae353dfa2017-07-18 19:23:29 +02003import java.util.List;
4
margarethad3c0fc92017-10-25 15:03:32 +02005import org.springframework.stereotype.Repository;
margaretha51e5e3f2018-10-17 15:10:03 +02006import org.springframework.transaction.annotation.Transactional;
margarethae353dfa2017-07-18 19:23:29 +02007
margaretha51e5e3f2018-10-17 15:10:03 +02008import de.ids_mannheim.korap.constant.AnnotationType;
margaretha5b708792023-05-12 16:55:29 +02009import de.ids_mannheim.korap.core.entity.Annotation;
10import de.ids_mannheim.korap.core.entity.AnnotationKey;
11import de.ids_mannheim.korap.core.entity.AnnotationKey_;
12import de.ids_mannheim.korap.core.entity.AnnotationLayer;
13import de.ids_mannheim.korap.core.entity.AnnotationLayer_;
14import de.ids_mannheim.korap.core.entity.Annotation_;
margaretha51e5e3f2018-10-17 15:10:03 +020015import de.ids_mannheim.korap.exceptions.KustvaktException;
16import de.ids_mannheim.korap.utils.ParameterChecker;
margaretha6e796842023-08-17 15:10:45 +020017import jakarta.persistence.EntityManager;
18import jakarta.persistence.NoResultException;
19import jakarta.persistence.PersistenceContext;
20import jakarta.persistence.Query;
21import jakarta.persistence.criteria.CriteriaBuilder;
22import jakarta.persistence.criteria.CriteriaQuery;
23import jakarta.persistence.criteria.Predicate;
24import jakarta.persistence.criteria.Root;
margaretha38d530e2017-07-11 19:06:50 +020025
26/**
margaretha35e1ca22023-11-16 22:00:01 +010027 * AnnotationDao manages queries to database regarding annotations
28 * including
margarethae353dfa2017-07-18 19:23:29 +020029 * foundry and layer pairs.
margaretha38d530e2017-07-11 19:06:50 +020030 *
31 * @author margaretha
32 *
33 */
margarethad3c0fc92017-10-25 15:03:32 +020034@Repository
margaretha38d530e2017-07-11 19:06:50 +020035public class AnnotationDao {
36
margarethae353dfa2017-07-18 19:23:29 +020037 @PersistenceContext
38 private EntityManager entityManager;
margaretha38d530e2017-07-11 19:06:50 +020039
margarethae353dfa2017-07-18 19:23:29 +020040 /**
margarethaa14f1c22017-07-19 18:51:04 +020041 * Retrieves all foundry-layer pairs.
margarethae353dfa2017-07-18 19:23:29 +020042 *
margarethaa14f1c22017-07-19 18:51:04 +020043 * @return a list of foundry-layer pairs.
margarethae353dfa2017-07-18 19:23:29 +020044 */
margarethadc515072018-08-03 17:01:19 +020045 @SuppressWarnings("unchecked")
margaretha51e5e3f2018-10-17 15:10:03 +020046 public List<AnnotationLayer> getAllFoundryLayerPairs () {
margarethae353dfa2017-07-18 19:23:29 +020047 CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
margaretha35e1ca22023-11-16 22:00:01 +010048 CriteriaQuery<AnnotationLayer> query = criteriaBuilder
49 .createQuery(AnnotationLayer.class);
margaretha51e5e3f2018-10-17 15:10:03 +020050 Root<AnnotationLayer> layer = query.from(AnnotationLayer.class);
51 layer.fetch(AnnotationLayer_.foundry);
52 layer.fetch(AnnotationLayer_.layer);
53 query.select(layer);
margarethae353dfa2017-07-18 19:23:29 +020054 Query q = entityManager.createQuery(query);
55 return q.getResultList();
margaretha38d530e2017-07-11 19:06:50 +020056 }
margarethae353dfa2017-07-18 19:23:29 +020057
margarethae353dfa2017-07-18 19:23:29 +020058 /**
margarethaa14f1c22017-07-19 18:51:04 +020059 * Retrieves foundry-layer pairs and their values for the given
60 * foundry and layer. If layer is empty, retrieves data for all
61 * layer in the given foundry. If foundry is empty, retrieves data
62 * for all foundry and layer pairs.
margarethae353dfa2017-07-18 19:23:29 +020063 *
margaretha50179c82017-07-20 15:36:05 +020064 * @param foundry
65 * a foundry code
66 * @param layer
67 * a layer code
margarethaa14f1c22017-07-19 18:51:04 +020068 * @return a list of foundry-layer pairs.
margarethae353dfa2017-07-18 19:23:29 +020069 */
margarethadc515072018-08-03 17:01:19 +020070 @SuppressWarnings("unchecked")
margaretha51e5e3f2018-10-17 15:10:03 +020071 public List<AnnotationLayer> getAnnotationDescriptions (String foundry,
margarethaa14f1c22017-07-19 18:51:04 +020072 String layer) {
margaretha50179c82017-07-20 15:36:05 +020073
margarethae353dfa2017-07-18 19:23:29 +020074 CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
margarethae353dfa2017-07-18 19:23:29 +020075 CriteriaQuery<Object> query = criteriaBuilder.createQuery();
margaretha35e1ca22023-11-16 22:00:01 +010076 Root<AnnotationLayer> annotationPair = query
77 .from(AnnotationLayer.class);
margaretha51e5e3f2018-10-17 15:10:03 +020078 annotationPair.fetch(AnnotationLayer_.foundry);
79 annotationPair.fetch(AnnotationLayer_.layer);
80 annotationPair.fetch(AnnotationLayer_.keys);
margaretha50179c82017-07-20 15:36:05 +020081
margaretha51e5e3f2018-10-17 15:10:03 +020082 // EM: Hibernate bug in join n:m (see AnnotationPair.values).
83 // There should not be any redundant AnnotationPair.
84 // The redundancy can be also avoided with
85 // fetch=FetchType.EAGER
86 // because Hibernate does 2 selects.
margaretha50179c82017-07-20 15:36:05 +020087 query.distinct(true);
margarethaa14f1c22017-07-19 18:51:04 +020088 query = query.select(annotationPair);
margaretha50179c82017-07-20 15:36:05 +020089
margarethaa14f1c22017-07-19 18:51:04 +020090 if (!foundry.isEmpty()) {
margarethad3c0fc92017-10-25 15:03:32 +020091 Predicate foundryPredicate = criteriaBuilder.equal(annotationPair
margaretha51e5e3f2018-10-17 15:10:03 +020092 .get(AnnotationLayer_.foundry).get(Annotation_.code),
margarethad3c0fc92017-10-25 15:03:32 +020093 foundry);
margarethaa14f1c22017-07-19 18:51:04 +020094 if (layer.isEmpty() || layer.equals("*")) {
95 query.where(foundryPredicate);
96 }
97 else {
margarethad3c0fc92017-10-25 15:03:32 +020098 Predicate layerPredicate = criteriaBuilder.equal(annotationPair
margaretha51e5e3f2018-10-17 15:10:03 +020099 .get(AnnotationLayer_.layer).get(Annotation_.code),
margarethad3c0fc92017-10-25 15:03:32 +0200100 layer);
margaretha35e1ca22023-11-16 22:00:01 +0100101 Predicate andPredicate = criteriaBuilder.and(foundryPredicate,
102 layerPredicate);
margarethaa14f1c22017-07-19 18:51:04 +0200103 query.where(andPredicate);
104 }
105 }
106
margarethae353dfa2017-07-18 19:23:29 +0200107 Query q = entityManager.createQuery(query);
108 return q.getResultList();
109 }
margaretha51e5e3f2018-10-17 15:10:03 +0200110
111 public Annotation retrieveAnnotation (String code, String type) {
112 CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
margaretha35e1ca22023-11-16 22:00:01 +0100113 CriteriaQuery<Annotation> query = criteriaBuilder
114 .createQuery(Annotation.class);
margaretha51e5e3f2018-10-17 15:10:03 +0200115
116 Root<Annotation> annotation = query.from(Annotation.class);
117 Predicate predicates = criteriaBuilder.and(
118 criteriaBuilder.equal(annotation.get(Annotation_.code), code),
119 criteriaBuilder.equal(annotation.get(Annotation_.type), type));
120 query.select(annotation).where(predicates);
121 Query q = entityManager.createQuery(query);
122 try {
123 return (Annotation) q.getSingleResult();
124 }
125 catch (NoResultException e) {
126 return null;
127 }
128 }
129
130 public AnnotationLayer retrieveAnnotationLayer (String foundry,
131 String layer) {
132 Annotation ann1 = retrieveAnnotation(foundry, AnnotationType.FOUNDRY);
133 Annotation ann2 = retrieveAnnotation(layer, AnnotationType.LAYER);
134
135 CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
margaretha35e1ca22023-11-16 22:00:01 +0100136 CriteriaQuery<AnnotationLayer> query = criteriaBuilder
137 .createQuery(AnnotationLayer.class);
margaretha51e5e3f2018-10-17 15:10:03 +0200138
139 Root<AnnotationLayer> annotation = query.from(AnnotationLayer.class);
140 Predicate predicates = criteriaBuilder.and(
141 criteriaBuilder.equal(annotation.get(AnnotationLayer_.foundry),
142 ann1),
143 criteriaBuilder.equal(annotation.get(AnnotationLayer_.layer),
144 ann2));
145 query.select(annotation).where(predicates);
146 Query q = entityManager.createQuery(query);
147 try {
148 return (AnnotationLayer) q.getSingleResult();
149 }
150 catch (NoResultException e) {
151 return null;
152 }
153
154 }
155
margarethad3a46ec2019-12-20 12:18:44 +0100156 @Transactional
margaretha51e5e3f2018-10-17 15:10:03 +0200157 public Annotation createAnnotation (String code, String type, String text,
158 String description) {
159 Annotation ann = new Annotation(code, type, text, description);
160 entityManager.persist(ann);
161 return ann;
162 }
163
margarethad3a46ec2019-12-20 12:18:44 +0100164 @Transactional
margaretha51e5e3f2018-10-17 15:10:03 +0200165 public AnnotationLayer createAnnotationLayer (Annotation foundry,
166 Annotation layer) throws KustvaktException {
167 ParameterChecker.checkObjectValue(foundry, "foundry");
168 ParameterChecker.checkObjectValue(layer, "layer");
169
170 AnnotationLayer annotationLayer = new AnnotationLayer();
171 annotationLayer.setFoundryId(foundry.getId());
172 annotationLayer.setLayerId(layer.getId());
173 annotationLayer.setDescription(
174 foundry.getDescription() + " " + layer.getDescription());
175 entityManager.persist(annotationLayer);
176 return annotationLayer;
177 }
178
margarethad3a46ec2019-12-20 12:18:44 +0100179 @Transactional
margaretha51e5e3f2018-10-17 15:10:03 +0200180 public void updateAnnotationLayer (AnnotationLayer layer) {
181 entityManager.merge(layer);
182 }
183
margarethad3a46ec2019-12-20 12:18:44 +0100184 @Transactional
margaretha51e5e3f2018-10-17 15:10:03 +0200185 public void updateAnnotationKey (AnnotationKey key) {
186 entityManager.merge(key);
187 }
188
margarethad3a46ec2019-12-20 12:18:44 +0100189 @Transactional
margaretha51e5e3f2018-10-17 15:10:03 +0200190 public AnnotationKey createAnnotationKey (AnnotationLayer layer,
191 Annotation key) {
margaretha35e1ca22023-11-16 22:00:01 +0100192 AnnotationKey annotation = new AnnotationKey(layer.getId(),
193 key.getId());
margaretha51e5e3f2018-10-17 15:10:03 +0200194 entityManager.persist(annotation);
195 return annotation;
196 }
197
198 public AnnotationKey retrieveAnnotationKey (AnnotationLayer layer,
199 Annotation key) {
200
201 CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
margaretha35e1ca22023-11-16 22:00:01 +0100202 CriteriaQuery<AnnotationKey> query = criteriaBuilder
203 .createQuery(AnnotationKey.class);
margaretha51e5e3f2018-10-17 15:10:03 +0200204
205 Root<AnnotationKey> annotation = query.from(AnnotationKey.class);
206 Predicate predicates = criteriaBuilder.and(
207 criteriaBuilder.equal(annotation.get(AnnotationKey_.layer),
208 layer),
209 criteriaBuilder.equal(annotation.get(AnnotationKey_.key), key));
210 query.select(annotation).where(predicates);
211 Query q = entityManager.createQuery(query);
212 try {
213 return (AnnotationKey) q.getSingleResult();
214 }
215 catch (NoResultException e) {
216 return null;
217 }
218 }
margaretha38d530e2017-07-11 19:06:50 +0200219}