Hyperledger Fabric Changing component item owner automatically with its parent

0 votes

This is my cto:

asset Item identified by itemId{
o String itemId
o String name
o String idgId
o String serialNumber
o String comment
--> BU owner
--> Item [] items optional
}
abstract participant BU identified by buId{
    o String buId
    o String name
    o String country
    o String city
}
participant Manufacturer extends BU{
}
participant Assembler extends BU{
}

And chaincode:

function tradeCommodity(trade) {
trade.item.owner = trade.newOwner;
return getAssetRegistry('org.dps.track.Item')
    .then(function (assetRegistry) {
        var tradeNotification = getFactory().newEvent('org.dps.track',       'TradeNotification'); 
        tradeNotification.item = trade.item;
        emit(tradeNotification);

        // persist the state of the commodity
        return assetRegistry.update(trade.item);
    });

}

then i make two items: I1 and I2 - which will be components of third item I3 like this:

    {
    "$class": "org.dps.track.Item",
    "itemId": "I1",
    "name": "c1",
    "idgId": "123",
    "serialNumber": "123",
    "comment": "component1",
    "owner": "resource:org.dps.track.Assembler#BU2"
  },
  {
    "$class": "org.dps.track.Item",
    "itemId": "I2",
    "name": "c2",
    "idgId": "456",
    "serialNumber": "456",
    "comment": "component2",
    "owner": "resource:org.dps.track.Assembler#BU2"
  },
  {
    "$class": "org.dps.track.Item",
    "itemId": "I3",
    "name": "complex",
    "idgId": "789",
    "serialNumber": "789",
    "comment": "item consists of items",
    "owner": "resource:org.dps.track.Assembler#BU2",
    "items": [
      "resource:org.dps.track.Item#I1",
      "resource:org.dps.track.Item#I2"
    ]
  }

Now, I want I1 and I2 to change owner automatically when I3 changes the owner. How can I do this?

Nov 16, 2018 in Blockchain by digger
• 26,740 points
817 views

1 answer to this question.

0 votes

You can try something like this:

/**
 * Sample transaction processor function.
 * @param {org.dps.track.Trade } trade The sample transaction instance.
 * @transaction
 */
async function tradeCommodity(trade) {

    const factory = getFactory();
    trade.item.owner = trade.newOwner;
    var list = [];
    if (trade.item.items && trade.item.items.length > 0) {
        trade.item.items.forEach((asset) => {
        list.push(asset);
        });
    }  


    const assetRegistry = await getAssetRegistry('org.dps.track.Item');


    // persist the state of the current ITEM
    await assetRegistry.update(trade.item);

    for (var i = 0; i < list.length; ++i) {

         let res = await assetRegistry.get(list[i].getIdentifier());
         res.owner = factory.newRelationship('org.dps.track', 'Assembler', trade.newOwner.getIdentifier());
         // persist the state of the ITEM with new owner as a relationship
         await assetRegistry.update(res);
    }

}
answered Nov 16, 2018 by Omkar
• 69,210 points

Related Questions In Blockchain

0 votes
1 answer
0 votes
1 answer

Not Able to register a user with Hyperledger-Fabric v1.1 preview

The error: "Certificate not found with AKI 'e729224e8b3f31784c8a93c5b8ef6f4c1c91d9e6e577c45c33163609fe40011' ...READ MORE

answered Jun 16, 2018 in Blockchain by charlie_brown
• 7,720 points
1,649 views
0 votes
1 answer
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,233 views
+1 vote
1 answer
0 votes
1 answer

Invalid Batch or signature in Savtooth

This will solve your problem import org.apache.commons.codec.binary.Hex; Transaction txn ...READ MORE

answered Aug 1, 2018 in Blockchain by digger
• 26,740 points
724 views
+1 vote
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP