in client/idrepo/console/src/main/java/org/apache/syncope/client/console/batch/BatchContent.java [100:360]
public BatchContent(
final String id,
final List<T> items,
final List<IColumn<T, S>> columns,
final Collection<ActionLink.ActionType> actions,
final RestClient batchExecutor,
final String keyFieldName) {
super(id);
setup(items, columns);
for (ActionLink.ActionType action : actions) {
actionPanel.add(new ActionLink<>() {
private static final long serialVersionUID = -3722207913631435501L;
@Override
protected boolean statusCondition(final Serializable modelObject) {
return !CollectionUtils.isEmpty(items);
}
@Override
@SuppressWarnings("unchecked")
public void onClick(final AjaxRequestTarget target, final Serializable ignore) {
if (CollectionUtils.isEmpty(items)) {
throw new IllegalArgumentException("Invalid items");
}
Map<String, String> results;
try {
T singleItem = items.getFirst();
if (singleItem instanceof ExecTO) {
results = new HashMap<>();
items.forEach(item -> {
ExecTO exec = ExecTO.class.cast(item);
try {
batchExecutor.getClass().getMethod("deleteExecution", String.class).
invoke(batchExecutor, exec.getKey());
results.put(exec.getKey(), ExecStatus.SUCCESS.name());
} catch (Exception e) {
LOG.error("Error deleting execution {}", exec.getKey(), e);
results.put(exec.getKey(), ExecStatus.FAILURE.name());
}
});
} else if (singleItem instanceof StatusBean) {
AbstractAnyRestClient<?> anyRestClient = AbstractAnyRestClient.class.cast(batchExecutor);
// Group bean information by anyKey
Map<String, List<StatusBean>> beans = new HashMap<>();
items.stream().map(StatusBean.class::cast).
forEachOrdered(sb -> {
final List<StatusBean> sblist;
if (beans.containsKey(sb.getKey())) {
sblist = beans.get(sb.getKey());
} else {
sblist = new ArrayList<>();
beans.put(sb.getKey(), sblist);
}
sblist.add(sb);
});
results = new HashMap<>();
beans.forEach((key, value) -> {
String etag = anyRestClient.read(key).getETagValue();
switch (action) {
case DEPROVISION:
results.putAll(anyRestClient.deassociate(
ResourceDeassociationAction.DEPROVISION, etag, key, value));
break;
case UNASSIGN:
results.putAll(anyRestClient.deassociate(
ResourceDeassociationAction.UNASSIGN, etag, key, value));
break;
case UNLINK:
results.putAll(anyRestClient.deassociate(
ResourceDeassociationAction.UNLINK, etag, key, value));
break;
case ASSIGN:
results.putAll(anyRestClient.associate(
ResourceAssociationAction.ASSIGN, etag, key, value));
break;
case LINK:
results.putAll(anyRestClient.associate(
ResourceAssociationAction.LINK, etag, key, value));
break;
case PROVISION:
results.putAll(anyRestClient.associate(
ResourceAssociationAction.PROVISION, etag, key, value));
break;
case SUSPEND:
results.putAll(((UserRestClient) anyRestClient).suspend(etag, key, value));
break;
case REACTIVATE:
results.putAll(((UserRestClient) anyRestClient).reactivate(etag, key, value));
break;
default:
}
});
} else {
BatchRequest batch = SyncopeConsoleSession.get().batch();
UserService batchUserService = batch.getService(UserService.class);
GroupService batchGroupService = batch.getService(GroupService.class);
AnyObjectService batchAnyObjectService = batch.getService(AnyObjectService.class);
AnyService<?> batchAnyService = singleItem instanceof UserTO
? batchUserService
: singleItem instanceof GroupTO
? batchGroupService
: batchAnyObjectService;
TaskService batchTaskService = batch.getService(TaskService.class);
ReportService batchReportService = batch.getService(ReportService.class);
Set<String> deletedAnys = new HashSet<>();
switch (action) {
case MUSTCHANGEPASSWORD:
items.forEach(item -> {
UserTO user = (UserTO) item;
UserUR req = new UserUR();
req.setKey(user.getKey());
req.setMustChangePassword(new BooleanReplacePatchItem.Builder().
value(!user.isMustChangePassword()).build());
batchUserService.update(req);
});
break;
case SUSPEND:
items.forEach(item -> {
UserTO user = (UserTO) item;
StatusR req = new StatusR.Builder(user.getKey(), StatusRType.SUSPEND).
onSyncope(true).
resources(user.getResources()).
build();
batchUserService.status(req);
});
break;
case REACTIVATE:
items.forEach(item -> {
UserTO user = (UserTO) item;
StatusR req = new StatusR.Builder(user.getKey(), StatusRType.REACTIVATE).
onSyncope(true).
resources(user.getResources()).
build();
batchUserService.status(req);
});
break;
case DELETE:
items.forEach(item -> {
if (singleItem instanceof AnyTO) {
AnyTO any = (AnyTO) item;
batchAnyService.delete(any.getKey());
deletedAnys.add(any.getKey());
} else if (singleItem instanceof TaskTO) {
TaskTO task = (TaskTO) item;
batchTaskService.delete(
TaskType.fromTOClass(task.getClass()),
task.getKey());
} else if (singleItem instanceof ReportTO) {
ReportTO report = (ReportTO) item;
batchReportService.delete(report.getKey());
} else {
LOG.warn("Unsupported for DELETE: {}", singleItem.getClass().getName());
}
});
break;
case DRYRUN:
items.forEach(item -> {
TaskTO task = (TaskTO) item;
batchTaskService.execute(
new ExecSpecs.Builder().dryRun(true).key(task.getKey()).build());
});
break;
case EXECUTE:
items.forEach(item -> {
if (singleItem instanceof TaskTO) {
TaskTO task = (TaskTO) item;
batchTaskService.execute(
new ExecSpecs.Builder().dryRun(false).key(task.getKey()).build());
} else if (singleItem instanceof ReportTO) {
ReportTO report = (ReportTO) item;
batchReportService.execute(
new ExecSpecs.Builder().key(report.getKey()).build());
}
});
break;
default:
}
results = CastUtils.cast(Map.class.cast(
batchExecutor.getClass().getMethod("batch",
BatchRequest.class).invoke(batchExecutor, batch)));
if (singleItem instanceof AnyTO) {
AbstractAnyRestClient<? extends AnyTO> anyRestClient = singleItem instanceof UserTO
? UserRestClient.class.cast(batchExecutor)
: singleItem instanceof GroupTO
? GroupRestClient.class.cast(batchExecutor)
: AnyObjectRestClient.class.cast(batchExecutor);
for (int i = 0; i < items.size(); i++) {
String key = ((AnyTO) items.get(i)).getKey();
if (!deletedAnys.contains(key)) {
items.set(i, (T) anyRestClient.read(key));
}
}
}
}
List<IColumn<T, S>> newColumnList = new ArrayList<>(columns);
newColumnList.add(newColumnList.size(), new BatchResponseColumn<>(results, keyFieldName));
container.addOrReplace(new AjaxFallbackDefaultDataTable<>(
"selectedObjects",
newColumnList,
dataProvider,
Integer.MAX_VALUE).setVisible(!items.isEmpty()));
actionPanel.setEnabled(false);
actionPanel.setVisible(false);
target.add(container);
target.add(actionPanel);
SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
} catch (Exception e) {
LOG.error("Batch failure", e);
SyncopeConsoleSession.get().error("Operation " + action.getActionId() + " failed");
}
((BasePage) getPage()).getNotificationPanel().refresh(target);
}
}, action, null).hideLabel();
}
}