This commit is contained in:
@@ -2,6 +2,7 @@ package be.seeseepuff.pcinv.controllers;
|
||||
|
||||
import be.seeseepuff.pcinv.models.WorkLogEntry;
|
||||
import be.seeseepuff.pcinv.services.AssetService;
|
||||
import be.seeseepuff.pcinv.services.BuildService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -42,6 +43,7 @@ public class WebController {
|
||||
private static final String INPUT_LIST = "inputLists";
|
||||
/// The name of the model attribute that holds the current work log entries.
|
||||
private static final String WORKLOG = "worklog";
|
||||
private static final String BUILDS = "builds";
|
||||
|
||||
/// The name of the input field for the current size of the work log.
|
||||
private static final String WORKLOG_SIZE = "worklog_size";
|
||||
@@ -49,6 +51,7 @@ public class WebController {
|
||||
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("dd MMM yyyy 'at' HH:mm");
|
||||
|
||||
private final AssetService assetService;
|
||||
private final BuildService buildService;
|
||||
|
||||
/**
|
||||
* Handles the root URL and returns the index page with asset descriptors and asset count.
|
||||
@@ -202,6 +205,7 @@ public class WebController {
|
||||
model.addAttribute(DESCRIPTORS, assetService.getAssetDescriptorTree(type));
|
||||
model.addAttribute(DESCRIPTOR, assetService.getAssetDescriptor(type));
|
||||
model.addAttribute(INPUT_LIST, assetService.getInputList(type));
|
||||
model.addAttribute(BUILDS, buildService.getAllBuilds());
|
||||
return "create_asset";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
package be.seeseepuff.pcinv.meta;
|
||||
|
||||
import be.seeseepuff.pcinv.models.Asset;
|
||||
import be.seeseepuff.pcinv.models.AssetCondition;
|
||||
import be.seeseepuff.pcinv.models.GenericAsset;
|
||||
import be.seeseepuff.pcinv.models.ReadWrite;
|
||||
import be.seeseepuff.pcinv.models.*;
|
||||
import jakarta.annotation.Nonnull;
|
||||
import jakarta.annotation.Nullable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Singular;
|
||||
@@ -28,7 +24,7 @@ public class AssetProperty {
|
||||
/// The name of the property as it should be displayed, e.g., "Brand", "Model", etc.
|
||||
private final String displayName;
|
||||
/// The type of the property, which can be a string or an integer.
|
||||
private final Type type;
|
||||
private final PropertyType type;
|
||||
/// Whether the property is required for the asset.
|
||||
private final boolean required;
|
||||
/// A set of options for the property, used for enum types.
|
||||
@@ -49,31 +45,6 @@ public class AssetProperty {
|
||||
/// A description of the property, if any.
|
||||
private final String description;
|
||||
|
||||
/**
|
||||
* Enum representing the possible types of asset properties.
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public enum Type {
|
||||
STRING(false),
|
||||
INTEGER(false),
|
||||
BOOLEAN(false),
|
||||
CAPACITY(false),
|
||||
CONDITION(true),
|
||||
READWRITE(true),
|
||||
;
|
||||
/// Set to `true` if the type is an enum, `false` otherwise.
|
||||
public final boolean isEnum;
|
||||
|
||||
/**
|
||||
* Returns the name of the type, or "enum" if it is an enum type.
|
||||
*
|
||||
* @return The name of the type or "enum" if it is an enum.
|
||||
*/
|
||||
public String nameOrEnum() {
|
||||
return isEnum ? "enum" : name();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads an AssetProperty from a given field.
|
||||
*
|
||||
@@ -131,7 +102,7 @@ public class AssetProperty {
|
||||
builder.option(option);
|
||||
}
|
||||
}
|
||||
if (type == Type.CAPACITY) {
|
||||
if (type == PropertyType.CAPACITY) {
|
||||
var capacityAnnotation = property.getAnnotation(Capacity.class);
|
||||
builder.capacityAsSI(capacityAnnotation.si());
|
||||
builder.capacityAsIEC(capacityAnnotation.iec());
|
||||
@@ -146,19 +117,21 @@ public class AssetProperty {
|
||||
* @return The type of the property.
|
||||
* @throws IllegalArgumentException if the property type is unsupported.
|
||||
*/
|
||||
private static Type determineType(Field property) {
|
||||
private static PropertyType determineType(Field property) {
|
||||
if (property.getType() == String.class) {
|
||||
return Type.STRING;
|
||||
return PropertyType.STRING;
|
||||
} else if (property.isAnnotationPresent(Capacity.class)) {
|
||||
return Type.CAPACITY;
|
||||
return PropertyType.CAPACITY;
|
||||
} else if (property.getType() == Integer.class || property.getType() == int.class || property.getType() == Long.class || property.getType() == long.class) {
|
||||
return Type.INTEGER;
|
||||
return PropertyType.INTEGER;
|
||||
} else if (property.getType() == Boolean.class || property.getType() == boolean.class) {
|
||||
return Type.BOOLEAN;
|
||||
return PropertyType.BOOLEAN;
|
||||
} else if (property.getType() == AssetCondition.class) {
|
||||
return Type.CONDITION;
|
||||
return PropertyType.CONDITION;
|
||||
} else if (property.getType() == ReadWrite.class) {
|
||||
return Type.READWRITE;
|
||||
return PropertyType.READWRITE;
|
||||
} else if (property.getType() == Build.class) {
|
||||
return PropertyType.BUILD;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unsupported property type: " + property.getType());
|
||||
}
|
||||
@@ -204,11 +177,11 @@ public class AssetProperty {
|
||||
var value = getValue(asset);
|
||||
if (value == null) {
|
||||
return "?";
|
||||
} else if (type == Type.BOOLEAN) {
|
||||
} else if (type == PropertyType.BOOLEAN) {
|
||||
return (boolean) value ? "Yes" : "No";
|
||||
} else if (type == Type.INTEGER || type == Type.STRING) {
|
||||
} else if (type == PropertyType.INTEGER || type == PropertyType.STRING) {
|
||||
return value.toString();
|
||||
} else if (type == Type.CAPACITY) {
|
||||
} else if (type == PropertyType.CAPACITY) {
|
||||
return convertCapacity((Long) value).toString();
|
||||
} else if (type.isEnum) {
|
||||
if (value instanceof AssetEnum assetEnum) {
|
||||
@@ -224,7 +197,7 @@ public class AssetProperty {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (type != Type.CAPACITY) {
|
||||
if (type != PropertyType.CAPACITY) {
|
||||
throw new IllegalStateException("Property '" + name + "' is not a capacity type.");
|
||||
}
|
||||
return CapacityInfo.of(value, capacityAsIEC, capacityAsSI);
|
||||
|
||||
31
src/main/java/be/seeseepuff/pcinv/meta/PropertyType.java
Normal file
31
src/main/java/be/seeseepuff/pcinv/meta/PropertyType.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package be.seeseepuff.pcinv.meta;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
/**
|
||||
* Enum representing the possible types of asset properties.
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public enum PropertyType
|
||||
{
|
||||
STRING(false),
|
||||
INTEGER(false),
|
||||
BOOLEAN(false),
|
||||
CAPACITY(false),
|
||||
CONDITION(true),
|
||||
READWRITE(true),
|
||||
BUILD(false),
|
||||
;
|
||||
/// Set to `true` if the type is an enum, `false` otherwise.
|
||||
public final boolean isEnum;
|
||||
|
||||
/**
|
||||
* Returns the name of the type, or "enum" if it is an enum type.
|
||||
*
|
||||
* @return The name of the type or "enum" if it is an enum.
|
||||
*/
|
||||
public String nameOrEnum()
|
||||
{
|
||||
return isEnum ? "enum" : name();
|
||||
}
|
||||
}
|
||||
30
src/main/java/be/seeseepuff/pcinv/models/Build.java
Normal file
30
src/main/java/be/seeseepuff/pcinv/models/Build.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package be.seeseepuff.pcinv.models;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@Entity
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table(
|
||||
name = "builds",
|
||||
uniqueConstraints = @UniqueConstraint(columnNames = "name")
|
||||
)
|
||||
public class Build
|
||||
{
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
private List<GenericAsset> parts;
|
||||
}
|
||||
@@ -73,4 +73,9 @@ public class GenericAsset
|
||||
|
||||
@OneToMany(mappedBy = "asset", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
private List<WorkLogEntry> workLog;
|
||||
|
||||
@ManyToOne
|
||||
@Property("Part of build")
|
||||
@Description("Select which build this asset is placed in.")
|
||||
private Build build;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package be.seeseepuff.pcinv.repositories;
|
||||
|
||||
import be.seeseepuff.pcinv.models.Build;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface BuildRepository extends JpaRepository<Build, Long>
|
||||
{
|
||||
}
|
||||
@@ -1,13 +1,12 @@
|
||||
package be.seeseepuff.pcinv.services;
|
||||
|
||||
import be.seeseepuff.pcinv.meta.AssetDescriptor;
|
||||
import be.seeseepuff.pcinv.meta.AssetDescriptors;
|
||||
import be.seeseepuff.pcinv.meta.AssetInfo;
|
||||
import be.seeseepuff.pcinv.meta.AssetProperty;
|
||||
import be.seeseepuff.pcinv.meta.*;
|
||||
import be.seeseepuff.pcinv.models.Asset;
|
||||
import be.seeseepuff.pcinv.models.Build;
|
||||
import be.seeseepuff.pcinv.models.GenericAsset;
|
||||
import be.seeseepuff.pcinv.models.WorkLogEntry;
|
||||
import be.seeseepuff.pcinv.repositories.AssetRepository;
|
||||
import be.seeseepuff.pcinv.repositories.BuildRepository;
|
||||
import be.seeseepuff.pcinv.repositories.GenericAssetRepository;
|
||||
import be.seeseepuff.pcinv.repositories.WorkLogRepository;
|
||||
import jakarta.persistence.EntityManager;
|
||||
@@ -225,7 +224,7 @@ public class AssetService {
|
||||
* @return The parsed value as an Object.
|
||||
*/
|
||||
private Object parseValue(AssetDescriptor descriptor, AssetProperty property, Map<String, String> values) {
|
||||
if (property.getType() == AssetProperty.Type.CAPACITY) {
|
||||
if (property.getType() == PropertyType.CAPACITY) {
|
||||
var value = values.get(descriptor.asString(property) + "-value");
|
||||
var unit = values.get(descriptor.asString(property) + "-unit");
|
||||
if (value == null || value.isBlank() || unit == null || unit.isBlank()) {
|
||||
@@ -244,11 +243,11 @@ public class AssetService {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (property.getType() == AssetProperty.Type.INTEGER) {
|
||||
if (property.getType() == PropertyType.INTEGER) {
|
||||
return Integer.parseInt(stringValue);
|
||||
} else if (property.getType() == AssetProperty.Type.STRING) {
|
||||
} else if (property.getType() == PropertyType.STRING) {
|
||||
return stringValue;
|
||||
} else if (property.getType() == AssetProperty.Type.BOOLEAN) {
|
||||
} else if (property.getType() == PropertyType.BOOLEAN) {
|
||||
return switch (stringValue.toLowerCase()) {
|
||||
case "true" -> true;
|
||||
case "false" -> false;
|
||||
|
||||
45
src/main/java/be/seeseepuff/pcinv/services/BuildService.java
Normal file
45
src/main/java/be/seeseepuff/pcinv/services/BuildService.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package be.seeseepuff.pcinv.services;
|
||||
|
||||
import be.seeseepuff.pcinv.models.Build;
|
||||
import be.seeseepuff.pcinv.repositories.BuildRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A service that manages computer builds.
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class BuildService
|
||||
{
|
||||
private final BuildRepository buildRepository;
|
||||
public static final Build EMPTY = Build.builder()
|
||||
.id(0)
|
||||
.name("(None)")
|
||||
.description("A meta build used to indicate that the part is not being used by anything")
|
||||
.build();
|
||||
|
||||
/**
|
||||
* Gets a list of all computer builds, including meta builds.
|
||||
*
|
||||
* @return A list of all builds.
|
||||
*/
|
||||
public List<Build> getAllBuilds() {
|
||||
var build = new ArrayList<Build>();
|
||||
build.add(EMPTY);
|
||||
build.addAll(getAllRealBuilds());
|
||||
return build;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of all computer builds, excluding meta builds.
|
||||
*
|
||||
* @return A list of all builds.
|
||||
*/
|
||||
public List<Build> getAllRealBuilds() {
|
||||
return buildRepository.findAll();
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
<tr th:each="p : ${d.getProperties()}">
|
||||
<td bgcolor="#d3d3d3"><b><label th:text="${p.displayName}" th:for="${d.asString(p)}" th:title="${p.description}"></label></b></td>
|
||||
<td th:switch="${p.type.nameOrEnum()}">
|
||||
<!-- Property Type: String List -->
|
||||
<span th:case="STRING">
|
||||
<select th:if="${p.inputList}" th:id="${d.asString(p)+'-list'}" th:name="${d.asString(p)+'-list'}">
|
||||
<option value="__new__">New...</option>
|
||||
@@ -17,8 +18,9 @@
|
||||
<input type="text" th:id="${d.asString(p)}" th:name="${d.asString(p)}" th:value="${p.getValue(asset)}" th:placeholder="${p.displayName}" th:required="${p.required}"/>
|
||||
</span>
|
||||
<input th:case="STRING" type="text" th:id="${d.asString(p)}" th:name="${d.asString(p)}" th:value="${p.getValue(asset)}" th:placeholder="${p.displayName}" th:required="${p.required}"/>
|
||||
<!-- Property Type: Integer -->
|
||||
<input th:case="INTEGER" type="number" th:id="${d.asString(p)}" th:name="${d.asString(p)}" th:value="${(p.name == 'qr' && action == 'duplicate') ? null : p.getValue(asset)}" th:required="${p.required}"/>
|
||||
<!-- <input th:case="BOOLEAN" type="checkbox" th:id="${d.asString(p)}" th:name="${d.asString(p)}" th:value="true" th:checked="${asset != null ? p.getValue(asset) : p.defaultValue}"/>-->
|
||||
<!-- Property Type: Boolean -->
|
||||
<span th:case="BOOLEAN">
|
||||
<input th:name="${d.asString(p)}" th:id="${d.asString(p)}+'-null'" type="radio" value="null" th:checked="${asset == null || (p.getValue(asset) == null)}">
|
||||
<label th:for="${d.asString(p)}+'-null'">Not known</label>
|
||||
@@ -27,9 +29,11 @@
|
||||
<input th:name="${d.asString(p)}" th:id="${d.asString(p)}+'-false'" type="radio" value="false" th:checked="${asset != null && (p.getValue(asset) == false)}">
|
||||
<label th:for="${d.asString(p)}+'-false'">No</label>
|
||||
</span>
|
||||
<!-- Property Type: Enum -->
|
||||
<select th:case="enum" th:id="${d.asString(p)}" th:name="${d.asString(p)}">
|
||||
<option th:each="o : ${p.options}" th:value="${o.value}" th:text="${o.displayName}" th:selected="${asset != null ? (p.getValue(asset) == o.enumConstant) : o.defaultValue}">Good</option>
|
||||
</select>
|
||||
<!-- Property Type: Capacity -->
|
||||
<span th:case="CAPACITY">
|
||||
<input type="number" th:id="${d.asString(p)+'-value'}" th:name="${d.asString(p)+'-value'}" th:required="${p.required}" th:value="${p.asCapacity(asset)?.getCapacityInUnit() ?: ''}"/>
|
||||
<select th:id="${d.asString(p)}+'-unit'" th:name="${d.asString(p)}+'-unit'">
|
||||
@@ -44,6 +48,13 @@
|
||||
<option value="1099511627776" th:if="${p.capacityAsIEC}" th:selected="${p.asCapacity(asset)?.getIdealUnit()?.name() == 'TEBIBYTES'}">TiB</option>
|
||||
</select>
|
||||
</span>
|
||||
<!-- Property Type: Build-->
|
||||
<span th:case="BUILD">
|
||||
<select th:id="${d.asString(p)}" th:name="${d.asString(p)}">
|
||||
<option value="" th:selected="${p.getValue(asset) == null}">(Unknown)</option>
|
||||
<option th:each="b : ${builds}" th:value="${b.getId()}" th:selected="${p.getValue(asset) == b}" th:text="${b.getName()}">My PC Build</option>
|
||||
</select>
|
||||
</span>
|
||||
<b th:case="*">Bad input type for <span th:text="${d.type}+'-'+${p.type}"></span></b>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
<a href="/">Home</a>
|
||||
<a href="/browse">Browse</a>
|
||||
<a href="/create">Create</a>
|
||||
<a href="/builds">Builds</a>
|
||||
<hr>
|
||||
<div th:replace="${content}">
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user