sascha@3208: package de.intevation.flys.artifacts.access; sascha@3208: sascha@3208: import de.intevation.artifactdatabase.data.StateData; sascha@3208: sascha@3208: import de.intevation.flys.artifacts.FLYSArtifact; sascha@3208: sascha@3210: import de.intevation.flys.artifacts.model.DateRange; sascha@3208: sascha@3208: import de.intevation.flys.utils.FLYSUtils; sascha@3208: sascha@3208: import gnu.trove.TDoubleArrayList; sascha@3208: sascha@3208: import java.util.ArrayList; sascha@3208: import java.util.Date; sascha@3208: sascha@3208: import org.apache.log4j.Logger; sascha@3208: sascha@3209: public class Access sascha@3208: { sascha@3208: private static Logger log = Logger.getLogger(Access.class); sascha@3208: sascha@3208: protected FLYSArtifact artifact; sascha@3208: sascha@3208: public Access() { sascha@3208: } sascha@3208: sascha@3208: public Access(FLYSArtifact artifact) { sascha@3208: this.artifact = artifact; sascha@3208: } sascha@3208: sascha@3208: public FLYSArtifact getArtifact() { sascha@3208: return artifact; sascha@3208: } sascha@3208: sascha@3208: public void setArtifact(FLYSArtifact artifact) { sascha@3208: this.artifact = artifact; sascha@3208: } sascha@3208: sascha@3208: protected String getString(String key) { sascha@3208: StateData sd = artifact.getData(key); sascha@3208: if (sd == null) { sascha@3208: log.warn("missing '" + key + "' value"); sascha@3208: return null; sascha@3208: } sascha@3208: return (String)sd.getValue(); sascha@3208: } sascha@3208: sascha@3208: protected Double getDouble(String key) { sascha@3208: StateData sd = artifact.getData(key); sascha@3208: if (sd == null) { sascha@3208: log.warn("missing '" + key + "' value"); sascha@3208: return null; sascha@3208: } sascha@3208: try { sascha@3208: return Double.valueOf((String)sd.getValue()); sascha@3208: } sascha@3208: catch (NumberFormatException nfe) { sascha@3208: log.warn(key + " '" + sd.getValue() + "' is not numeric."); sascha@3208: } sascha@3208: return null; sascha@3208: } sascha@3208: sascha@3208: protected Long getLong(String key) { sascha@3208: StateData sd = artifact.getData(key); sascha@3208: if (sd == null) { sascha@3208: log.warn("missing '" + key + "' value"); sascha@3208: return null; sascha@3208: } sascha@3208: try { sascha@3208: return Long.valueOf((String)sd.getValue()); sascha@3208: } sascha@3208: catch (NumberFormatException nfe) { sascha@3208: log.warn(key + " '" + sd.getValue() + "' is not a long integer."); sascha@3208: } sascha@3208: return null; sascha@3208: } sascha@3208: sascha@3208: protected Integer getInteger(String key) { sascha@3208: StateData sd = artifact.getData(key); sascha@3208: if (sd == null) { sascha@3208: log.warn("missing '" + key + "' value"); sascha@3208: return null; sascha@3208: } sascha@3208: try { sascha@3208: return Integer.valueOf((String)sd.getValue()); sascha@3208: } sascha@3208: catch (NumberFormatException nfe) { sascha@3208: log.warn(key + " '" + sd.getValue() + "' is not a integer."); sascha@3208: } sascha@3208: return null; sascha@3208: } sascha@3208: sascha@3208: protected int [] getIntArray(String key) { sascha@3208: StateData sd = artifact.getData(key); sascha@3208: if (sd == null) { sascha@3208: log.warn("missing '" + key +"' value"); sascha@3208: return null; sascha@3208: } sascha@3208: return FLYSUtils.intArrayFromString((String)sd.getValue()); sascha@3208: } sascha@3208: sascha@3208: protected DateRange [] getDateRange(String key) { sascha@3208: sascha@3208: StateData sd = artifact.getData(key); sascha@3208: sascha@3208: if (sd == null) { sascha@3208: log.warn("missing '" + key + "'"); sascha@3208: return null; sascha@3208: } sascha@3208: sascha@3208: String data = (String)sd.getValue(); sascha@3208: String[] pairs = data.split("\\s*;\\s*"); sascha@3208: sascha@3208: ArrayList aPs = new ArrayList(pairs.length); sascha@3208: sascha@3208: for (int i = 0; i < pairs.length; i++) { sascha@3208: String[] fromTo = pairs[i].split("\\s*,\\s*"); sascha@3208: if (fromTo.length >= 2) { sascha@3208: try { sascha@3208: Date from = new Date(Long.parseLong(fromTo[0])); sascha@3208: Date to = new Date(Long.parseLong(fromTo[1])); sascha@3208: DateRange aP = new DateRange(from, to); sascha@3208: if (!aPs.contains(aP)) { sascha@3208: aPs.add(aP); sascha@3208: } sascha@3208: } sascha@3208: catch (NumberFormatException nfe) { sascha@3208: log.warn(key + " contains no long values.", nfe); sascha@3208: } sascha@3208: } sascha@3208: } sascha@3208: sascha@3208: DateRange [] result = aPs.toArray(new DateRange[aPs.size()]); sascha@3208: sascha@3208: if (log.isDebugEnabled()) { sascha@3208: for (int i = 0; i < result.length; ++i) { sascha@3208: DateRange ap = result[i]; sascha@3208: log.debug("period " + sascha@3208: ap.getFrom() + " - " + ap.getTo()); sascha@3208: } sascha@3208: } sascha@3208: sascha@3208: return result; sascha@3208: } sascha@3208: sascha@3208: protected Boolean getBoolean(String key) { sascha@3208: StateData sd = artifact.getData(key); sascha@3208: if (sd == null) { sascha@3208: log.warn("missing '" + key + "' value"); sascha@3208: return null; sascha@3208: } sascha@3208: return Boolean.valueOf((String)sd.getValue()); sascha@3208: } sascha@3208: sascha@3208: protected double [] getDoubleArray(String key) { sascha@3208: StateData sd = artifact.getData(key); sascha@3208: if (sd == null) { sascha@3208: log.warn("missing '" + key + "'"); sascha@3208: return null; sascha@3208: } sascha@3208: String [] parts = ((String)sd.getValue()).split("[\\s;]+"); sascha@3208: TDoubleArrayList list = new TDoubleArrayList(parts.length); sascha@3208: for (String part: parts) { sascha@3208: try { sascha@3208: list.add(Double.parseDouble(part)); sascha@3208: } sascha@3208: catch (NumberFormatException nfe) { sascha@3208: log.warn("'" + part + "' is not numeric."); sascha@3208: } sascha@3208: } sascha@3208: return list.toNativeArray(); sascha@3208: } sascha@3208: } sascha@3208: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : sascha@3208: