annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/AnnotationsFactory.java @ 1924:57b03b3ab15c

Optimize query to fix annotations fetching on oracle, by Sascha Teichmann. flys-artifacts/trunk@3300 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 22 Nov 2011 13:39:23 +0000
parents bcef54a563ea
children 01f2473e362d
rev   line source
301
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.artifacts.model;
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
2
1031
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
3 import java.math.BigDecimal;
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
4
301
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
5 import java.util.List;
644
02c0cce0e469 Introduce a cache for the distance-info service
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 332
diff changeset
6 import java.util.Iterator;
1924
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
7 import java.util.Collections;
301
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
8
315
94c176fdcb89 Removed the SessionHolder and adapted imports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 311
diff changeset
9 import de.intevation.flys.backend.SessionHolder;
301
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
10 import de.intevation.flys.model.Annotation;
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
11 import de.intevation.flys.model.Range;
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
12 import de.intevation.flys.model.River;
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
13
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
14 import org.hibernate.Session;
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
15 import org.hibernate.Query;
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
16
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
17 /**
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
18 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
19 */
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
20 public class AnnotationsFactory {
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
21
1031
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
22 /**
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
23 * Get Annotations which do not have a "b" ("to")-value set.
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
24 *
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
25 * @param river name of the river of interest.
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
26 *
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
27 * @return List of Annotations for river which have only "a" ("from")
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
28 * value set.
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
29 */
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
30 public static List<Annotation> getPointAnnotations(String river) {
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
31 Session session = SessionHolder.HOLDER.get();
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
32
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
33 Query rangesQuery = session.createQuery(
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
34 "from Range where river.name=:name and b = null");
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
35 rangesQuery.setParameter("name", river);
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
36 List<Range> ranges = rangesQuery.list();
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
37
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
38 Query query = session.createQuery(
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
39 "from Annotation where range in (:ranges) order by range.a");
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
40 query.setParameterList("ranges", ranges);
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
41 return query.list();
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
42 }
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
43
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
44
1924
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
45 public static List<Annotation> getAnnotations(River river) {
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
46 Session session = SessionHolder.HOLDER.get();
301
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
47
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
48 Query query = session.createQuery(
1924
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
49 "from Annotation as an where an.range.river = :river" +
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
50 " order by an.range.a");
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
51 query.setParameter("river", river);
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
52 return query.list();
301
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
53 }
644
02c0cce0e469 Introduce a cache for the distance-info service
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 332
diff changeset
54
1031
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
55 /**
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
56 * Get minimal "a" ("from") and maximal "b" ("to") value of annotations'
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
57 * ranges of a river.
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
58 *
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
59 * @param river name of the river of interest.
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
60 *
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
61 * @return Array containing minimal "a" and max "b" value of any
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
62 * annotation stored for the given river.
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
63 */
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
64 public static double[] getAnnotationsBreadth(String river) {
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
65 Session session = SessionHolder.HOLDER.get();
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
66
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
67 Query minAQuery = session.createQuery(
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
68 "select min(a), max(b) from Range where river.name=:name");
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
69 minAQuery.setParameter("name", river);
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
70
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
71 double[] minAmaxB = {0.0f, 0.0f};
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
72 Object[] row = (Object[]) minAQuery.list().iterator().next();
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
73 minAmaxB[0] = ((BigDecimal) row[0]).doubleValue();
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
74 minAmaxB[1] = ((BigDecimal) row[1]).doubleValue();
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
75 return minAmaxB;
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
76 }
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
77
bcef54a563ea Added functionality to query range and point-annotations only to
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 644
diff changeset
78
1924
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
79 public static Iterator<Annotation> getAnnotationsIterator(
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
80 String riverName
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
81 ) {
644
02c0cce0e469 Introduce a cache for the distance-info service
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 332
diff changeset
82 Session session = SessionHolder.HOLDER.get();
02c0cce0e469 Introduce a cache for the distance-info service
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 332
diff changeset
83
1924
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
84 Query riverQuery = session.createQuery(
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
85 "from River where name = :name");
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
86 riverQuery.setParameter("name", riverName);
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
87 List<River> rivers = riverQuery.list();
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
88 if (rivers.isEmpty()) {
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
89 List<Annotation> list = Collections.emptyList();
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
90 return list.iterator();
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
91 }
644
02c0cce0e469 Introduce a cache for the distance-info service
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 332
diff changeset
92
02c0cce0e469 Introduce a cache for the distance-info service
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 332
diff changeset
93 Query query = session.createQuery(
1924
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
94 "from Annotation as an" +
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
95 " where an.range.river = :river order by an.range.a");
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
96 query.setParameter("river", rivers.get(0));
57b03b3ab15c Optimize query to fix annotations fetching on oracle, by Sascha Teichmann.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1031
diff changeset
97
644
02c0cce0e469 Introduce a cache for the distance-info service
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 332
diff changeset
98 return (Iterator<Annotation>)query.iterate();
02c0cce0e469 Introduce a cache for the distance-info service
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 332
diff changeset
99 }
301
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
100 }
1a00cb4998a8 Implemented a service that retrieves distance information for a specific river.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
101 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org