Commit 6dc42444 authored by Andrei Zhikulin's avatar Andrei Zhikulin
Browse files

recursion fix

parent fd7477f2
......@@ -34,6 +34,8 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<!--security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
......
package server.config;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import javax.sql.DataSource;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// @Autowired
// private DataSource dataSource;
//
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
// http
// .authorizeRequests()
// .antMatchers("/").permitAll()
// .anyRequest().authenticated()
// .and()
// .formLogin()
// .loginPage("/login")
// .permitAll()
// .and()
// .logout()
// .permitAll();
// http
// .csrf().disable()
// .headers().addHeaderWriter(
// new XFrameOptionsHeaderWriter(XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN)).and()
......@@ -33,24 +47,12 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// .and();
}
@Bean
@Override
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
// @Override
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// auth
// .inMemoryAuthentication()
// .withUser("user").password("user123").roles("USER").and()
// .withUser("admin").password("admin").roles("ADMIN","USER");
// auth.jdbcAuthentication()
// .dataSource(dataSource)
// .passwordEncoder(NoOpPasswordEncoder.getInstance())
// .usersByUsernameQuery("select email, password from passenger where email=?")
// .authoritiesByUsernameQuery("select u.username, ur.roles from usr u inner join user_role ur on u.id = ur.user_id where u.username=?");
// }
}
\ No newline at end of file
......@@ -32,6 +32,7 @@ public class FlightDto {
// private AirlineEntity airlineByAirlineid;
// @NotNull
// private TerminalEntity terminalByTerminalid;
private List<TicketEntity> ticketsById;
public int getId() {
......
package server.model;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.hibernate.annotations.CreationTimestamp;
import org.springframework.core.annotation.Order;
import server.model.serializer.PassengerSerializer;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@Entity
@Table(name = "flight", schema = "airbook", catalog = "ear2018zs_22")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class FlightEntity {
@Id
private int id;
private int planeid;
private int airlineid;
private Integer id;
@NotNull
private Integer planeid;
@NotNull
private Integer airlineid;
@NotNull
private Timestamp departmentdate;
@NotNull
private Timestamp arrilvalsdate;
@NotNull
private String department;
@NotNull
private String arrival;
private int distance;
private int terminalid;
@NotNull
private Integer distance;
@NotNull
private Integer terminalid;
private AirlineEntity airlineByAirlineid;
private TerminalEntity terminalByTerminalid;
private List<TicketEntity> ticketsById;
@JsonSerialize(using = PassengerSerializer.class)
private List<PassengerEntity> passengers;
@Id
@Column(name = "id")
public int getId() {
public Integer getId() {
return id;
}
public void setId(int id) {
public void setId(Integer id) {
this.id = id;
}
@Basic
@Column(name = "planeid")
public int getPlaneid() {
public Integer getPlaneid() {
return planeid;
}
public void setPlaneid(int planeid) {
public void setPlaneid(Integer planeid) {
this.planeid = planeid;
}
@Basic
@Column(name = "airlineid")
public int getAirlineid() {
public Integer getAirlineid() {
return airlineid;
}
public void setAirlineid(int airlineid) {
public void setAirlineid(Integer airlineid) {
this.airlineid = airlineid;
}
......@@ -108,46 +114,24 @@ public class FlightEntity {
@Basic
@Column(name = "distance")
public int getDistance() {
public Integer getDistance() {
return distance;
}
public void setDistance(int distance) {
public void setDistance(Integer distance) {
this.distance = distance;
}
@Basic
@Column(name = "terminalid")
public int getTerminalid() {
public Integer getTerminalid() {
return terminalid;
}
public void setTerminalid(int terminalid) {
public void setTerminalid(Integer terminalid) {
this.terminalid = terminalid;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FlightEntity that = (FlightEntity) o;
return id == that.id &&
planeid == that.planeid &&
airlineid == that.airlineid &&
distance == that.distance &&
terminalid == that.terminalid &&
Objects.equals(departmentdate, that.departmentdate) &&
Objects.equals(arrilvalsdate, that.arrilvalsdate) &&
Objects.equals(department, that.department) &&
Objects.equals(arrival, that.arrival);
}
// @Override
// public int hashCode() {
//
// return Objects.hash(id, planeid, airlineid, departmentdate, arrilvalsdate, department, arrival, distance, terminalid);
// }
@JsonIgnore
@ManyToOne(cascade = {CascadeType.PERSIST})
@JoinColumn(name = "airlineid", referencedColumnName = "id", nullable = false, insertable = false, updatable = false)
......@@ -180,20 +164,10 @@ public class FlightEntity {
this.ticketsById = ticketsById;
}
private List<PassengerEntity> passengers;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "ticket", catalog = "airbook", joinColumns = {
@JoinColumn(name = "flightid", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "passengerid",
nullable = false, updatable = false) })
// @ManyToMany(cascade = {CascadeType.MERGE})
// @JoinTable(name = "ticket",
// joinColumns = @JoinColumn(name = "flightid", referencedColumnName = "id"),
// inverseJoinColumns = @JoinColumn(name = "passengerid", referencedColumnName = "id"))
// @JsonIgnoreProperties("passengers")
@JoinTable(name = "ticket", catalog = "airbook",
joinColumns = {@JoinColumn(name = "flightid", nullable = false, updatable = false)},
inverseJoinColumns = { @JoinColumn(name = "passengerid", nullable = false, updatable = false)})
public List<PassengerEntity> getPassengers() {
return passengers;
}
......@@ -201,4 +175,25 @@ public class FlightEntity {
public void setPassengers(List<PassengerEntity> passengers) {
this.passengers = passengers;
}
@Override
public int hashCode() {
return Objects.hash(id, planeid, airlineid, departmentdate, arrilvalsdate, department, arrival, distance, terminalid);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FlightEntity that = (FlightEntity) o;
return id == that.id &&
planeid == that.planeid &&
airlineid == that.airlineid &&
distance == that.distance &&
terminalid == that.terminalid &&
Objects.equals(departmentdate, that.departmentdate) &&
Objects.equals(arrilvalsdate, that.arrilvalsdate) &&
Objects.equals(department, that.department) &&
Objects.equals(arrival, that.arrival);
}
}
\ No newline at end of file
package server.model;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import server.model.serializer.FlightSerializer;
import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;
@Entity
@Table(name = "passenger", schema = "airbook", catalog = "ear2018zs_22")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class PassengerEntity {
@NotNull
private int id;
@NotNull
@Email
private String email;
@NotNull
private String name;
@NotNull
private String surname;
@NotNull
private String password;
@NotNull
private int phonenumber;
private List<TicketEntity> ticketsById;
@JsonSerialize(using = FlightSerializer.class)
private List<FlightEntity> flights;
@Id
@Column(name = "id")
public int getId() {
......@@ -81,6 +91,24 @@ public class PassengerEntity {
this.phonenumber = phonenumber;
}
@OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true, mappedBy = "passengerByPassengerid")
public List<TicketEntity> getTicketsById() {
return ticketsById;
}
public void setTicketsById(List<TicketEntity> ticketsById) {
this.ticketsById = ticketsById;
}
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "passengers")
public List<FlightEntity> getFlights() {
return flights;
}
public void setFlights(List<FlightEntity> flights) {
this.flights = flights;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
......@@ -100,31 +128,6 @@ public class PassengerEntity {
return Objects.hash(id, email, name, surname, password, phonenumber);
}
@OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true, mappedBy = "passengerByPassengerid")
public List<TicketEntity> getTicketsById() {
return ticketsById;
}
public void setTicketsById(List<TicketEntity> ticketsById) {
this.ticketsById = ticketsById;
}
private List<FlightEntity> flights;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "passengers")
// @ManyToMany(cascade = {CascadeType.MERGE})
// @JoinTable(name = "ticket",
// joinColumns = @JoinColumn(name = "passengerid", referencedColumnName = "id"),
// inverseJoinColumns = @JoinColumn(name = "flightid", referencedColumnName = "id"))
// @JsonIgnoreProperties("flights")
public List<FlightEntity> getFlights() {
return flights;
}
public void setFlights(List<FlightEntity> flights) {
this.flights = flights;
}
@Override
public String toString() {
return "PassengerEntity{" +
......
package server.model.serializer;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import server.model.FlightEntity;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FlightSerializer extends StdSerializer<List<FlightEntity>> {
public FlightSerializer() {
this(null);
}
private FlightSerializer(final Class<List<FlightEntity>> t) {
super(t);
}
@Override
public void serialize(List<FlightEntity> flights, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException {
List<FlightEntity> ids = new ArrayList<>();
for (FlightEntity flight : flights) {
FlightEntity fl = flight;
fl.setPassengers(null);
fl.setTicketsById(null);
ids.add(fl);
}
generator.writeObject(ids);
}
}
package server.model.serializer;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import server.model.PassengerEntity;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PassengerSerializer extends StdSerializer<List<PassengerEntity>> {
public PassengerSerializer() {
this(null);
}
private PassengerSerializer(final Class<List<PassengerEntity>> t) {
super(t);
}
@Override
public void serialize(List<PassengerEntity> passengers, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException {
List<PassengerEntity> ids = new ArrayList<>();
for (PassengerEntity passenger : passengers) {
PassengerEntity pas = passenger;
pas.setFlights(null);
pas.setTicketsById(null);
ids.add(pas);
}
generator.writeObject(ids);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment