diff --git a/asset-transfer-basic/application-gateway-java/src/main/java/App.java b/asset-transfer-basic/application-gateway-java/src/main/java/App.java index df4044d4..64b501a4 100644 --- a/asset-transfer-basic/application-gateway-java/src/main/java/App.java +++ b/asset-transfer-basic/application-gateway-java/src/main/java/App.java @@ -30,8 +30,71 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.security.InvalidKeyException; import java.security.cert.CertificateException; -import java.time.Instant; import java.util.concurrent.TimeUnit; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; + +class Asset { + private String id; + private String owner; + private String prescripcionAnteriorId; + private String status; + private LocalDateTime statusChange; + private String prioridad; + private String medicacion; + private String razon; + private String notas; + private String periodoDeTratamiento; + private String instruccionesTratamiento; + private String periodoDeValidez; + private String dniPaciente; + private LocalDate fechaDeAutorizacion; + private int cantidad; + private LocalDate expectedSupplyDuration; + + public Asset(String id, String owner, String prescripcionAnteriorId, String status, LocalDateTime statusChange, + String prioridad, String medicacion, String razon, String notas, String periodoDeTratamiento, + String instruccionesTratamiento, String periodoDeValidez, String dniPaciente, + LocalDate fechaDeAutorizacion, int cantidad, LocalDate expectedSupplyDuration) { + this.id = id; + this.owner = owner; + this.prescripcionAnteriorId = prescripcionAnteriorId; + this.status = status; + this.statusChange = statusChange; + this.prioridad = prioridad; + this.medicacion = medicacion; + this.razon = razon; + this.notas = notas; + this.periodoDeTratamiento = periodoDeTratamiento; + this.instruccionesTratamiento = instruccionesTratamiento; + this.periodoDeValidez = periodoDeValidez; + this.dniPaciente = dniPaciente; + this.fechaDeAutorizacion = fechaDeAutorizacion; + this.cantidad = cantidad; + this.expectedSupplyDuration = expectedSupplyDuration; + } + + // Getters + public String getId() { return id; } + public String getOwner() { return owner; } + public String getPrescripcionAnteriorId() { return prescripcionAnteriorId; } + public String getStatus() { return status; } + public LocalDateTime getStatusChange() { return statusChange; } + public String getPrioridad() { return prioridad; } + public String getMedicacion() { return medicacion; } + public String getRazon() { return razon; } + public String getNotas() { return notas; } + public String getPeriodoDeTratamiento() { return periodoDeTratamiento; } + public String getInstruccionesTratamiento() { return instruccionesTratamiento; } + public String getPeriodoDeValidez() { return periodoDeValidez; } + public String getDniPaciente() { return dniPaciente; } + public LocalDate getFechaDeAutorizacion() { return fechaDeAutorizacion; } + public int getCantidad() { return cantidad; } + public LocalDate getExpectedSupplyDuration() { return expectedSupplyDuration; } +} + + public final class App { private static final String MSP_ID = System.getenv().getOrDefault("MSP_ID", "Org1MSP"); @@ -170,9 +233,46 @@ public final class App { * the ledger. */ private void createAsset() throws EndorseException, SubmitException, CommitStatusException, CommitException { - System.out.println("\n--> Submit Transaction: CreateAsset, creates new asset with ID, Color, Size, Owner and AppraisedValue arguments"); + System.out.println("\n--> Submit Transaction: CreateAsset, creates new asset with all arguments"); - contract.submitTransaction("CreateAsset", assetId, "yellow", "5", "Tom", "1300"); + Asset asset = new Asset( + assetId, + "Tom", + "presc456", + "active", + LocalDateTime.now(), + "high", + "Medicine XYZ", + "Condition ABC", + "Take with food", + "30 days", + "1 pill per day", + "1 year", + "12345678", + LocalDate.now(), + 30, + LocalDate.now() + ); + + contract.submitTransaction( + "CreateAsset", + asset.getId(), + asset.getOwner(), + asset.getPrescripcionAnteriorId(), + asset.getStatus(), + asset.getStatusChange().toString(), + asset.getPrioridad(), + asset.getMedicacion(), + asset.getRazon(), + asset.getNotas(), + asset.getPeriodoDeTratamiento(), + asset.getInstruccionesTratamiento(), + asset.getPeriodoDeValidez(), + asset.getDniPaciente(), + asset.getFechaDeAutorizacion().toString(), + Integer.toString(asset.getCantidad()), + asset.getExpectedSupplyDuration().toString() + ); System.out.println("*** Transaction committed successfully"); } @@ -222,7 +322,44 @@ public final class App { try { System.out.println("\n--> Submit Transaction: UpdateAsset asset70, asset70 does not exist and should return an error"); - contract.submitTransaction("UpdateAsset", "asset70", "blue", "5", "Tomoko", "300"); + Asset asset = new Asset( + "asset70", // Asset ID + "Tomoko", // Owner + "presc789", // Prescripcion Anterior Id + "inactive", // Status + LocalDateTime.now() , // Status Change + "medium", // Prioridad + "Medicine ABC", // Medicacion + "Condition XYZ", // Razon + "Take with water", // Notas + "60 days", // Periodo De Tratamiento + "2 pills per day", // Instrucciones Tratamiento + "60 days", // Periodo De Validez + "87654321", // Dni Paciente + LocalDate.now() , // Fecha De Autorizacion + 60, // Cantidad + LocalDate.now() // Expected Supply Duration + ); + + contract.submitTransaction( + "UpdateAsset", + "asset70", + asset.getOwner(), + asset.getPrescripcionAnteriorId(), + asset.getStatus(), + asset.getStatusChange().toString(), + asset.getPrioridad(), + asset.getMedicacion(), + asset.getRazon(), + asset.getNotas(), + asset.getPeriodoDeTratamiento(), + asset.getInstruccionesTratamiento(), + asset.getPeriodoDeValidez(), + asset.getDniPaciente(), + asset.getFechaDeAutorizacion().toString(), + Integer.toString(asset.getCantidad()), + asset.getExpectedSupplyDuration().toString() + ); System.out.println("******** FAILED to return an error"); } catch (EndorseException | SubmitException | CommitStatusException e) { diff --git a/asset-transfer-basic/application-gateway-java/src/main/java/models/Asset.Java b/asset-transfer-basic/application-gateway-java/src/main/java/models/Asset.Java new file mode 100644 index 00000000..6dcc25a7 --- /dev/null +++ b/asset-transfer-basic/application-gateway-java/src/main/java/models/Asset.Java @@ -0,0 +1,63 @@ +/* +package models; + + +class Asset { + private String id; + private String owner; + private String prescripcionAnteriorId; + private String status; + private String statusChange; + private String prioridad; + private String medicacion; + private String razon; + private String notas; + private String periodoDeTratamiento; + private String instruccionesTratamiento; + private String periodoDeValidez; + private String dniPaciente; + private String fechaDeAutorizacion; + private String cantidad; + private String expectedSupplyDuration; + + public Asset(String id, String owner, String prescripcionAnteriorId, String status, String statusChange, + String prioridad, String medicacion, String razon, String notas, String periodoDeTratamiento, + String instruccionesTratamiento, String periodoDeValidez, String dniPaciente, + String fechaDeAutorizacion, String cantidad, String expectedSupplyDuration) { + this.id = id; + this.owner = owner; + this.prescripcionAnteriorId = prescripcionAnteriorId; + this.status = status; + this.statusChange = statusChange; + this.prioridad = prioridad; + this.medicacion = medicacion; + this.razon = razon; + this.notas = notas; + this.periodoDeTratamiento = periodoDeTratamiento; + this.instruccionesTratamiento = instruccionesTratamiento; + this.periodoDeValidez = periodoDeValidez; + this.dniPaciente = dniPaciente; + this.fechaDeAutorizacion = fechaDeAutorizacion; + this.cantidad = cantidad; + this.expectedSupplyDuration = expectedSupplyDuration; + } + + // Getters + public String getId() { return id; } + public String getOwner() { return owner; } + public String getPrescripcionAnteriorId() { return prescripcionAnteriorId; } + public String getStatus() { return status; } + public String getStatusChange() { return statusChange; } + public String getPrioridad() { return prioridad; } + public String getMedicacion() { return medicacion; } + public String getRazon() { return razon; } + public String getNotas() { return notas; } + public String getPeriodoDeTratamiento() { return periodoDeTratamiento; } + public String getInstruccionesTratamiento() { return instruccionesTratamiento; } + public String getPeriodoDeValidez() { return periodoDeValidez; } + public String getDniPaciente() { return dniPaciente; } + public String getFechaDeAutorizacion() { return fechaDeAutorizacion; } + public String getCantidad() { return cantidad; } + public String getExpectedSupplyDuration() { return expectedSupplyDuration; } +} +*/ \ No newline at end of file diff --git a/asset-transfer-basic/chaincode-go/chaincode/smartcontract.go b/asset-transfer-basic/chaincode-go/chaincode/smartcontract.go index 4e2319d6..90491dce 100644 --- a/asset-transfer-basic/chaincode-go/chaincode/smartcontract.go +++ b/asset-transfer-basic/chaincode-go/chaincode/smartcontract.go @@ -3,10 +3,10 @@ package chaincode import ( "encoding/json" "fmt" - "time" "github.com/hyperledger/fabric-contract-api-go/v2/contractapi" ) + // SmartContract provides functions for managing an Asset type SmartContract struct { contractapi.Contract @@ -16,24 +16,25 @@ type SmartContract struct { // Insert struct field in alphabetic order => to achieve determinism across languages // golang keeps the order when marshal to json but doesn't order automatically type Asset struct { - ID string `json:"ID"` - Owner string `json:"Owner"` - PrescripcionAnteriorId string `json:"PrescripcionAnteriorId"` - Status string `json:"Status"` - StatusChange time.Time `json:"StatusChange"` - Prioridad string `json:"Prioridad"` - Medicacion string `json:"medicacion"` - Razon string `json:"Razon"` - Notas string `json:"Notas"` - PeriodoDeTratamiento string `json:"PeriodoDeTratamiento"` - InstruccionesTratamiento string `json:"PnstruccionesTratamiento"` - PeriodoDeValidez string `json:"PeriodoDeValidez"` - DniPaciente string `json:"DniPaciente"` - FechaDeAutorizacion time.Time `json:"FechaDeAutorizacion"` - Cantidad int `json:"Cantidad"` - ExpectedSupplyDuration time.Time `json:"ExpectedSupplyDuration"` + ID string `json:"ID"` + Owner string `json:"Owner"` + PrescripcionAnteriorId string `json:"PrescripcionAnteriorId"` + Status string `json:"Status"` + StatusChange string `json:"StatusChange"` // Changed to string + Prioridad string `json:"Prioridad"` + Medicacion string `json:"medicacion"` + Razon string `json:"Razon"` + Notas string `json:"Notas"` + PeriodoDeTratamiento string `json:"PeriodoDeTratamiento"` + InstruccionesTratamiento string `json:"InstruccionesTratamiento"` + PeriodoDeValidez string `json:"PeriodoDeValidez"` + DniPaciente string `json:"DniPaciente"` + FechaDeAutorizacion string `json:"FechaDeAutorizacion"` // Changed to string + Cantidad string `json:"Cantidad"` + ExpectedSupplyDuration string `json:"ExpectedSupplyDuration"` // Changed to string } +// InitLedger adds a base set of assets to the ledger // InitLedger adds a base set of assets to the ledger func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error { assets := []Asset{ @@ -42,7 +43,7 @@ func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) Owner: "Tomoko", PrescripcionAnteriorId: "presc123", Status: "active", - StatusChange: time.Date(2024, time.January, 15, 10, 0, 0, 0, time.UTC), + StatusChange: "2024-01-15T10:00:00Z", Prioridad: "high", Medicacion: "medication1", Razon: "reason1", @@ -51,16 +52,16 @@ func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) InstruccionesTratamiento: "take daily", PeriodoDeValidez: "1 year", DniPaciente: "12345678", - FechaDeAutorizacion: time.Date(2024, time.January, 1, 9, 0, 0, 0, time.UTC), - Cantidad: 5, - ExpectedSupplyDuration: time.Date(2024, time.February, 1, 9, 0, 0, 0, time.UTC), + FechaDeAutorizacion: "2024-01-01T09:00:00Z", + Cantidad: "5", + ExpectedSupplyDuration: "2024-02-01T09:00:00Z", }, { ID: "asset2", Owner: "Alice", PrescripcionAnteriorId: "presc456", Status: "completed", - StatusChange: time.Date(2024, time.February, 20, 11, 0, 0, 0, time.UTC), + StatusChange: "2024-02-20T11:00:00Z", Prioridad: "medium", Medicacion: "medication2", Razon: "reason2", @@ -69,9 +70,9 @@ func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) InstruccionesTratamiento: "take twice daily", PeriodoDeValidez: "2 years", DniPaciente: "87654321", - FechaDeAutorizacion: time.Date(2024, time.January, 10, 10, 0, 0, 0, time.UTC), - Cantidad: 10, - ExpectedSupplyDuration: time.Date(2024, time.April, 10, 10, 0, 0, 0, time.UTC), + FechaDeAutorizacion: "2024-01-10T10:00:00Z", + Cantidad: "10", + ExpectedSupplyDuration: "2024-04-10T10:00:00Z", }, } @@ -91,7 +92,7 @@ func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) } // CreateAsset issues a new asset to the world state with given details. -func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface, id string, owner string, prescripcionAnteriorId string, status string, statusChange time.Time, prioridad string, medicacion string, razon string, notas string, periodoDeTratamiento string, instruccionesTratamiento string, periodoDeValidez string, dniPaciente string, fechaDeAutorizacion time.Time, cantidad int, expectedSupplyDuration time.Time) error { +func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface, id string, owner string, prescripcionAnteriorId string, status string, statusChange string, prioridad string, medicacion string, razon string, notas string, periodoDeTratamiento string, instruccionesTratamiento string, periodoDeValidez string, dniPaciente string, fechaDeAutorizacion string, cantidad string, expectedSupplyDuration string) error { exists, err := s.AssetExists(ctx, id) if err != nil { return err @@ -126,6 +127,43 @@ func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface, return ctx.GetStub().PutState(id, assetJSON) } +// UpdateAsset updates an existing asset in the world state with provided parameters. +func (s *SmartContract) UpdateAsset(ctx contractapi.TransactionContextInterface, id string, owner string, prescripcionAnteriorId string, status string, statusChange string, prioridad string, medicacion string, razon string, notas string, periodoDeTratamiento string, instruccionesTratamiento string, periodoDeValidez string, dniPaciente string, fechaDeAutorizacion string, cantidad string, expectedSupplyDuration string) error { + exists, err := s.AssetExists(ctx, id) + if err != nil { + return err + } + if !exists { + return fmt.Errorf("the asset %s does not exist", id) + } + + // overwriting original asset with new asset + asset := Asset{ + ID: id, + Owner: owner, + PrescripcionAnteriorId: prescripcionAnteriorId, + Status: status, + StatusChange: statusChange, + Prioridad: prioridad, + Medicacion: medicacion, + Razon: razon, + Notas: notas, + PeriodoDeTratamiento: periodoDeTratamiento, + InstruccionesTratamiento: instruccionesTratamiento, + PeriodoDeValidez: periodoDeValidez, + DniPaciente: dniPaciente, + FechaDeAutorizacion: fechaDeAutorizacion, + Cantidad: cantidad, + ExpectedSupplyDuration: expectedSupplyDuration, + } + assetJSON, err := json.Marshal(asset) + if err != nil { + return err + } + + return ctx.GetStub().PutState(id, assetJSON) +} + // ReadAsset returns the asset stored in the world state with given id. func (s *SmartContract) ReadAsset(ctx contractapi.TransactionContextInterface, id string) (*Asset, error) { assetJSON, err := ctx.GetStub().GetState(id) @@ -145,43 +183,6 @@ func (s *SmartContract) ReadAsset(ctx contractapi.TransactionContextInterface, i return &asset, nil } -// UpdateAsset updates an existing asset in the world state with provided parameters. -func (s *SmartContract) UpdateAsset(ctx contractapi.TransactionContextInterface, id string, owner string, prescripcionAnteriorId string, status string, statusChange time.Time, prioridad string, medicacion string, razon string, notas string, periodoDeTratamiento string, instruccionesTratamiento string, periodoDeValidez string, dniPaciente string, fechaDeAutorizacion time.Time, cantidad int, expectedSupplyDuration time.Time) error { - exists, err := s.AssetExists(ctx, id) - if err != nil { - return err - } - if !exists { - return fmt.Errorf("the asset %s does not exist", id) - } - - // overwriting original asset with new asset - asset := Asset{ - ID: id, - Owner: owner, - PrescripcionAnteriorId: prescripcionAnteriorId, - Status: status, - StatusChange: statusChange, - Prioridad: prioridad, - Medicacion: medicacion, - Razon: razon, - Notas: notas, - PeriodoDeTratamiento: periodoDeTratamiento, - InstruccionesTratamiento: instruccionesTratamiento, - PeriodoDeValidez: periodoDeValidez, - DniPaciente: dniPaciente, - FechaDeAutorizacion: fechaDeAutorizacion, - Cantidad: cantidad, - ExpectedSupplyDuration: expectedSupplyDuration, - } - assetJSON, err := json.Marshal(asset) - if err != nil { - return err - } - - return ctx.GetStub().PutState(id, assetJSON) -} - // DeleteAsset deletes an given asset from the world state. func (s *SmartContract) DeleteAsset(ctx contractapi.TransactionContextInterface, id string) error { exists, err := s.AssetExists(ctx, id) diff --git a/asset-transfer-basic/chaincode-go/chaincode/smartcontract_test.go b/asset-transfer-basic/chaincode-go/chaincode/smartcontract_test.go index 801611f4..df3c323e 100644 --- a/asset-transfer-basic/chaincode-go/chaincode/smartcontract_test.go +++ b/asset-transfer-basic/chaincode-go/chaincode/smartcontract_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "testing" + "time" "github.com/hyperledger/fabric-chaincode-go/v2/shim" "github.com/hyperledger/fabric-contract-api-go/v2/contractapi" @@ -48,15 +49,15 @@ func TestCreateAsset(t *testing.T) { transactionContext.GetStubReturns(chaincodeStub) assetTransfer := chaincode.SmartContract{} - err := assetTransfer.CreateAsset(transactionContext, "", "", 0, "", 0) + err := assetTransfer.CreateAsset(transactionContext, "", "", "", "", time.Now(), "", "", "", "", "", "", "", "", time.Now(), 0, time.Now()) require.NoError(t, err) chaincodeStub.GetStateReturns([]byte{}, nil) - err = assetTransfer.CreateAsset(transactionContext, "asset1", "", 0, "", 0) + err = assetTransfer.CreateAsset(transactionContext, "asset1", "", "", "", time.Now(), "", "", "", "", "", "", "", "", time.Now(), 0, time.Now()) require.EqualError(t, err, "the asset asset1 already exists") chaincodeStub.GetStateReturns(nil, fmt.Errorf("unable to retrieve asset")) - err = assetTransfer.CreateAsset(transactionContext, "asset1", "", 0, "", 0) + err = assetTransfer.CreateAsset(transactionContext, "asset1", "", "", "", time.Now(), "", "", "", "", "", "", "", "", time.Now(), 0, time.Now()) require.EqualError(t, err, "failed to read from world state: unable to retrieve asset") } @@ -96,15 +97,15 @@ func TestUpdateAsset(t *testing.T) { chaincodeStub.GetStateReturns(bytes, nil) assetTransfer := chaincode.SmartContract{} - err = assetTransfer.UpdateAsset(transactionContext, "", "", 0, "", 0) + err = assetTransfer.UpdateAsset(transactionContext, "", "", "", "", time.Now(), "", "", "", "", "", "", "", "", time.Now(), 0, time.Now()) require.NoError(t, err) chaincodeStub.GetStateReturns(nil, nil) - err = assetTransfer.UpdateAsset(transactionContext, "asset1", "", 0, "", 0) + err = assetTransfer.UpdateAsset(transactionContext, "asset1", "", "", "", time.Now(), "", "", "", "", "", "", "", "", time.Now(), 0, time.Now()) require.EqualError(t, err, "the asset asset1 does not exist") chaincodeStub.GetStateReturns(nil, fmt.Errorf("unable to retrieve asset")) - err = assetTransfer.UpdateAsset(transactionContext, "asset1", "", 0, "", 0) + err = assetTransfer.UpdateAsset(transactionContext, "asset1", "", "", "", time.Now(), "", "", "", "", "", "", "", "", time.Now(), 0, time.Now()) require.EqualError(t, err, "failed to read from world state: unable to retrieve asset") }