Skip to content

Commit

Permalink
Filtering in recursive Initialization
Browse files Browse the repository at this point in the history
Serialization brikule (now i know how to serialize ids instead of fetching)
  • Loading branch information
rcMarty committed Feb 9, 2024
1 parent 256b169 commit 4ff2a12
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static void main(String[] args) {
//jdbc:mariadb://49.13.93.112:3306/tts_api

SpringApplication app = new SpringApplication(TtsApiApplication.class);

app.setDefaultProperties(env);
app.run(args);
log.debug("Running with login: " + dotenv.get("DB_USER") + " -> " + dotenv.get("DB_PASSWORD"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cz.trailsthroughshadows.api.rest.jsonfilter;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import jakarta.persistence.Persistence;
import org.hibernate.proxy.HibernateProxy;

import java.io.IOException;
import java.util.Collection;

public class IncludeNullOrEmptySerializer extends com.fasterxml.jackson.databind.JsonSerializer<Object> {
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value == null) {
gen.writeNull();
} else if (!Persistence.getPersistenceUtil().isLoaded(value)) {
if (value instanceof Collection) {
gen.writeStartArray();
for (Object o : (Collection) value) {
gen.writeObject(o);
}
gen.writeEndArray();
} else {
gen.writeObject(((HibernateProxy) value).getHibernateLazyInitializer().getIdentifier());
}
} else {
gen.writeObject(value);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package cz.trailsthroughshadows.api.rest.jsonfilter;

import jakarta.persistence.Persistence;
import org.hibernate.proxy.HibernateProxy;

import java.util.Collection;

public class LazyFieldsFilter {
@Override
public boolean equals(Object obj) {

if (obj == null)
return true;
return false;

if (obj instanceof Collection)
return false;

// if (obj instanceof HibernateProxy)
// return false;

return !Persistence.getPersistenceUtil().isLoaded(obj);
if (Persistence.getPersistenceUtil().isLoaded(obj))
return false;

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import cz.trailsthroughshadows.api.rest.jsonfilter.IncludeNullOrEmptySerializer;
import cz.trailsthroughshadows.api.rest.jsonfilter.LazyFieldsFilter;
import cz.trailsthroughshadows.api.table.action.attack.Attack;
import cz.trailsthroughshadows.api.table.action.movement.Movement;
Expand All @@ -15,13 +17,14 @@
import lombok.Setter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
@NoArgsConstructor
@Table(name = "Action")
@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = LazyFieldsFilter.class)
//@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = LazyFieldsFilter.class)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Action {

Expand All @@ -45,6 +48,7 @@ public class Action {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "movement")
@JsonSerialize(using = IncludeNullOrEmptySerializer.class)
private Movement movement;

@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -61,6 +65,7 @@ public class Action {

@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "idAction")
@JsonSerialize(using = IncludeNullOrEmptySerializer.class)
private List<SummonAction> summonActions;

public enum Discard implements Serializable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cz.trailsthroughshadows.api.table.action.summon;

import com.fasterxml.jackson.annotation.JsonInclude;
import cz.trailsthroughshadows.api.rest.jsonfilter.LazyFieldsFilter;
import cz.trailsthroughshadows.api.table.action.Action;
import cz.trailsthroughshadows.api.table.effect.Effect;
import cz.trailsthroughshadows.api.table.effect.foraction.SummonEffect;
Expand All @@ -16,6 +18,7 @@
@AllArgsConstructor
@Entity
@Table(name = "Summon")
@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = LazyFieldsFilter.class)
public class Summon implements Cloneable {

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
package cz.trailsthroughshadows.api.table.action.summon;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import cz.trailsthroughshadows.api.rest.jsonfilter.LazyFieldsFilter;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Externalizable;
import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "SummonAction")
@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = LazyFieldsFilter.class)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class SummonAction {

@EmbeddedId
private SummonActionId id;

@ManyToOne(fetch = FetchType.LAZY)
@MapsId("summon_id")
@MapsId("idSummon")
@JoinColumn(name = "idSummon")
private Summon summon;

@Column(name = "idAction", insertable = false, updatable = false)
private int idAction;

@Column
private Integer range;

@Embeddable
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class SummonActionId implements Serializable {
@Column(name = "idSummon")
private Integer summon_id;
private Integer idSummon;
@Column(name = "idAction")
private Integer action_id;
private Integer idAction;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,35 @@
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j2
public class Initialization {

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

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

private static void hibernateInitializeAll(Object entity, Map<Object, Integer> visited) {
private static void hibernateInitializeAll(Object entity, Map<Object, Integer> visited, List<String> filter) {

if (entity == null || visited.containsKey(entity)) {
if ((visited.get(entity) > 10))
Expand All @@ -32,24 +46,36 @@ private static void hibernateInitializeAll(Object entity, Map<Object, Integer> v

//log.debug("Initializing object: {}", entity.getClass().getSimpleName());
Hibernate.initialize(entity);
if (entity instanceof HibernateProxy) {
entity = ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation();
}
// if (entity instanceof HibernateProxy) {
// entity = ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation();
// }

initializeFields(entity, visited, filter);

}

private static void initializeFields(Object entity, Map<Object, Integer> visited, List<String> filter) {
for (Field field : entity.getClass().getDeclaredFields()) {
field.setAccessible(true);
//log.debug(" Initializing property: {}", field.getName());

if (!(filter.contains(field.getName()) || filter.isEmpty())) {
log.debug(" Skipping property: {}", field.getName());
continue;
}

log.debug(" Initializing property: {}", field.getName());

try {
Object child = field.get(entity);
if (child != null && !isPrimitiveOrWrapper(child.getClass())) {
Hibernate.initialize(child);
log.debug(" Initializing item: {}", child.getClass().getSimpleName());
if (child instanceof Collection) {
for (Object item : (Collection) child) {
hibernateInitializeAll(item, visited);
hibernateInitializeAll(item, visited, filter);
}
} else {
hibernateInitializeAll(child, visited);
hibernateInitializeAll(child, visited, filter);
}
}
} catch (IllegalAccessException e) {
Expand All @@ -59,7 +85,8 @@ private static void hibernateInitializeAll(Object entity, Map<Object, Integer> v
}

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);
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) || clazz.isEnum();

}

}

0 comments on commit 4ff2a12

Please sign in to comment.