Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright Siemens AG, 2025. Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.sw360.rest.resourceserver.licensedb.transformer;

import org.eclipse.sw360.datahandler.thrift.Quadratic;
import org.eclipse.sw360.datahandler.thrift.licenses.License;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
* Transformer class to convert LicenseDB license data to SW360 License format.
*
* <p>This transformer handles the conversion of license data received from the
* LicenseDB service into the SW360 internal License data model. It maps all
* relevant fields and handles null values gracefully.</p>
*
* <p>Field mapping:</p>
* <ul>
* <li>shortname -> shortname</li>
* <li>fullname -> fullname</li>
* <li>text -> text</li>
* <li>url -> url</li>
* <li>licenseType -> licenseType</li>
* <li>osiApproved -> osiApproved</li>
* </ul>
*
* <p>Example usage:</p>
* <pre>
* LicenseDBLicense licenseDbData = licenseDBClient.getLicense("Apache-2.0");
* LicenseTransformer transformer = new LicenseTransformer();
* License sw360License = transformer.transform(licenseDbData);
* </pre>
*/
@Component
public class LicenseTransformer {

/**
* Transform LicenseDB license data to SW360 License.
*
* @param licenseDbData the license data from LicenseDB as a Map
* @return SW360 License object, or null if input is null
*/
public License transform(Map<String, Object> licenseDbData) {
if (licenseDbData == null) {
return null;
}

License license = new License();

// Map basic string fields
license.setShortname(getStringValue(licenseDbData, "shortname"));
license.setFullname(getStringValue(licenseDbData, "fullname"));
license.setText(getStringValue(licenseDbData, "text"));
license.setExternalLicenseLink(getStringValue(licenseDbData, "url"));

// Map license type database ID
String licenseTypeId = getStringValue(licenseDbData, "licenseTypeId");
if (licenseTypeId != null) {
license.setLicenseTypeDatabaseId(licenseTypeId);
}

// Map OSI approved flag to Quadratic enum
Boolean osiApproved = getBooleanValue(licenseDbData, "osiApproved");
if (osiApproved != null) {
license.setOSIApproved(osiApproved ? Quadratic.YES : Quadratic.NA);
}

return license;
}

/**
* Extract a string value from the data map.
*
* @param data the data map
* @param key the key to look up
* @return the string value, or null if not found
*/
private String getStringValue(Map<String, Object> data, String key) {
Object value = data.get(key);
return value != null ? value.toString() : null;
}

/**
* Extract a boolean value from the data map.
*
* @param data the data map
* @param key the key to look up
* @return the boolean value, or null if not found
*/
private Boolean getBooleanValue(Map<String, Object> data, String key) {
Object value = data.get(key);
if (value == null) {
return null;
}
if (value instanceof Boolean) {
return (Boolean) value;
}
return Boolean.parseBoolean(value.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright Siemens AG, 2025. Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.sw360.rest.resourceserver.licensedb.transformer;

import org.eclipse.sw360.datahandler.thrift.licenses.Obligation;
import org.eclipse.sw360.datahandler.thrift.licenses.ObligationLevel;
import org.eclipse.sw360.datahandler.thrift.licenses.ObligationType;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
* Transformer class to convert LicenseDB obligation data to SW360 Obligation format.
*
* <p>This transformer handles the conversion of obligation data received from the
* LicenseDB service into the SW360 internal Obligation data model. It maps all
* relevant fields and handles null values gracefully.</p>
*
* <p>Field mapping:</p>
* <ul>
* <li>text -> text</li>
* <li>title -> title</li>
* <li>type -> obligationType</li>
* <li>level -> obligationLevel</li>
* </ul>
*
* <p>Example usage:</p>
* <pre>
* LicenseDBObligation obligationDbData = licenseDBClient.getObligation("obligation-1");
* ObligationTransformer transformer = new ObligationTransformer();
* Obligation sw360Obligation = transformer.transform(obligationDbData);
* </pre>
*/
@Component
public class ObligationTransformer {

/**
* Transform LicenseDB obligation data to SW360 Obligation.
*
* @param obligationDbData the obligation data from LicenseDB as a Map
* @return SW360 Obligation object, or null if input is null
*/
public Obligation transform(Map<String, Object> obligationDbData) {
if (obligationDbData == null) {
return null;
}

Obligation obligation = new Obligation();

// Map basic string fields
obligation.setText(getStringValue(obligationDbData, "text"));
obligation.setTitle(getStringValue(obligationDbData, "title"));

// Map obligation type enum if present
String type = getStringValue(obligationDbData, "type");
if (type != null && !type.isEmpty()) {
try {
obligation.setObligationType(ObligationType.valueOf(type.toUpperCase()));
} catch (IllegalArgumentException e) {
// Unknown obligation type - skip field
}
}

// Map obligation level enum if present
String level = getStringValue(obligationDbData, "level");
if (level != null && !level.isEmpty()) {
try {
obligation.setObligationLevel(ObligationLevel.valueOf(level.toUpperCase()));
} catch (IllegalArgumentException e) {
// Unknown obligation level - skip field
}
}

return obligation;
}

/**
* Extract a string value from the data map.
*
* @param data the data map
* @param key the key to look up
* @return the string value, or null if not found
*/
private String getStringValue(Map<String, Object> data, String key) {
Object value = data.get(key);
return value != null ? value.toString() : null;
}
}
Loading