Search in sources :

Example 11 with ServiceException

use of org.kie.workbench.common.screens.datamodeller.service.ServiceException 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 12 with ServiceException

use of org.kie.workbench.common.screens.datamodeller.service.ServiceException in project kie-wb-common by kiegroup.

the class DataModelerServiceImpl method delete.

@Override
public void delete(final Path path, final String comment) {
    try {
        KieModule module = moduleService.resolveModule(path);
        if (module == null) {
            logger.warn("File : " + path.toURI() + " do not belong to a valid module");
            return;
        }
        deleteService.delete(path, comment);
        String className = calculateClassName(module, path);
        DataObject dataObject = new DataObjectImpl(NamingUtils.extractPackageName(className), NamingUtils.extractClassName(className));
        dataObjectDeletedEvent.fire(new DataObjectDeletedEvent(module, dataObject));
    } catch (final Exception e) {
        logger.error("File: " + path.toURI() + " couldn't be deleted due to the following error. ", e);
        throw new ServiceException("File: " + path.toURI() + " couldn't be deleted due to the following error. " + e.getMessage());
    }
}
Also used : DataObjectImpl(org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) DataObjectDeletedEvent(org.kie.workbench.common.screens.datamodeller.events.DataObjectDeletedEvent) 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 13 with ServiceException

use of org.kie.workbench.common.screens.datamodeller.service.ServiceException 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

ServiceException (org.kie.workbench.common.screens.datamodeller.service.ServiceException)13 ModelDriverException (org.kie.workbench.common.services.datamodeller.driver.ModelDriverException)13 FileAlreadyExistsException (org.uberfire.java.nio.file.FileAlreadyExistsException)12 KieModule (org.kie.workbench.common.services.shared.project.KieModule)8 GenerationResult (org.kie.workbench.common.screens.datamodeller.model.GenerationResult)7 JavaRoasterModelDriver (org.kie.workbench.common.services.datamodeller.driver.impl.JavaRoasterModelDriver)6 ModelDriverResult (org.kie.workbench.common.services.datamodeller.driver.model.ModelDriverResult)6 Package (org.guvnor.common.services.project.model.Package)3 DataObject (org.kie.workbench.common.services.datamodeller.core.DataObject)3 Path (org.uberfire.backend.vfs.Path)3 ArrayList (java.util.ArrayList)2 DataModel (org.kie.workbench.common.services.datamodeller.core.DataModel)2 DataObjectImpl (org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl)2 ModelDriver (org.kie.workbench.common.services.datamodeller.driver.ModelDriver)2 List (java.util.List)1 ValidationMessage (org.guvnor.common.services.shared.validation.model.ValidationMessage)1 Test (org.junit.Test)1 ModuleDataModelOracle (org.kie.soup.project.datamodel.oracle.ModuleDataModelOracle)1 FormGenerationResult (org.kie.workbench.common.forms.jbpm.server.service.formGeneration.FormGenerationResult)1 FormDefinition (org.kie.workbench.common.forms.model.FormDefinition)1