Search in sources :

Example 1 with DataModelerSaveHelper

use of org.kie.workbench.common.screens.datamodeller.backend.server.helper.DataModelerSaveHelper 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 2 with DataModelerSaveHelper

use of org.kie.workbench.common.screens.datamodeller.backend.server.helper.DataModelerSaveHelper in project kie-wb-common by kiegroup.

the class DataModelerServiceTest method testSaveSource.

private void testSaveSource(String newPackageName, String newFileName) {
    Path dataObjectPath = PathFactory.newPath("TestDataObject", "file:///dataobjects/TestDataObject.java");
    Path srcPath = PathFactory.newPath("src", "file:///src");
    Package packageMock = mock(Package.class);
    when(packageMock.getPackageMainSrcPath()).thenReturn(srcPath);
    when(serviceHelper.ensurePackageStructure(any(Module.class), anyString())).thenReturn(packageMock);
    DataModelerSaveHelper saveHelper = mock(DataModelerSaveHelper.class);
    List<DataModelerSaveHelper> saveHelpers = Arrays.asList(saveHelper);
    when(saveHelperInstance.iterator()).thenReturn(saveHelpers.iterator());
    DataObject dataObject = new DataObjectImpl("dataobjects", "TestDataObject");
    dataModelerService.saveSource("Source", dataObjectPath, dataObject, mock(Metadata.class), "Commit message", newPackageName, newFileName);
    verify(ioService, times(1)).startBatch(any(FileSystem.class));
    if (newPackageName == null && newFileName == null) {
        verify(ioService, times(1)).write(any(org.uberfire.java.nio.file.Path.class), anyString(), anyMap(), any(CommentedOption.class));
    } else if (newPackageName != null && newFileName != null) {
        verify(ioService, times(1)).write(any(org.uberfire.java.nio.file.Path.class), anyString(), anyMap(), any(CommentedOption.class));
        verify(ioService, times(1)).move(any(org.uberfire.java.nio.file.Path.class), any(org.uberfire.java.nio.file.Path.class), any(CommentedOption.class));
    } else if (newPackageName != null) {
        verify(ioService, times(1)).write(any(org.uberfire.java.nio.file.Path.class), anyString(), anyMap(), any(CommentedOption.class));
        verify(ioService, times(1)).move(any(org.uberfire.java.nio.file.Path.class), any(org.uberfire.java.nio.file.Path.class), any(CommentedOption.class));
    } else {
        verify(renameService, times(1)).rename(any(Path.class), anyString(), anyString());
    }
    verify(saveHelper, times(1)).postProcess(any(Path.class), any(Path.class));
    verify(ioService, times(1)).endBatch();
}
Also used : Path(org.uberfire.backend.vfs.Path) Metadata(org.guvnor.common.services.shared.metadata.model.Metadata) DataObjectImpl(org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) CommentedOption(org.uberfire.java.nio.base.options.CommentedOption) FileSystem(org.uberfire.java.nio.file.FileSystem) Package(org.guvnor.common.services.project.model.Package) DataModelerSaveHelper(org.kie.workbench.common.screens.datamodeller.backend.server.helper.DataModelerSaveHelper) Module(org.guvnor.common.services.project.model.Module)

Aggregations

Package (org.guvnor.common.services.project.model.Package)2 DataModelerSaveHelper (org.kie.workbench.common.screens.datamodeller.backend.server.helper.DataModelerSaveHelper)2 Path (org.uberfire.backend.vfs.Path)2 Module (org.guvnor.common.services.project.model.Module)1 Metadata (org.guvnor.common.services.shared.metadata.model.Metadata)1 GenerationResult (org.kie.workbench.common.screens.datamodeller.model.GenerationResult)1 ServiceException (org.kie.workbench.common.screens.datamodeller.service.ServiceException)1 DataObject (org.kie.workbench.common.services.datamodeller.core.DataObject)1 DataObjectImpl (org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl)1 ModelDriverException (org.kie.workbench.common.services.datamodeller.driver.ModelDriverException)1 CommentedOption (org.uberfire.java.nio.base.options.CommentedOption)1 FileAlreadyExistsException (org.uberfire.java.nio.file.FileAlreadyExistsException)1 FileSystem (org.uberfire.java.nio.file.FileSystem)1