package com.alexanderlogistics.metrics; import java.util.Objects; import org.imixs.workflow.ItemCollection; import org.imixs.workflow.engine.DocumentService; import jakarta.annotation.security.DeclareRoles; import jakarta.annotation.security.RunAs; import jakarta.ejb.Stateless; import jakarta.ejb.TransactionAttribute; import jakarta.ejb.TransactionAttributeType; import jakarta.inject.Inject; /** * The MetricDataService provides methods to lookup and update metric data * objects. * The service runs with manager access. * * */ @DeclareRoles({ "org.imixs.ACCESSLEVEL.MANAGERACCESS" }) @RunAs("org.imixs.ACCESSLEVEL.MANAGERACCESS") @Stateless public class MetricDataService { @Inject DocumentService documentService; /** * Runs with Manager access * * @param metricData */ public void saveMetric(ItemCollection metricData) { documentService.save(metricData); } /** * This helper method reads a 'dirty' workitem in a new transaction. This is * used for calculating the new metric values * * @param uniqueId */ @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW) public ItemCollection readDirtyWorkitem(String uniqueId) { ItemCollection dirtyInvoice = documentService.load(uniqueId); return dirtyInvoice; } /** * Builds the metric hash key by the invoice attributes. The returned key can be * used for caching the metric. * * @param invoice - The invoice ItemCollection containing the necessary * attributes * @return A unique hash string based on the invoice attributes * @throws IllegalArgumentException if the invoice is null */ public static String buildKeyByInvoice(ItemCollection invoice) { // Validate input Objects.requireNonNull(invoice, "Invoice must not be null"); String country = invoice.getItemValueString("invoice.country"); String currency = invoice.getItemValueString("invoice.currency"); String department = invoice.getItemValueString("space.name"); // Concatenate the values and create a hash String combinedValue = String.format("%s::%s::%s", country, currency, department); String hash = String.valueOf(combinedValue.hashCode()); // a hash can start with '-' which we need to avoid and create a alphanumeric // key instead! return "HASH" + hash; } }