Company options values, portfolio options values migration done

This commit is contained in:
2026-08-12 09:38:18 +05:30
parent c835a9deb0
commit 61bea5fb6e
17 changed files with 616 additions and 22 deletions

View File

@@ -1,11 +1,14 @@
package matrix.services.commons;
import com.cygnus.db.CygnusDbExecutor;
import com.cygnus.client.CloudIdentityClient;
import com.cygnus.client.model.CloudDataItem;
import jakarta.servlet.http.HttpSession;
import lib.models.Option;
import lib.models.UserSession;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -23,16 +26,19 @@ public class CommonService {
Pattern.CASE_INSENSITIVE);
private final CygnusDbExecutor dbExecutor;
private final CloudIdentityClient cloudIdentityClient;
@Autowired
public CommonService(CygnusDbExecutor dbExecutor) {
public CommonService(CygnusDbExecutor dbExecutor, CloudIdentityClient cloudIdentityClient) {
this.dbExecutor = Objects.requireNonNull(dbExecutor, "dbExecutor");
this.cloudIdentityClient = Objects.requireNonNull(cloudIdentityClient, "cloudIdentityClient");
}
/* Used by the session-only unit tests. Production construction is handled
by Spring through the CygnusDbExecutor constructor above. */
public CommonService() {
this.dbExecutor = null;
this.cloudIdentityClient = null;
}
public Session getSession(HttpSession httpSession) {
@@ -138,4 +144,22 @@ public class CommonService {
Object[] parameters = queryParams == null ? new Object[0] : queryParams;
return dbExecutor.query(queryId, parameters, Option.class);
}
public List<Option> getCloudOptions(String scope, Map<String, Object> data) {
if (cloudIdentityClient == null) {
throw new IllegalStateException("CloudIdentityClient is not configured");
}
return cloudIdentityClient.fetchData(scope, data)
.map(items -> items.stream().map(this::option).toList())
.blockOptional()
.orElseGet(List::of);
}
private Option option(CloudDataItem item) {
Option option = new Option();
option.setValue(item.value());
option.setLabel(item.label());
option.setGroup(item.group());
return option;
}
}

View File

@@ -2,7 +2,6 @@ package matrix.services.edp;
import com.cygnus.db.CygnusDbExecutor;
import com.cygnus.db.RowView;
import com.cygnus.db.SqlArrayParameter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -10,6 +9,7 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;
import java.sql.SQLException;
import java.sql.Date;
@@ -60,8 +60,8 @@ public class PunchingService {
private static final String ADD_CASE_TEMPLATE = "ADDCASE";
private static final Pattern DYNAMIC_FIELD = Pattern.compile(
"(?:field(?:[1-9]|1[0-2]|150(?:_2)?)|dtfield1)", Pattern.CASE_INSENSITIVE);
private static final SqlArrayParameter GENERAL_OPTION_TYPES = SqlArrayParameter.text("CITY", "APPTYPE");
private static final SqlArrayParameter PORTFOLIO_OPTION_TYPES = SqlArrayParameter.text("CATEGORY", "PRODUCT");
private static final List<String> GENERAL_OPTION_TYPES = List.of("CITY", "APPTYPE");
private static final List<String> PORTFOLIO_OPTION_TYPES = List.of("CATEGORY", "PRODUCT");
private final CommonService commonService;
private final CygnusDbExecutor dbExecutor;
@@ -467,17 +467,43 @@ public class PunchingService {
Short branchId, Short companyId, Short portfolioId) {
Map<String, List<Option>> options = new LinkedHashMap<>();
addOptions(options, PORTFOLIO, commonService.getOptions(
3, new Object[] { branchId, ACTIVE }));
List<Option> portfolios = commonService.getOptions(
3, new Object[] { branchId, ACTIVE });
addOptions(options, PORTFOLIO, portfolios);
addOptions(options, BRANCH, commonService.getOptions(
4, new Object[] { portfolioId, ACTIVE }));
addGroupedOptions(options, commonService.getOptions(
5, new Object[] { companyId, GENERAL_OPTION_TYPES }));
addGroupedOptions(options, commonService.getOptions(
70, new Object[] { portfolioId, PORTFOLIO_OPTION_TYPES }));
addGroupedOptions(options, commonService.getCloudOptions(
"company-options", Map.of(
"companyId", companyId,
"descriptions", GENERAL_OPTION_TYPES)));
addGroupedOptions(options, commonService.getCloudOptions(
"portfolio-options", Map.of(
"companyId", companyId,
"branchId", branchId,
"portfolioReference", portfolioReference(portfolios, portfolioId),
"descriptions", PORTFOLIO_OPTION_TYPES)));
return freezeOptions(options);
}
private UUID portfolioReference(List<Option> portfolios, Short portfolioId) {
String reference = portfolios.stream()
.filter(option -> option != null
&& String.valueOf(option.getValue()).equals(String.valueOf(portfolioId)))
.map(Option::getDescription)
.filter(candidate -> candidate != null && !candidate.isBlank())
.findFirst()
.orElseThrow(() -> new ApplicationException(
ApplicationMessage.APPLICATION_ACCESS_DENIED,
"Portfolio reference is unavailable"));
try {
return UUID.fromString(reference);
} catch (IllegalArgumentException exception) {
throw new ApplicationException(
ApplicationMessage.APPLICATION_ACCESS_DENIED,
"Portfolio reference is invalid");
}
}
public CasePunching prepareApplicationEdit(
Short portfolioId,
String documentCaseId,

View File

@@ -6,7 +6,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import com.cygnus.db.CygnusDbExecutor;
import com.cygnus.db.RowView;
import com.cygnus.db.SqlArrayParameter;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
@@ -131,19 +130,12 @@ class PunchingServiceTest {
Map<String, List<Option>> options = service.loadOptions((short) 5, (short) 1, (short) 38);
assertEquals(List.of(3, 4, 5, 70),
assertEquals(List.of(3, 4),
commonService.calls.stream().map(Call::queryId).toList());
assertArrayEquals(new Object[] { (short) 5, (short) 1 },
commonService.calls.get(0).parameters());
assertArrayEquals(new Object[] { (short) 38, (short) 1 },
commonService.calls.get(1).parameters());
assertEquals((short) 1, commonService.calls.get(2).parameters()[0]);
assertArrayEquals(new Object[] { "CITY", "APPTYPE" },
((SqlArrayParameter) commonService.calls.get(2).parameters()[1]).values());
assertEquals((short) 38, commonService.calls.get(3).parameters()[0]);
assertArrayEquals(new Object[] { "CATEGORY", "PRODUCT" },
((SqlArrayParameter) commonService.calls.get(3).parameters()[1]).values());
assertEquals("Portfolio", options.get("portfolio.").getFirst().getLabel());
assertEquals("Branch", options.get("branch.").getFirst().getLabel());
assertEquals("City", options.get("city.").getFirst().getLabel());
@@ -196,12 +188,20 @@ class PunchingServiceTest {
public List<Option> getOptions(Integer queryId, Object[] queryParams) {
calls.add(new Call(queryId, queryParams.clone()));
return switch (queryId) {
case 3 -> List.of(option(38, "Portfolio", "portfolio."));
case 3 -> List.of(option(38, "Portfolio", "portfolio.",
"88e0cb4a-3b4d-401c-b11b-fe4117842fef"));
case 4 -> List.of(option(7, "Branch", "branch."));
case 5 -> List.of(
default -> List.of();
};
}
@Override
public List<Option> getCloudOptions(String scope, Map<String, Object> data) {
return switch (scope) {
case "company-options" -> List.of(
option("DEL", "City", "city."),
option("APPLICANT", "Applicant", "apptype."));
case 70 -> List.of(
case "portfolio-options" -> List.of(
option("DIRECTOR", "Category", "category."),
option("AUTO", "Product", "product."));
default -> List.of();
@@ -210,10 +210,15 @@ class PunchingServiceTest {
}
private static Option option(Object value, String label, String group) {
return option(value, label, group, null);
}
private static Option option(Object value, String label, String group, String description) {
Option option = new Option();
option.setValue(value);
option.setLabel(label);
option.setGroup(group);
option.setDescription(description);
return option;
}