Search in sources :

Example 6 with GenerationResult

use of org.kie.workbench.common.screens.datamodeller.model.GenerationResult in project kie-wb-common by kiegroup.

the class DataModelerServiceImpl method updateDataObject.

/**
 * Updates data object provided in the dataObject parameter with the Java code provided in the source parameter.
 * This method does not write changes in the file system.
 * @param dataObject Data object definition to be updated.
 * @param source Java code to use for the update.
 * @param path Path to the java file. (used for error messages adf)
 * @return returns a GenerationResult object with the updated data object and the source and path parameter as is.
 */
@Override
public GenerationResult updateDataObject(final DataObject dataObject, final String source, final Path path) {
    // Resolve the dataobject update in memory
    GenerationResult result = new GenerationResult();
    KieModule module;
    try {
        result.setSource(source);
        module = moduleService.resolveModule(path);
        if (module == null) {
            logger.warn("File : " + path.toURI() + " do not belong to a valid module");
            result.setSource(source);
            return result;
        }
        ClassLoader classLoader = classLoaderHelper.getModuleClassLoader(module);
        JavaRoasterModelDriver modelDriver = new JavaRoasterModelDriver(ioService, Paths.convert(path), classLoader, filterHolder);
        ModelDriverResult driverResult = modelDriver.loadDataObject(source, Paths.convert(path));
        if (driverResult.hasErrors()) {
            result.setErrors(serviceHelper.toDataModelerError(driverResult.getErrors()));
        } else {
            if (driverResult.getDataModel().getDataObjects().size() > 0) {
                result.setDataObject(driverResult.getDataModel().getDataObjects().iterator().next());
            }
        }
        return result;
    } catch (Exception e) {
        logger.error("Source file for data object: " + dataObject.getClassName() + ", couldn't be parsed", e);
        throw new ServiceException("Source file for data object: " + dataObject.getClassName() + ", couldn't be parsed", e);
    }
}
Also used : ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) GenerationResult(org.kie.workbench.common.screens.datamodeller.model.GenerationResult) ModelDriverResult(org.kie.workbench.common.services.datamodeller.driver.model.ModelDriverResult) KieModule(org.kie.workbench.common.services.shared.project.KieModule) JavaRoasterModelDriver(org.kie.workbench.common.services.datamodeller.driver.impl.JavaRoasterModelDriver) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) FileAlreadyExistsException(org.uberfire.java.nio.file.FileAlreadyExistsException)

Example 7 with GenerationResult

use of org.kie.workbench.common.screens.datamodeller.model.GenerationResult in project kie-wb-common by kiegroup.

the class DataModelerServiceImpl method rename.

public Path rename(final Path path, final String newName, String comment, final boolean refactor, final boolean saveCurrentChanges, final String source, final DataObject dataObject, final Metadata metadata) {
    GenerationResult saveResult = null;
    if (saveCurrentChanges) {
        saveResult = resolveSaveSource(source, path, dataObject);
        ioService.write(Paths.convert(path), saveResult.getSource(), metadataService.setUpAttributes(path, metadata), serviceHelper.makeCommentedOption(comment));
    }
    Path targetPath = null;
    String newContent = null;
    if (refactor) {
        String sourceToRefactor;
        if (saveCurrentChanges) {
            sourceToRefactor = (saveResult != null && !saveResult.hasErrors()) ? saveResult.getSource() : null;
        } else {
            sourceToRefactor = source;
        }
        if (sourceToRefactor != null) {
            try {
                GenerationResult refactoringResult = refactorClass(sourceToRefactor, path, null, newName);
                if (!refactoringResult.hasErrors()) {
                    targetPath = Paths.convert(Paths.convert(path).resolveSibling(newName + ".java"));
                    renameHelper.addRefactoredPath(targetPath, refactoringResult.getSource(), comment);
                    // TODO send data object renamed event.
                    newContent = refactoringResult.getSource();
                }
            } catch (Exception e) {
                // if the refactoring fails for whatever reason the file still needs to be renamed.
                logger.error("An error was produced during class refactoring at file renaming for file: " + path + ". The file renaming will continue without class refactoring", e);
            }
        }
    }
    try {
        // TODO we need to investigate why we have a DeleteEvent, and a CreateEvent for the case of .java files.
        boolean workaround = true;
        if (!workaround) {
            return renameService.rename(path, newName, comment);
        } else {
            // I will implement the rename here as a workaround
            // remove this workaround when we can find the error.
            Path updatedPath = renameWorkaround(path, newName, newContent, comment);
            dataObjectRenamedEvent.fire((DataObjectRenamedEvent) new DataObjectRenamedEvent().withPath(updatedPath));
            return updatedPath;
        }
    } finally {
        if (targetPath != null) {
            renameHelper.removeRefactoredPath(targetPath);
        }
    }
}
Also used : Path(org.uberfire.backend.vfs.Path) GenerationResult(org.kie.workbench.common.screens.datamodeller.model.GenerationResult) DataObjectRenamedEvent(org.kie.workbench.common.screens.datamodeller.events.DataObjectRenamedEvent) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) FileAlreadyExistsException(org.uberfire.java.nio.file.FileAlreadyExistsException)

Example 8 with GenerationResult

use of org.kie.workbench.common.screens.datamodeller.model.GenerationResult in project kie-wb-common by kiegroup.

the class DataModelerScreenPresenter method getSaveSuccessCallback.

private RemoteCallback<GenerationResult> getSaveSuccessCallback(final JavaTypeInfo newTypeInfo, final Path currentPath) {
    return new RemoteCallback<GenerationResult>() {

        @Override
        public void callback(GenerationResult result) {
            view.hideBusyIndicator();
            if (newTypeInfo == null) {
                Boolean oldDirtyStatus = isDirty();
                if (result.hasErrors()) {
                    context.setParseStatus(DataModelerContext.ParseStatus.PARSE_ERRORS);
                    updateEditorView(null);
                    context.setDataObject(null);
                    if (isEditorTabSelected()) {
                        // un common case
                        showParseErrorsDialog(Constants.INSTANCE.modelEditor_message_file_parsing_errors(), true, result.getErrors(), getOnSaveParseErrorCommand());
                    }
                } else {
                    context.setParseStatus(DataModelerContext.ParseStatus.PARSED);
                    if (context.isSourceChanged()) {
                        updateEditorView(result.getDataObject());
                        context.setDataObject(result.getDataObject());
                    }
                    cleanSystemMessages(getCurrentMessageType());
                }
                setSource(result.getSource());
                context.setEditionStatus(DataModelerContext.EditionStatus.NO_CHANGES);
                setOriginalHash(context.getDataObject() != null ? context.getDataObject().hashCode() : null);
                originalSourceHash = getSource().hashCode();
                notification.fire(new NotificationEvent(org.kie.workbench.common.widgets.client.resources.i18n.CommonConstants.INSTANCE.ItemSavedSuccessfully(), NotificationEvent.NotificationType.SUCCESS));
                dataModelerEvent.fire(new DataModelStatusChangeEvent(context.getContextId(), DataModelerEvent.DATA_MODEL_BROWSER, oldDirtyStatus, false));
                dataModelerEvent.fire(new DataModelSaved(context.getContextId(), null));
                versionRecordManager.reloadVersions(currentPath);
            } else {
                notification.fire(new NotificationEvent(org.uberfire.ext.editor.commons.client.resources.i18n.CommonConstants.INSTANCE.ItemRenamedSuccessfully(), NotificationEvent.NotificationType.SUCCESS));
            // If the file was renamed as part of the file saving, don't do anything.
            // A rename event will arrive, the same as for the "Rename" case.
            // and the file will be automatically reloaded.
            }
        }
    };
}
Also used : DataModelStatusChangeEvent(org.kie.workbench.common.screens.datamodeller.events.DataModelStatusChangeEvent) DataModelSaved(org.kie.workbench.common.screens.datamodeller.events.DataModelSaved) GenerationResult(org.kie.workbench.common.screens.datamodeller.model.GenerationResult) NotificationEvent(org.uberfire.workbench.events.NotificationEvent) RemoteCallback(org.jboss.errai.common.client.api.RemoteCallback)

Example 9 with GenerationResult

use of org.kie.workbench.common.screens.datamodeller.model.GenerationResult in project kie-wb-common by kiegroup.

the class DataModelerServiceImpl method saveSource.

@Override
public GenerationResult saveSource(final String source, final Path path, final DataObject dataObject, final Metadata metadata, final String commitMessage, final String newPackageName, final String newFileName) {
    boolean onBatch = false;
    try {
        GenerationResult result = resolveSaveSource(source, path, dataObject);
        Package currentPackage = moduleService.resolvePackage(path);
        Package targetPackage = currentPackage;
        String targetName = path.getFileName();
        org.uberfire.java.nio.file.Path targetPath = Paths.convert(path);
        boolean packageChanged = false;
        boolean nameChanged = false;
        if (newPackageName != null && (currentPackage == null || !newPackageName.equals(currentPackage.getPackageName()))) {
            // make sure destination package exists.
            targetPackage = serviceHelper.ensurePackageStructure(moduleService.resolveModule(path), newPackageName);
            packageChanged = true;
        }
        if (newFileName != null && !(newFileName + ".java").equals(path.getFileName())) {
            targetName = newFileName + ".java";
            nameChanged = true;
        }
        fireMetadataSocialEvents(path, metadataService.getMetadata(path), metadata);
        ioService.startBatch(targetPath.getFileSystem());
        onBatch = true;
        if (packageChanged) {
            targetPath = Paths.convert(targetPackage.getPackageMainSrcPath()).resolve(targetName);
            ioService.write(Paths.convert(path), result.getSource(), metadataService.setUpAttributes(path, metadata), serviceHelper.makeCommentedOption(commitMessage));
            // deleteService.delete( path, commitMessage );
            ioService.move(Paths.convert(path), targetPath, serviceHelper.makeCommentedOption(commitMessage));
            result.setPath(Paths.convert(targetPath));
        } else if (nameChanged) {
            ioService.write(Paths.convert(path), result.getSource(), metadataService.setUpAttributes(path, metadata), serviceHelper.makeCommentedOption(commitMessage));
            Path newPath = renameService.rename(path, newFileName, commitMessage);
            result.setPath(newPath);
        } else {
            ioService.write(Paths.convert(path), result.getSource(), metadataService.setUpAttributes(path, metadata), serviceHelper.makeCommentedOption(commitMessage));
            result.setPath(path);
        }
        if (saveHelperInstance != null) {
            for (DataModelerSaveHelper saveHelper : saveHelperInstance) {
                saveHelper.postProcess(path, result.getPath());
            }
        }
        return result;
    } catch (Exception e) {
        logger.error("Source file couldn't be updated, path: " + path.toURI() + ", dataObject: " + (dataObject != null ? dataObject.getClassName() : null) + ".", e);
        throw new ServiceException("Source file couldn't be updated, path: " + path.toURI() + ", dataObject: " + (dataObject != null ? dataObject.getClassName() : null) + ".", e);
    } finally {
        if (onBatch) {
            ioService.endBatch();
        }
    }
}
Also used : Path(org.uberfire.backend.vfs.Path) ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) GenerationResult(org.kie.workbench.common.screens.datamodeller.model.GenerationResult) Package(org.guvnor.common.services.project.model.Package) DataModelerSaveHelper(org.kie.workbench.common.screens.datamodeller.backend.server.helper.DataModelerSaveHelper) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) FileAlreadyExistsException(org.uberfire.java.nio.file.FileAlreadyExistsException)

Example 10 with GenerationResult

use of org.kie.workbench.common.screens.datamodeller.model.GenerationResult in project kie-wb-common by kiegroup.

the class DataModelerServiceImpl method resolveSaveSource.

private GenerationResult resolveSaveSource(final String source, final Path path, final DataObject dataObject) {
    GenerationResult result = new GenerationResult();
    KieModule module;
    String updatedSource;
    try {
        module = moduleService.resolveModule(path);
        if (module == null) {
            logger.warn("File : " + path.toURI() + " do not belong to a valid module");
            result.setSource(source);
            return result;
        }
        if (dataObject != null) {
            // the source needs to be updated with the DataObject definition prior to save
            result = updateSource(source, path, dataObject);
            updatedSource = result.getSource();
        } else {
            // if the dataObject wasn't provided the source is already prepared to be saved and likely
            // it's not parsed at the ui. So we will save the provided source and try to parse the data object
            updatedSource = source;
        }
        if (dataObject == null) {
            ClassLoader classLoader = classLoaderHelper.getModuleClassLoader(module);
            JavaRoasterModelDriver modelDriver = new JavaRoasterModelDriver(ioService, Paths.convert(path), classLoader, filterHolder);
            ModelDriverResult driverResult = modelDriver.loadDataObject(source, Paths.convert(path));
            if (driverResult.hasErrors()) {
                result.setErrors(serviceHelper.toDataModelerError(driverResult.getErrors()));
            } else {
                if (driverResult.getDataModel().getDataObjects().size() > 0) {
                    result.setDataObject(driverResult.getDataModel().getDataObjects().iterator().next());
                }
            }
        }
        result.setSource(updatedSource);
        return result;
    } catch (Exception e) {
        logger.error("Source file couldn't be updated, path: " + path.toURI() + ", dataObject: " + (dataObject != null ? dataObject.getClassName() : null) + ".", e);
        throw new ServiceException("Source file couldn't be updated, path: " + path.toURI() + ", dataObject: " + (dataObject != null ? dataObject.getClassName() : null) + ".", e);
    }
}
Also used : ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) GenerationResult(org.kie.workbench.common.screens.datamodeller.model.GenerationResult) ModelDriverResult(org.kie.workbench.common.services.datamodeller.driver.model.ModelDriverResult) KieModule(org.kie.workbench.common.services.shared.project.KieModule) JavaRoasterModelDriver(org.kie.workbench.common.services.datamodeller.driver.impl.JavaRoasterModelDriver) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) FileAlreadyExistsException(org.uberfire.java.nio.file.FileAlreadyExistsException)

Aggregations

GenerationResult (org.kie.workbench.common.screens.datamodeller.model.GenerationResult)10 ServiceException (org.kie.workbench.common.screens.datamodeller.service.ServiceException)9 ModelDriverException (org.kie.workbench.common.services.datamodeller.driver.ModelDriverException)9 FileAlreadyExistsException (org.uberfire.java.nio.file.FileAlreadyExistsException)9 KieModule (org.kie.workbench.common.services.shared.project.KieModule)6 JavaRoasterModelDriver (org.kie.workbench.common.services.datamodeller.driver.impl.JavaRoasterModelDriver)3 ModelDriverResult (org.kie.workbench.common.services.datamodeller.driver.model.ModelDriverResult)3 Path (org.uberfire.backend.vfs.Path)3 ArrayList (java.util.ArrayList)2 List (java.util.List)1 Package (org.guvnor.common.services.project.model.Package)1 ValidationMessage (org.guvnor.common.services.shared.validation.model.ValidationMessage)1 RemoteCallback (org.jboss.errai.common.client.api.RemoteCallback)1 DataModelerSaveHelper (org.kie.workbench.common.screens.datamodeller.backend.server.helper.DataModelerSaveHelper)1 DataModelSaved (org.kie.workbench.common.screens.datamodeller.events.DataModelSaved)1 DataModelStatusChangeEvent (org.kie.workbench.common.screens.datamodeller.events.DataModelStatusChangeEvent)1 DataObjectCreatedEvent (org.kie.workbench.common.screens.datamodeller.events.DataObjectCreatedEvent)1 DataObjectRenamedEvent (org.kie.workbench.common.screens.datamodeller.events.DataObjectRenamedEvent)1 DataModelerError (org.kie.workbench.common.screens.datamodeller.model.DataModelerError)1 DataObject (org.kie.workbench.common.services.datamodeller.core.DataObject)1