annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/CrossSectionFactory.java @ 4255:670e98f5a441

Fixed leak while merging facets. The ThemeList that is used by OutputHelper to sort the Facets for an Output now uses a list to store the ManagedFacets. The correct order is made up by sorting the List using Collections.sort() function of the Java JDK. Therfore, the ManagedFacet class implements the Comparable interface. The return value of its compareTo(other) method depends on the value of the 'position' field.
author Ingo Weinzierl <weinzierl.ingo@googlemail.com>
date Thu, 25 Oct 2012 14:01:46 +0200
parents b2ea89a665bc
children
rev   line source
1116
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.artifacts.model;
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
2
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
3 import java.util.List;
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
4
1965
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
5 import net.sf.ehcache.Cache;
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
6 import net.sf.ehcache.Element;
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
7
1116
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
8 import de.intevation.flys.backend.SessionHolder;
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
9 import de.intevation.flys.model.CrossSection;
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
10 import de.intevation.flys.model.River;
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
11
1965
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
12 import de.intevation.flys.artifacts.cache.CacheFactory;
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
13
1116
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
14 import org.hibernate.Session;
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
15 import org.hibernate.Query;
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
16
1965
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
17
1116
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
18 /**
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
19 * Get Cross Sections.
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
20 */
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
21 public class CrossSectionFactory {
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
22
1965
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
23 protected final static String CACHE_NAME = "cross_sections";
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
24
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
25 // TODO use caching consistently, streamline acces.
1116
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
26 /**
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
27 * Get CrossSections for an instantiated River.
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
28 *
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
29 * @param river river object.
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
30 *
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
31 * @return List of Cross Sections of river.
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
32 */
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
33 public static List<CrossSection> getCrossSections(River river) {
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
34 return getCrossSections(river.getName());
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
35 }
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
36
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
37
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
38 /**
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
39 * Get Cross Sections for a river by name.
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
40 *
3269
b2ea89a665bc Fix issues in 'documentation'.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 2989
diff changeset
41 * @param riverName name of the river of interest.
1116
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
42 *
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
43 * @return List of Cross Sections of river.
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
44 */
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
45 public static List<CrossSection> getCrossSections(String riverName) {
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
46 Session session = SessionHolder.HOLDER.get();
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
47 Query query = session.createQuery(
2142
ef337d9fa5ea Cosmetics.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 2039
diff changeset
48 "from CrossSection where river.name = :rivername");
1116
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
49 query.setParameter("rivername", riverName);
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
50 return query.list();
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
51 }
1965
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
52
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
53
2039
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
54 /**
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
55 * True if the given section is the "newest" for that river.
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
56 * @param section Given section
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
57 * @return true if the section has the most advanced end of its validity interval
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
58 * or the most advanced start of its validity interval.
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
59 */
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
60 public static boolean isNewest(CrossSection section) {
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
61 Session session = SessionHolder.HOLDER.get();
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
62 Query query = session.createQuery(
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
63 "from CrossSection where river.id = :riverid "
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
64 + " order by timeInterval.stopTime desc, timeInterval.startTime desc");
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
65 query.setParameter("riverid", section.getRiver().getId());
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
66
2989
ee3dbc4a802d Added more checks in CrossSectionFactory.isNewst() to prevent IndexOutOfBoundsExceptions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2142
diff changeset
67 List result = query.list();
ee3dbc4a802d Added more checks in CrossSectionFactory.isNewst() to prevent IndexOutOfBoundsExceptions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2142
diff changeset
68
ee3dbc4a802d Added more checks in CrossSectionFactory.isNewst() to prevent IndexOutOfBoundsExceptions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2142
diff changeset
69 if (result == null || result.isEmpty()) {
ee3dbc4a802d Added more checks in CrossSectionFactory.isNewst() to prevent IndexOutOfBoundsExceptions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2142
diff changeset
70 return true;
ee3dbc4a802d Added more checks in CrossSectionFactory.isNewst() to prevent IndexOutOfBoundsExceptions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2142
diff changeset
71 }
ee3dbc4a802d Added more checks in CrossSectionFactory.isNewst() to prevent IndexOutOfBoundsExceptions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2142
diff changeset
72 else {
ee3dbc4a802d Added more checks in CrossSectionFactory.isNewst() to prevent IndexOutOfBoundsExceptions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2142
diff changeset
73 CrossSection cs = (CrossSection) result.get(0);
ee3dbc4a802d Added more checks in CrossSectionFactory.isNewst() to prevent IndexOutOfBoundsExceptions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2142
diff changeset
74 return section.getId().equals(cs.getId());
ee3dbc4a802d Added more checks in CrossSectionFactory.isNewst() to prevent IndexOutOfBoundsExceptions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2142
diff changeset
75 }
2039
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
76 }
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
77
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
78
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
79 /**
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
80 * Get a specific CrossSection from db.
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
81 * @param id The dbid of the cross-section to load.
f7e2a84a6d88 Added function to check whether a CrossSection is the 'newest' for its river.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1965
diff changeset
82 */
1965
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
83 public static CrossSection getCrossSection(int id) {
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
84 Cache cache = CacheFactory.getCache(CACHE_NAME);
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
85 if (cache != null) {
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
86 Element element = cache.get(Integer.valueOf(id));
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
87 if (element != null) {
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
88 return (CrossSection) element.getValue();
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
89 }
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
90 }
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
91
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
92 CrossSection section = getCrossSectionUncached(id);
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
93 if (cache != null) {
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
94 Element element = new Element(Integer.valueOf(id), section);
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
95 cache.put(element);
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
96 }
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
97
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
98 return section;
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
99 }
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
100
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
101
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
102 /** Get specific CrossSection from database. */
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
103 protected static CrossSection getCrossSectionUncached(int id) {
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
104 Session session = SessionHolder.HOLDER.get();
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
105 Query query = session.createQuery(
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
106 "from CrossSection where id=:id");
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
107 query.setParameter("id", id);
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
108 List<CrossSection> list = query.list();
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
109 return list.isEmpty() ? null : list.get(0);
e71615d95afb Added method to CrossSectionFactory to get single CrossSection, employ cache.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 1962
diff changeset
110 }
1116
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
111 }
05e4ef0f9489 Improved stub for CrossSections (including Factory and Facet).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
112 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org