Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ws/TTS-API into development
  • Loading branch information
Firestone82 committed Feb 6, 2024
2 parents 6213051 + ea775a3 commit e0c356b
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cz.trailsthroughshadows.api.table.enemy.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import cz.trailsthroughshadows.api.table.enemy.model.dto.EnemyDTO;
import cz.trailsthroughshadows.api.table.schematic.hex.model.dto.HexDTO;
import lombok.Data;
Expand All @@ -8,7 +9,7 @@

@Data
@EqualsAndHashCode(callSuper = true)
//@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Enemy extends EnemyDTO {

private HexDTO hex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import cz.trailsthroughshadows.api.table.action.Action;
import cz.trailsthroughshadows.api.table.effect.Effect;
import cz.trailsthroughshadows.api.table.effect.forothers.EnemyEffect;
import jakarta.annotation.Nullable;
import cz.trailsthroughshadows.api.util.reflect.Initialization;
import jakarta.annotation.Nullable;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -61,10 +61,8 @@ public List<Action> getActions() {
return actions.stream().map(EnemyActionDTO::getAction).toList();
}


public void loadAll() throws Exception {
Initialization init = new Initialization();
init.initializeAndUnproxy(this);
public void loadAll() {
Initialization.hibernateInitializeAll(this);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cz.trailsthroughshadows.api.table.schematic.hex.model.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import cz.trailsthroughshadows.api.rest.jsonfilter.LazyFieldsFilter;
import cz.trailsthroughshadows.api.table.enemy.model.dto.EnemyDTO;
import jakarta.persistence.*;
import lombok.Data;
Expand All @@ -12,13 +14,14 @@
@Entity
@NoArgsConstructor
@Table(name = "HexEnemy")
@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = LazyFieldsFilter.class)
public class HexEnemyDTO {

@EmbeddedId
@GeneratedValue(strategy = GenerationType.IDENTITY)
private HexEnemyId key;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idEnemy", insertable = false, updatable = false)
private EnemyDTO enemy;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cz.trailsthroughshadows.api.table.schematic.hex.model.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import cz.trailsthroughshadows.api.rest.jsonfilter.LazyFieldsFilter;
import cz.trailsthroughshadows.api.table.schematic.obstacle.model.ObstacleDTO;
import jakarta.persistence.*;
import lombok.*;
Expand All @@ -11,14 +13,15 @@
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "HexObstacle")
@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = LazyFieldsFilter.class)
public class HexObstacleDTO {

@EmbeddedId
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter(AccessLevel.NONE)
private HexObstacleId key;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idObstacle", insertable = false, updatable = false)
private ObstacleDTO obstacle;

Expand All @@ -27,7 +30,7 @@ public class HexObstacleDTO {
public static class HexObstacleId implements Serializable {

@Column(nullable = false)
private int idObstacle;
private Integer idObstacle;

@Column(nullable = false)
private Integer idHex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,24 @@ public ResponseEntity<RestPaginatedResult<Location>> getLocations(
}

@GetMapping("/locations/{id}")
public ResponseEntity<Location> getLocationById(@PathVariable int id) {
public ResponseEntity<LocationDTO> getLocationById(@PathVariable int id) {
LocationDTO locationDTO = locationRepo
.findById(id)
.orElseThrow(() -> RestException.of(HttpStatus.NOT_FOUND, "Location with id '%d' not found!", id));

return new ResponseEntity<>(Location.fromDTO(locationDTO), HttpStatus.OK);
return new ResponseEntity<>(locationDTO, HttpStatus.OK);
}

@GetMapping("/locations/{locationId}/parts/{partId}")
public ResponseEntity<Part> getPartByLocationId(@PathVariable int locationId, @PathVariable int partId) {
Part part = Objects.requireNonNull(getLocationById(locationId)
.getBody())
.getParts()
.stream()
.filter(p -> p.getId() == partId).findFirst()
.orElseThrow(() -> RestException.of(HttpStatus.NOT_FOUND, "Part with id '%d' not found!", partId));

LocationDTO locationDTO = locationRepo
.findById(locationId)
.orElseThrow(() -> RestException.of(HttpStatus.NOT_FOUND, "Location with id '%d' not found!", locationId));
Location location = Location.fromDTO(locationDTO);

Part part = location.getParts().stream().filter(p -> p.getId() == partId).findFirst()
.orElseThrow(() -> RestException.of(HttpStatus.NOT_FOUND, "Part with id '%d' not found!", partId));

return new ResponseEntity<>(part, HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import cz.trailsthroughshadows.api.table.schematic.location.model.dto.LocationDTO;
import cz.trailsthroughshadows.api.table.schematic.part.model.Part;
import cz.trailsthroughshadows.api.table.schematic.part.model.PartDTO;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.modelmapper.ModelMapper;

import java.util.ArrayList;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
Expand All @@ -14,9 +19,29 @@ public class Location extends LocationDTO {
// TODO: Map locations only by specific campaign
public static Location fromDTO(LocationDTO dto) {
ModelMapper modelMapper = new ModelMapper();
dto.loadAll();
return modelMapper.map(dto, Location.class);
}

@Override
public List<Part> getParts() {
if (parts == null) {
return new ArrayList<>();
}

return parts.stream()
.map(locationPart -> Part.fromDTO(
locationPart.getPart(),
locationPart.getRotation(),
enemies.stream()
.filter(hexEnemy -> hexEnemy.getKey().getIdPart() == locationPart.getPart().getId())
.toList(),
obstacles.stream()
.filter(hexObstacle -> hexObstacle.getKey().getIdPart() == locationPart.getPart().getId())
.toList()
)).toList();
}

public enum Type {
CITY, DUNGEON, MARKET, QUEST
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
import cz.trailsthroughshadows.api.table.schematic.hex.model.dto.HexObstacleDTO;
import cz.trailsthroughshadows.api.table.schematic.location.model.Location;
import cz.trailsthroughshadows.api.table.schematic.part.model.Part;
import cz.trailsthroughshadows.api.table.schematic.part.model.PartDTO;
import cz.trailsthroughshadows.api.util.reflect.Initialization;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.modelmapper.ModelMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Entity
Expand All @@ -24,55 +28,51 @@ public class LocationDTO {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
protected int id;

@Column(name = "title", nullable = false)
private String title;
protected String title;

@Column(name = "tag", length = 32)
private String tag;
protected String tag;

@Enumerated(EnumType.STRING)
@Column(name = "type", nullable = false)
private Location.Type type;
protected Location.Type type;

@Column(name = "description", columnDefinition = "TEXT")
private String description;
protected String description;

@OneToMany(mappedBy = "key.idLocation", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<LocationPartDTO> parts;
protected List<LocationPartDTO> parts;

@OneToMany(mappedBy = "idLocation", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<LocationDoorDTO> doors;
protected List<LocationDoorDTO> doors;

@OneToMany(mappedBy = "idLocation", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<LocationStartDTO> startHexes;
protected List<LocationStartDTO> startHexes;

@OneToMany(mappedBy = "idStart", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<LocationPathDTO> paths;
protected List<LocationPathDTO> paths;

@OneToMany(mappedBy = "key.idLocation", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<HexEnemyDTO> enemies;
protected List<HexEnemyDTO> enemies;

@OneToMany(mappedBy = "key.idLocation", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<HexObstacleDTO> obstacles;
protected List<HexObstacleDTO> obstacles;

public List<Part> getParts() {
if (parts == null) {
return new ArrayList<>();
}

if (parts == null) return new ArrayList<>();
ModelMapper modelMapper = new ModelMapper();
return parts.stream()
.map(locationPart -> Part.fromDTO(
locationPart.getPart(),
locationPart.getRotation(),
enemies.stream()
.filter(hexEnemy -> hexEnemy.getKey().getIdPart() == locationPart.getPart().getId())
.toList(),
obstacles.stream()
.filter(hexObstacle -> hexObstacle.getKey().getIdPart() == locationPart.getPart().getId())
.toList()
)).toList();
.map(LocationPartDTO::getPart)
.map(partDTO -> modelMapper.map(partDTO, Part.class))
.collect(Collectors.toList());
}

public void loadAll(){
Initialization.hibernateInitializeAll(this);
}

}

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cz.trailsthroughshadows.api.table.schematic.obstacle.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import cz.trailsthroughshadows.api.rest.jsonfilter.LazyFieldsFilter;
import cz.trailsthroughshadows.api.table.effect.Effect;
import cz.trailsthroughshadows.api.table.effect.forothers.ObstacleEffect;
import jakarta.persistence.*;
Expand All @@ -13,6 +15,7 @@
@Entity
@NoArgsConstructor
@Table(name = "Obstacle")
@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = LazyFieldsFilter.class)
public class ObstacleDTO {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
@Log4j2
public class Initialization {

public void initializeAndUnproxy(Object entity) {
/**
* Initializes all fields of the given entity in recursive way.
* @param entity Entity to initialize
*/
public static void hibernateInitializeAll(Object entity) {
Map<Object, Integer> visited = new HashMap<>();
initializeAndUnproxy(entity, visited);
hibernateInitializeAll(entity, visited);
}

private void initializeAndUnproxy(Object entity, Map<Object, Integer> visited) {
private static void hibernateInitializeAll(Object entity, Map<Object, Integer> visited) {

if (entity == null || visited.containsKey(entity)) {
if ((visited.get(entity) > 10))
Expand All @@ -42,10 +46,10 @@ private void initializeAndUnproxy(Object entity, Map<Object, Integer> visited) {
Hibernate.initialize(child);
if (child instanceof Collection) {
for (Object item : (Collection) child) {
initializeAndUnproxy(item, visited);
hibernateInitializeAll(item, visited);
}
} else {
initializeAndUnproxy(child, visited);
hibernateInitializeAll(child, visited);
}
}
} catch (IllegalAccessException e) {
Expand All @@ -54,7 +58,7 @@ private void initializeAndUnproxy(Object entity, Map<Object, Integer> visited) {
}
}

private boolean isPrimitiveOrWrapper(Class<?> clazz) {
private static boolean isPrimitiveOrWrapper(Class<?> clazz) {
return clazz.isPrimitive() || clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Long.class) || clazz.equals(Double.class) || clazz.equals(Float.class) || clazz.equals(Character.class) || clazz.equals(Byte.class) || clazz.equals(Short.class) || clazz.equals(Boolean.class);
}

Expand Down

0 comments on commit e0c356b

Please sign in to comment.