Search in sources :

Example 1 with MidPointObject

use of com.evolveum.midpoint.studio.client.MidPointObject in project midpoint-studio by Evolveum.

the class CleanupFileTask method processObject.

@Override
public ProcessObjectResult processObject(MidPointObject object) throws Exception {
    String newContent = cleanupObject(object);
    MidPointObject newObject = MidPointObject.copy(object);
    newObject.setContent(newContent);
    return new ProcessObjectResult(null).object(newObject);
}
Also used : MidPointObject(com.evolveum.midpoint.studio.client.MidPointObject) ProcessObjectResult(com.evolveum.midpoint.studio.action.transfer.ProcessObjectResult)

Example 2 with MidPointObject

use of com.evolveum.midpoint.studio.client.MidPointObject in project midpoint-studio by Evolveum.

the class DiffLocalTask method doRun.

@Override
protected void doRun(ProgressIndicator indicator) {
    super.doRun(indicator);
    VirtualFile[] selectedFiles = ApplicationManager.getApplication().runReadAction((Computable<VirtualFile[]>) () -> event.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY));
    List<VirtualFile> toProcess = MidPointUtils.filterXmlFiles(selectedFiles);
    if (toProcess.size() != 2) {
        midPointService.printToConsole(getEnvironment(), DiffLocalTask.class, "Too many files selected, should be only two.");
        return;
    }
    VirtualFile firstFile = toProcess.get(0);
    VirtualFile secondFile = toProcess.get(1);
    List<MidPointObject> firstSet = loadObjects(firstFile);
    if (firstSet == null) {
        return;
    }
    List<MidPointObject> secondSet = loadObjects(secondFile);
    if (secondSet == null) {
        return;
    }
    if (firstSet.size() != secondSet.size()) {
        midPointService.printToConsole(getEnvironment(), DiffLocalTask.class, "Number of objects doesn't match in selected files: " + firstSet.size() + " vs. " + secondSet.size());
        return;
    }
    RunnableUtils.runWriteActionAndWait(() -> {
        Writer writer = null;
        VirtualFile vf = null;
        try {
            List<String> deltas = new ArrayList<>();
            for (int i = 0; i < firstSet.size(); i++) {
                MidPointObject first = firstSet.get(i);
                MidPointObject second = secondSet.get(i);
                deltas.add(createDiffXml(first, firstFile, LocationType.LOCAL, second, secondFile, LocationType.LOCAL));
            }
            vf = createScratchAndWriteDiff(deltas);
        } catch (Exception ex) {
            // failed.incrementAndGet();
            // 
            midPointService.printToConsole(getEnvironment(), DiffLocalTask.class, "Failed to compare files " + firstFile.getPath() + " with " + secondFile.getPath(), ex);
        } finally {
            IOUtils.closeQuietly(writer);
        }
        MidPointUtils.openFile(getProject(), vf);
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MidPointObject(com.evolveum.midpoint.studio.client.MidPointObject) ArrayList(java.util.ArrayList) Writer(java.io.Writer)

Example 3 with MidPointObject

use of com.evolveum.midpoint.studio.client.MidPointObject in project midpoint-studio by Evolveum.

the class DiffRemoteTask method processFiles.

private void processFiles(ProgressIndicator indicator, List<VirtualFile> files) {
    Environment env = getEnvironment();
    int skipped = 0;
    int missing = 0;
    AtomicInteger diffed = new AtomicInteger(0);
    AtomicInteger failed = new AtomicInteger(0);
    int current = 0;
    for (VirtualFile file : files) {
        ProgressManager.checkCanceled();
        current++;
        indicator.setFraction((double) current / files.size());
        List<MidPointObject> objects;
        try {
            objects = loadObjectsFromFile(file);
        } catch (Exception ex) {
            failed.incrementAndGet();
            midPointService.printToConsole(env, DiffRemoteTask.class, "Couldn't load objects from file " + file.getPath(), ex);
            continue;
        }
        if (objects.isEmpty()) {
            skipped++;
            midPointService.printToConsole(env, DiffRemoteTask.class, "Skipped file " + file.getPath() + " no objects found (parsed).");
            continue;
        }
        Map<String, MidPointObject> remoteObjects = new HashMap<>();
        for (MidPointObject object : objects) {
            ProgressManager.checkCanceled();
            try {
                MidPointObject newObject = client.get(object.getType().getClassDefinition(), object.getOid(), new SearchOptions().raw(true));
                if (newObject == null) {
                    missing++;
                    midPointService.printToConsole(env, DiffRemoteTask.class, "Couldn't find object " + object.getType().getTypeQName().getLocalPart() + "(" + object.getOid() + ").");
                    continue;
                }
                MidPointObject obj = MidPointObject.copy(object);
                obj.setContent(newObject.getContent());
                remoteObjects.put(obj.getOid(), obj);
                diffed.incrementAndGet();
            } catch (Exception ex) {
                failed.incrementAndGet();
                midPointService.printToConsole(env, DiffRemoteTask.class, "Error getting object" + object.getType().getTypeQName().getLocalPart() + "(" + object.getOid() + ")", ex);
            }
        }
        RunnableUtils.runWriteActionAndWait(() -> {
            Writer writer = null;
            VirtualFile vf = null;
            try {
                List<String> deltas = new ArrayList<>();
                for (MidPointObject local : objects) {
                    MidPointObject remote = remoteObjects.get(local.getOid());
                    if (remote == null) {
                        continue;
                    }
                    deltas.add(createDiffXml(local, file, LocationType.LOCAL, remote, null, LocationType.REMOTE));
                }
                vf = createScratchAndWriteDiff(deltas);
            } catch (Exception ex) {
                failed.incrementAndGet();
                midPointService.printToConsole(env, DiffRemoteTask.class, "Failed to compare file " + file.getPath(), ex);
            } finally {
                IOUtils.closeQuietly(writer);
            }
            MidPointUtils.openFile(getProject(), vf);
        });
    }
    NotificationType type = missing > 0 || failed.get() > 0 || skipped > 0 ? NotificationType.WARNING : NotificationType.INFORMATION;
    StringBuilder msg = new StringBuilder();
    msg.append("Compared ").append(diffed.get()).append(" objects<br/>");
    msg.append("Missing ").append(missing).append(" objects<br/>");
    msg.append("Failed to compare ").append(failed.get()).append(" objects<br/>");
    msg.append("Skipped ").append(skipped).append(" files");
    MidPointUtils.publishNotification(getProject(), NOTIFICATION_KEY, TITLE, msg.toString(), type);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SearchOptions(com.evolveum.midpoint.studio.impl.SearchOptions) MidPointObject(com.evolveum.midpoint.studio.client.MidPointObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NotificationType(com.intellij.notification.NotificationType) Environment(com.evolveum.midpoint.studio.impl.Environment) Writer(java.io.Writer)

Example 4 with MidPointObject

use of com.evolveum.midpoint.studio.client.MidPointObject in project midpoint-studio by Evolveum.

the class GeneratorTask method uploadContent.

private void uploadContent(ProgressIndicator indicator, String content) {
    Project project = getProject();
    Environment env = getEnvironment();
    updateIndicator(indicator, "Content created, uploading to " + env.getName());
    MidPointService mm = MidPointService.getInstance(project);
    MidPointClient client = new MidPointClient(project, env);
    List<MidPointObject> objects = MidPointUtils.parseText(project, content, null, NOTIFICATION_KEY);
    int fail = 0;
    int success = 0;
    for (MidPointObject object : objects) {
        try {
            OperationResult result = UploadExecuteTask.uploadExecute(client, object);
            boolean problem = result != null && !result.isSuccess();
            if (problem) {
                fail++;
                String msg = "Upload status of " + object.getName() + " was " + result.getStatus();
                mm.printToConsole(env, getClass(), msg);
                MidPointUtils.publishNotification(project, NOTIFICATION_KEY, "Warning", msg, NotificationType.WARNING, new ShowResultNotificationAction(result));
            } else {
                success++;
                mm.printToConsole(env, getClass(), "Content uploaded successfuly");
            }
        } catch (Exception ex) {
            fail++;
            mm.printToConsole(env, getClass(), "Couldn't upload generated content. Reason: " + ex.getMessage());
            MidPointUtils.publishExceptionNotification(project, env, GeneratorTask.class, NOTIFICATION_KEY, "Couldn't upload generated content", ex);
        }
    }
    showNotificationAfterFinish(project, success, fail);
    updateIndicator(indicator, "Content uploaded");
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) IOException(java.io.IOException) Project(com.intellij.openapi.project.Project) MidPointService(com.evolveum.midpoint.studio.impl.MidPointService) MidPointObject(com.evolveum.midpoint.studio.client.MidPointObject) ShowResultNotificationAction(com.evolveum.midpoint.studio.impl.ShowResultNotificationAction) MidPointClient(com.evolveum.midpoint.studio.impl.MidPointClient) Environment(com.evolveum.midpoint.studio.impl.Environment)

Example 5 with MidPointObject

use of com.evolveum.midpoint.studio.client.MidPointObject in project midpoint-studio by Evolveum.

the class RefreshTask method processFiles.

private void processFiles(ProgressIndicator indicator, Environment env, List<VirtualFile> files) {
    MidPointService mm = MidPointService.getInstance(getProject());
    MidPointClient client = new MidPointClient(getProject(), env);
    int current = 0;
    for (VirtualFile file : files) {
        ProgressManager.checkCanceled();
        current++;
        indicator.setFraction((double) current / files.size());
        List<MidPointObject> objects = new ArrayList<>();
        RunnableUtils.runWriteActionAndWait(() -> {
            MidPointUtils.forceSaveAndRefresh(getProject(), file);
            List<MidPointObject> obj = MidPointUtils.parseProjectFile(getProject(), file, NOTIFICATION_KEY);
            obj = ClientUtils.filterObjectTypeOnly(obj);
            objects.addAll(obj);
        });
        if (objects.isEmpty()) {
            state.incrementSkipped();
            mm.printToConsole(env, RefreshAction.class, "Skipped file " + file.getPath() + " no objects found (parsed).");
            continue;
        }
        List<String> newObjects = new ArrayList<>();
        for (MidPointObject object : objects) {
            ProgressManager.checkCanceled();
            ObjectTypes type = object.getType();
            try {
                MidPointObject newObject = client.get(type.getClassDefinition(), object.getOid(), new SearchOptions().raw(true));
                if (newObject == null) {
                    state.incrementMissing();
                    newObjects.add(object.getContent());
                    mm.printToConsole(env, RefreshAction.class, "Couldn't find object " + type.getTypeQName().getLocalPart() + "(" + object.getOid() + ").");
                    continue;
                }
                newObjects.add(newObject.getContent());
                state.incrementProcessed();
            } catch (Exception ex) {
                state.incrementFailed();
                newObjects.add(object.getContent());
                mm.printToConsole(env, RefreshAction.class, "Error getting object" + type.getTypeQName().getLocalPart() + "(" + object.getOid() + ")", ex);
            }
        }
        RunnableUtils.runWriteActionAndWait(() -> {
            try (Writer writer = new OutputStreamWriter(file.getOutputStream(this), file.getCharset())) {
                if (newObjects.size() > 1) {
                    writer.write(ClientUtils.OBJECTS_XML_PREFIX);
                    writer.write('\n');
                }
                for (String obj : newObjects) {
                    writer.write(obj);
                }
                if (newObjects.size() > 1) {
                    writer.write(ClientUtils.OBJECTS_XML_SUFFIX);
                    writer.write('\n');
                }
            } catch (IOException ex) {
                state.incrementFailed();
                mm.printToConsole(env, RefreshAction.class, "Failed to write refreshed file " + file.getPath(), ex);
            }
        });
    }
    NotificationType type = state.missing > 0 || state.failed > 0 || state.skipped > 0 ? NotificationType.WARNING : NotificationType.INFORMATION;
    String msg = buildFinishedNotification();
    MidPointUtils.publishNotification(getProject(), NOTIFICATION_KEY, "Refresh Action", msg, type);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) IOException(java.io.IOException) IOException(java.io.IOException) MidPointObject(com.evolveum.midpoint.studio.client.MidPointObject) RefreshAction(com.evolveum.midpoint.studio.action.transfer.RefreshAction) NotificationType(com.intellij.notification.NotificationType) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Aggregations

MidPointObject (com.evolveum.midpoint.studio.client.MidPointObject)16 IOException (java.io.IOException)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 ArrayList (java.util.ArrayList)6 Environment (com.evolveum.midpoint.studio.impl.Environment)5 MidPointClient (com.evolveum.midpoint.studio.impl.MidPointClient)5 PrismObject (com.evolveum.midpoint.prism.PrismObject)4 SearchResult (com.evolveum.midpoint.studio.client.SearchResult)4 ProcessObjectResult (com.evolveum.midpoint.studio.action.transfer.ProcessObjectResult)3 SearchOptions (com.evolveum.midpoint.studio.impl.SearchOptions)3 MidPointUtils (com.evolveum.midpoint.studio.util.MidPointUtils)3 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)3 ConnectorType (com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorType)3 NotificationType (com.intellij.notification.NotificationType)3 ControlFlowException (com.intellij.openapi.diagnostic.ControlFlowException)3 Logger (com.intellij.openapi.diagnostic.Logger)3 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)3 Project (com.intellij.openapi.project.Project)3 Writer (java.io.Writer)3 List (java.util.List)3