Added Observations.

This commit is contained in:
neviyn 2018-09-15 19:55:35 +01:00
parent 05b2b32895
commit 9de27e9998
4 changed files with 109 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import io.dropwizard.jersey.setup.JerseyEnvironment;
import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment; import io.dropwizard.setup.Environment;
import org.jdbi.v3.core.Jdbi; import org.jdbi.v3.core.Jdbi;
import uk.co.neviyn.Observations.dao.ObservationDao;
import uk.co.neviyn.Observations.dao.SiteDao; import uk.co.neviyn.Observations.dao.SiteDao;
import uk.co.neviyn.Observations.dao.TutorDao; import uk.co.neviyn.Observations.dao.TutorDao;
import uk.co.neviyn.Observations.resources.TutorResource; import uk.co.neviyn.Observations.resources.TutorResource;
@ -30,10 +31,14 @@ public class ObservationsApplication extends Application<ObservationsConfigurati
final Jdbi jdbi = factory.build(environment, observationsConfiguration.getDataSourceFactory(), "database"); final Jdbi jdbi = factory.build(environment, observationsConfiguration.getDataSourceFactory(), "database");
TutorDao tutorDao = jdbi.onDemand(TutorDao.class); TutorDao tutorDao = jdbi.onDemand(TutorDao.class);
SiteDao siteDao = jdbi.onDemand(SiteDao.class); SiteDao siteDao = jdbi.onDemand(SiteDao.class);
ObservationDao observationDao = jdbi.onDemand(ObservationDao.class);
jersey.register(tutorDao); jersey.register(tutorDao);
jersey.register(siteDao); jersey.register(siteDao);
jersey.register(observationDao);
siteDao.createSiteTable(); siteDao.createSiteTable();
tutorDao.createTutorTable(); tutorDao.createTutorTable();
observationDao.createObservationTable();
observationDao.createObservationTutorTable();
final TutorResource tutorResource = new TutorResource(tutorDao); final TutorResource tutorResource = new TutorResource(tutorDao);
jersey.register(tutorResource); jersey.register(tutorResource);
} }

View File

@ -1,4 +1,70 @@
package uk.co.neviyn.Observations.core; package uk.co.neviyn.Observations.core;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.sql.ResultSet;
import java.sql.SQLException;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.joda.time.LocalDate;
@Data
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class Observation { public class Observation {
@NonNull
@JsonProperty
private int id;
@NonNull
@JsonProperty
private int siteId;
@NonNull
@JsonProperty
private int tutorManyId;
@NonNull
@JsonProperty
private String observed;
@NonNull
@JsonProperty
private int monitoring, control, conservatism, teamwork, knowledge;
@NonNull
@JsonProperty
private String rawData;
@NonNull
@JsonProperty
private LocalDate date;
public Observation(int siteId, int tutorManyId, String observed, int monitoring, int control, int conservatism,
int teamwork, int knowledge, String rawData) {
this.siteId = siteId;
this.tutorManyId = tutorManyId;
this.observed = observed;
this.monitoring = monitoring;
this.control = control;
this.conservatism = conservatism;
this.teamwork = teamwork;
this.knowledge = knowledge;
this.rawData = rawData;
this.date = LocalDate.now();
}
public static class Mapper implements RowMapper<Observation>{
public Observation map(ResultSet rs, StatementContext ctx) throws SQLException {
return new Observation(rs.getInt("id"), rs.getInt("siteId"), rs.getInt("tutorManyId"), rs.getString("observed"),
rs.getInt("monitoring"), rs.getInt("control"), rs.getInt("conservatism"), rs.getInt("teamwork"),
rs.getInt("knowledge"), rs.getString("rawData"), new LocalDate(rs.getDate("date")));
}
}
} }

View File

@ -0,0 +1,35 @@
package uk.co.neviyn.Observations.dao;
import java.util.List;
import org.jdbi.v3.sqlobject.config.RegisterRowMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import org.joda.time.LocalDate;
import uk.co.neviyn.Observations.core.Observation;
@RegisterRowMapper(Observation.Mapper.class)
public interface ObservationDao {
@SqlUpdate("CREATE TABLE observations (id INTEGER PRIMARY KEY AUTO_INCREMENT, siteId INTEGER, tutorManyId INTEGER, " +
"observed TEXT, monitoring INTEGER, control INTEGER, conservatism INTEGER, teamwork INTEGER, knowledge INTEGER, " +
"rawData TEXT, date DATE)")
void createObservationTable();
@SqlUpdate("CREATE TABLE observation_tutor (tutorId INT NOT NULL, observationId INT NOT NULL, " +
"FOREIGN KEY (tutorID) REFERENCES tutor(id), FOREIGN KEY (observationId) REFERENCES observations(id))")
void createObservationTutorTable();
@SqlQuery("SELECT * FROM observations WHERE date BETWEEN :startDate AND :endDate")
List<Observation> observationsBetweenDates(@Bind("startDate")LocalDate startDate, @Bind("endDate")LocalDate endDate);
@SqlQuery("SELECT * FROM observations LEFT JOIN observation_tutor ON observation_tutor.observationId = observations.id " +
"LEFT JOIN tutor observation_tutor.tutorId = tutor.id WHERE tutor.id = :tutorId")
List<Observation> observationsByTutor(@Bind("tutorId")int tutorId);
@SqlQuery("SELECT * FROM observations WHERE date BETWEEN :startDate AND :endDate " +
"LEFT JOIN observation_tutor ON observation_tutor.observationId = observations.id " +
"LEFT JOIN tutor observation_tutor.tutorId = tutor.id WHERE tutor.id = :tutorId")
List<Observation> observationsByTutorBetweenDates(@Bind("tutorId")int tutorId, @Bind("startDate")LocalDate startDate,
@Bind("endDate")LocalDate endDate);
}

View File

@ -4,6 +4,7 @@ import java.util.List;
import org.jdbi.v3.sqlobject.config.RegisterRowMapper; import org.jdbi.v3.sqlobject.config.RegisterRowMapper;
import org.jdbi.v3.sqlobject.customizer.Bind; import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindBean; import org.jdbi.v3.sqlobject.customizer.BindBean;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery; import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate; import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import uk.co.neviyn.Observations.core.Tutor; import uk.co.neviyn.Observations.core.Tutor;
@ -16,7 +17,8 @@ public interface TutorDao {
void createTutorTable(); void createTutorTable();
@SqlUpdate("INSERT INTO tutor(name, site) VALUES (:name, :site)") @SqlUpdate("INSERT INTO tutor(name, site) VALUES (:name, :site)")
void add(@BindBean Tutor tutor); @GetGeneratedKeys("id")
int add(@BindBean Tutor tutor);
@SqlQuery("SELECT * FROM tutor WHERE site = :siteId") @SqlQuery("SELECT * FROM tutor WHERE site = :siteId")
List<Tutor> tutorsForSite(@Bind("siteId")int siteId); List<Tutor> tutorsForSite(@Bind("siteId")int siteId);