Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.00 KB | None | 0 0
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14.  
  15. /* global getAssetRegistry getFactory emit */
  16.  
  17. /**
  18. * Sample transaction processor function.
  19. * @param {org.doptrace.setupDemo} setupDemo The sample transaction instance.
  20. * @transaction
  21. */
  22.  
  23. function setupDemo(setupDemo) {
  24.  
  25. var factory = getFactory();
  26. var NS = 'org.doptrace';
  27.  
  28. /**
  29. * VALUE CHAIN OPERATORS
  30. */
  31.  
  32. // criar Producer
  33. var producer = factory.newResource(NS, 'Producer', 'PROD_001');
  34. producer.phoneNumber = "9444444444";
  35. producer.email = "producer@email.com";
  36. producer.street = "Rua do prod";
  37. producer.zip = "4900-200";
  38. producer.city = "Viana do Castelo";
  39. producer.country = "Portugal";
  40. producer.name = "Quinta Vianense";
  41. producer.description = 'Produtores de leite';
  42.  
  43. // criar Industry_Retailer
  44. var industry_retailer = factory.newResource(NS, 'Industry_Retailer', 'IR_001');
  45. industry_retailer.phoneNumber = "955555555";
  46. industry_retailer.email = "industry_retailer@email.com";
  47. industry_retailer.street = "Rua da industra";
  48. industry_retailer.zip = "4900-210";
  49. industry_retailer.city = "Viana do Castelo";
  50. industry_retailer.country = "Portugal";
  51.  
  52. industry_retailer.name = "Milky Way";
  53. industry_retailer.description = 'Industria e retalhe de produtos lacticinios';
  54.  
  55.  
  56. // criar CertificationEntity
  57. var certificationEntity = factory.newResource(NS, 'CertificationEntity', 'CE_001');
  58. certificationEntity.phoneNumber = "966666666";
  59. certificationEntity.email = "certificationEntity@email.com";
  60. certificationEntity.street = "Rua da entidade";
  61. certificationEntity.zip = "4900-212";
  62. certificationEntity.city = "Viana do Castelo";
  63. certificationEntity.country = "Portugal";
  64. certificationEntity.name = "CE";
  65. certificationEntity.description = 'Entidade de certificação de produtos DOP';
  66.  
  67.  
  68. // criar Logistics
  69. var logistics = factory.newResource(NS, 'Logistics', 'L_001');
  70.  
  71. logistics.phoneNumber = "955555555";
  72. logistics.email = "logistics@email.com";
  73.  
  74. logistics.street = "Rua do transporte";
  75. logistics.zip = "4900-100";
  76. logistics.city = "Viana do Castelo";
  77. logistics.country = "Portugal";
  78.  
  79. logistics.name = "Logistica AltoMinho";
  80. logistics.description = 'Companhia de distribuição';
  81.  
  82.  
  83. /**
  84. * PARTICIPANTS
  85. */
  86.  
  87. // criar SysAdmin (luisalves é username [sysadmin id])
  88. var sysAdmin = factory.newResource(NS, 'SysAdmin', 'luisalves');
  89. sysAdmin.phoneNumber = "911111111";
  90. sysAdmin.email = "admin@email.com";
  91. sysAdmin.firstname = "Luis";
  92. sysAdmin.lastname = "Alves";
  93. sysAdmin.password = "123";
  94.  
  95. // criar WorkerAdmin
  96. var workerAdmin = factory.newResource(NS, 'WorkerAdmin', 'tiagocarvalhido');
  97.  
  98. workerAdmin.phoneNumber = "922222222";
  99. workerAdmin.email = "workeradmin@email.com";
  100.  
  101. workerAdmin.firstname = "Tiago";
  102. workerAdmin.lastname = "Carvalhido";
  103. workerAdmin.password = "123"
  104. workerAdmin.operator = factory.newRelationship(NS, 'Producer', 'PROD_001');
  105.  
  106. // criar Worker
  107. var worker = factory.newResource(NS, 'Worker', 'joselima');
  108.  
  109. worker.phoneNumber = "933333333";
  110. worker.email = "worker@email.com";
  111.  
  112. worker.firstname = "Jose";
  113. worker.lastname = "Lima";
  114. worker.password = "123"
  115. worker.operator = factory.newRelationship(NS, 'Producer', 'PROD_001');
  116.  
  117. /**
  118. * EVENTS
  119. */
  120.  
  121. /**
  122. * BATCH
  123. */
  124.  
  125.  
  126. // assets
  127. return getAssetRegistry(NS + '.Producer')
  128. .then(function(producerRegistry) {
  129. return producerRegistry.addAll([producer]);
  130. })
  131. .then(function() {
  132. return getAssetRegistry(NS + '.Industry_Retailer');
  133. })
  134. .then(function(industry_retailerRegistry) {
  135. return industry_retailerRegistry.addAll([industry_retailer]);
  136. })
  137. .then(function() {
  138. return getAssetRegistry(NS + '.CertificationEntity');
  139. })
  140. .then(function(certificationEntityRegistry) {
  141. return certificationEntityRegistry.addAll([certificationEntity]);
  142. })
  143. .then(function() {
  144. return getAssetRegistry(NS + '.Logistics');
  145. })
  146. .then(function(logisticsRegistry) {
  147. return logisticsRegistry.addAll([logistics]);
  148. })
  149. // participants
  150. .then(function() {
  151. return getParticipantRegistry(NS + '.SysAdmin');
  152. })
  153. .then(function(sysAdminRegistry) {
  154. return sysAdminRegistry.addAll([sysAdmin]);
  155. })
  156. .then(function() {
  157. return getParticipantRegistry(NS + '.WorkerAdmin');
  158. })
  159. .then(function(workerAdminRegistry) {
  160. return workerAdminRegistry.addAll([workerAdmin]);
  161. })
  162. .then(function() {
  163. return getParticipantRegistry(NS + '.Worker');
  164. })
  165. .then(function(workerRegistry) {
  166. return workerRegistry.addAll([worker]);
  167. });
  168.  
  169. }
  170.  
  171. /**
  172. * Sample transaction processor function.
  173. * @param {org.doptrace.A_producerToLogistics} prodToLog The sample transaction instance.
  174. * @transaction
  175. */
  176.  
  177. async function prodToLog(prodToLog) {
  178. return getAssetRegistry('org.doptrace.Logistics').then(function(playerRegistry) {
  179. return playerRegistry.exists(prodToLog.newOwner.getIdentifier())
  180. }).then(function(exists) {
  181. return getAssetRegistry('org.doptrace.Producer')
  182. .then(function(playerRegistryHarvest) {
  183. return playerRegistryHarvest.exists(prodToLog.issuer.getIdentifier())
  184. }).then(function(existsHarvest) {
  185. if (!exists && existsHarvest) {
  186. throw Error('Companhia Logistica não registada !')
  187. } else if (!existsHarvest && exists) {
  188. throw Error('Produtor não Registado !')
  189.  
  190. } else if (!existsHarvest && !exists) {
  191. throw Error('Erro na trasação!')
  192.  
  193. } else if (exists && existsHarvest) {
  194. getAssetRegistry('org.doptrace.Batch')
  195. .then(function(assetRegistry) {
  196. prodToLog.batch.previousOperator = prodToLog.issuer
  197. prodToLog.batch.state = 'IN_TRANSIT'
  198. prodToLog.batch.currentOperator = prodToLog.newOwner
  199. // prodToLog.batch.previousEvents.push(prodToLog.)
  200. assetRegistry.update(prodToLog.batch)
  201.  
  202. ;
  203. });;
  204. }
  205. })
  206. })
  207. }
  208.  
  209. /**
  210. * Sample transaction processor function.
  211. * @param {org.doptrace.B_producerToIndustry} prodToIndustry The sample transaction instance.
  212. * @transaction
  213. */
  214. async function prodToIndustry(prodToIndustry) {
  215. return getAssetRegistry('org.doptrace.Industry_Retailer').then(function(playerRegistry) {
  216. return playerRegistry.exists(prodToIndustry.newOwner.getIdentifier())
  217. }).then(function(exists) {
  218. return getAssetRegistry('org.doptrace.Producer')
  219. .then(function(playerRegistryHarvest) {
  220. return playerRegistryHarvest.exists(prodToIndustry.issuer.getIdentifier())
  221. }).then(function(existsHarvest) {
  222. return getAssetRegistry('org.doptrace.Batch')
  223. .then(function(playerRegistryBatch) {
  224. return playerRegistryBatch.exists(prodToIndustry.batch.getIdentifier())
  225. }).then(function(existsBatch) {
  226. if (!exists) {
  227. throw Error('Companhia Logistica não registada !')
  228. } else if (!existsHarvest) {
  229. throw Error('Produtor não Registado !')
  230. } else if (!existsBatch) {
  231. throw Error('Lote não registado!')
  232.  
  233. } else if (exists && existsHarvest && existsBatch) {
  234. getAssetRegistry('org.doptrace.Batch')
  235. .then(function(assetRegistry) {
  236.  
  237. prodToIndustry.batch.issuer = prodToIndustry.issuer
  238. prodToIndustry.batch.state = 'IN_TRANSIT'
  239. prodToLog.batch.owner = prodToLog.newOwner
  240. prodToLog.batch.previousOwners.push(prodToLog.issuer)
  241.  
  242. assetRegistry.update(prodToLog.batch)
  243.  
  244.  
  245.  
  246. ;
  247. });;
  248. }
  249. })
  250. })
  251. })
  252. }
  253.  
  254. /**
  255. * Sample transaction processor function.
  256. * @param {org.doptrace.C_logisticsToIndustry} logisticsToIndustry The sample transaction instance.
  257. * @transaction
  258. */
  259.  
  260. async function logisticsToIndustry(logisticsToIndustry) {
  261. return getAssetRegistry('org.doptrace.Industry_Retailer').then(function(playerRegistry) {
  262. return playerRegistry.exists(logisticsToIndustry.newOwner.getIdentifier())
  263. }).then(function(exists) {
  264. return getAssetRegistry('org.doptrace.Logistics')
  265. .then(function(playerRegistryHarvest) {
  266. return playerRegistryHarvest.exists(logisticsToIndustry.issuer.getIdentifier())
  267. }).then(function(existsHarvest) {
  268. return getAssetRegistry('org.doptrace.Batch')
  269. .then(function(playerRegistryBatch) {
  270. return playerRegistryBatch.exists(logisticsToIndustry.batch.getIdentifier())
  271. }).then(function(existsBatch) {
  272. if (!exists) {
  273. throw Error('Industria não registada !')
  274. } else if (!existsHarvest) {
  275. throw Error('Companhia Logistica não Registada !')
  276. } else if (!existsBatch) {
  277. throw Error('Lote não registado!')
  278.  
  279. } else if (exists && existsHarvest && existsBatch) {
  280.  
  281. getAssetRegistry('org.doptrace.Batch')
  282. .then(function(assetRegistry) {
  283.  
  284. logisticsToIndustry.batch.issuer = logisticsToIndustry.issuer
  285. logisticsToIndustry.batch.state = 'IN_TRANSIT'
  286. logisticsToIndustry.batch.owner = logisticsToIndustry.newOwner
  287. logisticsToIndustry.batch.previousOwners.push(logisticsToIndustry.issuer)
  288.  
  289. assetRegistry.update(logisticsToIndustry.batch)
  290.  
  291.  
  292.  
  293. ;
  294. });;
  295. }
  296. })
  297. })
  298. })
  299. }
  300.  
  301.  
  302.  
  303. /**
  304. * Sample transaction processor function.
  305. * @param {org.doptrace.batchRegistration} batchRegistration The sample transaction instance.
  306. * @transaction
  307.  
  308.  
  309. async function batchRegistration(batchRegistration) {
  310.  
  311. return getAssetRegistry('org.doptrace.Industry_Retailer')
  312. .then(function(playerRegistry) {
  313. return playerRegistry.exists(batchRegistration.retailer.getIdentifier())
  314. }).then(async function(exists) {
  315. return getAssetRegistry('org.doptrace.Batch')
  316. .then(function(playerRegistryBatch) {
  317. return playerRegistryBatch.exists(batchRegistration.batch.getIdentifier())
  318. }).then(async function(existsHarvest) {
  319. if (!exists && existsHarvest) {
  320.  
  321. throw Error('Industria não registada !')
  322.  
  323. } else if (exists && !existsHarvest) {
  324.  
  325. throw Error('Lote não registado !')
  326.  
  327. } else if (!exists && !existsHarvest) {
  328.  
  329. throw Error('ERRO NA TRANSAÇÃO!')
  330.  
  331. } else if (exists && existsHarvest) {
  332.  
  333.  
  334. let result = await getAssetRegistry('org.doptrace.Batch')
  335.  
  336. let batchAsset = batchRegistration.batch
  337.  
  338. let batchId = batchAsset.id
  339.  
  340. let numberOfBatch = batchRegistration.numberOfBatchs;
  341.  
  342. let i;
  343.  
  344. for (i = 1; i <= numberOfBatch; i++) {
  345.  
  346. var factory = getFactory();
  347.  
  348. var random = Math.floor(Math.random() * (1000 - 100) + 100) / 100;
  349.  
  350. let batch = factory.newResource('org.doptrace', 'Batch', batchRegistration.batch.id + 'SubBatch' + random);
  351.  
  352. batch.unit = batchRegistration.newUnit
  353. batch.state = 'REGISTERED'
  354. batch.amount = batchRegistration.newAmount
  355. batch.previousOwners = batchRegistration.batch.previousOwners
  356. batch.issuer = batchRegistration.retailer
  357. batch.owner = batchRegistration.retailer
  358. batchRegistration.batch.previousOwners.push(batchRegistration.batch.issuer)
  359.  
  360. await result.addAll([batch]);
  361.  
  362.  
  363.  
  364. }
  365. await result.update(batchRegistration.batch)
  366. };
  367. });
  368.  
  369. })
  370. }
  371. */
  372.  
  373.  
  374. /**
  375. * Sample transaction processor function.
  376. * @param {org.doptrace.D_industryToEntity} industryToEntity The sample transaction instance.
  377. * @transaction
  378. */
  379.  
  380. async function industryToEntity(industryToEntity) {
  381. return getAssetRegistry('org.doptrace.CertificationEntity').then(function(playerRegistry) {
  382. return playerRegistry.exists(industryToEntity.newOwner.getIdentifier())
  383. }).then(function(exists) {
  384. return getAssetRegistry('org.doptrace.Industry_Retailer')
  385. .then(function(playerRegistryHarvest) {
  386. return playerRegistryHarvest.exists(industryToEntity.issuer.getIdentifier())
  387. }).then(function(existsHarvest) {
  388. return getAssetRegistry('org.doptrace.Batch')
  389. .then(function(playerRegistryBatch) {
  390. return playerRegistryBatch.exists(industryToEntity.batch.getIdentifier())
  391. }).then(function(existsBatch) {
  392. if (!exists) {
  393. throw Error('Entidade de certificação não registada !')
  394. } else if (!existsHarvest) {
  395. throw Error('Companhia Logistica não Registada !')
  396. } else if (!existsBatch) {
  397. throw Error('Lote não registado!')
  398.  
  399. } else if (exists && existsHarvest && existsBatch) {
  400. getAssetRegistry('org.doptrace.Batch')
  401. .then(function(assetRegistry) {
  402.  
  403. industryToEntity.batch.issuer = industryToEntity.issuer
  404. industryToEntity.batch.state = 'IN_TRANSIT'
  405. industryToEntity.batch.owner = industryToEntity.newOwner
  406. industryToEntity.batch.previousOwners.push(industryToEntity.issuer)
  407.  
  408. assetRegistry.update(industryToEntity.batch)
  409.  
  410. ;
  411. });;
  412. }
  413. })
  414. })
  415. })
  416. }
  417. /**
  418. * Sample transaction processor function.
  419. * @param {org.doptrace.F_EntityToIndustry} entityToIndustry The sample transaction instance.
  420. * @transaction
  421. */
  422.  
  423. async function entityToIndustry(entityToIndustry) {
  424. return getAssetRegistry('org.doptrace.Industry_Retailer').then(function(playerRegistry) {
  425. return playerRegistry.exists(entityToIndustry.newOwner.getIdentifier())
  426. }).then(function(exists) {
  427. return getAssetRegistry('org.doptrace.CertificationEntity')
  428. .then(function(playerRegistryHarvest) {
  429. return playerRegistryHarvest.exists(entityToIndustry.issuer.getIdentifier())
  430. }).then(function(existsHarvest) {
  431. return getAssetRegistry('org.doptrace.Batch')
  432. .then(function(playerRegistryBatch) {
  433. return playerRegistryBatch.exists(entityToIndustry.batch.getIdentifier())
  434. }).then(function(existsBatch) {
  435. if (!exists) {
  436. throw Error('Industria não registada !')
  437. } else if (!existsHarvest) {
  438. throw Error('Companhia Logistica não Registada !')
  439. } else if (!existsBatch) {
  440. throw Error('Lote não registado!')
  441.  
  442. } else if (exists && existsHarvest && existsBatch) {
  443. getAssetRegistry('org.doptrace.Batch')
  444. .then(function(assetRegistry) {
  445.  
  446. entityToIndustry.batch.issuer = entityToIndustry.issuer
  447. entityToIndustry.batch.state = 'IN_TRANSIT'
  448. entityToIndustry.batch.owner = entityToIndustry.newOwner
  449. entityToIndustry.batch.previousOwners.push(entityToIndustry.issuer)
  450.  
  451. assetRegistry.update(entityToIndustry.batch)
  452.  
  453. ;
  454. });;
  455. }
  456. })
  457. })
  458. })
  459. }
  460.  
  461. /**
  462. * Sample transaction processor function.
  463. * @param {org.doptrace.G_EntityToLogistics} entityToLogistics The sample transaction instance.
  464. * @transaction
  465. */
  466.  
  467. async function entityToLogistics(entityToLogistics) {
  468. return getAssetRegistry('org.doptrace.Logistics').then(function(playerRegistry) {
  469. return playerRegistry.exists(entityToLogistics.newOwner.getIdentifier())
  470. }).then(function(exists) {
  471. return getAssetRegistry('org.doptrace.CertificationEntity')
  472. .then(function(playerRegistryHarvest) {
  473. return playerRegistryHarvest.exists(entityToLogistics.issuer.getIdentifier())
  474. }).then(function(existsHarvest) {
  475. return getAssetRegistry('org.doptrace.Batch')
  476. .then(function(playerRegistryBatch) {
  477. return playerRegistryBatch.exists(entityToLogistics.batch.getIdentifier())
  478. }).then(function(existsBatch) {
  479. if (!exists) {
  480. throw Error('Companhia Logistica não registada !')
  481. } else if (!existsHarvest) {
  482. throw Error('Entidade de Certificação não Registada !')
  483. } else if (!existsBatch) {
  484. throw Error('Lote não registado!')
  485.  
  486. } else if (exists && existsHarvest && existsBatch) {
  487. getAssetRegistry('org.doptrace.Batch')
  488. .then(function(assetRegistry) {
  489.  
  490. entityToLogistics.batch.issuer = entityToLogistics.issuer
  491. entityToLogistics.batch.state = 'IN_TRANSIT'
  492. entityToLogistics.batch.owner = entityToLogistics.newOwner
  493. entityToLogistics.batch.previousOwners.push(entityToLogistics.issuer)
  494.  
  495. assetRegistry.update(entityToLogistics.batch)
  496.  
  497. ;
  498. });;
  499. }
  500. })
  501. })
  502. })
  503. }
  504.  
  505.  
  506. /**
  507. * Sample transaction processor function.
  508. * @param {org.doptrace.transactionProductRegistration} productRegistration The sample transaction instance
  509. * @transaction
  510. */
  511.  
  512. function productRegistation(productRegistation) {
  513.  
  514. var array = new Array();
  515.  
  516. return getAssetRegistry('org.doptrace.Producer').then(function(playerRegistry) {
  517. return playerRegistry.exists(productRegistation.productRegistration.producer.getIdentifier())
  518. }).then(function(exists) {
  519.  
  520. return getParticipantRegistry('org.doptrace.Worker').then(function(playerRegistry) {
  521. return playerRegistry.exists(productRegistation.productRegistration.worker.getIdentifier())
  522. }).then(function(existsWorker) {
  523.  
  524. return getAssetRegistry('org.doptrace.Product').then(function(playerRegistry) {
  525.  
  526. return playerRegistry.exists(productRegistation.productRegistration.newBatch.product.getIdentifier())
  527. }).then(function(existsProduct) {
  528.  
  529.  
  530. if (!exists) {
  531. throw Error('Produtor não registado !');
  532.  
  533. }else if(!existsWorker){
  534.  
  535. throw Error('Worker não registado !');
  536.  
  537. }else if(!existsProduct){
  538.  
  539. throw Error('Product não registado !');
  540.  
  541. } else if(exists &&existsWorker && existsProduct && productRegistation.productRegistration.newBatch != null) {
  542. getAssetRegistry('org.doptrace.ProductRegistration')
  543. .then(function(assetRegistry) {
  544. var factory = getFactory();
  545. var newAsset = factory.newResource(
  546. 'org.doptrace',
  547. 'ProductRegistration',
  548. productRegistation.productRegistration.id);
  549.  
  550. newAsset.description = productRegistation.productRegistration.description;
  551. newAsset.latitude = productRegistation.productRegistration.latitude;
  552. newAsset.longitude = productRegistation.productRegistration.longitude;
  553. newAsset.dateTime = productRegistation.productRegistration.dateTime;
  554. newAsset.worker = productRegistation.productRegistration.worker;
  555. newAsset.producer = productRegistation.productRegistration.producer;
  556. array.push(newAsset);
  557.  
  558.  
  559. return assetRegistry.add(newAsset);
  560.  
  561.  
  562.  
  563. ;
  564. }).then(function(batchRegistry){
  565. getAssetRegistry('org.doptrace.Batch')
  566. .then(function(batchRegistry2) {
  567.  
  568. var factory = getFactory();
  569.  
  570. var newBatch = factory.newResource(
  571. 'org.doptrace',
  572. 'Batch',
  573. productRegistation.productRegistration.newBatch.id);
  574.  
  575.  
  576.  
  577. newBatch.amount = productRegistation.productRegistration.newBatch.amount;
  578. newBatch.unit = productRegistation.productRegistration.newBatch.unit;
  579. newBatch.creationDate = productRegistation.productRegistration.newBatch.creationDate;
  580. newBatch.expirationDate = productRegistation.productRegistration.newBatch.expirationDate;
  581. newBatch.state = productRegistation.productRegistration.newBatch.state;
  582. newBatch.certificated = productRegistation.productRegistration.newBatch.certificated;
  583. newBatch.previousEvents = array
  584. newBatch.currentOperator = productRegistation.productRegistration.producer;
  585. newBatch.product = productRegistation.productRegistration.newBatch.product;
  586.  
  587. return batchRegistry2.add(newBatch);
  588.  
  589.  
  590. });;
  591.  
  592. }
  593.  
  594. )}else{
  595. throw Error('Novo Lote null');
  596. }})}
  597. )})}
  598.  
  599.  
  600.  
  601. /**
  602. * Sample transaction processor function.
  603. * @param {org.doptrace.transactionQualityAssessement} quality The sample transaction instance
  604. * @transaction
  605. */
  606.  
  607. function quality(quality) {
  608.  
  609. var array = new Array();
  610.  
  611. return getAssetRegistry('org.doptrace.Industry_Retailer').then(function(playerRegistry) {
  612. return playerRegistry.exists(quality.quality.industry_retailer.getIdentifier())
  613. }).then(function(existsIndustry) {
  614.  
  615. return getAssetRegistry('org.doptrace.Batch').then(function(playerRegistry) {
  616.  
  617. return playerRegistry.exists(quality.quality.assessedBatch.getIdentifier())
  618. }).then(function(existsBatch) {
  619.  
  620.  
  621. if (!existsIndustry) {
  622.  
  623. throw Error('Industria não registado !');
  624.  
  625. }else if(!existsBatch){
  626.  
  627. throw Error('Lote não registado !');
  628.  
  629. }else if( existsIndustry && existsBatch && quality.quality.amountDowned == 0.00){
  630.  
  631. getAssetRegistry('org.doptrace.QualityAssessment')
  632. .then(function(assetRegistry) {
  633. var factory = getFactory();
  634. var newAsset = factory.newResource(
  635. 'org.doptrace',
  636. 'QualityAssessment',
  637. quality.quality.id);
  638.  
  639. newAsset.description = quality.quality.description;
  640. newAsset.latitude = quality.quality.latitude;
  641. newAsset.longitude = quality.quality.longitude;
  642. newAsset.dateTime = quality.quality.dateTime;
  643. newAsset.worker = quality.quality.worker;
  644. newAsset.industry_retailer = quality.quality.industry_retailer;
  645. newAsset.assessedBatch = quality.quality.assessedBatch
  646. newAsset.amountDowned = quality.quality.amountDowned
  647. array.push(newAsset)
  648.  
  649. return assetRegistry.add(newAsset);
  650.  
  651. }).then(function(batchRegistry){
  652. getAssetRegistry('org.doptrace.Batch')
  653. .then(function(assetRegistry) {
  654.  
  655. quality.quality.assessedBatch.currentOperator = quality.quality.industry_retailer
  656.  
  657. assetRegistry.update(quality.quality.assessedBatch);
  658.  
  659.  
  660. })})}else if(existsBatch && existsIndustry && quality.quality.amountDowned != 0.00 /*quality.quality.industry_retailer.getIdentifier() != quality.quality.assessedBatch.currentOperator.getIdentifier()*/) {
  661.  
  662. getAssetRegistry('org.doptrace.QualityAssessment')
  663. .then(function(assetRegistry) {
  664. var factory = getFactory();
  665. var newAsset = factory.newResource(
  666. 'org.doptrace',
  667. 'QualityAssessment',
  668. quality.quality.id);
  669.  
  670. newAsset.description = quality.quality.description;
  671. newAsset.latitude = quality.quality.latitude;
  672. newAsset.longitude = quality.quality.longitude;
  673. newAsset.dateTime = quality.quality.dateTime;
  674. newAsset.worker = quality.quality.worker;
  675. newAsset.industry_retailer = quality.quality.industry_retailer;
  676. newAsset.assessedBatch = quality.quality.assessedBatch
  677. newAsset.amountDowned = quality.quality.amountDowned
  678. array.push(newAsset)
  679.  
  680.  
  681. return assetRegistry.add(newAsset);
  682.  
  683.  
  684.  
  685. ;
  686. }).then(function(batchRegistry){
  687. getAssetRegistry('org.doptrace.Batch')
  688. .then(function(batchRegistry2) {
  689.  
  690. var factory = getFactory();
  691.  
  692. var newBatch = factory.newResource(
  693. 'org.doptrace',
  694. 'Batch',
  695. quality.quality.newBatch.id);
  696.  
  697. newBatch.amount = quality.quality.assessedBatch.amount - quality.quality.amountDowned;
  698. newBatch.unit = quality.quality.assessedBatch.unit;
  699. newBatch.creationDate = quality.quality.assessedBatch.creationDate;
  700. newBatch.expirationDate = quality.quality.assessedBatch.expirationDate;
  701. newBatch.state = quality.quality.assessedBatch.state;
  702. newBatch.certificated = quality.quality.assessedBatch.certificated;
  703. newBatch.previousEvents = quality.quality.assessedBatch.previousEvents.concat(array)
  704. newBatch.currentOperator = quality.quality.assessedBatch.currentOperator;
  705. newBatch.product = quality.quality.assessedBatch.product;
  706.  
  707. return batchRegistry2.add(newBatch);
  708.  
  709.  
  710. });;
  711.  
  712.  
  713.  
  714. }
  715.  
  716.  
  717.  
  718. )}else{
  719. throw Error('Erro na transação');
  720. }}
  721.  
  722. )})}
  723.  
  724.  
  725. /**
  726. * Sample transaction processor function.
  727. * @param {org.doptrace.transactionTransformation} transformation The sample transaction instance
  728. * @transaction
  729. */
  730.  
  731. async function transformation(transformation){
  732.  
  733. //Tamanho dos arrays de Lotes e do array de quantidades
  734. var batchLength = transformation.transformation.usedBatches.length;
  735. var amountLength = transformation.transformation.amountPerBatch.length;
  736.  
  737. //Array de Erros para verificação do batch
  738. var errors = new Array();
  739.  
  740. //Array para concatenação de Eventos
  741. var array = new Array();
  742.  
  743.  
  744. //Verificação do Industry Retailer
  745. let registryIndustry = await getAssetRegistry('org.doptrace.Industry_Retailer');
  746. let resultIndustry = await registryIndustry.exists(transformation.transformation.industry_retailer.getIdentifier());
  747.  
  748. if(!resultIndustry){
  749.  
  750. throw Error('Industria nao registada !')
  751.  
  752. }else if(transformation.transformation.usedBatches.length == 0){
  753.  
  754. throw Error('Preencha os lotes usados para transformação !')
  755.  
  756. }
  757. //Verificação dos lotes usados
  758. for(var i=0;i<batchLength;i++) {
  759.  
  760. let registryBatch = await getAssetRegistry('org.doptrace.Batch');
  761.  
  762.  
  763.  
  764. let resultBatch = await registryBatch.exists(transformation.transformation.usedBatches[i].getIdentifier());
  765.  
  766. if(resultIndustry&& resultBatch && transformation.transformation.usedBatches != null){
  767. let transformRegistry = await getAssetRegistry('org.doptrace.Transformation');
  768.  
  769. var factory = getFactory();
  770.  
  771. var newAsset = factory.newResource(
  772. 'org.doptrace',
  773. 'Transformation',
  774. transformation.transformation.id);
  775.  
  776. newAsset.description = transformation.transformation.description;
  777. newAsset.latitude = transformation.transformation.latitude;
  778. newAsset.longitude = transformation.transformation.longitude;
  779. newAsset.dateTime = transformation.transformation.dateTime;
  780. newAsset.worker = transformation.transformation.worker;
  781. newAsset.processes = transformation.transformation.processes;
  782. newAsset.industry_retailer = transformation.transformation.industry_retailer
  783. newAsset.usedBatches = transformation.transformation.usedBatches;
  784. newAsset.amountPerBatch = transformation.transformation.amountPerBatch;
  785. array.push(newAsset)
  786.  
  787. if(batchLength != amountLength){
  788.  
  789. throw Error('Erro na transação');
  790.  
  791. }else{
  792.  
  793. return transformRegistry.add(newAsset);
  794.  
  795. let registryBatch = await getAssetRegistry('org.doptrace.Batch');
  796.  
  797. var factory = getFactory();
  798.  
  799. var newBatch = factory.newResource(
  800. 'org.doptrace',
  801. 'Batch',
  802. transformation.transformation.newBatch.id);
  803.  
  804. newBatch.amount = // transformation.transformation.usedBatch[i].amount - transformation.transformation.;
  805. newBatch.unit = quality.quality.assessedBatch.unit;
  806. newBatch.creationDate = quality.quality.assessedBatch.creationDate;
  807. newBatch.expirationDate = quality.quality.assessedBatch.expirationDate;
  808. newBatch.state = quality.quality.assessedBatch.state;
  809. newBatch.certificated = quality.quality.assessedBatch.certificated;
  810. newBatch.previousEvents = quality.quality.assessedBatch.previousEvents.concat(array)
  811. newBatch.currentOperator = quality.quality.assessedBatch.currentOperator;
  812. newBatch.product = quality.quality.assessedBatch.product;
  813.  
  814. }
  815.  
  816.  
  817.  
  818.  
  819. }else{
  820. errors.push(new Error('Lote ' + transformation.transformation.usedBatches[i].getIdentifier() + ' não se encontra registado ! '));
  821. }
  822.  
  823.  
  824. throw errors
  825.  
  826. }
  827. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement