fixed NPE for share-model

This commit is contained in:
Brian Long 2022-05-13 22:39:37 +01:00
parent a2befd7dbd
commit adb43d46b0

View File

@ -28,6 +28,9 @@ import com.inteligr8.maven.aps.modeling.util.Index;
@Component( role = org.apache.maven.plugin.Mojo.class ) @Component( role = org.apache.maven.plugin.Mojo.class )
public class ApsShareGoal extends ApsAddressibleGoal { public class ApsShareGoal extends ApsAddressibleGoal {
@Parameter( property = "aps-model.share.modelName" )
protected String modelName;
@Parameter( property = "aps-model.share.readers" ) @Parameter( property = "aps-model.share.readers" )
protected String readers; protected String readers;
@ -66,7 +69,6 @@ public class ApsShareGoal extends ApsAddressibleGoal {
@Override @Override
public void executeEnabled() throws MojoExecutionException, MojoFailureException { public void executeEnabled() throws MojoExecutionException, MojoFailureException {
this.getLog().info("editors: " + this.editors);
this.normalizeParameters(); this.normalizeParameters();
this.buildIdentityIndex(); this.buildIdentityIndex();
@ -82,9 +84,14 @@ public class ApsShareGoal extends ApsAddressibleGoal {
private void shareModels(ModelsApi.ModelType modelType, Set<String> readers, Set<String> editors) { private void shareModels(ModelsApi.ModelType modelType, Set<String> readers, Set<String> editors) {
ResultListDataRepresentation models = this.getApsApi().getModelsApi().get(null, null, modelType.getId(), null); ResultListDataRepresentation models = this.getApsApi().getModelsApi().get(null, null, modelType.getId(), null);
if (models.getData() == null)
return;
for (Datum datum : models.getData()) { for (Datum datum : models.getData()) {
Number modelId = (Number)datum.getAdditionalProperties().get("id"); Number modelId = (Number)datum.getAdditionalProperties().get("id");
String modelName = (String)datum.getAdditionalProperties().get("name"); String modelName = (String)datum.getAdditionalProperties().get("name");
if (this.modelName != null && !this.modelName.equals(modelName))
continue;
Set<String> groupsAddressed = new HashSet<>(); Set<String> groupsAddressed = new HashSet<>();
Set<String> readersUnaddressed = new HashSet<>(readers); Set<String> readersUnaddressed = new HashSet<>(readers);
@ -92,29 +99,52 @@ public class ApsShareGoal extends ApsAddressibleGoal {
ShareInfoRequest changeRequest = new ShareInfoRequest(); ShareInfoRequest changeRequest = new ShareInfoRequest();
ResultList<SharePermission> shares = this.getApsApi().getShareApi().getShareInfo(modelId.toString()); ResultList<SharePermission> shares = this.getApsApi().getShareApi().getShareInfo(modelId.toString());
if (shares.getData() != null) {
for (SharePermission share : shares.getData()) { for (SharePermission share : shares.getData()) {
if (share.getGroup() != null) { if (share.getGroup() != null) {
groupsAddressed.add(share.getGroup().getName()); groupsAddressed.add(share.getGroup().getName());
this.analyzeChanges(modelName, readers, editors, share, changeRequest);
} else if (share.getPerson() != null) {
this.getLog().debug("Person-based model sharing not supported at this time; ignoring");
}
}
}
readersUnaddressed.removeAll(groupsAddressed);
for (String reader : readersUnaddressed)
this.analyzeChanges(modelName, reader, PermissionLevel.Read, changeRequest);
editorsUnaddressed.removeAll(groupsAddressed);
for (String editor : editorsUnaddressed)
this.analyzeChanges(modelName, editor, PermissionLevel.Write, changeRequest);
if (!changeRequest.getAdded().isEmpty() || !changeRequest.getUpdated().isEmpty() || !changeRequest.getRemoved().isEmpty()) {
this.getLog().info("Sharing model: " + modelType + " => '" + modelName + "'");
this.getApsApi().getShareApi().setShareInfo(modelId.toString(), changeRequest);
}
}
}
private void analyzeChanges(String modelName, Set<String> readers, Set<String> editors, SharePermission share, ShareInfoRequest changeRequest) {
if (PermissionLevel.Write.equals(share.getPermission())) { if (PermissionLevel.Write.equals(share.getPermission())) {
if (editors.contains(share.getGroup().getName())) { if (editors.contains(share.getGroup().getName())) {
this.getLog().debug("The named group '" + share.getGroup().getName() + "' is already an editor of model '" + modelName + "'"); this.getLog().debug("The named group '" + share.getGroup().getName() + "' is already an editor of model '" + modelName + "'");
// no change // no change
continue; return;
} else if (readers.contains(share.getGroup().getName())) { } else if (readers.contains(share.getGroup().getName())) {
this.getLog().debug("The named group '" + share.getGroup().getName() + "' reverting from editor to reader of model '" + modelName + "'"); this.getLog().debug("The named group '" + share.getGroup().getName() + "' reverting from editor to reader of model '" + modelName + "'");
changeRequest.getUpdated().add(new PermissionLight().withId(share.getId()).withPermission(PermissionLevel.Read)); changeRequest.getUpdated().add(new PermissionLight().withId(share.getId()).withPermission(PermissionLevel.Read));
continue; return;
} }
} else { } else {
if (editors.contains(share.getGroup().getName())) { if (editors.contains(share.getGroup().getName())) {
this.getLog().debug("The named group '" + share.getGroup().getName() + "' elevating from reader to editor of model '" + modelName + "'"); this.getLog().debug("The named group '" + share.getGroup().getName() + "' elevating from reader to editor of model '" + modelName + "'");
changeRequest.getUpdated().add(new PermissionLight().withId(share.getId()).withPermission(PermissionLevel.Write)); changeRequest.getUpdated().add(new PermissionLight().withId(share.getId()).withPermission(PermissionLevel.Write));
continue; return;
} else if (readers.contains(share.getGroup().getName())) { } else if (readers.contains(share.getGroup().getName())) {
this.getLog().debug("The named group '" + share.getGroup().getName() + "' is already an reader of model '" + modelName + "'"); this.getLog().debug("The named group '" + share.getGroup().getName() + "' is already an reader of model '" + modelName + "'");
// no change // no change
continue; return;
} }
} }
@ -125,37 +155,15 @@ public class ApsShareGoal extends ApsAddressibleGoal {
this.getLog().debug("The named group '" + share.getGroup().getName() + "' is an unregulated editor of model '" + modelName + "'"); this.getLog().debug("The named group '" + share.getGroup().getName() + "' is an unregulated editor of model '" + modelName + "'");
// not touching extra unnamed permissions // not touching extra unnamed permissions
} }
} else if (share.getPerson() != null) {
this.getLog().debug("Person-based model sharing not supported at this time; ignoring");
}
} }
readersUnaddressed.removeAll(groupsAddressed); private void analyzeChanges(String modelName, String groupName, PermissionLevel plevel, ShareInfoRequest changeRequest) {
for (String reader : readersUnaddressed) { Long groupId = this.identityIndex.getValue(groupName);
Long groupId = this.identityIndex.getValue(reader);
if (groupId == null) { if (groupId == null) {
this.getLog().warn("The named group '" + reader + "' does not exist in APS; ignoring ..."); this.getLog().warn("The named group '" + groupName + "' does not exist in APS; ignoring ...");
} else { } else {
this.getLog().debug("The named group '" + reader + "' becoming a reader of model '" + modelName + "'"); this.getLog().debug("The named group '" + groupName + "' granted the " + plevel + " role of model '" + modelName + "'");
changeRequest.getAdded().add(new PermissionLight().withGroupId(groupId).withPermission(PermissionLevel.Read)); changeRequest.getAdded().add(new PermissionLight().withGroupId(groupId).withPermission(plevel));
}
}
editorsUnaddressed.removeAll(groupsAddressed);
for (String editor : editorsUnaddressed) {
Long groupId = this.identityIndex.getValue(editor);
if (groupId == null) {
this.getLog().warn("The named group '" + editor + "' does not exist in APS; ignoring ...");
} else {
this.getLog().debug("The named group '" + editor + "' becoming an editor of model '" + modelName + "'");
changeRequest.getAdded().add(new PermissionLight().withGroupId(groupId).withPermission(PermissionLevel.Write));
}
}
if (!changeRequest.getAdded().isEmpty() || !changeRequest.getUpdated().isEmpty() || !changeRequest.getRemoved().isEmpty()) {
this.getLog().info("Sharing model: " + modelType + " => '" + modelName + "'");
this.getApsApi().getShareApi().setShareInfo(modelId.toString(), changeRequest);
}
} }
} }