-
Notifications
You must be signed in to change notification settings - Fork 188
/
ObjectAlreadyExistHandler.java
165 lines (135 loc) · 6.93 KB
/
ObjectAlreadyExistHandler.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
/*
* Copyright (c) 2010-2013 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.provisioning.consistency.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.xml.namespace.QName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.query.AndFilter;
import com.evolveum.midpoint.prism.query.EqualFilter;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.RefFilter;
import com.evolveum.midpoint.provisioning.api.ProvisioningService;
import com.evolveum.midpoint.provisioning.api.ResourceObjectShadowChangeDescription;
import com.evolveum.midpoint.provisioning.consistency.api.ErrorHandler;
import com.evolveum.midpoint.provisioning.ucf.api.GenericFrameworkException;
import com.evolveum.midpoint.schema.ResultHandler;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.processor.ResourceAttribute;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ShadowUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
@Component
public class ObjectAlreadyExistHandler extends ErrorHandler {
@Autowired(required = true)
private ProvisioningService provisioningService;
@Autowired(required = true)
private PrismContext prismContext;
private static final Trace LOGGER = TraceManager.getTrace(ObjectAlreadyExistHandler.class);
@Override
public <T extends ShadowType> T handleError(T shadow, FailedOperation op, Exception ex, boolean compensate,
Task task, OperationResult parentResult) throws SchemaException, GenericFrameworkException, CommunicationException,
ObjectNotFoundException, ObjectAlreadyExistsException, ConfigurationException, SecurityViolationException {
if (!isDoDiscovery(shadow.getResource())){
throw new ObjectAlreadyExistsException();
}
LOGGER.trace("Start to hanlde ObjectAlreadyExitsException.");
OperationResult operationResult = parentResult
.createSubresult("Discovery for object already exists situation. Operation: " + op.name());
operationResult.addParam("shadow", shadow);
operationResult.addParam("currentOperation", op);
operationResult.addParam("exception", ex.getMessage());
ResourceObjectShadowChangeDescription change = new ResourceObjectShadowChangeDescription();
change.setResource(shadow.getResource().asPrismObject());
change.setSourceChannel(QNameUtil.qNameToUri(SchemaConstants.CHANGE_CHANNEL_DISCOVERY));
ObjectQuery query = createQueryByIcfName(shadow);
final List<PrismObject<ShadowType>> foundAccount = getExistingAccount(query, task, operationResult);
PrismObject<ShadowType> resourceAccount = null;
if (!foundAccount.isEmpty() && foundAccount.size() == 1) {
resourceAccount = foundAccount.get(0);
}
LOGGER.trace("Found conflicting resource account: {}", resourceAccount);
try{
if (resourceAccount != null) {
// Original object and found object share the same object class, therefore they must
// also share a kind. We can use this short-cut.
resourceAccount.asObjectable().setKind(shadow.getKind());
change.setCurrentShadow(resourceAccount);
// TODO: task initialization
// Task task = taskManager.createTaskInstance();
changeNotificationDispatcher.notifyChange(change, task, operationResult);
}
} finally {
operationResult.computeStatus();
}
if (operationResult.isSuccess()) {
parentResult.recordSuccess();
parentResult.muteLastSubresultError();
}
if (compensate){
throw new ObjectAlreadyExistsException(ex.getMessage(), ex);
}
return shadow;
}
private ObjectQuery createQueryByIcfName(ShadowType shadow) throws SchemaException {
// TODO: error handling TODO TODO TODO set matching rule instead of null in equlas filter
Collection<ResourceAttribute<?>> secondaryIdentifiers = ShadowUtil.getSecondaryIdentifiers(shadow);
PrismProperty nameProperty = null;
if (secondaryIdentifiers.size() != 1){
nameProperty = shadow.getAttributes().asPrismContainerValue()
.findProperty(new QName(SchemaConstants.NS_ICF_SCHEMA, "name"));
} else {
nameProperty = secondaryIdentifiers.iterator().next();
}
EqualFilter nameFilter = EqualFilter.createEqual(new ItemPath(ShadowType.F_ATTRIBUTES, nameProperty.getDefinition().getName()),nameProperty);
RefFilter resourceRefFilter = RefFilter.createReferenceEqual(ShadowType.F_RESOURCE_REF, ShadowType.class,
prismContext, shadow.getResourceRef().getOid());
EqualFilter objectClassFilter = EqualFilter.createEqual(ShadowType.F_OBJECT_CLASS, ShadowType.class, prismContext,
null, shadow.getObjectClass());
ObjectQuery query = ObjectQuery.createObjectQuery(AndFilter.createAnd(nameFilter, resourceRefFilter,
objectClassFilter));
return query;
}
private List<PrismObject<ShadowType>> getExistingAccount(ObjectQuery query, Task task, OperationResult parentResult)
throws ObjectNotFoundException, CommunicationException, ConfigurationException, SchemaException,
SecurityViolationException {
final List<PrismObject<ShadowType>> foundAccount = new ArrayList<PrismObject<ShadowType>>();
ResultHandler<ShadowType> handler = new ResultHandler() {
@Override
public boolean handle(PrismObject object, OperationResult parentResult) {
return foundAccount.add(object);
}
};
provisioningService.searchObjectsIterative(ShadowType.class, query, null, handler, parentResult);
return foundAccount;
}
}