Skip to content

Commit

Permalink
NLIC-1980: Group models & packages in a license bundle (#40)
Browse files Browse the repository at this point in the history
* add Bundle functionality

* add Bundle functionality

* rename "licenseTemplatesNumbers" to "licenseTemplateNumbers"

* remove BundleObtainParameters.java

---------

Co-authored-by: Viacheslav Rudkovskyi <viachaslau.rudkovski@labs64.de>
  • Loading branch information
v-rudkovskiy and Viacheslav Rudkovskyi committed Feb 23, 2024
1 parent bd5e210 commit 34bcf7f
Show file tree
Hide file tree
Showing 13 changed files with 771 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,11 @@ public static final class Notification {
public static final String PAYLOAD = "payload";
}

public static final class Bundle {
public static final String ENDPOINT_PATH = "bundle";
public static final String ENDPOINT_OBTAIN_PATH = "obtain";
public static final String DESCRIPTION = "description";
public static final String LICENSE_TEMPLATE_NUMBERS = "licenseTemplateNumbers";
}
// CHECKSTYLE:ON
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import java.util.Map;

import com.labs64.netlicensing.domain.entity.Bundle;
import com.labs64.netlicensing.domain.entity.Country;
import com.labs64.netlicensing.domain.entity.License;
import com.labs64.netlicensing.domain.entity.LicenseTemplate;
Expand All @@ -39,6 +40,7 @@
import com.labs64.netlicensing.schema.context.Item;
import com.labs64.netlicensing.schema.context.Netlicensing;
import com.labs64.netlicensing.schema.converter.Converter;
import com.labs64.netlicensing.schema.converter.ItemToBundleConverter;
import com.labs64.netlicensing.schema.converter.ItemToCountryConverter;
import com.labs64.netlicensing.schema.converter.ItemToLicenseConverter;
import com.labs64.netlicensing.schema.converter.ItemToLicenseTemplateConverter;
Expand Down Expand Up @@ -75,6 +77,7 @@ public class EntityFactory {
entityToConverterMap.put(LicensingModelProperties.class, ItemToLicensingModelPropertiesConverter.class);
entityToConverterMap.put(LicenseTypeProperties.class, ItemToLicenseTypePropertiesConverter.class);
entityToConverterMap.put(Notification.class, ItemToNotificationConverter.class);
entityToConverterMap.put(Bundle.class, ItemToBundleConverter.class);
}

private Map<Class<?>, Converter<Item, ?>> convertersCache;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.labs64.netlicensing.domain.entity;

import java.math.BigDecimal;
import java.util.List;

import com.labs64.netlicensing.domain.vo.Currency;

public interface Bundle extends BaseEntity {
void setName(String name);

String getName();

void setPrice(BigDecimal price);

BigDecimal getPrice();

void setCurrency(Currency currency);

Currency getCurrency();

void setDescription(String description);

String getDescription();

void setLicenseTemplateNumbers(List<String> licenseTemplateNumbers);

List<String> getLicenseTemplateNumbers();

void addLicenseTemplateNumber(String licenseTemplateNumber);

void removeLicenseTemplateNumber(String licenseTemplateNumber);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.labs64.netlicensing.domain.entity.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.MultivaluedMap;

import com.labs64.netlicensing.domain.Constants;
import com.labs64.netlicensing.domain.entity.Bundle;
import com.labs64.netlicensing.domain.vo.Currency;

public class BundleImpl extends BaseEntityImpl implements Bundle {
private String name;

private String description;

private BigDecimal price;

private Currency currency;

private List<String> licenseTemplateNumbers;

@Override
public void setName(final String name) {
this.name = name;
}

@Override
public String getName() {
return name;
}

@Override
public void setPrice(final BigDecimal price) {
this.price = price;
}

@Override
public BigDecimal getPrice() {
return price;
}

@Override
public void setCurrency(final Currency currency) {
this.currency = currency;
}

@Override
public Currency getCurrency() {
return currency;
}

@Override
public void setDescription(final String description) {
this.description = description;
}

@Override
public String getDescription() {
return description;
}

@Override
public void setLicenseTemplateNumbers(final List<String> licenseTemplateNumbers) {
this.licenseTemplateNumbers = licenseTemplateNumbers;
}

@Override
public List<String> getLicenseTemplateNumbers() {
return licenseTemplateNumbers;
}

@Override
public void addLicenseTemplateNumber(final String licenseTemplateNumber) {
if (getLicenseTemplateNumbers() == null) {
this.licenseTemplateNumbers = new ArrayList<>();
}

this.licenseTemplateNumbers.add(licenseTemplateNumber);
}

@Override
public void removeLicenseTemplateNumber(final String licenseTemplateNumber) {
if (getLicenseTemplateNumbers() == null) {
return;
}

getLicenseTemplateNumbers().remove(licenseTemplateNumber);
}

public static List<String> getReservedProps() {
final List<String> reserved = BaseEntityImpl.getReservedProps();
reserved.add(Constants.NAME);
reserved.add(Constants.Bundle.DESCRIPTION);
reserved.add(Constants.PRICE);
reserved.add(Constants.CURRENCY);
reserved.add(Constants.Bundle.LICENSE_TEMPLATE_NUMBERS);
return reserved;
}

@Override
protected MultivaluedMap<String, Object> asPropertiesMap() {
final MultivaluedMap<String, Object> map = super.asPropertiesMap();
map.add(Constants.NAME, getName());

if (getDescription() != null) {
map.add(Constants.Bundle.DESCRIPTION, getDescription());
}

map.add(Constants.PRICE, getPrice());
map.add(Constants.CURRENCY, getCurrency());

if (getLicenseTemplateNumbers() != null) {
map.add(Constants.Bundle.LICENSE_TEMPLATE_NUMBERS, String.join(",", getLicenseTemplateNumbers()));
}

return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,10 @@ public enum TransactionSource {
/**
* Transaction for cancel recurring payment.
*/
CANCEL_RECURRING_PAYMENT
CANCEL_RECURRING_PAYMENT,

/**
* Transaction for obtain bundle.
*/
OBTAIN_BUNDLE;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* 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
*
* https://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.
*/
package com.labs64.netlicensing.schema.converter;

import java.util.Arrays;

import com.labs64.netlicensing.domain.Constants;
import com.labs64.netlicensing.domain.entity.Bundle;
import com.labs64.netlicensing.domain.entity.impl.BundleImpl;
import com.labs64.netlicensing.domain.entity.impl.LicenseTemplateImpl;
import com.labs64.netlicensing.domain.vo.Currency;
import com.labs64.netlicensing.domain.vo.Money;
import com.labs64.netlicensing.exception.ConversionException;
import com.labs64.netlicensing.schema.SchemaFunction;
import com.labs64.netlicensing.schema.context.Item;
import com.labs64.netlicensing.schema.context.Property;

/**
* Convert {@link Item} entity into {@link Bundle} object.
*/
public class ItemToBundleConverter extends ItemToEntityBaseConverter<Bundle> {

@Override
public Bundle convert(final Item source) throws ConversionException {
final Bundle target = super.convert(source);

target.setName(SchemaFunction.propertyByName(source.getProperty(), Constants.NAME).getValue());
target.setDescription(SchemaFunction.propertyByName(source.getProperty(), Constants.Bundle.DESCRIPTION).getValue());
if (SchemaFunction.propertyByName(source.getProperty(), Constants.PRICE).getValue() != null) {
final Money price = convertPrice(source.getProperty(), Constants.PRICE);
target.setPrice(price.getAmount());
target.setCurrency(Currency.valueOf(price.getCurrencyCode()));
}

final String licenseTemplateNumbers = SchemaFunction.propertyByName(source.getProperty(), Constants.Bundle.LICENSE_TEMPLATE_NUMBERS).getValue();

if (licenseTemplateNumbers != null) {
target.setLicenseTemplateNumbers(Arrays.asList(licenseTemplateNumbers.split(",")));
}

// Custom properties
for (final Property property : source.getProperty()) {
if (!LicenseTemplateImpl.getReservedProps().contains(property.getName())) {
target.getProperties().put(property.getName(), property.getValue());
}
}

return target;
}

@Override
public Bundle newTarget() {
return new BundleImpl();
}

}

0 comments on commit 34bcf7f

Please sign in to comment.