-
Notifications
You must be signed in to change notification settings - Fork 188
/
PageResourceWizard.java
317 lines (270 loc) · 12.2 KB
/
PageResourceWizard.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright (c) 2010-2015 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.web.page.admin.resources;
import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.gui.api.model.NonEmptyLoadableModel;
import com.evolveum.midpoint.gui.api.model.NonEmptyModel;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.application.AuthorizationAction;
import com.evolveum.midpoint.web.application.PageDescriptor;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.component.wizard.Wizard;
import com.evolveum.midpoint.web.component.wizard.WizardStep;
import com.evolveum.midpoint.web.component.wizard.resource.*;
import com.evolveum.midpoint.web.page.admin.PageAdmin;
import com.evolveum.midpoint.web.page.error.PageError;
import com.evolveum.midpoint.web.util.OnePageParameterEncoder;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import org.apache.wicket.Component;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.wizard.WizardModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
/**
* @author lazyman
*/
@PageDescriptor(url = "/admin/resources/wizard", action = {
@AuthorizationAction(actionUri = AuthorizationConstants.AUTZ_UI_RESOURCES_ALL_URL,
label = "PageAdminResources.auth.resourcesAll.label",
description = "PageAdminResources.auth.resourcesAll.description"),
@AuthorizationAction(actionUri = AuthorizationConstants.AUTZ_UI_RESOURCE_EDIT_URL,
label = "PageResourceWizard.auth.resource.label",
description = "PageResourceWizard.auth.resource.description")})
public class PageResourceWizard extends PageAdmin {
private static final String ID_WIZARD = "wizard";
public static final String PARAM_OID = "oid";
public static final String PARAM_CONFIG_ONLY = "configOnly";
public static final String PARAM_READ_ONLY = "readOnly";
private static final Trace LOGGER = TraceManager.getTrace(PageResourceWizard.class);
// these models should be reset after each 'save' operation, in order to fetch current data (on demand)
// each step should use corresponding model
// these models have always non-null and mutable content
@NotNull private final NonEmptyLoadableModel<PrismObject<ResourceType>> modelRaw; // contains resolved connector as well
@NotNull private final NonEmptyLoadableModel<PrismObject<ResourceType>> modelNoFetch; // contains resolved connector as well
@NotNull private final NonEmptyLoadableModel<PrismObject<ResourceType>> modelFull;
@NotNull private final ResourceWizardIssuesModel issuesModel;
// additional models that have to be reset after each 'save' operation
@NotNull private final Collection<LoadableModel<?>> dependentModels = new HashSet<>();
// for new resources: should be set after first save; for others: should be set on page creation
private String editedResourceOid;
private final boolean configurationOnly;
private boolean readOnly;
private ConfigurationStep configurationStep;
public PageResourceWizard(@NotNull PageParameters parameters) {
getPageParameters().overwriteWith(parameters); // to be available in the methods called within this constructor as well
configurationOnly = parameters.get(PARAM_CONFIG_ONLY).toBoolean();
readOnly = parameters.get(PARAM_READ_ONLY).toBoolean();
editedResourceOid = getResourceOid(); // might be null at this moment
LOGGER.debug("Resource wizard called with oid={}, configOnly={}, readOnly={}", editedResourceOid, configurationOnly, readOnly);
modelRaw = createResourceModel(getOperationOptionsBuilder()
.raw()
.item(ResourceType.F_CONNECTOR_REF).resolve()
.build());
modelNoFetch = createResourceModel(getOperationOptionsBuilder()
.noFetch()
.item(ResourceType.F_CONNECTOR_REF).resolve()
.build());
modelFull = createResourceModel(null);
issuesModel = new ResourceWizardIssuesModel(modelFull, this);
initLayout();
}
@Override
protected void createBreadcrumb() {
createInstanceBreadcrumb();
}
@NotNull
private NonEmptyLoadableModel<PrismObject<ResourceType>> createResourceModel(final Collection<SelectorOptions<GetOperationOptions>> options) {
return new NonEmptyLoadableModel<PrismObject<ResourceType>>(false) {
@NotNull
@Override
protected PrismObject<ResourceType> load() {
try {
if (editedResourceOid == null) {
return getPrismContext().createObject(ResourceType.class);
}
PrismObject<ResourceType> resource = loadResourceModelObject(options);
if (resource == null) {
throw new RestartResponseException(PageError.class);
}
return resource;
} catch (Exception ex) {
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't load resource", ex);
throw new RestartResponseException(PageError.class);
}
}
};
}
// named differently from "loadResource", as this is used in the superclass
private PrismObject<ResourceType> loadResourceModelObject(Collection<SelectorOptions<GetOperationOptions>> options) {
Task task = createSimpleTask("loadResource");
return WebModelServiceUtils.loadObject(ResourceType.class, editedResourceOid, options, this, task, task.getResult());
}
public String getEditedResourceOid() {
return editedResourceOid;
}
public void setEditedResourceOid(String editedResourceOid) {
this.editedResourceOid = editedResourceOid;
}
@Override
protected IModel<String> createPageTitleModel() {
return new LoadableModel<String>(false) {
@Override
protected String load() {
if (editedResourceOid == null) {
return createStringResource("PageResourceWizard.title").getString();
}
String name = WebComponentUtil.getName(modelRaw.getObject());
return createStringResource("PageResourceWizard.title.edit", name).getString();
}
};
}
private void initLayout() {
WizardModel wizardModel = new ResourceWizardModel(this);
if (!configurationOnly) {
wizardModel.add(new NameStep(modelRaw, this));
}
configurationStep = new ConfigurationStep(modelNoFetch, this);
wizardModel.add(configurationStep);
if (!configurationOnly) {
wizardModel.add(new SchemaStep(modelFull, this));
wizardModel.add(new SchemaHandlingStep(modelFull, this));
wizardModel.add(new SynchronizationStep(modelFull, this));
wizardModel.add(new CapabilityStep(modelFull, this));
}
Wizard wizard = new Wizard(ID_WIZARD, new Model<>(wizardModel), issuesModel);
wizard.setOutputMarkupId(true);
add(wizard);
}
public void refreshIssues(@Nullable AjaxRequestTarget target) {
issuesModel.reset();
if (target != null) {
Wizard wizard = (Wizard) get(ID_WIZARD);
target.add(wizard.getIssuesPanel());
target.add(wizard.getSteps());
target.add(wizard.getButtons());
}
}
public ConfigurationStep getConfigurationStep() {
return configurationStep;
}
@NotNull public ResourceWizardIssuesModel getIssuesModel() {
return issuesModel;
}
public void resetModels() {
LOGGER.info("Resetting models");
modelRaw.reset();
modelNoFetch.reset();
modelFull.reset();
for (LoadableModel<?> model : dependentModels) {
model.reset();
}
}
public void registerDependentModel(@NotNull LoadableModel<?> model) {
dependentModels.add(model);
}
// questionable
public boolean isNewResource() {
return editedResourceOid == null;
}
public ObjectDelta<ResourceType> computeDiff(PrismObject<ResourceType> oldResource, PrismObject<ResourceType> newResource) {
ObjectDelta<ResourceType> delta = oldResource.diff(newResource);
if (!delta.isModify()) {
return delta;
}
delta.getModifications().removeIf(itemDelta -> ResourceType.F_FETCH_RESULT.equivalent(itemDelta.getPath()));
return delta;
}
// TODO change to debug
public void logDelta(ObjectDelta delta) {
LOGGER.info("Applying delta:\n{}", delta.debugDump());
}
public boolean isCurrentStepComplete() {
Wizard wizard = (Wizard) get(ID_WIZARD);
WizardStep activeStep = (WizardStep) wizard.getModelObject().getActiveStep();
return activeStep == null || activeStep.isComplete();
}
public Wizard getWizard() {
return (Wizard) get(ID_WIZARD);
}
public boolean isConfigurationOnly() {
return configurationOnly;
}
public boolean isReadOnly() {
return readOnly;
}
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
public void addEditingEnabledBehavior(Component... components) {
for (Component component : components) {
component.add(new VisibleEnableBehaviour() {
@Override
public boolean isEnabled() {
return !readOnly;
}
});
}
}
public void addEditingVisibleBehavior(Component... components) {
for (Component component : components) {
component.add(new VisibleEnableBehaviour() {
@Override
public boolean isVisible() {
return !readOnly;
}
});
}
}
public NonEmptyModel<Boolean> getReadOnlyModel() {
return new NonEmptyModel<Boolean>() {
@NotNull
@Override
public Boolean getObject() {
return readOnly;
}
@Override
public void setObject(@NotNull Boolean object) {
throw new UnsupportedOperationException();
}
@Override
public void detach() {
}
};
}
public void visualize(AjaxRequestTarget target) {
setResponsePage(new PageResourceVisualization(modelFull.getObject()));
}
public boolean showSaveResultInPage(boolean saved, OperationResult result) {
return saved || WebComponentUtil.showResultInPage(result);
}
protected String getResourceOid() {
StringValue resourceOid = getPageParameters().get(OnePageParameterEncoder.PARAMETER);
return resourceOid != null ? resourceOid.toString() : null;
}
}