view src/main/java/de/intevation/lada/importer/laf/LafWriter.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 fa922101a462
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.List;
import java.util.Map;

import javax.inject.Inject;
import javax.persistence.PersistenceException;

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.stammdaten.Ort;
import de.intevation.lada.util.annotation.AuthorizationConfig;
import de.intevation.lada.util.annotation.RepositoryConfig;
import de.intevation.lada.util.auth.Authorization;
import de.intevation.lada.util.auth.AuthorizationType;
import de.intevation.lada.util.auth.UserInfo;
import de.intevation.lada.util.data.Repository;
import de.intevation.lada.util.data.RepositoryType;

/**
 * Writer to persist new entities in the database.
 *
 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
 */
public class LafWriter {

    /**
     * The repository to write database objects.
     */
    @Inject
    @RepositoryConfig(type=RepositoryType.RW)
    private Repository repository;

    /**
     * The authorization module.
     */
    @Inject
    @AuthorizationConfig(type=AuthorizationType.HEADER)
    private Authorization authorization;

    /**
     * The errors.
     */
    private List<ReportItem> errors;

    /**
     * The warnings.
     */
    private List<ReportItem> warnings;

    /**
     * The current probe id used to create child objects.
     */
    private Integer currentProbeId;

    /**
     * Default constructor.
     */
    public LafWriter() {
        errors = new ArrayList<ReportItem>();
        warnings = new ArrayList<ReportItem>();
    }

    /**
     * Write a new {@link LProbe} object to the database using
     * authorization and validation.
     *
     * @param auth      The authentication information.
     * @param probe     The new {@link LProbe} object.
     * @return success
     */
    public boolean writeProbe(UserInfo userInfo, Probe probe) {
        if (!authorization.isAuthorized(userInfo, probe, Probe.class)) {
            errors.add(new ReportItem("auth", "not authorized", 699));
            return false;
        }
        if (probe.getIdAlt() == null) {
            errors.add(new ReportItem("probeId", "missing", 673));
            return false;
        }
        try {
            repository.create(probe, "land");
            this.currentProbeId = probe.getId();
        }
        catch (PersistenceException e) {
            errors.add(new ReportItem("probe", "writing", 670));
            return false;
        }
        return true;
    }

    /**
     * Write new {@link LMessung} objects to the database using
     * authorization and validation.
     *
     * @param auth      The authentication information.
     * @param messungen The new {@link LMessung} objects.
     * @return success
     */
    public boolean writeMessungen(
        UserInfo userInfo,
        Messung messung
    ) {
        messung.setProbeId(this.currentProbeId);
        repository.create(messung, "land");
        return true;
    }

    /**
     * Write new {@link SOrt} objects to the database.
     *
     * @param auth      The authentication information.
     * @param orte      List of {@link SOrt} objects.
     * @return success
     */
    public boolean writeOrte(UserInfo userInfo, List<Ort> orte) {
        return true;
    }

    /**
     * Write new {@link LOrt} objects to the database using validation.
     *
     * @param auth  The authentication information.
     * @param orte  List of {@link LOrt} objects.
     * @return success
     */
    public boolean writeOrtszuordung(UserInfo userInfo, Ortszuordnung ort) {
        ort.setProbeId(this.currentProbeId);
        repository.create(ort, "land");
        return true;
    }

    /**
     * Write new {@link LKommentarP} objects to the database.
     *
     * @param auth          The authentication information.
     * @param kommentare    List of {@link LKommentarP} objects.
     * @return success
     */
    public boolean writeProbenKommentare(
        UserInfo userInfo,
        List<KommentarP> kommentare
    ) {
        for(KommentarP kommentar: kommentare) {
            kommentar.setProbeId(this.currentProbeId);
            repository.create(kommentar, "land");
        }
        return true;
    }

    /**
     * Write new {@link LKommentarM} objects to the database.
     *
     * @param auth          The authentication information.
     * @param kommentare    List of {@link LKommentarM} objects.
     * @return success
     */
    public boolean writeMessungKommentare(
        UserInfo userInfo,
        Map<Messung, List<KommentarM>> kommentare
    ) {
        for (Messung messung : kommentare.keySet()) {
            for (KommentarM kommentar: kommentare.get(messung)) {
                kommentar.setMessungsId(messung.getId());
                repository.create(kommentar, "land");
            }
        }
        return true;
    }

    /**
     * Write new {@link LMesswert} objects to the database using validation.
     *
     * @param auth      The authentication information.
     * @param werte     List of {@link LMesswert} objects.
     * @return success
     */
    public boolean writeMesswerte(
        UserInfo userInfo,
        Map<Messung, List<Messwert>> werte
    ) {
        for (Messung messung : werte.keySet()) {
            for (Messwert messwert: werte.get(messung)) {
                messwert.setMessungsId(messung.getId());
                repository.create(messwert, "land");
            }
        }
        return true;
    }

    /**
     * @return the errors
     */
    public List<ReportItem> getErrors() {
        return errors;
    }

    /**
     * @return the warnings
     */
    public List<ReportItem> getWarnings() {
        return warnings;
    }

    /**
     * Reset the errors and warnings.
     */
    public void reset() {
        this.warnings = new ArrayList<ReportItem>();
        this.errors = new ArrayList<ReportItem>();
        this.currentProbeId = null;
    }
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)