view src/main/java/de/intevation/lada/importer/laf/LafProducer.java @ 1028:1c41c7b8f7c2 schema-update

Updated server application to new database model. THIS IS STILL WIP!!!
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 08 Jul 2016 15:32:36 +0200
parents 092e673cbb8d
children
line wrap: on
line source
/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU GPL (v>=3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out
 * the documentation coming with IMIS-Labordaten-Application for details.
 */
package de.intevation.lada.importer.laf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import de.intevation.lada.importer.ReportItem;
import de.intevation.lada.model.land.KommentarM;
import de.intevation.lada.model.land.KommentarP;
import de.intevation.lada.model.land.Messung;
import de.intevation.lada.model.land.Messwert;
import de.intevation.lada.model.land.Ortszuordnung;
import de.intevation.lada.model.land.Probe;
import de.intevation.lada.model.land.ZusatzWert;
import de.intevation.lada.model.stammdaten.Ort;

/**
 * The LAFProducer creates entity objects form key-value pairs using the
 * AttributeMapper.
 *
 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
 */
public class LafProducer
{
    /**
     * The probe object created by this producer.
     */
    private Probe probe;

    /**
     * The messung object created by this producer.
     */
    private Messung messung;

    /**
     * Creator used to build ort objects.
     */
    @Inject
    private OrtCreator ort;

    /**
     * Probekommentare created by this producer.
     */
    private List<KommentarP> pKommentare;

    /**
     * Messungskommentare created by this producer.
     */
    private Map<Messung, List<KommentarM>> mKommentare;

    /**
     * Messungen created by this producer.
     */
    private List<Messung> messungen;

    /**
     * Orte created by this producer.
     */
    private List<Ortszuordnung> ortszuordnungen;

    /**
     * SOrte created by this producer.
     */
    private List<Ort> orte;

    /**
     * Messwerte created by this producer.
     */
    private Map<Messung, List<Messwert>> messwerte;

    /**
     * Zusatzwerte created by this producer.
     */
    private List<ZusatzWert> zusatzwerte;

    /**
     * Format mapping for probe objects.
     */
    private List<EntryFormat> probenFormat;

    /**
     * Format mapping for messung objects.
     */
    private List<EntryFormat> messungFormat;

    /**
     * Format mapping for ort objects.
     */
    private List<EntryFormat> ortFormat;

    /**
     * The warnings.
     */
    private Map<String, List<ReportItem>> warnings;

    /**
     * The errors.
     */
    private Map<String, List<ReportItem>> errors;

    /**
     * The mapper used to set attributes.
     */
    @Inject
    private AttributeMapper mapper;

    /**
     * Default contructor. Initializes the producer and reads the config file
     * using the systemproperty "de.intevation.lada.importconfig".
     */
    public LafProducer() {
        this.warnings = new HashMap<String, List<ReportItem>>();
        this.errors = new HashMap<String, List<ReportItem>>();
        this.probe = new Probe();
        this.pKommentare = new ArrayList<KommentarP>();
        this.mKommentare = new HashMap<Messung, List<KommentarM>>();
        this.messungen = new ArrayList<Messung>();
        this.ortszuordnungen = new ArrayList<Ortszuordnung>();
        this.messwerte = new HashMap<Messung, List<Messwert>>();
        String fileName = "/import.json";
        LafFormat format = new LafFormat();
        format.readConfigFile(fileName);
        probenFormat = format.getFormat("probe");
        messungFormat = format.getFormat("messung");
        ortFormat = format.getFormat("ort");
    }

    /**
     * Add data to the producer.
     * This triggers the producer to create a new object or add data to
     * existing objects.
     *
     * @param key       The key.
     * @param values    The value
     * @throws LafParserException
     */
    public void addData(String key, Object values)
    throws LafParserException {
        String lKey = key.toLowerCase();
        if(lKey.equals("probenkommentar")) {
            KommentarP kommentar = new KommentarP();
            kommentar.setProbeId(this.probe.getId());
            this.pKommentare.add(
                mapper.addAttribute(lKey, values, kommentar));
        }
        else if (lKey.equals("kommentar")) {
            KommentarM kommentar = new KommentarM();
            kommentar.setMessungsId(this.messung.getId());
            if (this.mKommentare.get(this.messung) == null) {
                this.mKommentare.put(this.messung, new ArrayList<KommentarM>());
            }
            this.mKommentare.get(this.messung).add(
                mapper.addAttribute(lKey, values, kommentar));
        }
        else if (lKey.equals("probenzusatzbeschreibung")) {
            ZusatzWert wert = new ZusatzWert();
            ZusatzWert zusatzWert = mapper.addAttribute(lKey, values, wert);
            if (zusatzWert != null) {
                zusatzWert.setProbeId(probe.getId());
                this.zusatzwerte.add(zusatzWert);
            }
            else {
                String ekey = probe.getId() == null ?
                    "probeId" : probe.getId().toString();
                List<ReportItem> err = this.errors.get(ekey);
                if (err == null) {
                    this.errors.put(ekey, mapper.getErrors());
                }
                else {
                    err.addAll(mapper.getErrors());
                }
            }
        }
        else if (lKey.equals("pzb_s")) {
            ZusatzWert wert = new ZusatzWert();
            ZusatzWert zusatzWert = mapper.addAttributeS(lKey, values, wert);
            if (zusatzWert != null) {
                zusatzWert.setProbeId(probe.getId());
                this.zusatzwerte.add(zusatzWert);
            }
            else {
                String ekey = probe.getId() == null ?
                    "probeId" : probe.getId().toString();
                List<ReportItem> err = this.errors.get(ekey);
                if (err == null) {
                    this.errors.put(ekey, mapper.getErrors());
                }
                else {
                    err.addAll(mapper.getErrors());
                }
            }
        }
        else if (lKey.equals("messwert")) {
            Messwert m = new Messwert();
            m.setMessungsId(this.messung.getId());
            Messwert wert = mapper.addAttribute(lKey, values, m);
            if (wert != null) {
                if (this.messwerte.get(this.messung) == null) {
                    this.messwerte.put(this.messung, new ArrayList<Messwert>());
                }
                this.messwerte.get(this.messung).add(
                    mapper.addAttribute(lKey, values, wert));
            }
            else {
                String ekey = probe.getId() == null ?
                    "probeId" : probe.getId().toString();
                List<ReportItem> err = this.errors.get(ekey);
                if (err == null) {
                    this.errors.put(ekey, mapper.getErrors());
                }
                else {
                    err.addAll(mapper.getErrors());
                }
            }
        }
        else if (isValidMessung(lKey, values.toString())) {
            this.messung = mapper.addAttribute(lKey, values, this.messung);
        }
        else if (isValidProbe(lKey, values.toString())) {
            this.probe = mapper.addAttribute(lKey, values, this.probe);
            if (this.probe == null) {
                this.errors.put(values.toString(), mapper.getErrors());
                throw new LafParserException(values.toString() + " exists");
            }
        }
        else if (isValidOrt(lKey, values.toString())) {
            this.ort.addAttribute(lKey, values);
        }
    }

    /**
     * Check if the key is defined in the config file and validate the value
     * using the pattern defined for this key.
     *
     * @param key   The key.
     * @param value The value.
     * @return valid or not.
     */
    private boolean isValidOrt(String key, String value) {
        for (EntryFormat ef: ortFormat) {
            if (ef.getKey().equals(key.toLowerCase())) {
                if (ef.getPattern().matcher(value).matches()) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Check if the key is defined in the config file and validate the value
     * using the pattern defined for this key.
     *
     * @param key   The key.
     * @param value The value.
     * @return valid or not.
     */
    private boolean isValidMessung(String key, String value) {
        for (EntryFormat ef: messungFormat) {
            if (ef.getKey().equals(key.toLowerCase())) {
                if (ef.getPattern().matcher(value).matches()) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Check if the key is defined in the config file and validate the value
     * using the pattern defined for this key.
     *
     * @param key   The key.
     * @param value The value.
     * @return valid or not.
     */
    private boolean isValidProbe(String key, String value) {
        for (EntryFormat ef: probenFormat) {
            if (ef.getKey().equals(key.toLowerCase())) {
                if (ef.getPattern().matcher(value).matches()) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * @return the {@link LProbe} entity.
     */
    public Probe getProbe() {
        return this.probe;
    }

    /**
     * @return List of {@link LMessung} entities.
     */
    public List<Messung> getMessungen() {
        return this.messungen;
    }

    /**
     * @return List of {@link Ort} entities.
     */
    public List<Ort> getOrte() {
        return this.orte;
    }

    /**
     * @return List of {@link LOrt} entities.
     */
    public List<Ortszuordnung> getOrtszuordnungen() {
        return this.ortszuordnungen;
    }

    /**
     * @return List of {@link LKommentarP} entities.
     */
    public List<KommentarP> getProbenKommentare() {
        return this.pKommentare;
    }

    /**
     * @return List of {@link LKommentarM} entities.
     */
    public Map<Messung, List<KommentarM>> getMessungsKommentare() {
        return this.mKommentare;
    }

    /**
     * @return List of {@link LMesswert} entities.
     */
    public Map<Messung, List<Messwert>> getMesswerte() {
        return this.messwerte;
    }

    /**
     * @return List of {@link LZusatzWert} entities.
     */
    public List<ZusatzWert> getZusatzwerte() {
        return this.zusatzwerte;
    }

    /**
     * Reset errors and warnings.
     */
    public void reset() {
        this.errors = new HashMap<String, List<ReportItem>>();
        this.warnings = new HashMap<String, List<ReportItem>>();
        this.probe = new Probe();
        this.messungen = new ArrayList<Messung>();
        this.messung = null;
        this.ortszuordnungen = new ArrayList<Ortszuordnung>();
        this.orte = new ArrayList<Ort>();
        this.ort.reset();
        this.messwerte = new HashMap<Messung, List<Messwert>>();
        this.mKommentare = new HashMap<Messung, List<KommentarM>>();
        this.pKommentare = new ArrayList<KommentarP>();
        mapper.reset();
    }

    /**
     * Add the current {@link LMessung} entity to the List and create a new one.
     */
    public void newMessung() {
        if (this.messung != null) {
            if (this.messung.getFertig() == null) {
                this.messung.setFertig(false);
            }
            if (this.messung.getGeplant() == null) {
                this.messung.setGeplant(false);
            }
            this.messungen.add(this.messung);
        }
        this.messung = new Messung();
    }

    /**
     * Add the {@link Ort} and {@link LOrt} entities to the lists and create
     * a new {@link OrtCreator}.
     */
    public void newOrt() {
        if (this.ort != null) {
            Ort o = this.ort.toOrt();
            if (o != null) {
                this.orte.add(o);
            }
            Ortszuordnung lo = this.ort.toOrtszuordnung();
            if (lo != null) {
                this.ortszuordnungen.add(lo);
            }
        }
        OrtCreator creator = this.ort;
        creator.reset();
        creator.setProbeId(this.probe.getId());
    }

    /**
     * @return the warnings
     */
    public Map<String, List<ReportItem>> getWarnings() {
        if (this.probe == null) {
            return this.warnings;
        }
        if (mapper.getWarnings() == null || mapper.getWarnings().size() == 0) {
            return this.warnings;
        }
        String key = probe.getId() == null ? "probeId" : probe.getId().toString();
        List<ReportItem> warn = this.warnings.get(key);
        if (warn == null) {
            this.warnings.put(key, mapper.getWarnings());
        }
        else {
            warn.addAll(mapper.getWarnings());
        }
        return this.warnings;
    }

    /**
     * @return the errors
     */
    public Map<String, List<ReportItem>> getErrors() {
        if (this.probe == null) {
            return this.errors;
        }
        if (mapper.getErrors() == null || mapper.getErrors().size() == 0) {
            return this.errors;
        }
        String key = probe.getIdAlt() == null ?
            "probeId" : probe.getIdAlt();
        List<ReportItem> err = this.errors.get(key);
        if (err == null) {
            this.errors.put(key, mapper.getErrors());
        }
        else {
            err.addAll(mapper.getErrors());
        }
        return this.errors;
    }

    public void finishOrt() {
        if (orte.isEmpty()) {
            return;
        }
    }
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)