Search in sources :

Example 1 with DataObjectRenamedEvent

use of org.kie.workbench.common.screens.datamodeller.events.DataObjectRenamedEvent 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)

Aggregations

DataObjectRenamedEvent (org.kie.workbench.common.screens.datamodeller.events.DataObjectRenamedEvent)1 GenerationResult (org.kie.workbench.common.screens.datamodeller.model.GenerationResult)1 ServiceException (org.kie.workbench.common.screens.datamodeller.service.ServiceException)1 ModelDriverException (org.kie.workbench.common.services.datamodeller.driver.ModelDriverException)1 Path (org.uberfire.backend.vfs.Path)1 FileAlreadyExistsException (org.uberfire.java.nio.file.FileAlreadyExistsException)1