4 Commits

Author SHA1 Message Date
ad1cb70cdc Update .gitea/workflows/deploy.yml
All checks were successful
Build / build (push) Successful in 1m35s
Deploy / build (push) Successful in 2m7s
2026-02-14 19:08:58 +01:00
ce01b7ad14 Update .gitea/workflows/deploy.yml
Some checks failed
Build / build (push) Has been cancelled
2026-02-14 19:08:32 +01:00
02c7b966eb Update .gitea/workflows/build.yml
Some checks failed
Build / build (push) Has been cancelled
2026-02-14 19:08:22 +01:00
a3d9e1ebb9 Update .gitea/workflows/deploy.yml
Some checks failed
Build / build (push) Has been cancelled
2026-02-14 19:08:14 +01:00
14 changed files with 68 additions and 323 deletions

View File

@@ -5,7 +5,7 @@ on:
- '*' - '*'
jobs: jobs:
build: build:
runs-on: standard-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4

View File

@@ -1,11 +1,12 @@
name: Deploy name: Deploy
on: on:
push: push:
tags: branches:
- 'v*' - master
jobs: jobs:
build: build:
runs-on: standard-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
@@ -18,10 +19,10 @@ jobs:
cache: 'gradle' cache: 'gradle'
- name: Build Jar - name: Build Jar
run: ./gradlew bootJar run: ./gradlew bootJar --no-daemon
- name: Build Container - name: Build Container
run: docker build --tag gitea.seeseepuff.be/seeseemelk/pcinv:${{github.ref_name}} . run: docker build --tag gitea.seeseepuff.be/seeseemelk/pcinv:latest .
- name: Login - name: Login
with: # Set the secret as an input with: # Set the secret as an input
@@ -29,4 +30,4 @@ jobs:
run: docker login gitea.seeseepuff.be -u seeseemelk -p ${{ secrets.PACKAGE_RW }} run: docker login gitea.seeseepuff.be -u seeseemelk -p ${{ secrets.PACKAGE_RW }}
- name: Push Container - name: Push Container
run: docker push gitea.seeseepuff.be/seeseemelk/pcinv:${{github.ref_name}} run: docker push gitea.seeseepuff.be/seeseemelk/pcinv:latest

View File

@@ -1,12 +1,9 @@
package be.seeseepuff.pcinv.controllers; package be.seeseepuff.pcinv.controllers;
import be.seeseepuff.pcinv.meta.AssetDescriptor;
import be.seeseepuff.pcinv.models.Asset; import be.seeseepuff.pcinv.models.Asset;
import be.seeseepuff.pcinv.models.GenericAsset;
import be.seeseepuff.pcinv.models.WorkLogEntry; import be.seeseepuff.pcinv.models.WorkLogEntry;
import be.seeseepuff.pcinv.services.AssetService; import be.seeseepuff.pcinv.services.AssetService;
import be.seeseepuff.pcinv.services.BuildService; import be.seeseepuff.pcinv.services.BuildService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -19,10 +16,8 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set;
/** /**
* Controller for handling web requests related to assets. * Controller for handling web requests related to assets.
@@ -39,8 +34,6 @@ public class WebController {
private static final String ASSETS = "assets"; private static final String ASSETS = "assets";
/// The name of the model attribute that holds the asset being viewed or edited. /// The name of the model attribute that holds the asset being viewed or edited.
private static final String ASSET = "asset"; 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. /// The name of the model attribute that holds a list of all properties of all descriptors.
private static final String PROPERTIES = "properties"; private static final String PROPERTIES = "properties";
/// The name of the model attribute that holds the action to be performed. /// The name of the model attribute that holds the action to be performed.
@@ -139,39 +132,6 @@ public class WebController {
return "build_view"; return "build_view";
} }
/**
* Shows a view where the user can create a specific type of composite asset.
*/
@GetMapping("/create_composite")
public String createCompositeType(Model model, HttpServletRequest request) {
var parameters = request.getParameterMap();
if (parameters == null || parameters.isEmpty()) {
model.addAttribute(TIME, System.currentTimeMillis());
model.addAttribute(DESCRIPTORS, assetService.getAssetDescriptors());
return "create_composite";
} else {
model.addAttribute(TIME, System.currentTimeMillis());
model.addAttribute(ACTION, "create");
var descriptors = new ArrayList<AssetDescriptor>();
var inputLists = new HashMap<String, Set<String>>();
descriptors.add(assetService.getAssetDescriptor(GenericAsset.TYPE));
for (String assetType : parameters.keySet()) {
descriptors.add(assetService.getAssetDescriptor(assetType));
inputLists.putAll(assetService.getInputList(assetType));
}
model.addAttribute(DESCRIPTORS, descriptors);
model.addAttribute(DESCRIPTOR, AssetDescriptor.builder()
.type("composite")
.displayName("Composite Asset")
.pluralName("Composite Assets")
.build());
model.addAttribute(INPUT_LIST, inputLists);
model.addAttribute(BUILDS, buildService.getAllBuilds());
model.addAttribute(TYPES, parameters.keySet().stream().toList());
return "create_asset";
}
}
/** /**
* Handles the creation of a new build. * Handles the creation of a new build.
*/ */
@@ -215,18 +175,18 @@ public class WebController {
model.addAttribute(TIME, System.currentTimeMillis()); model.addAttribute(TIME, System.currentTimeMillis());
model.addAttribute(ACTION, "view"); model.addAttribute(ACTION, "view");
var composite = assetService.getAssetByQr(qr); var asset = assetService.getAssetByQr(qr);
if (composite == null) { if (asset == null) {
return "redirect:/"; return "redirect:/";
} }
var workLogSizeStr = formData.getFirst(WORKLOG_SIZE); var workLogSizeStr = formData.getFirst(WORKLOG_SIZE);
if (workLogSizeStr != null) { if (workLogSizeStr != null) {
var workLogSize = Integer.parseInt(workLogSizeStr); var workLogSize = Integer.parseInt(workLogSizeStr);
if (composite.getAsset().getWorkLog().size() == workLogSize) { if (asset.getAsset().getWorkLog().size() == workLogSize) {
var comment = formData.getFirst("comment"); var comment = formData.getFirst("comment");
if (comment != null && !comment.isBlank()) { if (comment != null && !comment.isBlank()) {
assetService.addWorkLogEntry(composite, comment); assetService.addWorkLogEntry(asset, comment);
} }
} }
} }
@@ -269,7 +229,7 @@ public class WebController {
return "redirect:/"; return "redirect:/";
} }
model.addAttribute(ASSET, asset); model.addAttribute(ASSET, asset);
model.addAttribute(DESCRIPTORS, assetService.getAssetDescriptorTree(asset)); model.addAttribute(DESCRIPTORS, assetService.getAssetDescriptorTree(asset.getAsset().getType()));
model.addAttribute(DESCRIPTOR, assetService.getAssetDescriptor(asset.getAsset().getType())); model.addAttribute(DESCRIPTOR, assetService.getAssetDescriptor(asset.getAsset().getType()));
model.addAttribute(WORKLOG, asset.getAsset().getWorkLog().stream() model.addAttribute(WORKLOG, asset.getAsset().getWorkLog().stream()
.sorted(Comparator.comparing(WorkLogEntry::getDate).reversed()) .sorted(Comparator.comparing(WorkLogEntry::getDate).reversed())
@@ -308,35 +268,6 @@ public class WebController {
return "create_asset"; 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<String, String> formData) {
model.addAttribute(TIME, System.currentTimeMillis());
var formMap = new HashMap<String, String>();
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. * Shows a view where the user can edit an existing asset.
* *
@@ -352,7 +283,7 @@ public class WebController {
String assetType = asset.getAsset().getType(); String assetType = asset.getAsset().getType();
model.addAttribute(ACTION, "edit"); model.addAttribute(ACTION, "edit");
model.addAttribute(ASSET, asset); model.addAttribute(ASSET, asset);
model.addAttribute(DESCRIPTORS, assetService.getAssetDescriptorTree(asset)); model.addAttribute(DESCRIPTORS, assetService.getAssetDescriptorTree(assetType));
model.addAttribute(DESCRIPTOR, assetService.getAssetDescriptor(assetType)); model.addAttribute(DESCRIPTOR, assetService.getAssetDescriptor(assetType));
model.addAttribute(INPUT_LIST, assetService.getInputList(assetType)); model.addAttribute(INPUT_LIST, assetService.getInputList(assetType));
model.addAttribute(BUILDS, buildService.getAllBuilds()); model.addAttribute(BUILDS, buildService.getAllBuilds());
@@ -374,7 +305,7 @@ public class WebController {
String assetType = asset.getAsset().getType(); String assetType = asset.getAsset().getType();
model.addAttribute(ACTION, "duplicate"); model.addAttribute(ACTION, "duplicate");
model.addAttribute(ASSET, asset); model.addAttribute(ASSET, asset);
model.addAttribute(DESCRIPTORS, assetService.getAssetDescriptorTree(asset)); model.addAttribute(DESCRIPTORS, assetService.getAssetDescriptorTree(assetType));
model.addAttribute(DESCRIPTOR, assetService.getAssetDescriptor(assetType)); model.addAttribute(DESCRIPTOR, assetService.getAssetDescriptor(assetType));
model.addAttribute(INPUT_LIST, assetService.getInputList(assetType)); model.addAttribute(INPUT_LIST, assetService.getInputList(assetType));
return "create_asset"; return "create_asset";
@@ -397,4 +328,27 @@ public class WebController {
var asset = assetService.editAsset(qr, formMap); var asset = assetService.editAsset(qr, formMap);
return "redirect:/view/" + asset.getQr(); 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<String, String> formData) {
model.addAttribute(TIME, System.currentTimeMillis());
var formMap = new HashMap<String, String>();
formData.forEach((key, values) -> {
if (!values.isEmpty()) {
formMap.put(key, values.getFirst());
}
});
var asset = assetService.createAsset(type, formMap);
return "redirect:/view/" + asset.getQr();
}
} }

View File

@@ -16,19 +16,9 @@ import java.util.function.Supplier;
@Getter @Getter
@Builder @Builder
public class AssetDescriptor { 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... /// The type of property, e.g.: ram, asset, etc...
private final String type; private final String type;
/// The Java class of the type.
private final Class<?> assetClass;
/// The displayable name of the property, e.g.: "Random Access Memory" /// The displayable name of the property, e.g.: "Random Access Memory"
private final String displayName; private final String displayName;
@@ -56,7 +46,6 @@ public class AssetDescriptor {
Objects.requireNonNull(assetInfo, "Asset class must be annotated with @AssetInfo"); Objects.requireNonNull(assetInfo, "Asset class must be annotated with @AssetInfo");
var builder = AssetDescriptor.builder() var builder = AssetDescriptor.builder()
.type(assetInfo.type()) .type(assetInfo.type())
.assetClass(assetType)
.displayName(assetInfo.displayName()) .displayName(assetInfo.displayName())
.pluralName(assetInfo.pluralName()) .pluralName(assetInfo.pluralName())
.visible(assetInfo.isVisible()) .visible(assetInfo.isVisible())

View File

@@ -20,16 +20,8 @@ public class AssetDescriptors {
* @param assetType The type of the asset to load properties for. * @param assetType The type of the asset to load properties for.
*/ */
public void loadFrom(Class<?> assetType) { public void loadFrom(Class<?> assetType) {
add(AssetDescriptor.loadFrom(assetType)); var property = AssetDescriptor.loadFrom(assetType);
} assets.add(property);
/**
* Adds a new asset descriptor to the collection.
*
* @param assetDescriptor The asset descriptor to add.
*/
public void add(AssetDescriptor assetDescriptor) {
assets.add(assetDescriptor);
} }
/** /**

View File

@@ -70,11 +70,7 @@ public class AssetProperty {
.setter((obj, value) -> { .setter((obj, value) -> {
try { try {
property.setAccessible(true); property.setAccessible(true);
if (obj instanceof Asset asset) { property.set(obj, Converters.convert(value, property.getType()));
property.set(asset.getAsset(property.getDeclaringClass()), Converters.convert(value, property.getType()));
} else {
property.set(obj, Converters.convert(value, property.getType()));
}
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@@ -85,12 +81,7 @@ public class AssetProperty {
obj = asset.getAsset(); obj = asset.getAsset();
} }
property.setAccessible(true); property.setAccessible(true);
return property.get(obj);
if (obj instanceof Asset asset) {
return property.get(asset.getAsset(property.getDeclaringClass()));
} else {
return property.get(obj);
}
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@@ -9,24 +9,5 @@ public interface Asset {
return getAsset().getQr(); return getAsset().getQr();
} }
/**
* Returns the asset as a specific type.
* @param assetType The type of asset to return, e.g., CpuAsset.class.
* @return The asset cast to the specified type.
* @param <T> The type of asset to return, must extend Asset.
* @throws IllegalArgumentException if the requested assetType is not compatible with this asset.
*/
@SuppressWarnings("unchecked")
default <T> T getAsset(Class<T> assetType) {
if (assetType.equals(GenericAsset.class)) {
return (T) getAsset();
}
if (assetType.equals(this.getClass())) {
return (T) this;
}
throw new IllegalArgumentException("No asset of type " + assetType.getSimpleName() + " found in composite.");
}
void setAsset(GenericAsset asset); void setAsset(GenericAsset asset);
} }

View File

@@ -1,76 +0,0 @@
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<Asset> 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);
}
@Override
public <T> T getAsset(Class<T> assetType) {
if (assetType.equals(GenericAsset.class)) {
//noinspection unchecked
return (T) getAsset();
}
if (assetType.equals(Composite.class)) {
return assetType.cast(this);
}
for (Asset asset : assets) {
if (assetType.isInstance(asset)) {
return assetType.cast(asset);
}
}
throw new IllegalArgumentException("No asset of type " + assetType.getSimpleName() + " found in composite.");
}
/**
* 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.");
}
}

View File

@@ -30,24 +30,24 @@ public class CpuAsset implements Asset
@Description("The number of cores in the CPU.") @Description("The number of cores in the CPU.")
@Property("Cores") @Property("Cores")
private Integer cores; private int cores;
@Description("The number of threads in the CPU.") @Description("The number of threads in the CPU.")
@Property("Threads") @Property("Threads")
private Integer threads; private int threads;
@Description("The base clock speed of the CPU in MHz.") @Description("The base clock speed of the CPU in MHz.")
@Property("Base Clock Speed (MHz)") @Property("Base Clock Speed (MHz)")
private Integer baseClockSpeed; private int baseClockSpeed;
@Description("The boost clock speed of the CPU in MHz.") @Description("The boost clock speed of the CPU in MHz.")
@Property("Boost Clock Speed (MHz)") @Property("Boost Clock Speed (MHz)")
@HideInOverview @HideInOverview
private Integer boostClockSpeed; private int boostClockSpeed;
@Description("The thermal design power (TDP) of the CPU in watts.") @Description("The thermal design power (TDP) of the CPU in watts.")
@Property("Thermal Design Power (TDP) (W)") @Property("Thermal Design Power (TDP) (W)")
private Integer tdp; private int tdp;
@Description("The socket type of the CPU.") @Description("The socket type of the CPU.")
@Property("Socket Type") @Property("Socket Type")
@@ -63,5 +63,5 @@ public class CpuAsset implements Asset
@Description("The manufacturing process of the CPU in nanometers.") @Description("The manufacturing process of the CPU in nanometers.")
@Property("Manufacturing Process (nm)") @Property("Manufacturing Process (nm)")
@HideInOverview @HideInOverview
private Integer manufacturingProcess; private int manufacturingProcess;
} }

View File

@@ -28,7 +28,7 @@ public class PowerSupplyAsset implements Asset
@Description("The wattage rating of the power supply in watts.") @Description("The wattage rating of the power supply in watts.")
@Property("Wattage") @Property("Wattage")
private Integer wattage; private int wattage;
@Description("The efficiency rating of the power supply, e.g., 80 Plus Bronze, Silver, Gold, Platinum, Titanium.") @Description("The efficiency rating of the power supply, e.g., 80 Plus Bronze, Silver, Gold, Platinum, Titanium.")
@Property("Efficiency Rating") @Property("Efficiency Rating")
@@ -42,25 +42,25 @@ public class PowerSupplyAsset implements Asset
@Description("The number number of Molex connectors.") @Description("The number number of Molex connectors.")
@Property("4-pin Molex Connectors") @Property("4-pin Molex Connectors")
private Integer molexConnectors; private int molexConnectors;
@Description("The number of 4-pin floppy connectors.") @Description("The number of 4-pin floppy connectors.")
@Property("4-pin Floppy Connectors") @Property("4-pin Floppy Connectors")
private Integer floppyConnectors; private int floppyConnectors;
@Description("The number of SATA power connectors.") @Description("The number of SATA power connectors.")
@Property("15-pin SATA Connectors") @Property("15-pin SATA Connectors")
private Integer sataConnectors; private int sataConnectors;
@Description("The number of 6-pin PCIe connectors.") @Description("The number of 6-pin PCIe connectors.")
@Property("6-pin PCIe Connectors") @Property("6-pin PCIe Connectors")
private Integer pcie6Connectors; private int pcie6Connectors;
@Description("The number of 8-pin PCIe connectors.") @Description("The number of 8-pin PCIe connectors.")
@Property("8-pin PCIe Connectors") @Property("8-pin PCIe Connectors")
private Integer pcie8Connectors; private int pcie8Connectors;
@Description("The number of 4-pin ATX 12V connectors.") @Description("The number of 4-pin ATX 12V connectors.")
@Property("4-pin ATX 12V Connectors") @Property("4-pin ATX 12V Connectors")
private Integer atx12vConnectors; private int atx12vConnectors;
} }

View File

@@ -2,7 +2,6 @@ package be.seeseepuff.pcinv.services;
import be.seeseepuff.pcinv.meta.*; import be.seeseepuff.pcinv.meta.*;
import be.seeseepuff.pcinv.models.Asset; import be.seeseepuff.pcinv.models.Asset;
import be.seeseepuff.pcinv.models.Composite;
import be.seeseepuff.pcinv.models.GenericAsset; import be.seeseepuff.pcinv.models.GenericAsset;
import be.seeseepuff.pcinv.models.WorkLogEntry; import be.seeseepuff.pcinv.models.WorkLogEntry;
import be.seeseepuff.pcinv.repositories.AssetRepository; import be.seeseepuff.pcinv.repositories.AssetRepository;
@@ -47,23 +46,12 @@ public class AssetService {
* @return the Asset associated with the given QR code * @return the Asset associated with the given QR code
* @throws IllegalArgumentException if no asset is found with the given QR code * @throws IllegalArgumentException if no asset is found with the given QR code
*/ */
public Composite getAssetByQr(long qr) { public Asset getAssetByQr(long qr) {
var genericAsset = genericRepository.findByQr(qr); var genericAsset = genericRepository.findByQr(qr);
if (genericAsset == null) { if (genericAsset == null) {
throw new IllegalArgumentException("No asset found with QR code: " + qr); throw new IllegalArgumentException("No asset found with QR code: " + qr);
} }
if (genericAsset.getType().equals("composite")) { return getRepositoryFor(genericAsset.getType()).findByAsset(genericAsset);
var assets = new ArrayList<Asset>();
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));
}
} }
/** /**
@@ -121,34 +109,18 @@ public class AssetService {
* @return the AssetProperties for the specified type * @return the AssetProperties for the specified type
*/ */
public AssetDescriptor getAssetDescriptor(String type) { public AssetDescriptor getAssetDescriptor(String type) {
if (type.equals("composite")) {
return AssetDescriptor.COMPOSITE;
}
return getAssetDescriptors().getAssets().stream() return getAssetDescriptors().getAssets().stream()
.filter(asset -> asset.getType().equals(type)) .filter(asset -> asset.getType().equals(type))
.findFirst() .findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown asset type: " + type)); .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. * Retrieves a tree of asset descriptors for the specified type.
* *
* @param type the type of asset to retrieve descriptors for * @param type the type of asset to retrieve descriptors for
* @return a list of AssetDescriptors for the specified type * @return a list of AssetDescriptors for the specified type
* @deprecated Change to use Composite instead
*/ */
@Deprecated
public List<AssetDescriptor> getAssetDescriptorTree(String type) { public List<AssetDescriptor> getAssetDescriptorTree(String type) {
if (type.equals(GenericAsset.TYPE)) { if (type.equals(GenericAsset.TYPE)) {
return List.of(getAssetDescriptor(GenericAsset.TYPE)); return List.of(getAssetDescriptor(GenericAsset.TYPE));
@@ -156,21 +128,6 @@ public class AssetService {
return List.of(getAssetDescriptor(GenericAsset.TYPE), getAssetDescriptor(type)); 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 set of AssetDescriptors for the composite
*/
public Set<AssetDescriptor> getAssetDescriptorTree(Composite composite) {
var tree = new TreeSet<>(Comparator.comparing(AssetDescriptor::getDisplayName));
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. * Creates a new asset of the specified type with the provided form data.
* *
@@ -181,12 +138,12 @@ public class AssetService {
@Transactional @Transactional
public Asset createAsset(String type, Map<String, String> formData) { public Asset createAsset(String type, Map<String, String> formData) {
var genericDescriptor = getAssetDescriptor(GenericAsset.TYPE); var genericDescriptor = getAssetDescriptor(GenericAsset.TYPE);
var assetDescriptor = getAssetDescriptor(type);
var genericAsset = new GenericAsset(); var genericAsset = new GenericAsset();
genericAsset.setType(type); genericAsset.setType(type);
fillIn(genericAsset, genericDescriptor, formData); fillIn(genericAsset, genericDescriptor, formData);
var assetDescriptor = getAssetDescriptor(type);
var asset = assetDescriptor.newInstance(); var asset = assetDescriptor.newInstance();
fillIn(asset, assetDescriptor, formData); fillIn(asset, assetDescriptor, formData);
@@ -196,36 +153,6 @@ public class AssetService {
return asset; 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<String> type, Map<String, String> 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);
genericAsset = genericRepository.saveAndFlush(genericAsset);
for (var assetType : type) {
var assetDescriptor = getAssetDescriptor(assetType);
var asset = assetDescriptor.newInstance();
fillIn(asset, assetDescriptor, formData);
asset.setAsset(genericAsset);
getRepositoryFor(assetType).saveAndFlushAsset(asset);
}
return genericAsset;
}
/** /**
* Edits an existing asset with the provided form data. * Edits an existing asset with the provided form data.
* *
@@ -235,21 +162,20 @@ public class AssetService {
*/ */
@Transactional @Transactional
public Asset editAsset(long qr, Map<String, String> formData) { public Asset editAsset(long qr, Map<String, String> formData) {
var composite = getAssetByQr(qr); var genericAsset = genericRepository.findByQr(qr);
if (composite == null) { if (genericAsset == null) {
throw new IllegalArgumentException("No asset found with QR code: " + qr); throw new IllegalArgumentException("No asset found with QR code: " + qr);
} }
fillIn(composite.getGenericAsset(), getAssetDescriptor(GenericAsset.TYPE), formData); var assetType = genericAsset.getType();
genericRepository.saveAndFlush(composite.getGenericAsset()); var assetDescriptor = getAssetDescriptor(assetType);
var asset = getRepositoryFor(assetType).findByAsset(genericAsset);
for (var asset : composite.getAssets()) { fillIn(genericAsset, getAssetDescriptor(GenericAsset.TYPE), formData);
var assetDescriptor = getAssetDescriptor(asset); fillIn(asset, assetDescriptor, formData);
fillIn(asset, assetDescriptor, formData);
getRepositoryFor(assetDescriptor.getType()).saveAndFlushAsset(asset);
}
return getAssetByQr(qr); genericRepository.saveAndFlush(genericAsset);
return getRepositoryFor(assetType).saveAndFlushAsset(asset);
} }
/** /**

View File

@@ -2,7 +2,6 @@
<div th:fragment="content"> <div th:fragment="content">
<h2>Create a <span th:text="${descriptor.displayName}"></span></h2> <h2>Create a <span th:text="${descriptor.displayName}"></span></h2>
<form th:action="'/'+${(action == 'duplicate') ? 'create' : action}+'/'+${(asset != null && action != 'duplicate') ? asset.getQr() : descriptor.getType()}" method="post"> <form th:action="'/'+${(action == 'duplicate') ? 'create' : action}+'/'+${(asset != null && action != 'duplicate') ? asset.getQr() : descriptor.getType()}" method="post">
<input th:each="t : ${types}" type="hidden" name="type" th:value="${t}"/>
<div th:each="d : ${descriptors}"> <div th:each="d : ${descriptors}">
<h2 th:text="${d.displayName}"></h2> <h2 th:text="${d.displayName}"></h2>
<table border="1" cellpadding="4"> <table border="1" cellpadding="4">

View File

@@ -1,11 +0,0 @@
<body th:replace="~{fragments :: base(title='Select type to create', content=~{::content})}">
<div th:fragment="content">
<h2>Create a new composite device</h2>
<form method="get" action="/create_composite">
<div th:each="d : ${descriptors.getAssets()}" th:if="${d.visible}"><label><input type="checkbox" value="on" th:name="${d.type}"><span th:text="${d.displayName}"></span></label></div>
<p>
<input type="submit" value="Create Composite">
</p>
</form>
</div>
</body>

View File

@@ -2,7 +2,6 @@
<div th:fragment="content"> <div th:fragment="content">
<h2>Create a new device</h2> <h2>Create a new device</h2>
<ul> <ul>
<li><a href="/create_composite"><i>Composite</i></a></li>
<li th:each="d : ${descriptors.getAssets()}" th:if="${d.visible}"><a th:href="'/create/'+${d.getType()}" th:text="${d.displayName}"></a></li> <li th:each="d : ${descriptors.getAssets()}" th:if="${d.visible}"><a th:href="'/create/'+${d.getType()}" th:text="${d.displayName}"></a></li>
</ul> </ul>
</div> </div>