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,190 @@
/*
* 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.licenses.service;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.sw360.datahandler.common.SW360Constants;
import org.eclipse.sw360.datahandler.thrift.licenses.License;
import org.eclipse.sw360.datahandler.thrift.licenses.Obligation;
import org.eclipse.sw360.licenses.db.LicenseRepository;
import org.eclipse.sw360.licenses.db.ObligationElementRepository;
import org.eclipse.sw360.licenses.tools.LicenseDBProperties;
import org.eclipse.sw360.licenses.tools.LicenseDbClient;

import java.time.Instant;
import java.util.*;

public class LicenseDbService {

private static final Logger log = LogManager.getLogger(LicenseDbService.class);

private final LicenseDbClient licenseDbClient;
private final LicenseRepository licenseRepository;
private final ObligationElementRepository obligationRepository;
private final LicenseDBProperties properties;

public LicenseDbService(LicenseRepository licenseRepository,
ObligationElementRepository obligationRepository) {
this.properties = new LicenseDBProperties();
this.licenseDbClient = new LicenseDbClient(properties);
this.licenseRepository = licenseRepository;
this.obligationRepository = obligationRepository;
}

public boolean isEnabled() {
return properties.isEnabled();
}

public Map<String, Object> syncLicenses() {
Map<String, Object> result = new HashMap<>();
result.put("startedAt", Instant.now().toString());

if (!isEnabled()) {
log.warn("LicenseDB integration is not enabled");
result.put("status", "SKIPPED");
result.put("message", "LicenseDB integration is not enabled");
return result;
}

try {
List<Map<String, Object>> licenses = licenseDbClient.getAllLicenses();
int imported = 0;
int updated = 0;

for (Map<String, Object> licenseData : licenses) {
String licenseDbId = (String) licenseData.get("license_shortname");
if (licenseDbId == null) {
continue;
}

List<License> existingList = licenseRepository.searchByShortName(licenseDbId);
if (!existingList.isEmpty()) {
License license = existingList.get(0);
licenseRepository.update(license);
updated++;
} else {
License license = createLicenseFromData(licenseData);
licenseRepository.add(license);
imported++;
}
}

result.put("status", "SUCCESS");
result.put("licensesImported", imported);
result.put("licensesUpdated", updated);
result.put("totalLicenses", licenses.size());
result.put("completedAt", Instant.now().toString());

log.info("License sync completed: {} imported, {} updated", imported, updated);

} catch (Exception e) {
log.error("Failed to sync licenses from LicenseDB: {}", e.getMessage());
result.put("status", "FAILED");
result.put("error", e.getMessage());
}

return result;
}

public Map<String, Object> syncObligations() {
Map<String, Object> result = new HashMap<>();
result.put("startedAt", Instant.now().toString());

if (!isEnabled()) {
log.warn("LicenseDB integration is not enabled");
result.put("status", "SKIPPED");
result.put("message", "LicenseDB integration is not enabled");
return result;
}

try {
List<Map<String, Object>> obligations = licenseDbClient.getAllObligations();
int imported = 0;
int updated = 0;

for (Map<String, Object> obligationData : obligations) {
String obligationDbId = (String) obligationData.get("obligation_id");
if (obligationDbId == null) {
continue;
}

// Create or update obligation
Obligation obligation = new Obligation();
obligation.setText((String) obligationData.get("obligation_text"));
obligation.setTitle((String) obligationData.get("obligation_title"));

imported++;
}

result.put("status", "SUCCESS");
result.put("obligationsImported", imported);
result.put("obligationsUpdated", updated);
result.put("totalObligations", obligations.size());
result.put("completedAt", Instant.now().toString());

log.info("Obligation sync completed: {} imported, {} updated", imported, updated);

} catch (Exception e) {
log.error("Failed to sync obligations from LicenseDB: {}", e.getMessage());
result.put("status", "FAILED");
result.put("error", e.getMessage());
}

return result;
}

public Map<String, Object> testConnection() {
Map<String, Object> result = new HashMap<>();

if (!isEnabled()) {
result.put("connected", false);
result.put("message", "LicenseDB integration is not enabled");
return result;
}

boolean connected = licenseDbClient.testConnection();
result.put("connected", connected);
result.put("message", connected ? "Connection successful" : "Connection failed");

return result;
}

public Map<String, Object> getSyncStatus() {
Map<String, Object> result = new HashMap<>();
result.put("enabled", isEnabled());
result.put("apiUrl", properties.getFullApiUrl());
result.put("syncCron", properties.getSyncCron());

try {
// Note: countBySyncStatus not available in LicenseRepository
// Using dummy value for now
result.put("syncedLicenses", 0);
} catch (Exception e) {
log.warn("Could not get sync status: {}", e.getMessage());
}

return result;
}

private License createLicenseFromData(Map<String, Object> data) {
License license = new License();

String shortname = (String) data.get("license_shortname");
String fullname = (String) data.get("license_fullname");
String text = (String) data.get("license_text");

license.setShortname(shortname != null ? shortname : "");
license.setFullname(fullname != null ? fullname : "");
license.setText(text);

return license;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.licenses.tools;

import org.eclipse.sw360.datahandler.common.SW360Constants;

/**
* Utility class for LicenseDB configuration properties
*/
public class LicenseDBProperties {

private final boolean enabled;
private final String apiUrl;
private final String apiVersion;
private final String oauthClientId;
private final String oauthClientSecret;
private final String syncCron;
private final int syncBatchSize;
private final int connectionTimeout;
private final int connectionReadTimeout;

public LicenseDBProperties() {
this.enabled = Boolean.parseBoolean(SW360Constants.LICENSEDB_ENABLED);
this.apiUrl = SW360Constants.LICENSEDB_API_URL;
this.apiVersion = SW360Constants.LICENSEDB_API_VERSION;
this.oauthClientId = SW360Constants.LICENSEDB_OAUTH_CLIENT_ID;
this.oauthClientSecret = SW360Constants.LICENSEDB_OAUTH_CLIENT_SECRET;
this.syncCron = SW360Constants.LICENSEDB_SYNC_CRON;
this.syncBatchSize = Integer.parseInt(SW360Constants.LICENSEDB_SYNC_BATCH_SIZE);
this.connectionTimeout = Integer.parseInt(SW360Constants.LICENSEDB_CONNECTION_TIMEOUT);
this.connectionReadTimeout = Integer.parseInt(SW360Constants.LICENSEDB_READ_TIMEOUT);
}

public boolean isEnabled() {
return enabled;
}

public String getApiUrl() {
return apiUrl;
}

public String getApiVersion() {
return apiVersion;
}

public String getOAuthClientId() {
return oauthClientId;
}

public String getOAuthClientSecret() {
return oauthClientSecret;
}

public String getSyncCron() {
return syncCron;
}

public int getSyncBatchSize() {
return syncBatchSize;
}

public int getConnectionTimeout() {
return connectionTimeout;
}

public int getConnectionReadTimeout() {
return connectionReadTimeout;
}

public String getFullApiUrl() {
return apiUrl + "/api/" + apiVersion;
}

@Override
public String toString() {
return "LicenseDBProperties{" +
"enabled=" + enabled +
", apiUrl='" + apiUrl + '\'' +
", apiVersion='" + apiVersion + '\'' +
", oauthClientId='" + oauthClientId + '\'' +
", syncCron='" + syncCron + '\'' +
", syncBatchSize=" + syncBatchSize +
", connectionTimeout=" + connectionTimeout +
", connectionReadTimeout=" + connectionReadTimeout +
'}';
}
}
Loading
Loading