automatically adding missing organizations

This commit is contained in:
Brian Long 2022-05-03 15:55:24 +01:00
parent 0c9dff2e23
commit 32d4e807a1
5 changed files with 91 additions and 32 deletions

View File

@ -9,25 +9,26 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.inteligr8.alfresco.activiti.ApsPublicRestApi;
import com.inteligr8.alfresco.activiti.model.GroupLight;
import com.inteligr8.maven.aps.modeling.util.Index;
import com.inteligr8.maven.aps.modeling.util.ModelUtil;
public class ApsAppJsonTranslator implements ApsFileTranslator {
public class ApsAppJsonTranslator extends ApsOrganizationHandler implements ApsFileTranslator {
private final Logger logger = LoggerFactory.getLogger(ApsAppJsonTranslator.class);
private Map<String, GroupLight> apsOrgIndex;
private Index<Long, String> apsProcessIndex;
private Index<Long, String> fileProcessIndex;
private final Map<String, GroupLight> apsOrgIndex;
private final Index<Long, String> apsProcessIndex;
private final Index<Long, String> fileProcessIndex;
public ApsAppJsonTranslator(
ApsPublicRestApi api,
Map<String, GroupLight> apsOrgIndex,
Index<Long, String> apsProcessIndex,
Index<Long, String> fileProcessIndex) {
super(api, apsOrgIndex);
this.apsOrgIndex = apsOrgIndex;
this.apsProcessIndex = apsProcessIndex;
this.fileProcessIndex = fileProcessIndex;
}
@ -84,11 +85,16 @@ public class ApsAppJsonTranslator implements ApsFileTranslator {
if (this.apsOrgIndex.containsKey(fileOrgName)) {
long fileOrgId = jsonGroup.get("id").asLong();
long apsOrgId = this.apsOrgIndex.get(fileOrgName).getId();
GroupLight apsOrg = this.apsOrgIndex.get(fileOrgName);
if (fileOrgId != apsOrgId) {
this.logger.debug("The organization '{}' exists in APS with ID {}; changing descriptor", fileOrgName, apsOrgId);
jsonGroup.put("id", apsOrgId);
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);

View File

@ -95,6 +95,7 @@ public class ApsAppTranslator implements ApsAppCrawlable {
throw new IllegalStateException("The indexes are never built");
return new ApsAppJsonTranslator(
this.api,
this.apsOrgIndex,
this.apsProcessIndex,
this.fileProcessIndex);
@ -115,6 +116,7 @@ public class ApsAppTranslator implements ApsAppCrawlable {
throw new IllegalStateException("The indexes are never built");
return new ApsProcessJsonTranslator(
this.api,
this.apsProcessIndex,
this.apsOrgIndex,
this.apsFormIndex);
@ -126,6 +128,7 @@ public class ApsAppTranslator implements ApsAppCrawlable {
throw new IllegalStateException("The indexes are never built");
return new ApsProcessBpmnTranslator(
this.api,
this.apsProcessIndex,
this.apsOrgIndex,
this.apsFormIndex,

View File

@ -0,0 +1,35 @@
package com.inteligr8.maven.aps.modeling.translator;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.inteligr8.alfresco.activiti.ApsPublicRestApi;
import com.inteligr8.alfresco.activiti.model.Group;
import com.inteligr8.alfresco.activiti.model.GroupLight;
public class ApsOrganizationHandler {
private final Logger logger = LoggerFactory.getLogger(ApsOrganizationHandler.class);
private final ApsPublicRestApi api;
private final Map<String, GroupLight> apsOrgIndex;
public ApsOrganizationHandler(
ApsPublicRestApi api,
Map<String, GroupLight> apsOrgIndex) {
this.api = api;
this.apsOrgIndex = apsOrgIndex;
}
protected long createOrganization(String groupName) {
this.logger.info("Creating organization '{}' in APS", groupName);
GroupLight group = this.api.getAdminApi().createGroup(new Group()
.withName(groupName)
.withType(1L)); // an organization, not capability
this.apsOrgIndex.put(groupName, group);
return group.getId();
}
}

View File

@ -22,11 +22,12 @@ import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.inteligr8.alfresco.activiti.ApsPublicRestApi;
import com.inteligr8.alfresco.activiti.model.GroupLight;
import com.inteligr8.maven.aps.modeling.util.Index;
import com.inteligr8.maven.aps.modeling.util.ModelUtil;
public class ApsProcessBpmnTranslator implements ApsFileTranslator {
public class ApsProcessBpmnTranslator extends ApsOrganizationHandler implements ApsFileTranslator {
private static final String NAMESPACE_ACTIVITI = "http://activiti.org/bpmn";
private static final String NAMESPACE_ACTIVITI_MODELER = "http://activiti.com/modeler";
@ -38,14 +39,15 @@ public class ApsProcessBpmnTranslator implements ApsFileTranslator {
private final Index<Long, String> fileFormIndex;
public ApsProcessBpmnTranslator(
ApsPublicRestApi api,
Index<Long, String> apsProcessIndex,
Map<String, GroupLight> apsOrgIndex,
Index<Long, String> apsFormIndex,
Index<Long, String> fileFormIndex) {
super(api, apsOrgIndex);
this.apsIndex = apsProcessIndex;
this.apsOrgIndex = apsOrgIndex;
this.apsFormIndex = apsFormIndex;
this.fileFormIndex = fileFormIndex;
}
@ -141,22 +143,26 @@ public class ApsProcessBpmnTranslator implements ApsFileTranslator {
String groupName = groupNameElement.getTextContent().trim();
this.logger.trace("Found '{}' candidate group in the APS Process BPMN model", groupName);
if (this.apsOrgIndex.containsKey(groupName)) {
long apsOrgId = this.apsOrgIndex.get(groupName).getId();
groupIdsList.add(apsOrgId);
if (apsOrgId != orgId) {
this.logger.debug("The organization '{}' exists in APS with ID {}; changing model", groupName, apsOrgId);
groupNameElement = (Element)groupNameElement.getOwnerDocument().renameNode(groupNameElement,
groupNameElement.getNamespaceURI(), "modeler:group-info-name-" + apsOrgId);
changed = true;
} else {
this.logger.trace("The organization '{}' ID does not change; leaving unchanged", groupName);
}
GroupLight apsOrg = this.apsOrgIndex.get(groupName);
long apsOrgId = 0L;
if (apsOrg == null) {
this.logger.debug("The organization '{}' does not exist in APS; creating it ...", groupName);
apsOrgId = this.createOrganization(groupName);
} else {
// FIXME automatically add the group?
this.logger.debug("The organization '{}' does not exist in APS; leaving unchanged", groupName);
groupIdsList.add(orgId);
apsOrgId = apsOrg.getId();
}
if (apsOrgId != orgId) {
this.logger.debug("The organization '{}' exists in APS with ID {}; changing model", groupName, apsOrgId);
groupNameElement = (Element)groupNameElement.getOwnerDocument().renameNode(groupNameElement,
groupNameElement.getNamespaceURI(), "modeler:group-info-name-" + apsOrgId);
groupIdsList.add(apsOrgId);
changed = true;
} else {
this.logger.trace("The organization '{}' ID does not change; leaving unchanged", groupName);
groupIdsList.add(apsOrgId);
}
}

View File

@ -10,11 +10,12 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.inteligr8.alfresco.activiti.ApsPublicRestApi;
import com.inteligr8.alfresco.activiti.model.GroupLight;
import com.inteligr8.maven.aps.modeling.util.Index;
import com.inteligr8.maven.aps.modeling.util.ModelUtil;
public class ApsProcessJsonTranslator implements ApsFileTranslator {
public class ApsProcessJsonTranslator extends ApsOrganizationHandler implements ApsFileTranslator {
private final Logger logger = LoggerFactory.getLogger(ApsProcessJsonTranslator.class);
private final Index<Long, String> apsIndex;
@ -22,9 +23,11 @@ public class ApsProcessJsonTranslator implements ApsFileTranslator {
private final Index<Long, String> apsFormIndex;
public ApsProcessJsonTranslator(
ApsPublicRestApi api,
Index<Long, String> apsProcessIndex,
Map<String, GroupLight> apsOrgIndex,
Index<Long, String> apsFormIndex) {
super(api, apsOrgIndex);
this.apsIndex = apsProcessIndex;
this.apsOrgIndex = apsOrgIndex;
this.apsFormIndex = apsFormIndex;
@ -107,10 +110,16 @@ public class ApsProcessJsonTranslator implements ApsFileTranslator {
if (this.apsOrgIndex.containsKey(fileOrgName)) {
long fileOrgId = jsonCandidateGroup.get("id").asLong();
long apsOrgId = this.apsOrgIndex.get(fileOrgName).getId();
if (apsOrgId != fileOrgId) {
this.logger.debug("The organization '{}' exists in APS with ID {}; changing descriptor", fileOrgName, apsOrgId);
jsonCandidateGroup.put("id", apsOrgId);
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);
jsonCandidateGroup.put("id", apsGroupId);
return true;
} else if (apsOrg.getId() != fileOrgId) {
this.logger.debug("The organization '{}' exists in APS with ID {}; changing descriptor", fileOrgName, apsOrg.getId());
jsonCandidateGroup.put("id", apsOrg.getId());
return true;
} else {
this.logger.trace("The organization '{}' ID does not change; leaving unchanged", fileOrgName);