如何使组件项与其父项自动更改所有者?

时间:2018-08-02 12:59:19

标签: api hyperledger-fabric hyperledger hyperledger-composer

我正在学习Hyperledger作曲者,而且我不知道如何修改链码来使以下模型起作用:

这是我的首席技术官:

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{

}

和链码:

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);
    });

}

然后我输入两项:I1和I2-这将是第三项I3的组成部分 像这样:

    {
    "$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"
    ]
  }

然后,如果我进行交易,则I3更改所有者,但其组件仍为先前的所有者。当I3更改所有者时,如何使I1和I2自动更改所有者。有可能实现这一目标吗?感谢您的帮助或指导。

1 个答案:

答案 0 :(得分:0)

类似的事情(请注意,这是示例代码,可能会有更有效的方法来进行此操作,例如,汇总所有所有权变更所影响的“相关项目”,然后执行一个updateAll()调用以更新Item注册表-示例here)-还请注意,我使用了async / await(在Node 8中更易读)。

请注意我的交易模型是:

transaction Trade { 
  --> Item item
  --> Assembler newOwner
}

已更新:您还需要资产Item具有--> Assembler owner作为资源类(与之建立关系实例),而不是--> BU owner`作为抽象类。

在模型文件中(即,汇编器是参与者资源)

/**
 * 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);
    }

}