Java tutorial
/* * Copyright (C) 2007-2012 GeoSolutions S.A.S. * http://www.geo-solutions.it * * GPLv3 + Classpath exception * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package it.geosolutions.geobatch.destination.vulnerability; import it.geosolutions.geobatch.destination.common.utils.FeatureLoaderUtils; import it.geosolutions.geobatch.destination.common.utils.PropertiesManager; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; import org.apache.commons.collections.ListUtils; import org.geotools.data.DataStore; import org.geotools.jdbc.JDBCDataStore; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Loads all external property files used by the vulnerability process. * TODO: create a unified xml containing all the needed info. * * @author DamianoG * */ public class TargetPropertiesLoader extends PropertiesManager { private final static Logger LOGGER = LoggerFactory.getLogger(TargetPropertiesLoader.class); private final static String DISTANZA_ATTRIBUTE_NAME = "distanza"; private final static String EXTERNAL_PROP_DIR_PATH = "EXTERNAL_PROP_DIR_PATH"; private final static String RASTER_PATH_PROP = "/rasterpath.properties"; private final static String RASTER_FIELDS_PROP = "/rasterfields.properties"; private final static String ZONE_VALUES = "/zonevalues.properties"; //private final static String DISTANZE_PROP = "/distances.properties"; //private final static String VULNERABILITY_CFG_FILE = "/vulnerability.xml"; public final static String NON_UMANI_PREFIX = "nonumani"; public final static String COP_SUOLO_PREFIX = "nonumani.copsuolo"; public final static String UMANI_PREFIX = "umani"; public final static String PROP_NAMESPACE_SEPARATOR = "."; private Properties targetURIs; private Properties targetMapping; private Properties targetZoneValues; private Map<Integer, String> targetValuesZone; private List<Integer> allCopSuoloValues; public TargetPropertiesLoader() { // TODO load them into the getter method. String basePath = System.getProperty(EXTERNAL_PROP_DIR_PATH, ""); targetURIs = loadProperty(basePath, RASTER_PATH_PROP); targetMapping = loadProperty(basePath, RASTER_FIELDS_PROP); targetZoneValues = loadProperty(basePath, ZONE_VALUES); allCopSuoloValues = getKeysStartsWith(targetZoneValues, COP_SUOLO_PREFIX); targetValuesZone = loadTargetValuesZone(); } private Map<Integer, String> loadTargetValuesZone() { targetValuesZone = new HashMap<Integer, String>(); Iterator iter = targetZoneValues.keySet().iterator(); while (iter.hasNext()) { String el = (String) iter.next(); String elSplitted[] = el.split("\\."); targetValuesZone.put(Integer.valueOf((String) targetZoneValues.get(el)), elSplitted[elSplitted.length - 1]); } return targetValuesZone; } /*public static Map<String, Double[]> loadDistances(){ String basePath = System.getProperty(EXTERNAL_PROP_DIR_PATH, ""); Map<String, Double[]> map = new HashMap<String, Double[]>(); Properties distancesProp = loadProperty(basePath, DISTANZE_PROP); Iterator iter = distancesProp.keySet().iterator(); while(iter.hasNext()){ String key = (String)iter.next(); String el = distancesProp.getProperty(key); Double[] distances = createDistancesArray(el); map.put(key, distances); } return map; } public static Map<String, DistancesPair> loadDistancesOld(){ Map<String, DistancesPair> map = new HashMap<String, DistancesPair>(); Properties distancesProp = new Properties(); try { distancesProp.load(loadInputStream(DISTANZE_PROP)); } catch (Exception e) { LOGGER.error(e.getMessage(), e); return null; } Iterator iter = distancesProp.keySet().iterator(); for(int i=1;iter.hasNext();i++){ String key = (String)iter.next(); String el = distancesProp.getProperty(key); DistancesPair pair = createPairDistance(el); map.put("distance"+i, pair); } return map; }*/ public static Double[] createDistancesArray(String csvDistances) { String[] distances = csvDistances.split(";"); Double[] distancesInt = new Double[distances.length]; for (int j = 0; j < distancesInt.length; j++) { distancesInt[j] = Double.parseDouble(distances[j]); } return distancesInt; } public static DistancesPair createPairDistance(String csvDistances) { String[] distances = csvDistances.split(";"); Double[] distancesInt = new Double[distances.length]; String[] distancesNames = new String[distances.length]; for (int j = 0; j < distancesInt.length; j++) { distancesInt[j] = Double.parseDouble(distances[j]); distancesNames[j] = "distance" + j; } return new DistancesPair(distancesNames, distancesInt); } public static DistancesPair createPairDistance(List<Double> distances) { Double[] distancesInt = distances.toArray(new Double[distances.size()]); String[] distancesNames = new String[distancesInt.length]; for (int j = 0; j < distancesInt.length; j++) { distancesNames[j] = "distance" + j; } return new DistancesPair(distancesNames, distancesInt); } public static List<Double> loadDistances(DataStore dataStore, String featureTypeName) { List<Double> list = FeatureLoaderUtils.loadFeatureAttributesInt(dataStore, featureTypeName, DISTANZA_ATTRIBUTE_NAME, true); list.remove(new Double(0.0d)); return list; } /** * @return the targetURIs */ public Properties getTargetURIs() { return (Properties) targetURIs.clone(); } /** * @return the targetMapping */ public Properties getTargetMapping() { return (Properties) targetMapping.clone(); } /** * @return the targetZoneValues */ public Properties getTargetZoneValues() { return (Properties) targetZoneValues.clone(); } /** * @return the targetValuesZone */ public Map<Integer, String> getTargetValuesZone() { return targetValuesZone; } /** * @return the allCopSuoloValues */ public List<Integer> getAllCopSuoloValues() { return ListUtils.unmodifiableList(allCopSuoloValues); } }