comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/CrossSectionFactory.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/CrossSectionFactory.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts.model;
2
3 import java.util.List;
4
5 import net.sf.ehcache.Cache;
6 import net.sf.ehcache.Element;
7
8 import org.dive4elements.river.backend.SessionHolder;
9 import org.dive4elements.river.model.CrossSection;
10 import org.dive4elements.river.model.River;
11
12 import org.dive4elements.river.artifacts.cache.CacheFactory;
13
14 import org.hibernate.Session;
15 import org.hibernate.Query;
16
17
18 /**
19 * Get Cross Sections.
20 */
21 public class CrossSectionFactory {
22
23 protected final static String CACHE_NAME = "cross_sections";
24
25 // TODO use caching consistently, streamline acces.
26 /**
27 * Get CrossSections for an instantiated River.
28 *
29 * @param river river object.
30 *
31 * @return List of Cross Sections of river.
32 */
33 public static List<CrossSection> getCrossSections(River river) {
34 return getCrossSections(river.getName());
35 }
36
37
38 /**
39 * Get Cross Sections for a river by name.
40 *
41 * @param riverName name of the river of interest.
42 *
43 * @return List of Cross Sections of river.
44 */
45 public static List<CrossSection> getCrossSections(String riverName) {
46 Session session = SessionHolder.HOLDER.get();
47 Query query = session.createQuery(
48 "from CrossSection where river.name = :rivername");
49 query.setParameter("rivername", riverName);
50 return query.list();
51 }
52
53
54 /**
55 * True if the given section is the "newest" for that river.
56 * @param section Given section
57 * @return true if the section has the most advanced end of its validity interval
58 * or the most advanced start of its validity interval.
59 */
60 public static boolean isNewest(CrossSection section) {
61 Session session = SessionHolder.HOLDER.get();
62 Query query = session.createQuery(
63 "from CrossSection where river.id = :riverid "
64 + " order by timeInterval.stopTime desc, timeInterval.startTime desc");
65 query.setParameter("riverid", section.getRiver().getId());
66
67 List result = query.list();
68
69 if (result == null || result.isEmpty()) {
70 return true;
71 }
72 else {
73 CrossSection cs = (CrossSection) result.get(0);
74 return section.getId().equals(cs.getId());
75 }
76 }
77
78
79 /**
80 * Get a specific CrossSection from db.
81 * @param id The dbid of the cross-section to load.
82 */
83 public static CrossSection getCrossSection(int id) {
84 Cache cache = CacheFactory.getCache(CACHE_NAME);
85 if (cache != null) {
86 Element element = cache.get(Integer.valueOf(id));
87 if (element != null) {
88 return (CrossSection) element.getValue();
89 }
90 }
91
92 CrossSection section = getCrossSectionUncached(id);
93 if (cache != null) {
94 Element element = new Element(Integer.valueOf(id), section);
95 cache.put(element);
96 }
97
98 return section;
99 }
100
101
102 /** Get specific CrossSection from database. */
103 protected static CrossSection getCrossSectionUncached(int id) {
104 Session session = SessionHolder.HOLDER.get();
105 Query query = session.createQuery(
106 "from CrossSection where id=:id");
107 query.setParameter("id", id);
108 List<CrossSection> list = query.list();
109 return list.isEmpty() ? null : list.get(0);
110 }
111 }
112 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org