fabric-samples/off_chain_data/application-java/app/src/main/java/AssetTransferBasic.java
Mark S. Lewis f8e7bfe803
Update Gateway samples for v1.1 release (#779)
- Updated build to use Go 1.18 since Go 1.16 is no longer supported.
- Use Java 11 in updated samples, and take advantage of new language features.

Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
2022-06-30 15:46:32 +01:00

51 lines
2 KiB
Java

/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
import com.google.gson.Gson;
import org.hyperledger.fabric.client.CommitException;
import org.hyperledger.fabric.client.CommitStatusException;
import org.hyperledger.fabric.client.Contract;
import org.hyperledger.fabric.client.EndorseException;
import org.hyperledger.fabric.client.SubmitException;
import java.nio.charset.StandardCharsets;
import java.util.List;
public final class AssetTransferBasic {
private static final Gson GSON = new Gson();
private final Contract contract;
public AssetTransferBasic(final Contract contract) {
this.contract = contract;
}
public void createAsset(final Asset asset) throws EndorseException, CommitException, SubmitException, CommitStatusException {
contract.submitTransaction(
"CreateAsset",
asset.getId(),
asset.getColor(),
Integer.toString(asset.getSize()),
asset.getOwner(),
Integer.toString(asset.getAppraisedValue())
);
}
public String transferAsset(final String id, final String newOwner) throws EndorseException, CommitException, SubmitException, CommitStatusException {
var resultBytes = contract.submitTransaction("TransferAsset", id, newOwner);
return new String(resultBytes, StandardCharsets.UTF_8);
}
public void deleteAsset(final String id) throws EndorseException, CommitException, SubmitException, CommitStatusException {
contract.submitTransaction("DeleteAsset", id);
}
public List<Asset> getAllAssets() throws EndorseException, CommitException, SubmitException, CommitStatusException {
var resultBytes = contract.submitTransaction("GetAllAssets");
var resultJson = new String(resultBytes, StandardCharsets.UTF_8);
var assets = GSON.fromJson(resultJson, Asset[].class);
return assets != null ? List.of(assets) : List.of();
}
}