代码之家  ›  专栏  ›  技术社区  ›  bryniek

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

  •  0
  • bryniek  · 技术社区  · 7 年前

    我正在学习hyperledger composer,我不知道如何修改链码以使以下模型工作:

    这是我的首席技术官:

    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更改所有者,但其组件仍具有以前的所有者。如何使I1和I2在I3更改所有者时自动更改所有者。有可能做到这一点吗?我将感谢任何帮助或指导。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Paul O'Mahony    7 年前

    类似这样的事情(注意,这是示例代码,可能有更有效的方法来实现它,例如,聚合所有受所有权更改影响的“相关项”,然后执行一个 updateAll() 调用以更新项注册表-示例 here )-还有我使用的注释 async / await (在节点8中更具可读性)。

    注意我的交易模式是:

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

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

    在模型文件中(ie Assembler是参与者资源)

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