Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/model/RiverAxis.java @ 8747:e9d912c97fa8
We expect that every river has only one RiverAxis of kind 1
This allowed to reduce code duplication and some cleanups.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Fri, 05 Jun 2015 16:56:47 +0200 |
parents | 4c3ccf2b0304 |
children | 5e38e2924c07 |
comparison
equal
deleted
inserted
replaced
8746:e701ef544ffa | 8747:e9d912c97fa8 |
---|---|
27 import org.hibernate.Query; | 27 import org.hibernate.Query; |
28 import org.hibernate.Session; | 28 import org.hibernate.Session; |
29 | 29 |
30 import org.hibernate.annotations.Type; | 30 import org.hibernate.annotations.Type; |
31 | 31 |
32 import org.apache.log4j.Logger; | |
33 | |
32 /** | 34 /** |
33 * A river has one axis that is used for calculation. | 35 * A river has one axis that is used for calculation. |
34 * Additional axes of a river can be used to be painted int maps etc. | 36 * Additional axes of a river can be used to be painted int maps etc. |
35 * which one is the main river axis can be determined over the axis kind. | 37 * which one is the main river axis can be determined over the axis kind. |
36 */ | 38 */ |
37 @Entity | 39 @Entity |
38 @Table(name = "river_axes") | 40 @Table(name = "river_axes") |
39 public class RiverAxis | 41 public class RiverAxis |
40 implements Serializable | 42 implements Serializable |
41 { | 43 { |
44 private static Logger log = Logger.getLogger(RiverAxis.class); | |
45 | |
42 private Integer id; | 46 private Integer id; |
43 private AxisKind kind; | 47 private AxisKind kind; |
44 private River river; | 48 private River river; |
45 private String name; | 49 private String name; |
46 private MultiLineString geom; | 50 private MultiLineString geom; |
117 public void setGeom(MultiLineString geom) { | 121 public void setGeom(MultiLineString geom) { |
118 this.geom = geom; | 122 this.geom = geom; |
119 } | 123 } |
120 | 124 |
121 | 125 |
122 public static List<RiverAxis> getRiverAxis(String river) | |
123 throws IllegalArgumentException { | |
124 return getRiverAxis(river, KIND_CURRENT); | |
125 } | |
126 | |
127 public static List<RiverAxis> getRiverAxis(String river, String name, int kind) | 126 public static List<RiverAxis> getRiverAxis(String river, String name, int kind) |
128 throws HibernateException { | 127 throws HibernateException { |
129 Session session = SessionHolder.HOLDER.get(); | 128 Session session = SessionHolder.HOLDER.get(); |
130 Query query = session.createQuery( | 129 Query query = session.createQuery( |
131 "from RiverAxis as ax where river.name =:river" + | 130 "from RiverAxis as ax where river.name =:river" + |
137 | 136 |
138 List<RiverAxis> list = query.list(); | 137 List<RiverAxis> list = query.list(); |
139 return list.isEmpty() ? null : list; | 138 return list.isEmpty() ? null : list; |
140 } | 139 } |
141 | 140 |
142 public static List<RiverAxis> getRiverAxis(String river, int kind) | 141 public static RiverAxis getRiverAxis(String river) |
143 throws HibernateException { | 142 throws HibernateException { |
144 Session session = SessionHolder.HOLDER.get(); | 143 Session session = SessionHolder.HOLDER.get(); |
145 Query query = session.createQuery( | 144 Query query = session.createQuery( |
146 "from RiverAxis where river.name =:river AND kind.id =:kind"); | 145 "from RiverAxis where river.name =:river AND kind.id =:kind"); |
147 query.setParameter("river", river); | 146 query.setParameter("river", river); |
148 query.setParameter("kind", kind); | 147 query.setParameter("kind", KIND_CURRENT); |
149 | 148 |
150 List<RiverAxis> list = query.list(); | 149 List<RiverAxis> list = query.list(); |
151 return list.isEmpty() ? null : list; | 150 |
151 /* We expect that every river has only one RiverAxis of kind 1 | |
152 thow this is not currently enforced in database schema. */ | |
153 if (list.size() > 1) { | |
154 log.error("River " + river + " has more than one current axis."); | |
155 } | |
156 | |
157 return list.isEmpty() ? null : list.get(0); | |
152 } | 158 } |
153 } | 159 } |
154 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 160 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |