-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backend/frontend] duplicate XLS mapper (#1250)
- Loading branch information
Showing
9 changed files
with
191 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 43 additions & 7 deletions
50
openbas-api/src/main/java/io/openbas/utils/CopyObjectListUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,72 @@ | ||
package io.openbas.utils; | ||
|
||
import io.openbas.database.model.Base; | ||
import jakarta.persistence.Id; | ||
import jakarta.validation.constraints.NotNull; | ||
import org.apache.commons.beanutils.BeanUtils; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.*; | ||
|
||
public class CopyObjectListUtils { | ||
|
||
public static <T extends Base> List<T> copyWithoutIds(@NotNull final List<T> origins, Class<T> clazz) { | ||
List<T> destinations = new ArrayList<>(); | ||
return copyCollection(origins, clazz, destinations, true); | ||
} | ||
public static <T extends Base> List<T> copy(@NotNull final List<T> origins, Class<T> clazz) { | ||
List<T> destinations = new ArrayList<>(); | ||
return copyCollection(origins, clazz, destinations); | ||
return copyCollection(origins, clazz, destinations, false); | ||
} | ||
|
||
public static <T extends Base> Set<T> copy(@NotNull final Set<T> origins, Class<T> clazz) { | ||
Set<T> destinations = new HashSet<>(); | ||
return copyCollection(origins, clazz, destinations); | ||
return copyCollection(origins, clazz, destinations, false); | ||
} | ||
|
||
public static <T extends Base, C extends Collection<T>> C copyCollection(@NotNull final C origins, Class<T> clazz, C destinations) { | ||
public static <T extends Base, C extends Collection<T>> C copyCollection( | ||
@NotNull final C origins, Class<T> clazz, C destinations, Boolean withoutId) { | ||
origins.forEach(origin -> { | ||
try { | ||
T destination = clazz.getDeclaredConstructor().newInstance(); | ||
BeanUtils.copyProperties(destination, origin); | ||
destinations.add(destination); | ||
if (withoutId){ | ||
destinations.add(copyObjectWithoutId(origin, clazz)); | ||
} else { | ||
T destination = clazz.getDeclaredConstructor().newInstance(); | ||
BeanUtils.copyProperties(destination, origin); | ||
destinations.add(destination); | ||
} | ||
} catch (IllegalAccessException | InvocationTargetException | InstantiationException | | ||
NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
throw new RuntimeException("Failed to copy object", e); | ||
} | ||
}); | ||
return destinations; | ||
} | ||
|
||
public static <T, C> T copyObjectWithoutId(C origin, Class<T> targetClass) { | ||
try { | ||
T target = targetClass.getDeclaredConstructor().newInstance(); | ||
|
||
// Get all declared fields from the source object | ||
Field[] fields = origin.getClass().getDeclaredFields(); | ||
|
||
for (Field field : fields) { | ||
field.setAccessible(true); | ||
|
||
// Skip the 'id' field | ||
if (field.isAnnotationPresent(Id.class)) { | ||
continue; | ||
} | ||
|
||
// Copy the field value from source to target | ||
Field targetField = target.getClass().getDeclaredField(field.getName()); | ||
targetField.setAccessible(true); | ||
targetField.set(target, field.get(origin)); | ||
} | ||
return target; | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to copy object", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.