changeset 8974:a275ddf7a3a1

Added some trim and lowercase in the where clauses of the selects of existing recordsets; added AnnotationType select
author mschaefer
date Tue, 03 Apr 2018 10:37:30 +0200
parents 35afa0349782
children a0a0a7f912ab
files backend/src/main/java/org/dive4elements/river/importer/ImportAnnotationType.java backend/src/main/java/org/dive4elements/river/importer/ImportAttribute.java backend/src/main/java/org/dive4elements/river/importer/ImportBedHeightType.java backend/src/main/java/org/dive4elements/river/importer/ImportElevationModel.java backend/src/main/java/org/dive4elements/river/importer/ImportLocationSystem.java backend/src/main/java/org/dive4elements/river/model/AnnotationType.java
diffstat 6 files changed, 143 insertions(+), 151 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportAnnotationType.java	Tue Apr 03 10:29:57 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportAnnotationType.java	Tue Apr 03 10:37:30 2018 +0200
@@ -8,56 +8,57 @@
 
 package org.dive4elements.river.importer;
 
+import java.util.List;
+
+import org.apache.log4j.Logger;
 import org.dive4elements.river.model.AnnotationType;
-
+import org.hibernate.Query;
 import org.hibernate.Session;
-import org.hibernate.Query;
-
-import java.util.List;
 
 public class ImportAnnotationType
 implements   Comparable<ImportAnnotationType>
 {
+    private static final Logger log = Logger.getLogger(ImportAnnotationType.class);
     protected String         name;
     protected AnnotationType peer;
 
     public ImportAnnotationType() {
     }
 
-    public ImportAnnotationType(String name) {
+    public ImportAnnotationType(final String name) {
         this.name = name;
     }
 
-    public int compareTo(ImportAnnotationType other) {
-        return name.compareTo(other.name);
+    @Override
+    public int compareTo(final ImportAnnotationType other) {
+        return this.name.compareTo(other.name);
     }
 
     public String getName() {
-        return name;
+        return this.name;
     }
 
-    public void setName(String name) {
+    public void setName(final String name) {
         this.name = name;
     }
 
 
     public AnnotationType getPeer() {
-        if (peer == null) {
-            Session session = ImporterSession.getInstance()
-                .getDatabaseSession();
-            Query query = session.createQuery(
-                "from AnnotationType where name=:name");
-            query.setParameter("name", name);
-            List<AnnotationType> types = query.list();
-            if (types.isEmpty()) {
-                peer = new AnnotationType(name);
-                session.save(peer);
-            }
-            else {
-                peer = types.get(0);
-            }
+        if (this.peer != null)
+            return this.peer;
+        final Session session = ImporterSession.getInstance().getDatabaseSession();
+        final Query query = session.createQuery("FROM AnnotationType WHERE lower(name)=:name");
+        query.setParameter("name", this.name.trim().toLowerCase());
+        final List<AnnotationType> types = query.list();
+        if (types.isEmpty()) {
+            this.peer = new AnnotationType(this.name);
+            session.save(this.peer);
+            log.info(String.format("Create new database instance: %d, '%s'", this.peer.getId(), this.name));
         }
-        return peer;
+        else {
+            this.peer = types.get(0);
+        }
+        return this.peer;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportAttribute.java	Tue Apr 03 10:29:57 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportAttribute.java	Tue Apr 03 10:37:30 2018 +0200
@@ -8,12 +8,11 @@
 
 package org.dive4elements.river.importer;
 
+import java.util.List;
+
 import org.dive4elements.river.model.Attribute;
-
+import org.hibernate.Query;
 import org.hibernate.Session;
-import org.hibernate.Query;
-
-import java.util.List;
 
 public class ImportAttribute
 implements   Comparable<ImportAttribute>
@@ -25,51 +24,52 @@
     public ImportAttribute() {
     }
 
-    public ImportAttribute(String value) {
+    public ImportAttribute(final String value) {
         this.value = value;
     }
 
     public String getValue() {
-        return value;
+        return this.value;
     }
 
-    public void setValue(String value) {
+    public void setValue(final String value) {
         this.value = value;
     }
 
-    public int compareTo(ImportAttribute other) {
-        return value.compareTo(other.value);
+    @Override
+    public int compareTo(final ImportAttribute other) {
+        return this.value.compareTo(other.value);
     }
 
     @Override
-    public boolean equals(Object other) {
-        if (other == this) return true;
-        if (!(other instanceof ImportAttribute)) return false;
-        return value.equals(((ImportAttribute)other).value);
+    public boolean equals(final Object other) {
+        if (other == this)
+            return true;
+        if (!(other instanceof ImportAttribute))
+            return false;
+        return this.value.equals(((ImportAttribute) other).value);
     }
 
     @Override
     public int hashCode() {
-        return value.hashCode();
+        return this.value.hashCode();
     }
 
     public Attribute getPeer() {
-        if (peer == null) {
-            Session session = ImporterSession.getInstance()
-                .getDatabaseSession();
-            Query query = session.createQuery(
-                "from Attribute where value=:value");
-            query.setString("value", value);
-            List<Attribute> attributes = query.list();
-            if (attributes.isEmpty()) {
-                peer = new Attribute(value);
-                session.save(peer);
-            }
-            else {
-                peer = attributes.get(0);
-            }
+        if (this.peer != null)
+            return this.peer;
+        final Session session = ImporterSession.getInstance().getDatabaseSession();
+        final Query query = session.createQuery("FROM Attribute WHERE lower(value)=:value");
+        query.setString("value", this.value.trim().toLowerCase());
+        final List<Attribute> attributes = query.list();
+        if (attributes.isEmpty()) {
+            this.peer = new Attribute(this.value);
+            session.save(this.peer);
         }
-        return peer;
+        else {
+            this.peer = attributes.get(0);
+        }
+        return this.peer;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportBedHeightType.java	Tue Apr 03 10:29:57 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportBedHeightType.java	Tue Apr 03 10:37:30 2018 +0200
@@ -11,29 +11,27 @@
 import java.util.List;
 
 import org.apache.log4j.Logger;
-
+import org.dive4elements.river.model.BedHeightType;
+import org.hibernate.Query;
 import org.hibernate.Session;
-import org.hibernate.Query;
-
-import org.dive4elements.river.model.BedHeightType;
 
 
 public class ImportBedHeightType {
 
     private static final Logger log =
-        Logger.getLogger(ImportBedHeightType.class);
+            Logger.getLogger(ImportBedHeightType.class);
 
     protected String name;
 
     protected BedHeightType peer;
 
-    public ImportBedHeightType(BedHeightType peer)  {
+    public ImportBedHeightType(final BedHeightType peer)  {
         this.peer = peer;
-        name = peer.getName();
+        this.name = peer.getName();
     }
 
 
-    public ImportBedHeightType(String name) {
+    public ImportBedHeightType(final String name) {
         this.name        = name;
     }
 
@@ -44,27 +42,19 @@
 
 
     public BedHeightType getPeer() {
-        if (peer == null) {
-            Session session = ImporterSession.getInstance()
-                .getDatabaseSession();
-
-            Query query = session.createQuery("from BedHeightType "
-                + "where name=:name and description=:description");
-
-            query.setParameter("name", name);
-
-            List<BedHeightType> types = query.list();
-
-            if (types.isEmpty()) {
-                peer = new BedHeightType(name);
-                session.save(peer);
-            }
-            else {
-                peer = types.get(0);
-            }
+        if (this.peer != null)
+            return this.peer;
+        final Session session = ImporterSession.getInstance().getDatabaseSession();
+        final Query query = session.createQuery("FROM BedHeightType WHERE (trim(name)=:name) AND (trim(description)=:description)");
+        query.setParameter("name", this.name);
+        final List<BedHeightType> types = query.list();
+        if (types.isEmpty()) {
+            this.peer = new BedHeightType(this.name);
+            session.save(this.peer);
+        } else {
+            this.peer = types.get(0);
         }
-
-        return peer;
+        return this.peer;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportElevationModel.java	Tue Apr 03 10:29:57 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportElevationModel.java	Tue Apr 03 10:37:30 2018 +0200
@@ -11,17 +11,15 @@
 import java.util.List;
 
 import org.apache.log4j.Logger;
-
+import org.dive4elements.river.model.ElevationModel;
+import org.hibernate.Query;
 import org.hibernate.Session;
-import org.hibernate.Query;
-
-import org.dive4elements.river.model.ElevationModel;
 
 
 public class ImportElevationModel {
 
     private static final Logger log =
-        Logger.getLogger(ImportElevationModel.class);
+            Logger.getLogger(ImportElevationModel.class);
 
     protected String name;
 
@@ -30,44 +28,40 @@
     protected ElevationModel peer;
 
 
-    public ImportElevationModel(String name, ImportUnit unit) {
+    public ImportElevationModel(final String name, final ImportUnit unit) {
         this.name = name;
         this.unit = unit;
     }
 
 
     public void storeDependencies() {
-        ElevationModel model = getPeer();
+        final ElevationModel model = getPeer();
     }
 
     public ElevationModel getPeer() {
-        if (unit == null) {
+        if (this.unit == null) {
             log.warn("No elevation model specified.");
             return null;
         }
-
-        if (peer == null) {
-            Session session = ImporterSession.getInstance()
-                .getDatabaseSession();
-            Query query = session.createQuery(
-                "from ElevationModel where " +
-                "name=:name and unit=:unit");
-            query.setParameter("name", name);
-            query.setParameter("unit", unit.getPeer());
-            List<ElevationModel> models = query.list();
+        if (this.peer != null)
+            return this.peer;
 
-            if (models.isEmpty()) {
-                log.info("Create new ElevationModel DB instance.");
+        final Session session = ImporterSession.getInstance().getDatabaseSession();
+        final Query query = session.createQuery("FROM ElevationModel WHERE (trim(name)=:name) AND (unit=:unit)");
+        query.setParameter("name", this.name);
+        query.setParameter("unit", this.unit.getPeer());
+        final List<ElevationModel> models = query.list();
 
-                peer = new ElevationModel(name, unit.getPeer());
-                session.save(peer);
-            }
-            else {
-                peer = models.get(0);
-            }
+        if (models.isEmpty()) {
+            log.info("Create new ElevationModel DB instance.");
+
+            this.peer = new ElevationModel(this.name, this.unit.getPeer());
+            session.save(this.peer);
         }
-
-        return peer;
+        else {
+            this.peer = models.get(0);
+        }
+        return this.peer;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportLocationSystem.java	Tue Apr 03 10:29:57 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportLocationSystem.java	Tue Apr 03 10:37:30 2018 +0200
@@ -11,17 +11,15 @@
 import java.util.List;
 
 import org.apache.log4j.Logger;
-
+import org.dive4elements.river.model.LocationSystem;
+import org.hibernate.Query;
 import org.hibernate.Session;
-import org.hibernate.Query;
-
-import org.dive4elements.river.model.LocationSystem;
 
 
 public class ImportLocationSystem {
 
     private static final Logger log =
-        Logger.getLogger(ImportLocationSystem.class);
+            Logger.getLogger(ImportLocationSystem.class);
 
 
     protected String name;
@@ -30,40 +28,34 @@
     protected LocationSystem peer;
 
 
-    public ImportLocationSystem(String name, String description) {
+    public ImportLocationSystem(final String name, final String description) {
         this.name        = name;
         this.description = description;
     }
 
     public void storeDependencies() {
-        log.info("store LocationSystem '" + name + "'");
-        LocationSystem ls = getPeer();
+        log.info("store LocationSystem '" + this.name + "'");
+        final LocationSystem ls = getPeer();
 
-        Session session = ImporterSession.getInstance().getDatabaseSession();
+        final Session session = ImporterSession.getInstance().getDatabaseSession();
         session.flush();
     }
 
     public LocationSystem getPeer() {
-        if (peer == null) {
-            Session session = ImporterSession.getInstance()
-                .getDatabaseSession();
-            Query query = session.createQuery(
-                "from LocationSystem where " +
-                "name=:name and description=:description");
-            query.setParameter("name", name);
-            query.setParameter("description", description);
-
-            List<LocationSystem> lss = query.list();
-            if (lss.isEmpty()) {
-                peer = new LocationSystem(name, description);
-                session.save(peer);
-            }
-            else {
-                peer = lss.get(0);
-            }
+        if (this.peer != null)
+            return this.peer;
+        final Session session = ImporterSession.getInstance().getDatabaseSession();
+        final Query query = session.createQuery("FROM LocationSystem WHERE (trim(name)=:name) AND (trim(description)=:description)");
+        query.setParameter("name", this.name);
+        query.setParameter("description", this.description);
+        final List<LocationSystem> lss = query.list();
+        if (lss.isEmpty()) {
+            this.peer = new LocationSystem(this.name, this.description);
+            session.save(this.peer);
+        } else {
+            this.peer = lss.get(0);
         }
-
-        return peer;
+        return this.peer;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/model/AnnotationType.java	Tue Apr 03 10:29:57 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/AnnotationType.java	Tue Apr 03 10:37:30 2018 +0200
@@ -9,14 +9,20 @@
 package org.dive4elements.river.model;
 
 import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
 
+import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.SequenceGenerator;
 import javax.persistence.Table;
-import javax.persistence.GeneratedValue;
-import javax.persistence.Column;
-import javax.persistence.SequenceGenerator;
-import javax.persistence.GenerationType;
+
+import org.dive4elements.river.backend.SessionHolder;
+import org.hibernate.Query;
+import org.hibernate.Session;
 
 @Entity
 @Table(name = "annotation_types")
@@ -29,34 +35,43 @@
     public AnnotationType() {
     }
 
-    public AnnotationType(String name) {
+    public AnnotationType(final String name) {
         this.name = name;
     }
 
     @Id
     @SequenceGenerator(
-        name           = "SEQUENCE_ANNOTATION_TYPES_ID_SEQ",
-        sequenceName   = "ANNOTATION_TYPES_ID_SEQ",
-        allocationSize = 1)
+            name           = "SEQUENCE_ANNOTATION_TYPES_ID_SEQ",
+            sequenceName   = "ANNOTATION_TYPES_ID_SEQ",
+            allocationSize = 1)
     @GeneratedValue(
-        strategy  = GenerationType.SEQUENCE,
-        generator = "SEQUENCE_ANNOTATION_TYPES_ID_SEQ")
+            strategy  = GenerationType.SEQUENCE,
+            generator = "SEQUENCE_ANNOTATION_TYPES_ID_SEQ")
     @Column(name = "id")
     public Integer getId() {
-        return id;
+        return this.id;
     }
 
-    public void setId(Integer id) {
+    public void setId(final Integer id) {
         this.id = id;
     }
 
     @Column(name = "name")
     public String getName() {
-        return name;
+        return this.name;
     }
 
-    public void setName(String name) {
+    public void setName(final String name) {
         this.name = name;
     }
+
+    /**
+     * Queries all annotation types from the database, orders by name
+     */
+    public static List<AnnotationType> getTypes() {
+        final Session session = SessionHolder.HOLDER.get();
+        final Query query = session.createQuery("FROM AnnotationType ORDER BY name");
+        return new ArrayList<>(query.list());
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org