Add support for duplicating assets
This commit is contained in:
@@ -226,6 +226,27 @@ public class WebController {
|
||||
return "create_asset";
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a view where the user can edit an existing asset.
|
||||
*
|
||||
* @param qr The QR code of the asset to edit.
|
||||
*/
|
||||
@GetMapping("/duplicate/{qr}")
|
||||
public String duplicate(Model model, @PathVariable long qr) {
|
||||
model.addAttribute(TIME, System.currentTimeMillis());
|
||||
var asset = assetService.getAssetByQr(qr);
|
||||
if (asset == null) {
|
||||
throw new RuntimeException("Asset not found");
|
||||
}
|
||||
String assetType = asset.getAsset().getType();
|
||||
model.addAttribute(ACTION, "duplicate");
|
||||
model.addAttribute(ASSET, asset);
|
||||
model.addAttribute(DESCRIPTORS, assetService.getAssetDescriptorTree(assetType));
|
||||
model.addAttribute(DESCRIPTOR, assetService.getAssetDescriptor(assetType));
|
||||
model.addAttribute(INPUT_LIST, assetService.getInputList(assetType));
|
||||
return "create_asset";
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually edits an asset based on the form data submitted.
|
||||
*
|
||||
|
||||
@@ -12,6 +12,8 @@ import be.seeseepuff.pcinv.repositories.GenericAssetRepository;
|
||||
import be.seeseepuff.pcinv.repositories.WorkLogRepository;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@@ -54,6 +56,30 @@ public class AssetService {
|
||||
return getRepositoryFor(genericAsset.getType()).findByAsset(genericAsset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves an asset by its QR code and duplicates the object.
|
||||
*
|
||||
* @param qr the QR code of the asset to retrieve
|
||||
* @return the Asset associated with the given QR code, or null if no duplicate is found
|
||||
*/
|
||||
@SneakyThrows
|
||||
public Asset getAssetDuplicateByQr(long qr) {
|
||||
var mapper = new ModelMapper();
|
||||
var originalGenericAsset = genericRepository.findByQr(qr);
|
||||
if (originalGenericAsset == null) {
|
||||
throw new IllegalArgumentException("No asset found with QR code: " + qr);
|
||||
}
|
||||
var originalAsset = getRepositoryFor(originalGenericAsset.getType()).findByAsset(originalGenericAsset);
|
||||
|
||||
var genericAsset = mapper.map(originalGenericAsset, GenericAsset.class);
|
||||
genericAsset.setId(0); // Reset ID to create a new instance
|
||||
genericAsset.setQr(0);
|
||||
var asset = mapper.map(originalAsset, originalAsset.getClass());
|
||||
asset.setAsset(genericAsset);
|
||||
|
||||
return asset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all assets of a specific type.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user