Skip to content

Commit 4592db5

Browse files
Add bundle + examples (#11)
Co-authored-by: Konstantin Korotkov <konstantin.korotkov@labs64.de>
1 parent 3edffc2 commit 4592db5

9 files changed

Lines changed: 364 additions & 1 deletion

File tree

client_demo/netlicensing_client_demo.cc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ int main(int argc, char* argv[]) {
4242
std::string licenseeNumber = "L"+randomNumber;
4343
std::string licenseNumber = "LC"+randomNumber;
4444
std::string licenseeName = "Licensee "+randomNumber;
45+
std::string bundleNumber = "B"+randomNumber;
46+
std::string bundleName = "Bundle "+randomNumber;
4547

4648
std::cout << "Hello, this is NetLicensing demo client\n";
4749
std::cout << "Product endpoint " << endpoint<Product>() << std::endl;
@@ -454,6 +456,89 @@ int main(int argc, char* argv[]) {
454456

455457
// endregion
456458

459+
// region ********* Bundle
460+
std::list<String_t> licenseTemplateNumbers;
461+
licenseTemplateNumbers.push_back(licenseTemplateNumber);
462+
463+
Bundle newBundle;
464+
newBundle.setNumber(bundleNumber);
465+
newBundle.setName(bundleName);
466+
newBundle.setDescription("Demo bundle");
467+
newBundle.setLicenseTemplateNumbers(licenseTemplateNumbers);
468+
newBundle.setPrice(FixedPoint("12.50"));
469+
newBundle.setCurrency(Currency::EUR);
470+
newBundle.addProperty("CustomKey", "CustomValue");
471+
472+
Bundle bundle = BundleService::create(ctx, newBundle);
473+
std::cout << "Added bundle: " << bundle.getName().toString() << std::endl;
474+
475+
bundle = BundleService::get(ctx, bundleNumber);
476+
std::string bundleString(bundle.toString());
477+
std::cout << "Got bundle: " << bundleString << std::endl;
478+
479+
std::list<Bundle> bundles = BundleService::list(ctx, "");
480+
if (bundles.size()) {
481+
std::cout << "Got the following bundles: " << std::endl;
482+
for (auto const& i : bundles) {
483+
std::cout << i.toString() << std::endl;
484+
}
485+
}
486+
487+
std::string bProductModuleNumber = "BP" + randomNumber;
488+
ProductModule bProductModule;
489+
bProductModule.setNumber(bProductModuleNumber);
490+
bProductModule.setName("Demo product module");
491+
bProductModule.setLicensingModel(LICENSING_MODEL_SUBSCRIPTION_NAME);
492+
bProductModule.setProductNumber(productNumber);
493+
494+
std::string bLicenseTemplateNumber = "BLT" + randomNumber;
495+
LicenseTemplate bLicenseTemplate;
496+
bLicenseTemplate.setNumber(bLicenseTemplateNumber);
497+
bLicenseTemplate.setName("Template for Bundle");
498+
bLicenseTemplate.setLicenseType(LicenseTypeEnum::TIMEVOLUME);
499+
bLicenseTemplate.addProperty("timeVolume", "3");
500+
bLicenseTemplate.setPrice(FixedPoint("5.00"));
501+
bLicenseTemplate.setCurrency(Currency::EUR);
502+
bLicenseTemplate.setAutomatic(false);
503+
bLicenseTemplate.setHidden(false);
504+
bLicenseTemplate.setProductModuleNumber(bProductModuleNumber);
505+
506+
bProductModule = ProductModuleService::create(ctx, bProductModule);
507+
bLicenseTemplate = LicenseTemplateService::create(ctx, bLicenseTemplate);
508+
509+
bundle.addLicenseTemplateNumber(bLicenseTemplateNumber);
510+
bundle.addLicenseTemplateNumber(bLicenseTemplateNumber);
511+
bundle.addLicenseTemplateNumber(bLicenseTemplateNumber);
512+
513+
bundle.setName("Updated name");
514+
bundle = BundleService::update(ctx, bundleNumber, bundle);
515+
bundleString = newLicenseTemplate.toString();
516+
std::cout << "Updated bundle: " << bundleString << std::endl;
517+
518+
std::list<License> obtainedLicenses = BundleService::obtain(ctx, bundleNumber, licenseeNumber, "");
519+
std::cout << "Obtained licenses: " << std::endl;
520+
for (auto const& i : obtainedLicenses) {
521+
std::cout << i.toString() << std::endl;
522+
}
523+
524+
// bundle shop token
525+
Token bundleShopToken;
526+
bundleShopToken.setTokenType(TokenType::SHOP);
527+
bundleShopToken.addProperty(LICENSEE_NUMBER, licenseeNumber);
528+
bundleShopToken.addProperty(BUNDLE_NUMBER, bundleNumber);
529+
530+
bundleShopToken = TokenService::create(ctx, bundleShopToken);
531+
std::string bundleShopTokenString = bundleShopToken.toString();
532+
std::cout << "Bundle shop token: " << bundleShopTokenString << std::endl;
533+
534+
// cleanup
535+
LicenseTemplateService::del(ctx, bLicenseTemplateNumber, true);
536+
ProductModuleService::del(ctx, bProductModuleNumber, true);
537+
BundleService::del(ctx, bundleNumber, true);
538+
std::cout << "Deleted bundle!" << std::endl;
539+
540+
// endregion
541+
457542
std::cout << "All done." << std::endl;
458543

459544
cleanUp(ctx, productNumber, true);

include/netlicensing/bundle.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#ifndef __BUNDLE_H__
2+
#define __BUNDLE_H__
3+
4+
#include <list>
5+
6+
#include "netlicensing/constants.h"
7+
#include "netlicensing/entity.h"
8+
9+
10+
namespace netlicensing {
11+
12+
class Bundle : public BaseEntity {
13+
private:
14+
String_t name_i;
15+
String_t description_i;
16+
FixedPoint price_i = FixedPoint(0);
17+
Currency currency_i;
18+
std::list<String_t> licenseTemplateNumbers_i;
19+
20+
21+
public:
22+
Bundle() : name_i(), description_i(), price_i(), currency_i(), licenseTemplateNumbers_i() { }
23+
24+
void setName(const String_t& name) {
25+
name_i = name;
26+
}
27+
28+
const String_t& getName() const {
29+
return name_i;
30+
}
31+
32+
void setDescription(const String_t& description) {
33+
description_i = description;
34+
}
35+
36+
const String_t& getDescription() const {
37+
return description_i;
38+
}
39+
40+
void setPrice(const FixedPoint& price) {
41+
price_i = price;
42+
}
43+
44+
const FixedPoint& getPrice() const {
45+
return price_i;
46+
}
47+
48+
void setCurrency(const Currency currency) {
49+
currency_i = currency;
50+
}
51+
52+
const Currency getCurrency() const {
53+
return currency_i;
54+
}
55+
56+
void setLicenseTemplateNumbers(const std::list<String_t>& licenseTemplateNumbers) {
57+
licenseTemplateNumbers_i = licenseTemplateNumbers;
58+
}
59+
60+
std::list<String_t>& getLicenseTemplateNumbers() {
61+
return licenseTemplateNumbers_i;
62+
}
63+
64+
void addLicenseTemplateNumber(const String_t& licenseTemplateNumber) {
65+
licenseTemplateNumbers_i.push_back(licenseTemplateNumber);
66+
}
67+
68+
std::string toString() const {
69+
std::string number(getNumber());
70+
Boolean_t active(getActive());
71+
std::string name(getName());
72+
std::string description(getDescription());
73+
std::string price(getPrice().toString());
74+
std::string currency(currencyToString(getCurrency()));
75+
76+
std::stringstream ss;
77+
ss << "Bundle [";
78+
ss << NUMBER << ": " << number << ", ";
79+
ss << ACTIVE << ": " << active << ", ";
80+
ss << NAME << ": " << name << ", ";
81+
ss << DESCRIPTION << ": " << description << ", ";
82+
ss << PRICE << ": " << price << ", ";
83+
ss << CURRENCY << ": " << currency << ", ";
84+
ss << LICENSE_TEMPLATE_NUMBERS << ": " << join(licenseTemplateNumbers_i, ",");
85+
86+
for (const auto& pair : getProperties()) {
87+
ss << ", " << pair.first << ": " << pair.second.toString();
88+
}
89+
90+
ss << "]";
91+
return ss.str();
92+
}
93+
94+
};
95+
96+
}
97+
98+
#endif // __BUNDLE_H__

include/netlicensing/constants.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ namespace netlicensing {
129129
static const char* const LICENSING_MODEL_QUOTA_NAME = "Quota";
130130
static const char* const LICENSING_MODEL_NODE_LOCKED_NAME = "NodeLocked";
131131

132+
//Bundle
133+
static const char* const BUNDLE_ENDPOINT_PATH = "bundle";
134+
static const char* const ENDPOINT_PATH_OBTAIN = "obtain";
135+
static const char* const BUNDLE_NUMBER = "bundleNumber";
136+
static const char* const LICENSE_TEMPLATE_NUMBERS = "licenseTemplateNumbers";
137+
132138
} // namespace netlicensing
133139

134140
#endif // __CONSTANTS_H__

include/netlicensing/converters.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "netlicensing/transaction.h"
1515
#include "netlicensing/licensing_model.h"
1616
#include "netlicensing/license_type.h"
17+
#include "netlicensing/bundle.h"
1718

1819
namespace netlicensing {
1920

@@ -147,6 +148,17 @@ namespace netlicensing {
147148
return params;
148149
}
149150

151+
template<>
152+
inline parameters_type toParametersList<Bundle>(Bundle value) {
153+
parameters_type params = toParametersList<BaseEntity>(value);
154+
params.push_back(std::make_pair(NAME, value.getName()));
155+
params.push_back(std::make_pair(DESCRIPTION, value.getDescription()));
156+
params.push_back(std::make_pair(PRICE, value.getPrice().toString()));
157+
params.push_back(std::make_pair(CURRENCY, currencyToString(value.getCurrency())));
158+
params.push_back(std::make_pair(LICENSE_TEMPLATE_NUMBERS, join(value.getLicenseTemplateNumbers(), ",")));
159+
return params;
160+
}
161+
150162
} // namespace netlicensing
151163

152164
#endif // __CONVERTERS_H__

include/netlicensing/datatypes.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace netlicensing {
6767
std::istringstream is(rawValue);
6868
is >> value_i;
6969
} else {
70-
throw MalformedArgumentsException("Not supported formatting for fixed point value");
70+
throw MalformedArgumentsException("Not supported formatting for fixed point value: '" + value + "', expected format: '123.45'");
7171
}
7272
}
7373
FixedPoint() {
@@ -334,6 +334,38 @@ namespace netlicensing {
334334
else if (std::string(v) == "QUANTITY") return LicenseTypeEnum::QUANTITY;
335335
else return LicenseTypeEnum::FEATURE;
336336
}
337+
338+
inline const std::string join(const std::list<String_t>& list, const std::string delimiter) {
339+
std::ostringstream oss;
340+
if (!list.empty()) {
341+
for (auto it = list.begin(); it != list.end(); ++it) {
342+
oss << it->toString();
343+
344+
if (std::next(it) != list.end()) {
345+
oss << delimiter;
346+
}
347+
}
348+
}
349+
350+
return oss.str();
351+
}
352+
353+
inline std::list<String_t> split(const String_t& str, const std::string& delimiter) {
354+
std::list<String_t> result;
355+
if (str.toString().length() > 0) {
356+
std::istringstream stream(str.toString());
357+
std::string item;
358+
359+
while (std::getline(stream, item, delimiter[0])) {
360+
item.erase(item.find_last_not_of(" \t\r\n") + 1);
361+
item.erase(0, item.find_first_not_of(" \t\r\n"));
362+
363+
result.push_back(item);
364+
}
365+
}
366+
367+
return result;
368+
}
337369
} // namespace netlicensing
338370

339371
#endif // __DATATYPES_H__

include/netlicensing/mapper.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "netlicensing/licensing_model.h"
2020
#include "netlicensing/license_type.h"
2121
#include "netlicensing/license_template.h"
22+
#include "netlicensing/bundle.h"
2223

2324
namespace netlicensing {
2425

@@ -365,6 +366,42 @@ namespace netlicensing {
365366
}
366367
};
367368

369+
class BundleWrapper : public ItemWrapper {
370+
Bundle item_i;
371+
372+
public:
373+
const Bundle& getItem() {
374+
return item_i;
375+
}
376+
377+
virtual void addProperty(const std::string& key, const std::string& value) {
378+
if (key == NUMBER) {
379+
item_i.setNumber(value);
380+
}
381+
else if (key == ACTIVE) {
382+
item_i.setActive(value.c_str());
383+
}
384+
else if (key == NAME) {
385+
item_i.setName(value);
386+
}
387+
else if (key == DESCRIPTION) {
388+
item_i.setDescription(value);
389+
}
390+
else if (key == PRICE) {
391+
item_i.setPrice(FixedPoint(value));
392+
}
393+
else if (key == CURRENCY) {
394+
item_i.setCurrency(stringToCurrency(value));
395+
}
396+
else if (key == LICENSE_TEMPLATE_NUMBERS) {
397+
item_i.setLicenseTemplateNumbers(split(value, ","));
398+
}
399+
else {
400+
item_i.addProperty(key, value);
401+
}
402+
}
403+
};
404+
368405
template<class T>
369406
struct ItemTraits;
370407

@@ -434,6 +471,12 @@ namespace netlicensing {
434471
static std::string getType() { return "Transaction"; }
435472
};
436473

474+
template<>
475+
struct ItemTraits<Bundle> {
476+
typedef BundleWrapper Wrapper_t;
477+
static std::string getType() { return "Bundle"; }
478+
};
479+
437480
template<typename T>
438481
class StandardMapper : public MapperBase {
439482
std::list<T> items_i;

include/netlicensing/netlicensing.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "netlicensing/token.h"
1616
#include "netlicensing/transaction.h"
1717
#include "netlicensing/licensing_model.h"
18+
#include "netlicensing/bundle.h"
1819

1920
namespace netlicensing {
2021

@@ -100,6 +101,15 @@ class UtilityService {
100101
static std::list<LicenseType> listLicenseTypes(Context& ctx);
101102
};
102103

104+
class BundleService {
105+
public:
106+
static Bundle get(Context& ctx, const std::string& number);
107+
static Bundle create(Context& ctx, const Bundle&);
108+
static Bundle update(Context& ctx, const std::string& number, const Bundle&);
109+
static void del(Context& ctx, const std::string& number, bool forceCascade);
110+
static std::list<Bundle> list(Context& ctx, const std::string& filter);
111+
static std::list<License> obtain(Context& ctx, const std::string& bundleNumber, const std::string& licenseeNumber, const std::string& transactionNumber);
112+
};
103113

104114
}
105115

include/netlicensing/service.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ template<> inline std::string endpoint<Transaction>() { return std::string(TRANS
2020
template<> inline std::string endpoint<Country>() { return std::string(UTILITY_ENDPOINT_PATH) + "/" +std::string(ENDPOINT_PATH_COUNTRIES); }
2121
template<> inline std::string endpoint<LicensingModel>() { return std::string(UTILITY_ENDPOINT_PATH) + "/" + std::string(ENDPOINT_PATH_LICENSING_MODELS); }
2222
template<> inline std::string endpoint<LicenseType>() { return std::string(UTILITY_ENDPOINT_PATH) + "/" + std::string(ENDPOINT_PATH_LICENSE_TYPES); }
23+
template<> inline std::string endpoint<Bundle>() { return std::string(BUNDLE_ENDPOINT_PATH); }
2324

2425
template<typename M>
2526
void getEntity(Context& ctx, M& mapper, const std::string& number) {

0 commit comments

Comments
 (0)