From cdaccb3840a98b330f5883e6a760dd18a8e0261c Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Mon, 16 Jun 2025 06:56:35 +0200 Subject: [PATCH] Add composite asset support with creation and descriptor handling --- .../pcinv/controllers/WebController.java | 67 +++++++++------- .../pcinv/meta/AssetDescriptor.java | 7 ++ .../be/seeseepuff/pcinv/models/Composite.java | 59 ++++++++++++++ .../be/seeseepuff/pcinv/models/CpuAsset.java | 12 +-- .../pcinv/services/AssetService.java | 80 ++++++++++++++++++- .../resources/templates/create_asset.html | 1 + 6 files changed, 186 insertions(+), 40 deletions(-) create mode 100644 src/main/java/be/seeseepuff/pcinv/models/Composite.java diff --git a/src/main/java/be/seeseepuff/pcinv/controllers/WebController.java b/src/main/java/be/seeseepuff/pcinv/controllers/WebController.java index 51680c9..d0411b0 100644 --- a/src/main/java/be/seeseepuff/pcinv/controllers/WebController.java +++ b/src/main/java/be/seeseepuff/pcinv/controllers/WebController.java @@ -39,6 +39,8 @@ public class WebController { private static final String ASSETS = "assets"; /// The name of the model attribute that holds the asset being viewed or edited. private static final String ASSET = "asset"; + /// The name of the model attribute that holds the list of asset types. + private static final String TYPES = "types"; /// The name of the model attribute that holds a list of all properties of all descriptors. private static final String PROPERTIES = "properties"; /// The name of the model attribute that holds the action to be performed. @@ -139,8 +141,6 @@ public class WebController { /** * Shows a view where the user can create a specific type of composite asset. - * - * @param type The type of composite asset to create. */ @GetMapping("/create_composite") public String createCompositeType(Model model, HttpServletRequest request) { @@ -167,6 +167,7 @@ public class WebController { .build()); model.addAttribute(INPUT_LIST, inputLists); model.addAttribute(BUILDS, buildService.getAllBuilds()); + model.addAttribute(TYPES, parameters.keySet().stream().toList()); return "create_asset"; } } @@ -214,18 +215,18 @@ public class WebController { model.addAttribute(TIME, System.currentTimeMillis()); model.addAttribute(ACTION, "view"); - var asset = assetService.getAssetByQr(qr); - if (asset == null) { + var composite = assetService.getAssetByQr(qr); + if (composite == null) { return "redirect:/"; } var workLogSizeStr = formData.getFirst(WORKLOG_SIZE); if (workLogSizeStr != null) { var workLogSize = Integer.parseInt(workLogSizeStr); - if (asset.getAsset().getWorkLog().size() == workLogSize) { + if (composite.getAsset().getWorkLog().size() == workLogSize) { var comment = formData.getFirst("comment"); if (comment != null && !comment.isBlank()) { - assetService.addWorkLogEntry(asset, comment); + assetService.addWorkLogEntry(composite, comment); } } } @@ -268,7 +269,7 @@ public class WebController { return "redirect:/"; } model.addAttribute(ASSET, asset); - model.addAttribute(DESCRIPTORS, assetService.getAssetDescriptorTree(asset.getAsset().getType())); + model.addAttribute(DESCRIPTORS, assetService.getAssetDescriptorTree(asset)); model.addAttribute(DESCRIPTOR, assetService.getAssetDescriptor(asset.getAsset().getType())); model.addAttribute(WORKLOG, asset.getAsset().getWorkLog().stream() .sorted(Comparator.comparing(WorkLogEntry::getDate).reversed()) @@ -307,6 +308,35 @@ public class WebController { return "create_asset"; } + /** + * Handles the form submission for creating an asset. + * + * @param model The model to add attributes to. + * @param type The type of asset to create. + * @return The view name for creating the asset. + */ + @PostMapping( + value = "/create/{type}", + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE + ) + public String createTypePost(Model model, @PathVariable String type, @RequestBody MultiValueMap formData) { + model.addAttribute(TIME, System.currentTimeMillis()); + var formMap = new HashMap(); + formData.forEach((key, values) -> { + if (!values.isEmpty()) { + formMap.put(key, values.getFirst()); + } + }); + if (type.equals("composite")) { + var compositeTypes = formData.get("type"); + var asset = assetService.createCompositeAsset(compositeTypes, formMap); + return "redirect:/view/" + asset.getQr(); + } else { + var asset = assetService.createAsset(type, formMap); + return "redirect:/view/" + asset.getQr(); + } + } + /** * Shows a view where the user can edit an existing asset. * @@ -367,27 +397,4 @@ public class WebController { var asset = assetService.editAsset(qr, formMap); return "redirect:/view/" + asset.getQr(); } - - /** - * Handles the form submission for creating an asset. - * - * @param model The model to add attributes to. - * @param type The type of asset to create. - * @return The view name for creating the asset. - */ - @PostMapping( - value = "/create/{type}", - consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE - ) - public String createTypePost(Model model, @PathVariable String type, @RequestBody MultiValueMap formData) { - model.addAttribute(TIME, System.currentTimeMillis()); - var formMap = new HashMap(); - formData.forEach((key, values) -> { - if (!values.isEmpty()) { - formMap.put(key, values.getFirst()); - } - }); - var asset = assetService.createAsset(type, formMap); - return "redirect:/view/" + asset.getQr(); - } } diff --git a/src/main/java/be/seeseepuff/pcinv/meta/AssetDescriptor.java b/src/main/java/be/seeseepuff/pcinv/meta/AssetDescriptor.java index 1cde430..72d8275 100644 --- a/src/main/java/be/seeseepuff/pcinv/meta/AssetDescriptor.java +++ b/src/main/java/be/seeseepuff/pcinv/meta/AssetDescriptor.java @@ -16,6 +16,13 @@ import java.util.function.Supplier; @Getter @Builder public class AssetDescriptor { + public static final AssetDescriptor COMPOSITE = AssetDescriptor.builder() + .type("composite") + .displayName("Composite Asset") + .pluralName("Composite Assets") + .visible(false) + .build(); + /// The type of property, e.g.: ram, asset, etc... private final String type; diff --git a/src/main/java/be/seeseepuff/pcinv/models/Composite.java b/src/main/java/be/seeseepuff/pcinv/models/Composite.java new file mode 100644 index 0000000..b25a7d7 --- /dev/null +++ b/src/main/java/be/seeseepuff/pcinv/models/Composite.java @@ -0,0 +1,59 @@ +package be.seeseepuff.pcinv.models; + +import jakarta.annotation.Nullable; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +/** + * Represents a composite asset that can contain multiple other assets. + */ +@Getter +@Setter +@RequiredArgsConstructor +@AllArgsConstructor +public class Composite implements Asset { + private final GenericAsset genericAsset; + private List assets = new ArrayList<>(); + + /** + * Constructs a Composite with a single asset. + * + * @param asset The asset to be added to the composite. + */ + public Composite(Asset asset) { + this.genericAsset = asset.getAsset(); + addAsset(asset); + } + + /** + * Adds an asset to the composite. + * + * @param asset The asset to add. If null, it will not be added. + */ + public void addAsset(@Nullable Asset asset) { + if (asset == null) { + return; + } + assets.add(asset); + } + + @Override + public long getId() { + return genericAsset.getId(); + } + + @Override + public GenericAsset getAsset() { + return genericAsset; + } + + @Override + public void setAsset(GenericAsset asset) { + throw new UnsupportedOperationException("Composite is note a modifiable database record."); + } +} diff --git a/src/main/java/be/seeseepuff/pcinv/models/CpuAsset.java b/src/main/java/be/seeseepuff/pcinv/models/CpuAsset.java index 23bbef0..1176f0f 100644 --- a/src/main/java/be/seeseepuff/pcinv/models/CpuAsset.java +++ b/src/main/java/be/seeseepuff/pcinv/models/CpuAsset.java @@ -30,24 +30,24 @@ public class CpuAsset implements Asset @Description("The number of cores in the CPU.") @Property("Cores") - private int cores; + private Integer cores; @Description("The number of threads in the CPU.") @Property("Threads") - private int threads; + private Integer threads; @Description("The base clock speed of the CPU in MHz.") @Property("Base Clock Speed (MHz)") - private int baseClockSpeed; + private Integer baseClockSpeed; @Description("The boost clock speed of the CPU in MHz.") @Property("Boost Clock Speed (MHz)") @HideInOverview - private int boostClockSpeed; + private Integer boostClockSpeed; @Description("The thermal design power (TDP) of the CPU in watts.") @Property("Thermal Design Power (TDP) (W)") - private int tdp; + private Integer tdp; @Description("The socket type of the CPU.") @Property("Socket Type") @@ -63,5 +63,5 @@ public class CpuAsset implements Asset @Description("The manufacturing process of the CPU in nanometers.") @Property("Manufacturing Process (nm)") @HideInOverview - private int manufacturingProcess; + private Integer manufacturingProcess; } diff --git a/src/main/java/be/seeseepuff/pcinv/services/AssetService.java b/src/main/java/be/seeseepuff/pcinv/services/AssetService.java index 1ceb2cb..f8b9dce 100644 --- a/src/main/java/be/seeseepuff/pcinv/services/AssetService.java +++ b/src/main/java/be/seeseepuff/pcinv/services/AssetService.java @@ -2,6 +2,7 @@ package be.seeseepuff.pcinv.services; import be.seeseepuff.pcinv.meta.*; import be.seeseepuff.pcinv.models.Asset; +import be.seeseepuff.pcinv.models.Composite; import be.seeseepuff.pcinv.models.GenericAsset; import be.seeseepuff.pcinv.models.WorkLogEntry; import be.seeseepuff.pcinv.repositories.AssetRepository; @@ -46,12 +47,23 @@ public class AssetService { * @return the Asset associated with the given QR code * @throws IllegalArgumentException if no asset is found with the given QR code */ - public Asset getAssetByQr(long qr) { + public Composite getAssetByQr(long qr) { var genericAsset = genericRepository.findByQr(qr); if (genericAsset == null) { throw new IllegalArgumentException("No asset found with QR code: " + qr); } - return getRepositoryFor(genericAsset.getType()).findByAsset(genericAsset); + if (genericAsset.getType().equals("composite")) { + var assets = new ArrayList(); + for (var repository : repositories) { + var asset = repository.findByAsset(genericAsset); + if (asset != null) { + assets.add(asset); + } + } + return new Composite(genericAsset, assets); + } else { + return new Composite(getRepositoryFor(genericAsset.getType()).findByAsset(genericAsset)); + } } /** @@ -109,18 +121,34 @@ public class AssetService { * @return the AssetProperties for the specified type */ public AssetDescriptor getAssetDescriptor(String type) { + if (type.equals("composite")) { + return AssetDescriptor.COMPOSITE; + } return getAssetDescriptors().getAssets().stream() .filter(asset -> asset.getType().equals(type)) .findFirst() .orElseThrow(() -> new IllegalArgumentException("Unknown asset type: " + type)); } + /** + * Gets the asset descriptor for a specific asset instance. + * + * @param asset the asset instance to retrieve the descriptor for + * @return the AssetDescriptor for the specified asset + */ + public AssetDescriptor getAssetDescriptor(Asset asset) { + var type = asset.getClass().getAnnotation(AssetInfo.class).type(); + return getAssetDescriptor(type); + } + /** * Retrieves a tree of asset descriptors for the specified type. * * @param type the type of asset to retrieve descriptors for * @return a list of AssetDescriptors for the specified type + * @deprecated Change to use Composite instead */ + @Deprecated public List getAssetDescriptorTree(String type) { if (type.equals(GenericAsset.TYPE)) { return List.of(getAssetDescriptor(GenericAsset.TYPE)); @@ -128,6 +156,21 @@ public class AssetService { return List.of(getAssetDescriptor(GenericAsset.TYPE), getAssetDescriptor(type)); } + /** + * Retrieves a tree of asset descriptors for the specified composite. + * + * @param composite the composite to retrieve descriptors for + * @return a list of AssetDescriptors for the composite + */ + public List getAssetDescriptorTree(Composite composite) { + var tree = new ArrayList(); + tree.add(getAssetDescriptor(GenericAsset.TYPE)); + for (var asset : composite.getAssets()) { + tree.add(getAssetDescriptor(asset)); + } + return tree; + } + /** * Creates a new asset of the specified type with the provided form data. * @@ -138,12 +181,12 @@ public class AssetService { @Transactional public Asset createAsset(String type, Map formData) { var genericDescriptor = getAssetDescriptor(GenericAsset.TYPE); - var assetDescriptor = getAssetDescriptor(type); - var genericAsset = new GenericAsset(); genericAsset.setType(type); fillIn(genericAsset, genericDescriptor, formData); + var assetDescriptor = getAssetDescriptor(type); + var asset = assetDescriptor.newInstance(); fillIn(asset, assetDescriptor, formData); @@ -153,6 +196,35 @@ public class AssetService { return asset; } + /** + * Creates a composite asset with multiple asset types. + * + * @param type The list of asset types to include in the composite asset. + * @param formData The form data containing the properties for each asset type. + * @return The generic asset. + */ + @Transactional + public GenericAsset createCompositeAsset(List type, Map formData) { + if (type.isEmpty()) { + throw new IllegalArgumentException("At least one asset type must be provided."); + } + + var genericDescriptor = getAssetDescriptor(GenericAsset.TYPE); + var genericAsset = new GenericAsset(); + genericAsset.setType("composite"); + fillIn(genericAsset, genericDescriptor, formData); + + for (var assetType : type) { + var assetDescriptor = getAssetDescriptor(assetType); + var asset = assetDescriptor.newInstance(); + fillIn(asset, assetDescriptor, formData); + asset.setAsset(genericAsset); + getRepositoryFor(assetType).saveAndFlushAsset(asset); + } + + return genericRepository.saveAndFlush(genericAsset); + } + /** * Edits an existing asset with the provided form data. * diff --git a/src/main/resources/templates/create_asset.html b/src/main/resources/templates/create_asset.html index b5b4ba1..d8c7815 100644 --- a/src/main/resources/templates/create_asset.html +++ b/src/main/resources/templates/create_asset.html @@ -2,6 +2,7 @@

Create a

+