fixed user-based translation

This commit is contained in:
Brian Long 2022-06-03 13:53:05 +01:00
parent 17594dc520
commit dd9d5735bf
2 changed files with 79 additions and 33 deletions

3
.gitignore vendored
View File

@ -7,3 +7,6 @@ pom.xml.versionsBackup
.project
.classpath
# Codegen
.apt_generated
.apt_generated_tests

View File

@ -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;
}
String fileOrgName = jsonGroup.get("name").asText();
this.logger.trace("Found organization '{}' in APS App descriptor", fileOrgName);
private boolean translateIdentities(JsonNode descriptor) {
boolean changed = false;
if (this.apsOrgIndex.containsKey(fileOrgName)) {
long fileOrgId = jsonGroup.get("id").asLong();
GroupLight apsOrg = this.apsOrgIndex.get(fileOrgName);
Iterator<JsonNode> i = descriptor.get("definition").get("publishIdentityInfo").iterator();
while (i.hasNext()) {
JsonNode jsonIdentityInfo = i.next();
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);
String identityType = jsonIdentityInfo.get("type").asText();
if (identityType == null) {
this.logger.warn("Found identity with no type; ignoring ...");
continue;
}
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;
}
}