view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java @ 2081:aa9cc91c8193

#444 Take care on NullPointerExceptions in getWQMode. Take 'false' as default value if no 'wq_free' item is existing. flys-artifacts/trunk@3597 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 05 Jan 2012 14:15:07 +0000
parents 97a25b54eea3
children 0c7847b8e85e
line wrap: on
line source
package de.intevation.flys.artifacts.model;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.apache.log4j.Logger;

import de.intevation.flys.model.Annotation;
import de.intevation.flys.model.Position;

import de.intevation.flys.artifacts.cache.CacheFactory;
import de.intevation.flys.artifacts.model.AnnotationsFactory;


public class LocationProvider {

    public static final String CACHE_KEY = "location-provider";


    private static final Logger logger =
        Logger.getLogger(LocationProvider.class);


    private LocationProvider() {
    }


    public static String getLocation(String river, double km) {
        return getLocation(getLocationHash(river, km), river, km);
    }


    public static String getLocation(String hash, String river, double km) {
        logger.debug("Fetch location for '" + river + "' at '" + km + "'");

        Cache cache = CacheFactory.getCache(CACHE_KEY);

        if (cache != null) {
            return getCachedLocation(cache, hash, river, km);
        }
        else {
            logger.info("No Cache for Locations configured.");
            return getUncachedLocation(river, km);
        }
    }


    protected static String getCachedLocation(
        Cache cache,
        String hash,
        String river,
        double km
    ) {
        logger.debug("Fetch location from cache.");

        Element element = cache.get(hash);

        if (element == null) {
            logger.debug("Element is not in cache yet.");

            String location = getUncachedLocation(river, km);
            element = new Element(hash, location);
            cache.put(element);
        }

        return (String) element.getValue();
    }


    protected static String getUncachedLocation(String river, double km) {
        logger.debug("Fetch location from backend.");

        Annotation annotation = AnnotationsFactory.getAnnotation(river, km);

        if (annotation != null) {
            logger.debug("Found an annotation.");
            Position pos = annotation.getPosition();
            return pos.getValue();
        }

        return "";
    }


    protected static String getLocationHash(String river, double km) {
        return river + "#" + km;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org