This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 300
/
mozart.cto.js
238 lines (214 loc) · 7.37 KB
/
mozart.cto.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
/*eslint-disable no-unused-vars*/
/*eslint-disable no-undef*/
/**
*
* @param {com.biz.AnimalMovementDeparture} movementDeparture
* @transaction
*/
function onAnimalMovementDeparture(movementDeparture) {
console.log('onAnimalMovementDeparture');
if (movementDeparture.animal.movementStatus !== 'IN_FIELD') {
throw new Error('Animal is already IN_TRANSIT');
}
// set the movement status of the animal
movementDeparture.animal.movementStatus = 'IN_TRANSIT';
// save the animal
return getAssetRegistry('com.biz.Animal')
.then(function(ar) {
return ar.update(movementDeparture.animal);
})
.then(function() {
// add the animal to the incoming animals of the
// destination business
if (movementDeparture.to.incomingAnimals) {
movementDeparture.to.incomingAnimals.push(movementDeparture.animal);
} else {
movementDeparture.to.incomingAnimals = [movementDeparture.animal];
}
// save the business
return getAssetRegistry('com.biz.Business')
})
.then(function(br) {
return br.update(movementDeparture.to);
});
}
/**
*
* @param {com.biz.AnimalMovementArrival} movementArrival
* @transaction
*/
function onAnimalMovementArrival(movementArrival) {
console.log('onAnimalMovementArrival');
if (movementArrival.animal.movementStatus !== 'IN_TRANSIT') {
throw new Error('Animal is not IN_TRANSIT');
}
// set the movement status of the animal
movementArrival.animal.movementStatus = 'IN_FIELD';
// set the new owner of the animal
// to the owner of the 'to' business
movementArrival.animal.owner = movementArrival.to.owner;
// set the new location of the animal
movementArrival.animal.location = movementArrival.arrivalField;
// save the animal
return getAssetRegistry('com.biz.Animal')
.then(function(ar) {
return ar.update(movementArrival.animal);
})
.then(function() {
// remove the animal from the incoming animals
// of the 'to' business
if (!movementArrival.to.incomingAnimals) {
throw new Error('Incoming business should have incomingAnimals on AnimalMovementArrival.');
}
movementArrival.to.incomingAnimals = movementArrival.to.incomingAnimals
.filter(function(animal) {
return animal.animalId !== movementArrival.animal.animalId;
});
// save the business
return getAssetRegistry('com.biz.Business');
})
.then(function(br) {
return br.update(movementArrival.to);
});
}
/**
*
* @param {com.biz.SetupDemo} setupDemo
* @transaction
*/
function setupDemo(setupDemo) {
var factory = getFactory();
var NS = 'com.biz';
var farmers = [
factory.newResource(NS, 'Farmer', 'FARMER_1'),
factory.newResource(NS, 'Farmer', 'FARMER_2')
];
var businesses = [
factory.newResource(NS, 'Business', 'BUSINESS_1'),
factory.newResource(NS, 'Business', 'BUSINESS_2')
];
var fields = [
factory.newResource(NS, 'Field','FIELD_1'),
factory.newResource(NS, 'Field','FIELD_2'),
factory.newResource(NS, 'Field','FIELD_3'),
factory.newResource(NS, 'Field','FIELD_4')
];
var animals = [
factory.newResource(NS, 'Animal', 'ANIMAL_1'),
factory.newResource(NS, 'Animal', 'ANIMAL_2'),
factory.newResource(NS, 'Animal', 'ANIMAL_3'),
factory.newResource(NS, 'Animal', 'ANIMAL_4'),
factory.newResource(NS, 'Animal', 'ANIMAL_5'),
factory.newResource(NS, 'Animal', 'ANIMAL_6'),
factory.newResource(NS, 'Animal', 'ANIMAL_7'),
factory.newResource(NS, 'Animal', 'ANIMAL_8')
];
return getParticipantRegistry(NS + '.Regulator')
.then(function(regulatorRegistry) {
var regulator = factory.newResource(NS, 'Regulator', 'REGULATOR');
regulator.email = 'REGULATOR';
regulator.firstName = 'Ronnie';
regulator.lastName = 'Regulator';
return regulatorRegistry.addAll([regulator]);
})
.then(function() {
return getParticipantRegistry(NS + '.Farmer');
})
.then(function(farmerRegistry) {
farmers.forEach(function(farmer) {
var sbi = 'BUSINESS_' + farmer.getIdentifier().split('_')[1];
farmer.firstName = farmer.getIdentifier();
farmer.lastName = '';
farmer.address1 = 'Address1';
farmer.address2 = 'Address2';
farmer.county = 'County';
farmer.postcode = 'PO57C0D3';
farmer.business = factory.newResource(NS, 'Business', sbi);
});
return farmerRegistry.addAll(farmers);
})
.then(function() {
return getAssetRegistry(NS + '.Business');
})
.then(function(businessRegistry) {
businesses.forEach(function(business, index) {
var cph = 'FIELD_' + (index + 1);
var farmer = 'FARMER_' + (index + 1);
business.address1 = 'Address1';
business.address2 = 'Address2';
business.county = 'County';
business.postcode = 'PO57C0D3';
business.owner = factory.newRelationship(NS, 'Farmer', farmer);
});
return businessRegistry.addAll(businesses);
})
.then(function() {
return getAssetRegistry(NS + '.Field');
})
.then(function(fieldRegistry) {
fields.forEach(function(field, index) {
var business = 'BUSINESS_' + (((index + 1) % 2) + 1);
field.name = 'FIELD_' + (index + 1);
field.business = factory.newRelationship(NS, 'Business', business);
});
return fieldRegistry.addAll(fields);
})
.then(function() {
return getAssetRegistry(NS + '.Animal');
})
.then(function(animalRegistry) {
animals.forEach(function(animal, index) {
var field = 'FIELD_' + (((index + 1) % 2) + 1);
var farmer = 'FARMER_' + (((index + 1) % 2) + 1);
animal.species = 'SHEEP_GOAT';
animal.movementStatus = 'IN_FIELD';
animal.productionType = 'MEAT';
animal.location = factory.newRelationship(NS, 'Field', field);
animal.owner = factory.newRelationship(NS, 'Farmer', farmer);
});
return animalRegistry.addAll(animals);
});
}
/**
* Get the Animals, but do not resolve contained relationships
* @query
* @param {String} farmerId - the email of the farmer
* @returns {Animal[]} - the animals that belong to the farmer
*/
function findAnimalsByOwnerId(farmerId) {
return query('select a from Animal a where a.owner == :farmerId');
}
/**
* Get the Animals, but and selectively resolve relationships
* @query
* @param {String} farmerId - the email of the farmer
* @returns {Animal[]} - the animals that belong to the farmer
*/
function findAnimalsByOwnerIdWithDetails(farmerId) {
return query('select resolve(a, a.location, a.owner) from Animal a where a.owner == :farmerId');
}
/**
* Get the incoming animals for a farmer and do not resolve relationships
* @query
* @param {String} farmerId - the email of the farmer
* @returns {Animal[]} - the animals that belong to the farmer
*/
function findIncomingAnimalsByFarmerId(farmerId) {
return query('select b.incomingAnimals from Business b where b.owner == :farmerId');
}
/*eslint-enable no-unused-vars*/
/*eslint-enable no-undef*/