Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/context/GNVArtifactContextFactory.java @ 438:7399bb8f83ea
Added manager to handle several levels of palette subdivsions.
Added parameter ids to identify the palette associated with a parameter
gnv-artifacts/trunk@486 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 28 Dec 2009 11:50:27 +0000 |
parents | 3a0c0ad113d9 |
children | 52e031261eaa |
line wrap: on
line source
/** * */ package de.intevation.gnv.artifacts.context; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.File; import java.util.Properties; import java.util.HashMap; import java.util.ArrayList; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import de.intevation.gnv.geobackend.base.connectionpool.ConnectionPoolFactory; import de.intevation.gnv.geobackend.base.query.container.QueryContainerFactory; import de.intevation.gnv.geobackend.base.query.container.exception.QueryContainerException; import de.intevation.gnv.chart.XMLChartTheme; import de.intevation.gnv.artifacts.cache.CacheFactory; import de.intevation.gnv.raster.Palette; import de.intevation.gnv.raster.PaletteManager; import de.intevation.artifacts.ArtifactContextFactory; import de.intevation.artifactdatabase.Config; import de.intevation.artifactdatabase.XMLUtils; /** * @author Tim Englich <tim.englich@intevation.de> * */ public class GNVArtifactContextFactory implements ArtifactContextFactory { /** * the logger, used to log exceptions and additonaly information */ private static Logger log = Logger.getLogger(GNVArtifactContext.class); public static final String XPATH_GEOBACKEND_CONFIGURATION = "artifact-database/geo-backend/backend-configuration"; public static final String XPATH_GEOBACKEND_QUERYCONFIGURATION = "artifact-database/geo-backend/query-configuration"; private final static String CACHECONFIGNODEPATH = "/artifact-database/ehcache/configuration"; private final static String CHARTCONFIGNODEPATH = "/artifact-database/charttemplate/configuration"; public final static String PALETTES_PATH = "/artifact-database/palettes"; public final static String PALETTE_ITEMS = "palette"; public final static String PALETTES = "color.palettes"; public final static String CHARTTEMPLATE = "template"; /** * Constructor */ public GNVArtifactContextFactory() { super(); log.debug("GNVArtifactContextFactory.Constructor"); } /** * @see de.intevation.artifacts.ArtifactContextFactory#createArtifactContext(org.w3c.dom.Document) */ public Object createArtifactContext(Document config) { GNVArtifactContext returnValue = null; try { log.debug("GNVArtifactContextFactory.createArtifactContext"); log.info("Initialisation of the Geo-BackendConnectionPool"); String backendConfigurationFile = Config.getStringXPath(config, XPATH_GEOBACKEND_CONFIGURATION); backendConfigurationFile = Config.replaceConfigDir( backendConfigurationFile); Properties properties = getProperties(backendConfigurationFile); ConnectionPoolFactory cpf = ConnectionPoolFactory.getInstance(); cpf.initializeConnectionPool(properties); log.info("Initialisation of the QueryContainer"); String queryConfigurationFile = Config.getStringXPath(config, XPATH_GEOBACKEND_QUERYCONFIGURATION); queryConfigurationFile = Config.replaceConfigDir( queryConfigurationFile); Properties queryProperties = getProperties(queryConfigurationFile); QueryContainerFactory qcf = QueryContainerFactory.getInstance(); qcf.initializeQueryContainer(queryProperties); log.info("Initialisation of the Cache"); String cacheConfigurationFile = Config.getStringXPath(config, CACHECONFIGNODEPATH); cacheConfigurationFile = Config.replaceConfigDir( cacheConfigurationFile); CacheFactory cf = CacheFactory.getInstance(); cf.initializeCache(cacheConfigurationFile); returnValue = new GNVArtifactContext(config); configurePalettes(config, returnValue); configureChartTemplate(config, returnValue); } catch (FileNotFoundException e) { log.error(e, e); } catch (IOException e) { log.error(e, e); } catch (QueryContainerException e) { log.error(e, e); } return returnValue; } protected void configureChartTemplate( Document config, GNVArtifactContext context ) { log.info("Initialisation of chart template"); String chartConfigFile = Config.getStringXPath( config, CHARTCONFIGNODEPATH ); chartConfigFile = Config.replaceConfigDir(chartConfigFile); log.debug("Parse xml configuration of " + chartConfigFile); Document tmpl = XMLUtils.parseDocument(new File(chartConfigFile)); XMLChartTheme theme = new XMLChartTheme("XMLChartTheme"); if (tmpl != null) { theme.applyXMLConfiguration(tmpl); } else { log.error( "Cannot load chart template from '" + chartConfigFile + "'"); } context.put(CHARTTEMPLATE, theme); } protected void configurePalettes( Document config, GNVArtifactContext context ) { log.info("configure palettes"); HashMap<Integer, PaletteManager> palettes = new HashMap(); Node node = Config.getNodeXPath(config, PALETTES_PATH); if (node == null) { log.error("No palettes found"); } else { NodeList pals = Config.getNodeSetXPath(PALETTE_ITEMS); for (int i = 0, N = pals == null ? 0 : pals.getLength(); i < N; ++i) { Element pal = (Element)pals.item(i); String name = pal.getAttribute("name"); String description = pal.getAttribute("description"); String filename = pal.getAttribute("file"); String parameterIds = pal.getAttribute("parameter-ids"); if (name == null || (name = name.trim()).length() == 0) { log.error("Palette has no 'name' attribute."); } else if (filename == null || (filename = filename.trim()).length() == 0) { log.error("Palette '" + name + "' has no 'file' attribute."); } else if (parameterIds == null || (parameterIds = parameterIds.trim()).length() == 0) { log.error("no parameter ids given for '" + name + "'"); } else { ArrayList<Integer> ids = new ArrayList<Integer>(); for (String idString: parameterIds.split("[\t ,]+")) { try { ids.add(Integer.valueOf(idString)); } catch (NumberFormatException nfe) { log.error( "parameter id '" + idString + "' is integer"); } } filename = Config.replaceConfigDir(filename); Document document = XMLUtils.parseDocument( new File(filename)); if (document == null) { log.error("Cannot load palette file '" + filename + "'"); } else { PaletteManager manager = new PaletteManager( name, description, new Palette(document)); for (Integer id: ids) { palettes.put(id, manager); } } } } // for all palettes } context.put(PALETTES, palettes); } /** * @param filePath * @return * @throws FileNotFoundException * @throws IOException */ private Properties getProperties(String filePath) throws FileNotFoundException, IOException { InputStream inputStream = null; try { inputStream = new FileInputStream(filePath); Properties properties = new Properties(); properties.load(inputStream); return properties; } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ioe) {} } } } }