Search in sources :

Example 1 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 2 with GenerationResult

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

the class DataModelerScreenPresenterTest method setupSave.

private GenerationResult setupSave() {
    presenter.context = mock(DataModelerContext.class);
    when(validationService.validateForSave(Mockito.<Path>any(), Mockito.<DataObject>any())).thenReturn(Collections.emptyList());
    when(presenter.context.getDataObject()).thenReturn(testObject1);
    when(presenter.context.isEditorChanged()).thenReturn(true);
    when(presenter.context.getEditorModelContent()).thenReturn(mock(EditorModelContent.class));
    when(presenter.context.getEditorModelContent().getOriginalPackageName()).thenReturn(testObject1.getPackageName());
    final ObservablePath mockPath = mock(ObservablePath.class);
    when(versionRecordManager.getPathToLatest()).thenReturn(mockPath);
    when(mockPath.getFileName()).thenReturn("testCurrentFile.java");
    final GenerationResult result = mock(GenerationResult.class);
    when(result.hasErrors()).thenReturn(false);
    when(presenter.getSource()).thenReturn("testSource");
    return result;
}
Also used : GenerationResult(org.kie.workbench.common.screens.datamodeller.model.GenerationResult) EditorModelContent(org.kie.workbench.common.screens.datamodeller.model.EditorModelContent) ObservablePath(org.uberfire.backend.vfs.ObservablePath)

Example 3 with GenerationResult

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

the class DataModelerServiceImpl method copy.

public Path copy(final Path path, final String newName, final String newPackageName, final Path targetDirectory, final String comment, final boolean refactor) {
    Path targetPath = null;
    if (refactor) {
        try {
            GenerationResult refactoringResult = refactorClass(path, newPackageName, newName);
            if (!refactoringResult.hasErrors()) {
                targetPath = Paths.convert(Paths.convert(targetDirectory).resolve(newName + ".java"));
                copyHelper.addRefactoredPath(targetPath, refactoringResult.getSource(), comment);
                KieModule module = moduleService.resolveModule(targetPath);
                if (module != null) {
                    dataObjectCreatedEvent.fire(new DataObjectCreatedEvent(module, refactoringResult.getDataObject()));
                }
            }
        } catch (Exception e) {
            // if the refactoring fails for whatever reason the file still needs to be copied.
            logger.error("An error was produced during class refactoring at file copying for file: " + path + ". The file copying will continue without class refactoring", e);
        }
    }
    try {
        return copyService.copy(path, newName, targetDirectory, comment);
    } finally {
        if (targetPath != null) {
            copyHelper.removeRefactoredPath(targetPath);
        }
    }
}
Also used : Path(org.uberfire.backend.vfs.Path) DataObjectCreatedEvent(org.kie.workbench.common.screens.datamodeller.events.DataObjectCreatedEvent) GenerationResult(org.kie.workbench.common.screens.datamodeller.model.GenerationResult) KieModule(org.kie.workbench.common.services.shared.project.KieModule) 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 4 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 5 with GenerationResult

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

the class DataModelerServiceImpl method saveModel.

@Override
public GenerationResult saveModel(final DataModel dataModel, final KieModule module, final boolean overwrite, final String commitMessage) {
    Long startTime = System.currentTimeMillis();
    boolean onBatch = false;
    try {
        // Start IOService bath processing. IOService batch processing causes a blocking operation on the file system
        // to it must be treated carefully.
        CommentedOption option = serviceHelper.makeCommentedOption(commitMessage);
        ioService.startBatch(Paths.convert(module.getRootPath()).getFileSystem());
        onBatch = true;
        generateModel(dataModel, module, option);
        onBatch = false;
        ioService.endBatch();
        Long endTime = System.currentTimeMillis();
        if (logger.isDebugEnabled()) {
            logger.debug("Time elapsed when saving " + module.getModuleName() + ": " + (endTime - startTime) + " ms");
        }
        GenerationResult result = new GenerationResult();
        result.setGenerationTime(endTime - startTime);
        return result;
    } catch (Exception e) {
        logger.error("An error was produced during data model adf, dataModel: " + dataModel + ", path: " + module.getRootPath(), e);
        if (onBatch) {
            try {
                logger.warn("IOService batch method is still on, trying to end batch processing.");
                ioService.endBatch();
                logger.warn("IOService batch method is was successfully finished. The user will still get the exception, but the batch processing was finished.");
            } catch (Exception ex) {
                logger.error("An error was produced when the IOService.endBatch processing was executed.", ex);
            }
        }
        throw new ServiceException("Data model couldn't be generated due to the following error. " + e);
    }
}
Also used : ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) CommentedOption(org.uberfire.java.nio.base.options.CommentedOption) GenerationResult(org.kie.workbench.common.screens.datamodeller.model.GenerationResult) 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)14 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 Package (org.guvnor.common.services.project.model.Package)2 RemoteCallback (org.jboss.errai.common.client.api.RemoteCallback)2 Test (org.junit.Test)2 DataModelSaved (org.kie.workbench.common.screens.datamodeller.events.DataModelSaved)2 DataModelStatusChangeEvent (org.kie.workbench.common.screens.datamodeller.events.DataModelStatusChangeEvent)2 NotificationEvent (org.uberfire.workbench.events.NotificationEvent)2 List (java.util.List)1 ValidationMessage (org.guvnor.common.services.shared.validation.model.ValidationMessage)1 DataModelerSaveHelper (org.kie.workbench.common.screens.datamodeller.backend.server.helper.DataModelerSaveHelper)1 DataObjectCreatedEvent (org.kie.workbench.common.screens.datamodeller.events.DataObjectCreatedEvent)1 DataObjectRenamedEvent (org.kie.workbench.common.screens.datamodeller.events.DataObjectRenamedEvent)1