Srikar Reddy Karemma
Srikar Reddy Karemma

Reputation: 163

How to update the relationship values inside transaction processor functions?

I have a model file org.acme.interm.container.cto which looks something like this :

namespace org.acme.interm.container

asset Container identified by containerId {

  o String containerId regex=/[A-Z][A-Z][0-9][0-9][0-9]-[0-9][0-9]-[0-3][0-9]-[0-9][0-9]/
  o String containerNumber
  --> Truck truck  optional
}

asset Truck identified by truckId {
  o String truckId          
  o Ownership ownershipType default="LEASED"
  o Integer normalWeight range = [,100]
  o Integer fragileWeight range = [,50]          
}

enum Ownership {
  o LEASED
  o OWNED
}

transaction AssignTruck {
  o String containerId
  o String truckId
}

event TruckAssigned {
  o String containerId
  o String truckId
}

transaction LoadContainer {
  o String containerId
  --> Truck    truck
  o Integer fragileWeight
  o Integer normalWeight   
}

I am assigning the Truck relation to the container asset in the following transaction :

function AssignTruck(containerTruckData) {  
   console.log("ASSIGN TRUCK CALLED!");
   var containerRegistry={}
   return getAssetRegistry('org.acme.interm.container.Container').then(function(registry){
            containerRegistry = registry
            return containerRegistry.get(containerTruckData.containerId);
        }).then(function(container){
            console.log("Got Container",container);
            if(!container) throw new Error("Container : "+containerTruckData.containerId," Not Found!!!");
            var factory = getFactory();
            var relationship = factory.newRelationship('org.acme.interm.truck','Truck',containerTruckData.truckId);
            container.truck = relationship;

            return containerRegistry.update(container);
        }).then(function(){
            // Successful update
            var event = getFactory().newEvent('org.acme.interm.container', 'TruckAssigned');
            event.containerId = containerTruckData.containerId;
            event.truckId = containerTruckData.truckId;
            emit(event);
        }).catch(function(error){
            throw new Error(error);
        });

}

Now, how can i write the loadContainer transaction, where the existing values of normalWeight and fragileWeight properties of the container(for given containerId) should be added to the newly passed fragileWeight and normalWeight values?

Upvotes: 0

Views: 54

Answers (1)

Varun Agarwal
Varun Agarwal

Reputation: 1587

Firstly unless each container has a weight variable, it is impossible to track the individual weight. Even if you keep updating the weight in the trucks, you will know the collective weight of containers which is not enough. Especially if you want to unload containers. I would suggest this approach

asset Container identified by containerId {

  o String containerId regex=/[A-Z][A-Z][0-9][0-9][0-9]-[0-9][0-9]-[0-3][0-9]-[0-9][0-9]/
  o String containerNumber
  o Integer normalWeight range = [,100]
  o Integer fragileWeight range = [,50]    
  --> Truck truck  optional
}

asset Truck identified by truckId {
  o String truckId          
  o Ownership ownershipType default="LEASED"
  o Integer totalNormalWeight range default=0
  o Integer totalFragileWeight range default=0
  --> Container containersLoaded[] optional
}

So this will let you track weight per container and then add them to get the total in truck. The containersLoaded variable will let you keep track of containers loaded. An advantage of this is, you can accurately reduce the weight if a container is unloaded or split (which I assume will be part of the user flow)

function AssignTruck(containerTruckData) {
   var myContainer; // store data from the promise
   var myTruck;
   console.log("ASSIGN TRUCK CALLED!");
   var containerRegistry={}
   var truckRegistry;
   return getAssetRegistry('org.acme.interm.container.Container').then(function(registry){
            containerRegistry = registry
            return containerRegistry.get(containerTruckData.containerId);
        }).then(function(container){
            console.log("Got Container",container);
            myContainer = container;
            if(!container) throw new Error("Container : "+containerTruckData.containerId," Not Found!!!");
            var factory = getFactory();
            var relationship = factory.newRelationship('org.acme.interm.truck','Truck',containerTruckData.truckId);
            container.truck = relationship;
            return containerRegistry.update(container);
        }).then(function() {
          return getAssetRegistry('org.acme.interm.truck.Truck');
        }).then(function (truckRegistry) {
          truckRegistry = truckRegistry;
          return truckRegistry.get(containerTruckData.truckId);
        }).then(function (truck) {
          truck.totalNormalWeight +=  myContainer.normalWeight;
          truck.totalFragileWeight += myContainer.fragileWeight;
          if (!truck.containersLoaded) {
            truck.containersLoaded = [];
          } else {
            truck.containersLoaded.push(factory.newRelationship('org.acme.interm.container', 'Container', myContainer.containerId))
          };
          return truckRegistry.update(truck)
        }).then(function(){
            // Successful update
            var event = getFactory().newEvent('org.acme.interm.container', 'TruckAssigned');
            event.containerId = containerTruckData.containerId;
            event.truckId = containerTruckData.truckId;
            emit(event);
        }).catch(function(error){
            throw new Error(error);
        });
}

This should work for you. Now you can query the truck and get total weight of all containers by checking the totalNormalWeight and totalFragileWeight variables. You can also check the containersLoaded array to get the list of the containers. To get the individual weight of the a container, either you can query the container and get its weight or get it directly from the containersLoaded variable. When the containers need to be removed/ unloaded from the truck, you can get their individual weight and subtract from the total, and pop the relationship from the containersLoaded.

Upvotes: 1

Related Questions