felix@1116: package de.intevation.flys.artifacts.model; felix@1116: felix@1116: import java.util.List; felix@1116: felix@1965: import net.sf.ehcache.Cache; felix@1965: import net.sf.ehcache.Element; felix@1965: felix@1116: import de.intevation.flys.backend.SessionHolder; felix@1116: import de.intevation.flys.model.CrossSection; felix@1116: import de.intevation.flys.model.River; felix@1116: felix@1965: import de.intevation.flys.artifacts.cache.CacheFactory; felix@1965: felix@1116: import org.hibernate.Session; felix@1116: import org.hibernate.Query; felix@1116: felix@1965: felix@1116: /** felix@1116: * Get Cross Sections. felix@1116: */ felix@1116: public class CrossSectionFactory { felix@1116: felix@1965: protected final static String CACHE_NAME = "cross_sections"; felix@1965: felix@1965: // TODO use caching consistently, streamline acces. felix@1116: /** felix@1116: * Get CrossSections for an instantiated River. felix@1116: * felix@1116: * @param river river object. felix@1116: * felix@1116: * @return List of Cross Sections of river. felix@1116: */ felix@1116: public static List getCrossSections(River river) { felix@1116: return getCrossSections(river.getName()); felix@1116: } felix@1116: felix@1116: felix@1116: /** felix@1116: * Get Cross Sections for a river by name. felix@1116: * felix@1116: * @param river name of the river of interest. felix@1116: * felix@1116: * @return List of Cross Sections of river. felix@1116: */ felix@1116: public static List getCrossSections(String riverName) { felix@1116: Session session = SessionHolder.HOLDER.get(); felix@1116: Query query = session.createQuery( felix@2142: "from CrossSection where river.name = :rivername"); felix@1116: query.setParameter("rivername", riverName); felix@1116: return query.list(); felix@1116: } felix@1965: felix@1965: felix@2039: /** felix@2039: * True if the given section is the "newest" for that river. felix@2039: * @param section Given section felix@2039: * @return true if the section has the most advanced end of its validity interval felix@2039: * or the most advanced start of its validity interval. felix@2039: */ felix@2039: public static boolean isNewest(CrossSection section) { felix@2039: Session session = SessionHolder.HOLDER.get(); felix@2039: Query query = session.createQuery( felix@2039: "from CrossSection where river.id = :riverid " felix@2039: + " order by timeInterval.stopTime desc, timeInterval.startTime desc"); felix@2039: query.setParameter("riverid", section.getRiver().getId()); felix@2039: felix@2039: CrossSection cs = (CrossSection) query.list().get(0); felix@2039: return section.getId().equals(cs.getId()); felix@2039: } felix@2039: felix@2039: felix@2039: /** felix@2039: * Get a specific CrossSection from db. felix@2039: * @param id The dbid of the cross-section to load. felix@2039: */ felix@1965: public static CrossSection getCrossSection(int id) { felix@1965: Cache cache = CacheFactory.getCache(CACHE_NAME); felix@1965: if (cache != null) { felix@1965: Element element = cache.get(Integer.valueOf(id)); felix@1965: if (element != null) { felix@1965: return (CrossSection) element.getValue(); felix@1965: } felix@1965: } felix@1965: felix@1965: CrossSection section = getCrossSectionUncached(id); felix@1965: if (cache != null) { felix@1965: Element element = new Element(Integer.valueOf(id), section); felix@1965: cache.put(element); felix@1965: } felix@1965: felix@1965: return section; felix@1965: } felix@1965: felix@1965: felix@1965: /** Get specific CrossSection from database. */ felix@1965: protected static CrossSection getCrossSectionUncached(int id) { felix@1965: Session session = SessionHolder.HOLDER.get(); felix@1965: Query query = session.createQuery( felix@1965: "from CrossSection where id=:id"); felix@1965: query.setParameter("id", id); felix@1965: List list = query.list(); felix@1965: return list.isEmpty() ? null : list.get(0); felix@1965: } felix@1116: } felix@1116: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :