Skip to content

Commit

Permalink
MID-8842 ninja - upgrade object action impl, first attempt, now upgra…
Browse files Browse the repository at this point in the history
…de files. wip
  • Loading branch information
1azyman committed Jul 7, 2023
1 parent 7b145be commit 0bbf304
Show file tree
Hide file tree
Showing 7 changed files with 369 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public String debugDump(int indent) {
DebugUtil.debugDumpWithLabel(sb, "items", items, indent + 1);
return sb.toString();
}

public boolean isEmpty() {
return items.isEmpty();
}

public boolean hasChanges() {
return items.stream().anyMatch(i -> i.isChanged());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2010-2023 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.schema.validator.processor;

import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.schema.validator.UpgradeObjectProcessor;
import com.evolveum.midpoint.schema.validator.UpgradePhase;
import com.evolveum.midpoint.schema.validator.UpgradePriority;
import com.evolveum.midpoint.schema.validator.UpgradeType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.NotificationConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SmsConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType;

import java.util.List;

// todo implement + tests
@SuppressWarnings("unused")
public class SmsConfigurationProcessor implements UpgradeObjectProcessor<SystemConfigurationType> {

@Override
public UpgradePhase getPhase() {
// todo before in 4.7.* but after in 4.4.*
return UpgradePhase.BEFORE;
}

@Override
public UpgradePriority getPriority() {
return UpgradePriority.NECESSARY;
}

@Override
public UpgradeType getType() {
return UpgradeType.SEAMLESS;
}

@Override
public boolean isApplicable(PrismObject<?> object, ItemPath path) {
return matchesTypeAndHasPathItem(object, path, SystemConfigurationType.class, ItemPath.create(
SystemConfigurationType.F_NOTIFICATION_CONFIGURATION, NotificationConfigurationType.F_SMS
));
}

@Override
public boolean process(PrismObject<SystemConfigurationType> object, ItemPath path) {
SystemConfigurationType system = object.asObjectable();
NotificationConfigurationType notification = system.getNotificationConfiguration();
List<SmsConfigurationType> smsList = notification.getSms();


return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,47 @@
~ and European Union Public License. See LICENSE file for details.
-->
<systemConfiguration xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
oid="3947d3b1-b6e8-46ec-b382-1bde67da6f07">
oid="3947d3b1-b6e8-46ec-b382-1bde67da6f07">

<notificationConfiguration>
<!--<sms>
<gateway>
<urlExpression>
<script>
<code>
return 'one.example'
</code>
</script>
</urlExpression>
</gateway>
</sms>
<sms>
<redirectToFile>redirected.log</redirectToFile>
<blackList>blacklisted@example.com</blackList>
<debug>true</debug>
<logToFile>log.log</logToFile>
<recipientFilterExpression>
<script>
<code>
return null
</code>
</script>
</recipientFilterExpression>
<whiteList>whitelisted@example.com</whiteList>
<gateway>
<method>get</method>
<urlExpression>
<script>
<code>
return 'example.com'
</code>
</script>
</urlExpression>
<username>user</username>
<password>pass</password>
</gateway>
</sms>-->
</notificationConfiguration>

<roleManagement>
<roleCatalogRef oid="0e5b7304-ea5c-438e-84d1-2b0ce40517ce" type="OrgType"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package com.evolveum.midpoint.ninja.action.upgrade;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

import com.evolveum.midpoint.ninja.action.upgrade.action.UpgradeObjectsOptions;
import com.evolveum.midpoint.ninja.action.worker.BaseWorker;
import com.evolveum.midpoint.ninja.impl.Log;
import com.evolveum.midpoint.ninja.impl.NinjaContext;
import com.evolveum.midpoint.ninja.util.ConsoleFormat;
import com.evolveum.midpoint.ninja.util.OperationStatus;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.validator.UpgradeObjectsHandler;
import com.evolveum.midpoint.schema.validator.ObjectUpgradeValidator;
import com.evolveum.midpoint.schema.validator.UpgradeValidationResult;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

public class UpgradeObjectsConsumerWorker<T extends ObjectType> extends BaseWorker<UpgradeObjectsOptions, T> {
Expand Down Expand Up @@ -69,36 +67,78 @@ public void run() {
}

private void processObject(RepositoryService repository, T object) {
PrismObject<? extends ObjectType> prismObject = object.asPrismObject();
PrismObject prismObject = object.asPrismObject();

// todo skip upgrade for specific objects based on CSV report
Set<String> identifiers = skipUpgradeForOids.get(UUID.fromString(object.getOid()));
if (identifiers == null) {
identifiers = new HashSet<>();
}


// if (skipUpgradeForOids.contains(object.getOid())) {
// log.info(ConsoleFormat.formatInfoMessageWithParameter(
// "Skipping object: ", prismObject.getBusinessDisplayName() + "(" + object.getOid() + ")"));
// return;
// }
//
// UpgradeObjectsHandler upgradeHandler = new UpgradeObjectsHandler();
// UpgradeObjectResult result = upgradeHandler.handle(prismObject);
//
// if (result.isChanged()) {
// ObjectDelta<?> delta = result.getDelta();
//
// OperationResult opResult = new OperationResult("Modify object");
// try {
// repository.modifyObject(object.getClass(), object.getOid(), delta.getModifications(), opResult);
// } catch (Exception ex) {
// log.error("Couldn't modify object");
// } finally {
// opResult.computeStatusIfUnknown();
//
// // todo if not success, print?
// }
// }

ObjectUpgradeValidator validator = new ObjectUpgradeValidator(context.getPrismContext());
UpgradeValidationResult result = validator.validate(prismObject);
if (!result.hasChanges()) {
return;
}

PrismObject cloned = prismObject.clone();
result.getItems().forEach(item -> {
if (!item.isChanged()) {
return;
}

if (!matchesOption(options.getIdentifiers(), item.getIdentifier())) {
return;
}

if (!matchesOption(options.getTypes(), item.getType())) {
return;
}

if (!matchesOption(options.getPhases(), item.getPhase())) {
return;
}

if (!matchesOption(options.getPriorities(), item.getPriority())) {
return;
}

try {
ObjectDelta delta = item.getDelta();
if (!delta.isEmpty()) {
delta.applyTo(cloned);
}
} catch (SchemaException ex) {
// todo error handling
ex.printStackTrace();
}
});

OperationResult opResult = new OperationResult("Modify object");
try {
ObjectDelta<?> delta = cloned.diff(prismObject);
repository.modifyObject(object.getClass(), object.getOid(), delta.getModifications(), opResult);
} catch (Exception ex) {
log.error("Couldn't modify object");
} finally {
opResult.computeStatusIfUnknown();

// todo if not success, print?
}
}

private <T> boolean matchesOption(List<T> options, T option) {
if (options == null || options.isEmpty()) {
return true;
}

return options.stream().anyMatch(o -> o.equals(option));
}
}

0 comments on commit 0bbf304

Please sign in to comment.