From dd9d5735bf29f4a8fca2cc7b0fd5d088a62f4061 Mon Sep 17 00:00:00 2001 From: Brian Long Date: Fri, 3 Jun 2022 13:53:05 +0100 Subject: [PATCH] fixed user-based translation --- .gitignore | 3 + .../translator/ApsAppJsonTranslator.java | 109 ++++++++++++------ 2 files changed, 79 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 620a11b..fe98920 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ pom.xml.versionsBackup .project .classpath +# Codegen +.apt_generated +.apt_generated_tests diff --git a/src/main/java/com/inteligr8/maven/aps/modeling/translator/ApsAppJsonTranslator.java b/src/main/java/com/inteligr8/maven/aps/modeling/translator/ApsAppJsonTranslator.java index 78862a9..8982aab 100644 --- a/src/main/java/com/inteligr8/maven/aps/modeling/translator/ApsAppJsonTranslator.java +++ b/src/main/java/com/inteligr8/maven/aps/modeling/translator/ApsAppJsonTranslator.java @@ -2,6 +2,7 @@ package com.inteligr8.maven.aps.modeling.translator; import java.io.File; import java.io.IOException; +import java.util.Iterator; import java.util.Map; import org.slf4j.Logger; @@ -39,6 +40,16 @@ public class ApsAppJsonTranslator extends ApsOrganizationHandler implements ApsF boolean changed = false; JsonNode descriptor = ModelUtil.getInstance().readJson(file); + changed = this.translateModels(descriptor) || changed; + changed = this.translateIdentities(descriptor) || changed; + + if (changed) + ModelUtil.getInstance().writeJson(descriptor, file, false); + } + + private boolean translateModels(JsonNode descriptor) { + boolean changed = false; + for (JsonNode _jsonModel : descriptor.get("definition").get("models")) { ObjectNode jsonModel = (ObjectNode)_jsonModel; @@ -77,45 +88,77 @@ public class ApsAppJsonTranslator extends ApsOrganizationHandler implements ApsF // changed = true; } - for (JsonNode _jsonIdentityInfo : descriptor.get("definition").get("publishIdentityInfo")) { - ObjectNode jsonGroup = (ObjectNode)_jsonIdentityInfo.get("group"); + return changed; + } + + private boolean translateIdentities(JsonNode descriptor) { + boolean changed = false; + + Iterator i = descriptor.get("definition").get("publishIdentityInfo").iterator(); + while (i.hasNext()) { + JsonNode jsonIdentityInfo = i.next(); - String fileOrgName = jsonGroup.get("name").asText(); - this.logger.trace("Found organization '{}' in APS App descriptor", fileOrgName); + String identityType = jsonIdentityInfo.get("type").asText(); + if (identityType == null) { + this.logger.warn("Found identity with no type; ignoring ..."); + continue; + } - if (this.apsOrgIndex.containsKey(fileOrgName)) { - long fileOrgId = jsonGroup.get("id").asLong(); - GroupLight apsOrg = this.apsOrgIndex.get(fileOrgName); - - if (apsOrg == null) { - this.logger.debug("The organization '{}' does not exist in APS; adding to APS", fileOrgName); - long apsGroupId = this.createOrganization(fileOrgName); - jsonGroup.put("id", apsGroupId); + switch (identityType.toLowerCase()) { + case "user": + this.logger.warn("Found 'user' identity, which should never be used for portablility; removing ..."); changed = true; - } else if (fileOrgId != apsOrg.getId()) { - this.logger.debug("The organization '{}' exists in APS with ID {}; changing descriptor", fileOrgName, apsOrg.getId()); - jsonGroup.put("id", apsOrg.getId()); - changed = true; - } else { - this.logger.trace("The organization '{}' ID does not change; leaving unchanged", fileOrgName); - } - - if (jsonGroup.has("parentGroupId")) { - jsonGroup.put("parentGroupId", 0); - changed = true; - } -// if (jsonGroup.has("parentGroupId")) { -// jsonGroup.remove("parentGroupId"); -// changed = true; -// } - } else { - this.logger.warn("The organization '{}' does not exist in APS", fileOrgName); - // FIXME create the organization + i.remove(); + break; + case "group": + changed = this.translateGroup(jsonIdentityInfo) || changed; + break; + default: + this.logger.warn("Found '{}' identity, which is not expected; ignoring ...", identityType); } } - if (changed) - ModelUtil.getInstance().writeJson(descriptor, file, false); + return changed; + } + + private boolean translateGroup(JsonNode jsonIdentityInfo) { + boolean changed = false; + ObjectNode jsonGroup = (ObjectNode)jsonIdentityInfo.get("group"); + + String fileOrgName = jsonGroup.get("name").asText(); + this.logger.trace("Found organization '{}' in APS App descriptor", fileOrgName); + + if (this.apsOrgIndex.containsKey(fileOrgName)) { + long fileOrgId = jsonGroup.get("id").asLong(); + GroupLight apsOrg = this.apsOrgIndex.get(fileOrgName); + + if (apsOrg == null) { + this.logger.debug("The organization '{}' does not exist in APS; adding to APS", fileOrgName); + long apsGroupId = this.createOrganization(fileOrgName); + jsonGroup.put("id", apsGroupId); + changed = true; + } else if (fileOrgId != apsOrg.getId()) { + this.logger.debug("The organization '{}' exists in APS with ID {}; changing descriptor", fileOrgName, apsOrg.getId()); + jsonGroup.put("id", apsOrg.getId()); + changed = true; + } else { + this.logger.trace("The organization '{}' ID does not change; leaving unchanged", fileOrgName); + } + + if (jsonGroup.has("parentGroupId")) { + jsonGroup.put("parentGroupId", 0); + changed = true; + } +// if (jsonGroup.has("parentGroupId")) { +// jsonGroup.remove("parentGroupId"); +// changed = true; +// } + } else { + this.logger.warn("The organization '{}' does not exist in APS", fileOrgName); + // FIXME create the organization + } + + return changed; } }