grid-sdk 0.3.5

Hyperledger Grid is a platform for building supply chain solutions that include distributed ledger components. It provides a growing set of tools that accelerate development for supply chain smart contractsand client interfaces.
Documentation
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// Copyright 2018-2021 Cargill Incorporated
//
// 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.

#[cfg(feature = "diesel")]
pub(in crate) mod diesel;
pub mod error;

use crate::paging::Paging;

#[cfg(feature = "diesel")]
pub use self::diesel::{DieselConnectionProductStore, DieselProductStore};
pub use error::{ProductBuilderError, ProductStoreError};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Product {
    product_id: String,
    product_address: String,
    product_namespace: String,
    owner: String,
    start_commit_num: i64,
    end_commit_num: i64,
    service_id: Option<String>,
    last_updated: Option<i64>,
    properties: Vec<PropertyValue>,
}

impl Product {
    /// Returns the product_id for the product
    pub fn product_id(&self) -> &str {
        &self.product_id
    }

    /// Returns the product_address for the product
    pub fn product_address(&self) -> &str {
        &self.product_address
    }

    /// Returns the product_namespace for the product
    pub fn product_namespace(&self) -> &str {
        &self.product_namespace
    }

    /// Returns the owner for the product
    pub fn owner(&self) -> &str {
        &self.owner
    }

    /// Returns the start_commit_num for the product
    pub fn start_commit_num(&self) -> &i64 {
        &self.start_commit_num
    }

    /// Returns the end_commit_num for the product
    pub fn end_commit_num(&self) -> &i64 {
        &self.end_commit_num
    }

    /// Returns the service_id for the product
    pub fn service_id(&self) -> Option<&str> {
        self.service_id.as_deref()
    }

    /// Returns the last updated timestamp for the product
    pub fn last_updated(&self) -> Option<&i64> {
        self.last_updated.as_ref()
    }

    /// Returns the properties for the product
    pub fn properties(&self) -> Vec<PropertyValue> {
        self.properties.to_vec()
    }
}

/// Builder used to create a Product
#[derive(Default, Clone)]
pub struct ProductBuilder {
    product_id: String,
    product_address: String,
    product_namespace: String,
    owner: String,
    start_commit_num: i64,
    end_commit_num: i64,
    service_id: Option<String>,
    last_updated: Option<i64>,
    properties: Vec<PropertyValue>,
}

impl ProductBuilder {
    /// Sets the product ID for this product
    pub fn with_product_id(mut self, product_id: String) -> Self {
        self.product_id = product_id;
        self
    }

    /// Sets the product address for this product
    pub fn with_product_address(mut self, product_address: String) -> Self {
        self.product_address = product_address;
        self
    }

    /// Sets the product namespace for this product
    pub fn with_product_namespace(mut self, product_namespace: String) -> Self {
        self.product_namespace = product_namespace;
        self
    }

    /// Sets the owner of the product
    pub fn with_owner(mut self, owner: String) -> Self {
        self.owner = owner;
        self
    }

    /// Sets the start commit number for this product
    pub fn with_start_commit_number(mut self, start_commit_num: i64) -> Self {
        self.start_commit_num = start_commit_num;
        self
    }

    /// Sets the end commit number for this product
    pub fn with_end_commit_number(mut self, end_commit_num: i64) -> Self {
        self.end_commit_num = end_commit_num;
        self
    }

    /// Sets the service ID for this product
    pub fn with_service_id(mut self, service_id: Option<String>) -> Self {
        self.service_id = service_id;
        self
    }

    /// Sets the last updated timestamp for this product
    pub fn with_last_updated(mut self, last_updated: Option<i64>) -> Self {
        self.last_updated = last_updated;
        self
    }

    /// Sets the properties for this product
    pub fn with_properties(mut self, properties: Vec<PropertyValue>) -> Self {
        self.properties = properties;
        self
    }

    pub fn build(self) -> Result<Product, ProductBuilderError> {
        let ProductBuilder {
            product_id,
            product_address,
            product_namespace,
            owner,
            start_commit_num,
            end_commit_num,
            service_id,
            last_updated,
            properties,
        } = self;

        if product_id.is_empty() {
            return Err(ProductBuilderError::MissingRequiredField(
                "Missing product_id".to_string(),
            ));
        };

        if product_address.is_empty() {
            return Err(ProductBuilderError::MissingRequiredField(
                "Missing product_address".to_string(),
            ));
        };

        if product_namespace.is_empty() {
            return Err(ProductBuilderError::MissingRequiredField(
                "Missing product_namespace".to_string(),
            ));
        };

        if owner.is_empty() {
            return Err(ProductBuilderError::MissingRequiredField(
                "Missing owner".to_string(),
            ));
        };

        if start_commit_num >= end_commit_num {
            return Err(ProductBuilderError::MissingRequiredField(
                "start_commit_number must be less than end_commit_num".to_string(),
            ));
        };

        if end_commit_num <= start_commit_num {
            return Err(ProductBuilderError::MissingRequiredField(
                "end_commit_number must be greater than start_commit_num".to_string(),
            ));
        };

        Ok(Product {
            product_id,
            product_address,
            product_namespace,
            owner,
            start_commit_num,
            end_commit_num,
            service_id,
            last_updated,
            properties,
        })
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PropertyValue {
    product_id: String,
    product_address: String,
    property_name: String,
    data_type: String,
    bytes_value: Option<Vec<u8>>,
    boolean_value: Option<bool>,
    number_value: Option<i64>,
    string_value: Option<String>,
    enum_value: Option<i32>,
    struct_values: Vec<PropertyValue>,
    lat_long_value: Option<LatLongValue>,
    start_commit_num: i64,
    end_commit_num: i64,
    service_id: Option<String>,
}

impl PropertyValue {
    /// Returns the product_id for the property value
    pub fn product_id(&self) -> &str {
        &self.product_id
    }

    /// Returns the product_address for the property value
    pub fn product_address(&self) -> &str {
        &self.product_address
    }

    /// Returns the property_name for the property value
    pub fn property_name(&self) -> &str {
        &self.property_name
    }

    /// Returns the data_type for the property value
    pub fn data_type(&self) -> &str {
        &self.data_type
    }

    /// Returns the bytes_value for the property value
    pub fn bytes_value(&self) -> Option<Vec<u8>> {
        self.bytes_value.clone()
    }

    /// Returns the boolean_value for the property value
    pub fn boolean_value(&self) -> Option<bool> {
        self.boolean_value
    }

    /// Returns the number_value for the property value
    pub fn number_value(&self) -> Option<i64> {
        self.number_value
    }

    /// Returns the string_value for the property value
    pub fn string_value(&self) -> Option<&str> {
        self.string_value.as_deref()
    }

    /// Returns the enum_value for the property value
    pub fn enum_value(&self) -> Option<i32> {
        self.enum_value
    }

    /// Returns the struct_values for the property value
    pub fn struct_values(&self) -> Vec<PropertyValue> {
        self.struct_values.clone()
    }

    /// Returns the lat_long_value for the property value
    pub fn lat_long_value(&self) -> Option<LatLongValue> {
        self.lat_long_value.as_ref().cloned()
    }

    /// Returns the start_commit_num for the property value
    pub fn start_commit_num(&self) -> &i64 {
        &self.start_commit_num
    }

    /// Returns the end_commit_num for the property value
    pub fn end_commit_num(&self) -> &i64 {
        &self.end_commit_num
    }

    /// Returns the service_id for the property value
    pub fn service_id(&self) -> Option<&str> {
        self.service_id.as_deref()
    }
}

/// Builder used to create a PropertyValue
#[derive(Default, Clone)]
pub struct PropertyValueBuilder {
    product_id: String,
    product_address: String,
    property_name: String,
    data_type: String,
    bytes_value: Option<Vec<u8>>,
    boolean_value: Option<bool>,
    number_value: Option<i64>,
    string_value: Option<String>,
    enum_value: Option<i32>,
    struct_values: Vec<PropertyValue>,
    lat_long_value: Option<LatLongValue>,
    start_commit_num: i64,
    end_commit_num: i64,
    service_id: Option<String>,
}

impl PropertyValueBuilder {
    /// Sets the product ID for this property value
    pub fn with_product_id(mut self, product_id: String) -> Self {
        self.product_id = product_id;
        self
    }

    /// Sets the product address for this property value
    pub fn with_product_address(mut self, product_address: String) -> Self {
        self.product_address = product_address;
        self
    }

    /// Sets the property name for this property value
    pub fn with_property_name(mut self, property_name: String) -> Self {
        self.property_name = property_name;
        self
    }

    /// Sets the data type for this property value
    pub fn with_data_type(mut self, data_type: String) -> Self {
        self.data_type = data_type;
        self
    }

    /// Sets the bytes value for this property value
    pub fn with_bytes_value(mut self, bytes_value: Option<Vec<u8>>) -> Self {
        self.bytes_value = bytes_value;
        self
    }

    /// Sets the boolean value for this property value
    pub fn with_boolean_value(mut self, boolean_value: Option<bool>) -> Self {
        self.boolean_value = boolean_value;
        self
    }

    /// Sets the number value for this property value
    pub fn with_number_value(mut self, number_value: Option<i64>) -> Self {
        self.number_value = number_value;
        self
    }

    /// Sets the string value for this property value
    pub fn with_string_value(mut self, string_value: Option<String>) -> Self {
        self.string_value = string_value;
        self
    }

    /// Sets the enum value for this property value
    pub fn with_enum_value(mut self, enum_value: Option<i32>) -> Self {
        self.enum_value = enum_value;
        self
    }

    /// Sets the struct values for this property value
    pub fn with_struct_values(mut self, struct_values: Vec<PropertyValue>) -> Self {
        self.struct_values = struct_values;
        self
    }

    /// Sets the LatLong value for this property value
    pub fn with_lat_long_value(mut self, lat_long_value: Option<LatLongValue>) -> Self {
        self.lat_long_value = lat_long_value;
        self
    }

    /// Sets the start commit number for this property value
    pub fn with_start_commit_number(mut self, start_commit_num: i64) -> Self {
        self.start_commit_num = start_commit_num;
        self
    }

    /// Sets the end commit number for this property value
    pub fn with_end_commit_number(mut self, end_commit_num: i64) -> Self {
        self.end_commit_num = end_commit_num;
        self
    }

    /// Sets the service ID for this property value
    pub fn with_service_id(mut self, service_id: Option<String>) -> Self {
        self.service_id = service_id;
        self
    }

    pub fn build(self) -> Result<PropertyValue, ProductBuilderError> {
        let PropertyValueBuilder {
            product_id,
            product_address,
            property_name,
            data_type,
            bytes_value,
            boolean_value,
            number_value,
            string_value,
            enum_value,
            struct_values,
            lat_long_value,
            start_commit_num,
            end_commit_num,
            service_id,
        } = self;

        if product_id.is_empty() {
            return Err(ProductBuilderError::MissingRequiredField(
                "Missing product_id".to_string(),
            ));
        };

        if product_address.is_empty() {
            return Err(ProductBuilderError::MissingRequiredField(
                "Missing product_address".to_string(),
            ));
        };

        if property_name.is_empty() {
            return Err(ProductBuilderError::MissingRequiredField(
                "Missing product_name".to_string(),
            ));
        };

        if data_type.is_empty() {
            return Err(ProductBuilderError::MissingRequiredField(
                "Missing data_type".to_string(),
            ));
        };

        if start_commit_num >= end_commit_num {
            return Err(ProductBuilderError::MissingRequiredField(
                "start_commit_number must be less than end_commit_num".to_string(),
            ));
        };

        if end_commit_num <= start_commit_num {
            return Err(ProductBuilderError::MissingRequiredField(
                "end_commit_number must be greater than start_commit_num".to_string(),
            ));
        };

        Ok(PropertyValue {
            product_id,
            product_address,
            property_name,
            data_type,
            bytes_value,
            boolean_value,
            number_value,
            string_value,
            enum_value,
            struct_values,
            lat_long_value,
            start_commit_num,
            end_commit_num,
            service_id,
        })
    }
}

#[derive(Debug, Clone)]
pub struct ProductList {
    data: Vec<Product>,
    paging: Paging,
}

impl ProductList {
    pub fn new(data: Vec<Product>, paging: Paging) -> Self {
        Self { data, paging }
    }

    /// Returns the data for the product list
    pub fn data(&self) -> Vec<Product> {
        self.data.to_vec()
    }

    /// Returns the paging information for the product list
    pub fn paging(&self) -> &Paging {
        &self.paging
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LatLongValue {
    pub latitude: i64,
    pub longitude: i64,
}

pub trait ProductStore {
    /// Adds a product to the underlying storage
    ///
    /// # Arguments
    ///
    ///  * `product` - The product to be added
    fn add_product(&self, product: Product) -> Result<(), ProductStoreError>;

    /// Gets a product from the underlying storage
    ///
    /// # Arguments
    ///
    ///  * `product_id` - The ID of the product to be fetched
    ///  * `service_id` - The service ID to fetch the product for
    fn get_product(
        &self,
        product_id: &str,
        service_id: Option<&str>,
    ) -> Result<Option<Product>, ProductStoreError>;

    /// Gets a list of products from the underlying storage
    ///
    /// # Arguments
    ///
    ///  * `service_id` - The service ID to fetch the product for
    ///  * `offset` - The index of the first in storage to retrieve
    ///  * `limit` - The number of items to retrieve from the offset
    fn list_products(
        &self,
        service_id: Option<&str>,
        offset: i64,
        limit: i64,
    ) -> Result<ProductList, ProductStoreError>;

    /// Updates a product in the underlying storage
    ///
    /// # Arguments
    ///
    ///  * `product` - The updated product
    ///  * `service_id` - The service ID to fetch the product for
    ///  * `current_commit_num` - The current commit height
    fn update_product(
        &self,
        product_id: &str,
        service_id: Option<&str>,
        current_commit_num: i64,
    ) -> Result<(), ProductStoreError>;

    /// Deletes a product from the underlying storage
    ///
    /// # Arguments
    ///
    ///  * `address` - The address of the record to be deleted
    ///  * `current_commit_num` - The current commit height
    fn delete_product(
        &self,
        address: &str,
        current_commit_num: i64,
    ) -> Result<(), ProductStoreError>;
}

impl<PS> ProductStore for Box<PS>
where
    PS: ProductStore + ?Sized,
{
    fn add_product(&self, product: Product) -> Result<(), ProductStoreError> {
        (**self).add_product(product)
    }

    fn get_product(
        &self,
        product_id: &str,
        service_id: Option<&str>,
    ) -> Result<Option<Product>, ProductStoreError> {
        (**self).get_product(product_id, service_id)
    }

    fn list_products(
        &self,
        service_id: Option<&str>,
        offset: i64,
        limit: i64,
    ) -> Result<ProductList, ProductStoreError> {
        (**self).list_products(service_id, offset, limit)
    }

    fn update_product(
        &self,
        product_id: &str,
        service_id: Option<&str>,
        current_commit_num: i64,
    ) -> Result<(), ProductStoreError> {
        (**self).update_product(product_id, service_id, current_commit_num)
    }

    fn delete_product(
        &self,
        address: &str,
        current_commit_num: i64,
    ) -> Result<(), ProductStoreError> {
        (**self).delete_product(address, current_commit_num)
    }
}