traffic warning forms
This commit is contained in:
4
pom.xml
4
pom.xml
@@ -14,7 +14,7 @@
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
|
||||
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
|
||||
<quarkus.platform.version>2.2.2.Final</quarkus.platform.version>
|
||||
<quarkus.platform.version>2.3.1.Final</quarkus.platform.version>
|
||||
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
|
||||
</properties>
|
||||
<dependencyManagement>
|
||||
@@ -31,7 +31,7 @@
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-resteasy</artifactId>
|
||||
<artifactId>quarkus-resteasy-jsonb</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
|
||||
@@ -1,19 +1,41 @@
|
||||
package de.mbremer.configuration;
|
||||
|
||||
import io.quarkus.hibernate.orm.panache.PanacheEntity;
|
||||
import io.quarkus.hibernate.orm.panache.PanacheQuery;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.NoResultException;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Configuration extends PanacheEntity {
|
||||
String propertyName;
|
||||
String value;
|
||||
public static final String GOOGLE_API_KEY = "google_api_key";
|
||||
public static final String OFFICE_ADRESS_KEY = "office_address";
|
||||
|
||||
private String propertyName;
|
||||
private String value;
|
||||
|
||||
public static String findGooleApiKey() {
|
||||
return findValueOrEmpty(GOOGLE_API_KEY);
|
||||
}
|
||||
|
||||
public static String findOfficeAdress() {
|
||||
return findValueOrEmpty(OFFICE_ADRESS_KEY);
|
||||
}
|
||||
|
||||
private static String findValueOrEmpty(String key) {
|
||||
PanacheQuery<Configuration> configuration = find("propertyName", key);
|
||||
try {
|
||||
return configuration.singleResult().getValue();
|
||||
} catch (NoResultException e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package de.mbremer.configuration;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
@ApplicationScoped
|
||||
public class ConfigurationService {
|
||||
|
||||
@Inject
|
||||
Logger log;
|
||||
|
||||
@Transactional
|
||||
public void createOrUpdate(String configurationKey, String configurationValue) {
|
||||
try {
|
||||
Configuration apiKeyConfig = Configuration.find("propertyName", configurationKey).singleResult();
|
||||
apiKeyConfig.setValue(configurationValue);
|
||||
log.info("update " + configurationKey + ": " + configurationValue);
|
||||
} catch (NoResultException e) {
|
||||
new Configuration(configurationKey, configurationValue).persist();
|
||||
log.info("create " + configurationKey + ": " + configurationValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package de.mbremer.configuration;
|
||||
|
||||
import io.quarkus.hibernate.orm.panache.PanacheQuery;
|
||||
import org.eclipse.microprofile.config.spi.ConfigSource;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class DatabaseConfigSource implements ConfigSource {
|
||||
|
||||
@Override
|
||||
public Set<String> getPropertyNames() {
|
||||
try {
|
||||
return Configuration.streamAll().map(c -> ((Configuration) c).getPropertyName()).collect(Collectors.toSet());
|
||||
} catch (IllegalStateException e) {
|
||||
// DB in not up
|
||||
return Set.of();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue(String propertyName) {
|
||||
try {
|
||||
PanacheQuery<Configuration> conf = Configuration.find("propertyName", propertyName);
|
||||
if (conf == null) {
|
||||
// DB in not up
|
||||
return null;
|
||||
}
|
||||
return conf.singleResultOptional().map(c -> ((Configuration) c).getValue()).orElse(null);
|
||||
} catch (IllegalStateException e) {
|
||||
// DB in not up
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return DatabaseConfigSource.class.getSimpleName();
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
package de.mbremer.secutity;
|
||||
|
||||
import de.mbremer.configuration.Configuration;
|
||||
import de.mbremer.configuration.ConfigurationService;
|
||||
import de.mbremer.room.Room;
|
||||
import de.mbremer.traffic.TrafficAdminForm;
|
||||
import de.mbremer.traffic.TrafficUserForm;
|
||||
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
|
||||
import de.mbremer.traffic.TrafficWarningConfig;
|
||||
import io.quarkus.panache.common.Sort;
|
||||
import io.quarkus.qute.Location;
|
||||
import io.quarkus.qute.Template;
|
||||
import io.quarkus.qute.TemplateInstance;
|
||||
import io.quarkus.security.identity.SecurityIdentity;
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.jboss.resteasy.annotations.jaxrs.PathParam;
|
||||
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
|
||||
@@ -18,15 +18,15 @@ import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
|
||||
import javax.annotation.security.PermitAll;
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.transaction.Transactional;
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.NewCookie;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.net.URI;
|
||||
import java.util.Optional;
|
||||
|
||||
import static de.mbremer.configuration.Configuration.GOOGLE_API_KEY;
|
||||
import static de.mbremer.configuration.Configuration.OFFICE_ADRESS_KEY;
|
||||
import static de.mbremer.secutity.Role.ADMIN;
|
||||
|
||||
@Path("/user")
|
||||
@@ -40,29 +40,42 @@ public class UserResource {
|
||||
@Inject
|
||||
UserService userService;
|
||||
@Inject
|
||||
ConfigurationService configurationService;
|
||||
@Inject
|
||||
Template userinit;
|
||||
@Inject
|
||||
@Location("user.html")
|
||||
Template userTemplate;
|
||||
// TODO: 12.10.21
|
||||
// @ConfigProperty(name = "google_api_key")
|
||||
// Optional<String> googleApiKey;
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.TEXT_HTML)
|
||||
@RolesAllowed({"USER", "ADMIN"})
|
||||
public TemplateInstance getUser() {
|
||||
|
||||
TrafficWarningConfig trafficWarningConfig =
|
||||
(TrafficWarningConfig) TrafficWarningConfig.find("user", userService.getCurrentUser()).singleResultOptional().orElse(null);
|
||||
|
||||
TemplateInstance templateInstance = userTemplate
|
||||
.data("current_user", User.find("username", identity.getPrincipal().getName()).singleResult());
|
||||
|
||||
// log.info("Google-API-Key" + googleApiKey.orElse("-"));
|
||||
if (trafficWarningConfig != null) {
|
||||
log.info(trafficWarningConfig.toString());
|
||||
templateInstance
|
||||
.data("traffic_email", trafficWarningConfig.getEmailadress())
|
||||
.data("traffic_active", trafficWarningConfig.isActive())
|
||||
.data("traffic_address", trafficWarningConfig.getAddress())
|
||||
.data("traffic_away", trafficWarningConfig.getTimeAway())
|
||||
.data("traffic_back", trafficWarningConfig.getTimeBack())
|
||||
.data("traffic_duration", trafficWarningConfig.getThreshold());
|
||||
}
|
||||
|
||||
if (identity.hasRole("ADMIN")) {
|
||||
templateInstance
|
||||
.data("is_admin", true)
|
||||
.data("users", User.listAll(Sort.by("username")))
|
||||
.data("rooms", Room.listAll(Sort.by("name")));
|
||||
// .data("google_api_key", googleApiKey.orElse(""));
|
||||
.data("is_admin", true)
|
||||
.data("users", User.listAll(Sort.by("username")))
|
||||
.data("rooms", Room.listAll(Sort.by("name")))
|
||||
.data("google_api_key", Configuration.findGooleApiKey())
|
||||
.data("office_address", Configuration.findOfficeAdress());
|
||||
}
|
||||
|
||||
return templateInstance;
|
||||
@@ -74,10 +87,10 @@ public class UserResource {
|
||||
@Path("init")
|
||||
@PermitAll
|
||||
public Response initialAdd(@MultipartForm UserForm userForm) {
|
||||
if(User.count("role", ADMIN.name()) > 0) {
|
||||
if (User.count("role", ADMIN.name()) > 0) {
|
||||
throw new BadRequestException("Only allowed on initial set up");
|
||||
}
|
||||
if(!ADMIN.name().equals(userForm.role)) {
|
||||
if (!ADMIN.name().equals(userForm.role)) {
|
||||
throw new BadRequestException("Only role ADMIN");
|
||||
}
|
||||
|
||||
@@ -94,7 +107,7 @@ public class UserResource {
|
||||
@Path("init")
|
||||
@PermitAll
|
||||
public TemplateInstance initial() {
|
||||
if(User.count("role", ADMIN.name()) > 0) {
|
||||
if (User.count("role", ADMIN.name()) > 0) {
|
||||
throw new BadRequestException("Only allowed on initial set up");
|
||||
}
|
||||
return userinit.instance();
|
||||
@@ -107,7 +120,7 @@ public class UserResource {
|
||||
public Response logout() {
|
||||
return Response
|
||||
.temporaryRedirect(URI.create("/"))
|
||||
.cookie(new NewCookie("quarkus-credential", null, "/", null, null, 0, false,true))
|
||||
.cookie(new NewCookie("quarkus-credential", null, "/", null, null, 0, false, true))
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -165,16 +178,8 @@ public class UserResource {
|
||||
@Transactional
|
||||
@Path("/traffic/config/")
|
||||
public TemplateInstance configureTrafficAdmin(@MultipartForm TrafficAdminForm trafficAdminForm) {
|
||||
log.info("configure traffic");
|
||||
|
||||
try {
|
||||
Configuration apiKeyConfig = Configuration.find("propertyName", "google_api_key").singleResult();
|
||||
apiKeyConfig.setValue(trafficAdminForm.googleApiKey);
|
||||
log.info("update API-Key: " + trafficAdminForm.googleApiKey);
|
||||
} catch (NoResultException e) {
|
||||
new Configuration("google_api_key", trafficAdminForm.googleApiKey).persist();
|
||||
log.info("create API-Key: " + trafficAdminForm.googleApiKey);
|
||||
}
|
||||
configurationService.createOrUpdate(GOOGLE_API_KEY, trafficAdminForm.googleApiKey);
|
||||
configurationService.createOrUpdate(OFFICE_ADRESS_KEY, trafficAdminForm.officeAddress);
|
||||
|
||||
return getUser();
|
||||
}
|
||||
@@ -187,6 +192,18 @@ public class UserResource {
|
||||
public TemplateInstance configureTrafficUser(@PathParam("username") String username, @MultipartForm TrafficUserForm trafficUserForm) {
|
||||
log.info("configure traffic for " + username);
|
||||
|
||||
User currentUser = userService.getCurrentUser();
|
||||
TrafficWarningConfig trafficWarningConfig = (TrafficWarningConfig) TrafficWarningConfig.find("user", currentUser).singleResultOptional().orElse(null);
|
||||
|
||||
if (trafficWarningConfig == null) {
|
||||
log.info("create new trafficWarningConfig. user " + currentUser.getUsername());
|
||||
trafficWarningConfig = new TrafficWarningConfig();
|
||||
trafficWarningConfig.setUser(currentUser);
|
||||
}
|
||||
trafficWarningConfig = trafficUserForm.updateTrafficWarningConfig(trafficWarningConfig);
|
||||
log.info(trafficWarningConfig.toString());
|
||||
|
||||
trafficWarningConfig.persist();
|
||||
return getUser();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package de.mbremer.traffic;
|
||||
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import javax.ws.rs.ext.MessageBodyReader;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
import java.time.LocalTime;
|
||||
|
||||
@Provider
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
public class LocalTimeMessageBodyReader implements MessageBodyReader<LocalTime> {
|
||||
|
||||
@Override
|
||||
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
|
||||
return type == LocalTime.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalTime readFrom(Class<LocalTime> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
|
||||
return LocalTime.parse(new String(entityStream.readAllBytes()));
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,6 @@ import javax.ws.rs.FormParam;
|
||||
public class TrafficAdminForm {
|
||||
public @FormParam("googleapikey")
|
||||
String googleApiKey;
|
||||
public @FormParam("officeadress")
|
||||
String officeAdress;
|
||||
public @FormParam("officeaddress")
|
||||
String officeAddress;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,29 @@
|
||||
package de.mbremer.traffic;
|
||||
|
||||
import javax.ws.rs.FormParam;
|
||||
import java.time.LocalTime;
|
||||
|
||||
public class TrafficUserForm {
|
||||
public @FormParam("traffic_email")
|
||||
String email;
|
||||
public @FormParam("traffic_address")
|
||||
String address;
|
||||
public @FormParam("traffic_away")
|
||||
LocalTime away;
|
||||
public @FormParam("traffic_back")
|
||||
LocalTime back;
|
||||
public @FormParam("traffic_duration")
|
||||
Integer duration;
|
||||
public @FormParam("traffic_active")
|
||||
boolean active;
|
||||
|
||||
public TrafficWarningConfig updateTrafficWarningConfig(TrafficWarningConfig trafficWarningConfig) {
|
||||
trafficWarningConfig.setEmailadress(email);
|
||||
trafficWarningConfig.setAddress(address);
|
||||
trafficWarningConfig.setThreshold(duration);
|
||||
trafficWarningConfig.setTimeAway(away);
|
||||
trafficWarningConfig.setTimeBack(back);
|
||||
trafficWarningConfig.setActive(active);
|
||||
return trafficWarningConfig;
|
||||
}
|
||||
}
|
||||
|
||||
31
src/main/java/de/mbremer/traffic/TrafficWarningConfig.java
Normal file
31
src/main/java/de/mbremer/traffic/TrafficWarningConfig.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package de.mbremer.traffic;
|
||||
|
||||
import de.mbremer.secutity.User;
|
||||
import io.quarkus.hibernate.orm.panache.PanacheEntity;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.time.LocalTime;
|
||||
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@ToString(exclude = "user")
|
||||
public class TrafficWarningConfig extends PanacheEntity {
|
||||
@NotNull
|
||||
private String emailadress;
|
||||
@NotNull
|
||||
private String address;
|
||||
LocalTime timeAway;
|
||||
LocalTime timeBack;
|
||||
private Integer threshold;
|
||||
private boolean active;
|
||||
@NotNull
|
||||
@OneToOne
|
||||
private User user;
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
de.mbremer.configuration.DatabaseConfigSource
|
||||
@@ -6,7 +6,7 @@
|
||||
quarkus.datasource.db-kind=postgresql
|
||||
|
||||
quarkus.hibernate-orm.dialect=org.hibernate.dialect.PostgreSQL95Dialect
|
||||
%dev.quarkus.hibernate-orm.log.sql=true
|
||||
%dev.quarkus.hibernate-orm.log.sql=false
|
||||
|
||||
quarkus.flyway.migrate-at-start=true
|
||||
# theoretische soll auch 'filesystem:' funktionieren
|
||||
|
||||
10
src/main/resources/db/migration/V0005__trafficwarning.sql
Normal file
10
src/main/resources/db/migration/V0005__trafficwarning.sql
Normal file
@@ -0,0 +1,10 @@
|
||||
create table trafficwarningconfig (
|
||||
id bigint not null primary key,
|
||||
emailadress varchar not null,
|
||||
address varchar not null,
|
||||
timeaway time,
|
||||
timeback time,
|
||||
threshold integer not null,
|
||||
active boolean not null,
|
||||
user_id bigint unique not null references users
|
||||
);
|
||||
@@ -43,13 +43,14 @@
|
||||
<form class="row g-3 mt-2" action="/user/traffic/config" method="POST" name="trafficAdminForm" enctype="multipart/form-data">
|
||||
<div class="col-md-4">
|
||||
<div class="form-floating">
|
||||
<input type="text" name="googleapikey" class="form-control" id="key" placeholder="google-API-Key" required>
|
||||
<input type="text" name="googleapikey" class="form-control" id="key" placeholder="google-API-Key" value="{google_api_key}" required>
|
||||
<label for="key">google-API-Key</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-7">
|
||||
<div class="col-md-8">
|
||||
<div class="form-floating">
|
||||
<input type="text" name="officeadress" class="form-control" id="office" placeholder="Büroadresse" required>
|
||||
<input type="text" name="officeaddress" class="form-control" id="office" placeholder="Büroadresse" value="{office_address}"
|
||||
required>
|
||||
<label for="home">Büroadresse</label>
|
||||
</div>
|
||||
</div>
|
||||
@@ -73,50 +74,57 @@
|
||||
<h3>Verkehrsinfos</h3>
|
||||
Die Fahrzeit wird über die googlemaps-API ermittelt und bei Überschreitung des Grenzwerts via E-Mail gewarnt.
|
||||
<div class="mx-auto">
|
||||
<form class="row g-3 mt-2" action="/user/traffic/config/{current_user.username}" method="POST" name="trafficAdminForm" enctype="multipart/form-data">
|
||||
<form class="row g-3 mt-2" action="/user/traffic/config/{current_user.username}" method="POST" name="trafficUserForm"
|
||||
enctype="multipart/form-data">
|
||||
<div class="col-md-4">
|
||||
<div class="form-floating">
|
||||
<input type="email" name="email" class="form-control" id="email" placeholder="E-Mail-Adresse" required>
|
||||
<input type="email" name="traffic_email" class="form-control" id="email" placeholder="E-Mail-Adresse"
|
||||
value="{traffic_email ?: ''}" required>
|
||||
<label for="email">E-Mail-Adresse</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-8">
|
||||
<div class="form-floating">
|
||||
<input type="text" name="home" class="form-control" id="home" placeholder="Heimatadresse" required>
|
||||
<input type="text" name="traffic_address" class="form-control" id="home" placeholder="Heimatadresse"
|
||||
value="{traffic_address ?: ''}" required>
|
||||
<label for="home">Heimatadresse</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-2">
|
||||
<div class="form-floating">
|
||||
<input type="time" name="away" class="form-control" id="away" required>
|
||||
<input type="time" name="traffic_away" class="form-control" id="away" value="{traffic_away ?: ''}" required>
|
||||
<label for="away">Hinfahrt</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-2">
|
||||
<div class="form-floating">
|
||||
<input type="time" name="back" class="form-control" id="back" required>
|
||||
<input type="time" name="traffic_back" class="form-control" id="back" value="{traffic_back ?: ''}" required>
|
||||
<label for="back">Rückfahrt</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-3">
|
||||
<div class="form-floating">
|
||||
<input type="number" min="1" max="999" name="duration" class="form-control" placeholder="Grenzwert Fahrzeit" id="duration"
|
||||
required>
|
||||
<input type="number" min="1" max="999" name="traffic_duration" class="form-control" placeholder="Grenzwert Fahrzeit" id="duration"
|
||||
value="{traffic_duration ?: ''}" required>
|
||||
<label for="duration">Grenzwert Fahrzeit</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-12">
|
||||
<div class="form-check form-switch">
|
||||
<input id="trafficswitch" class="form-check-input" type="checkbox" name="trafficwarning"
|
||||
onchange="this.form.reportValidity() && this.form.submit()" >
|
||||
<input id="trafficswitch" class="form-check-input" type="checkbox" name="traffic_active"
|
||||
{#if traffic_active??}checked{/if} value="true" >
|
||||
<label class="form-check-label" for="trafficswitch">Verkehrsinfos via google-API aktiv</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-1 mt-4">
|
||||
<button type="submit" class="btn btn-primary">Speichern</button>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user