Search in sources :

Example 1 with DataObjectCreatedEvent

use of org.kie.workbench.common.screens.datamodeller.events.DataObjectCreatedEvent 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 2 with DataObjectCreatedEvent

use of org.kie.workbench.common.screens.datamodeller.events.DataObjectCreatedEvent in project kie-wb-common by kiegroup.

the class DataModelerEventObserverTest method onNonPersistableDataObjectDeletedTest.

@Test
public void onNonPersistableDataObjectDeletedTest() {
    DataModelerEventObserver eventObserver = createObserver();
    PersistenceDescriptorModel descriptorModel = createModel();
    DataObject dataObject = new DataObjectImpl("package1", "NonPersistableObject");
    when(descriptorPath.toURI()).thenReturn(DESCRIPTOR_PATH);
    when(descriptorService.calculatePersistenceDescriptorPath(any(Module.class))).thenReturn(descriptorPath);
    when(descriptorService.load(descriptorPath)).thenReturn(descriptorModel);
    when(ioService.exists(any(org.uberfire.java.nio.file.Path.class))).thenReturn(true);
    DataObjectCreatedEvent createdEvent = new DataObjectCreatedEvent(module, dataObject);
    eventObserver.onDataObjectCreated(createdEvent);
    verify(descriptorService, times(0)).save(eq(descriptorPath), eq(descriptorModel), any(Metadata.class), anyString());
    assertEquals(0, descriptorModel.getPersistenceUnit().getClasses().size());
}
Also used : DataObjectImpl(org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl) Path(org.uberfire.backend.vfs.Path) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) DataObjectCreatedEvent(org.kie.workbench.common.screens.datamodeller.events.DataObjectCreatedEvent) Metadata(org.guvnor.common.services.shared.metadata.model.Metadata) PersistenceDescriptorModel(org.kie.workbench.common.screens.datamodeller.model.persistence.PersistenceDescriptorModel) Module(org.guvnor.common.services.project.model.Module) Test(org.junit.Test)

Example 3 with DataObjectCreatedEvent

use of org.kie.workbench.common.screens.datamodeller.events.DataObjectCreatedEvent in project kie-wb-common by kiegroup.

the class DataModelerEventObserverTest method onPersistableDataObjectCreatedTest.

@Test
public void onPersistableDataObjectCreatedTest() {
    DataModelerEventObserver eventObserver = createObserver();
    PersistenceDescriptorModel descriptorModel = createModel();
    DataObject dataObject = new DataObjectImpl("package1", "PersistableObject");
    dataObject.addAnnotation(new AnnotationImpl(DriverUtils.buildAnnotationDefinition(Entity.class)));
    when(descriptorPath.toURI()).thenReturn(DESCRIPTOR_PATH);
    when(descriptorService.calculatePersistenceDescriptorPath(any(Module.class))).thenReturn(descriptorPath);
    when(descriptorService.load(descriptorPath)).thenReturn(descriptorModel);
    when(ioService.exists(any(org.uberfire.java.nio.file.Path.class))).thenReturn(true);
    DataObjectCreatedEvent createdEvent = new DataObjectCreatedEvent(module, dataObject);
    eventObserver.onDataObjectCreated(createdEvent);
    verify(descriptorService, times(1)).save(eq(descriptorPath), eq(descriptorModel), any(Metadata.class), anyString());
    assertTrue(descriptorModel.getPersistenceUnit().getClasses().contains(dataObject.getClassName()));
}
Also used : DataObjectImpl(org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl) Path(org.uberfire.backend.vfs.Path) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) DataObjectCreatedEvent(org.kie.workbench.common.screens.datamodeller.events.DataObjectCreatedEvent) Metadata(org.guvnor.common.services.shared.metadata.model.Metadata) PersistenceDescriptorModel(org.kie.workbench.common.screens.datamodeller.model.persistence.PersistenceDescriptorModel) Module(org.guvnor.common.services.project.model.Module) AnnotationImpl(org.kie.workbench.common.services.datamodeller.core.impl.AnnotationImpl) Test(org.junit.Test)

Example 4 with DataObjectCreatedEvent

use of org.kie.workbench.common.screens.datamodeller.events.DataObjectCreatedEvent in project kie-wb-common by kiegroup.

the class DataModelerEventObserverTest method onNonPersistableDataObjectCreatedTest.

@Test
public void onNonPersistableDataObjectCreatedTest() {
    DataModelerEventObserver eventObserver = createObserver();
    PersistenceDescriptorModel descriptorModel = createModel();
    descriptorModel.getPersistenceUnit().getClasses().add("package1.PersistableObject");
    DataObject dataObject = new DataObjectImpl("package1", "NonPersistableObject");
    when(descriptorPath.toURI()).thenReturn(DESCRIPTOR_PATH);
    when(descriptorService.calculatePersistenceDescriptorPath(any(Module.class))).thenReturn(descriptorPath);
    when(descriptorService.load(descriptorPath)).thenReturn(descriptorModel);
    when(ioService.exists(any(org.uberfire.java.nio.file.Path.class))).thenReturn(true);
    DataObjectCreatedEvent createdEvent = new DataObjectCreatedEvent(module, dataObject);
    eventObserver.onDataObjectCreated(createdEvent);
    verify(descriptorService, times(0)).save(eq(descriptorPath), eq(descriptorModel), any(Metadata.class), anyString());
    assertEquals(1, descriptorModel.getPersistenceUnit().getClasses().size());
}
Also used : DataObjectImpl(org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl) Path(org.uberfire.backend.vfs.Path) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) DataObjectCreatedEvent(org.kie.workbench.common.screens.datamodeller.events.DataObjectCreatedEvent) Metadata(org.guvnor.common.services.shared.metadata.model.Metadata) PersistenceDescriptorModel(org.kie.workbench.common.screens.datamodeller.model.persistence.PersistenceDescriptorModel) Module(org.guvnor.common.services.project.model.Module) Test(org.junit.Test)

Example 5 with DataObjectCreatedEvent

use of org.kie.workbench.common.screens.datamodeller.events.DataObjectCreatedEvent in project kie-wb-common by kiegroup.

the class DataModelerServiceImpl method createJavaFile.

@Override
public Path createJavaFile(final Path context, final String fileName, final String comment, final Map<String, Object> options) {
    final org.uberfire.java.nio.file.Path nioPath = Paths.convert(context).resolve(fileName);
    final Path newPath = Paths.convert(nioPath);
    if (ioService.exists(nioPath)) {
        throw new FileAlreadyExistsException(nioPath.toString());
    }
    try {
        final Package currentPackage = moduleService.resolvePackage(context);
        String packageName = currentPackage.getPackageName();
        String className = fileName.substring(0, fileName.indexOf(".java"));
        final KieModule currentModule = moduleService.resolveModule(context);
        DataObject dataObject = new DataObjectImpl(packageName, className);
        Iterator<DomainHandler> it = domainHandlers != null ? domainHandlers.iterator() : null;
        while (it != null && it.hasNext()) {
            it.next().setDefaultValues(dataObject, options);
        }
        String source = createJavaSource(dataObject);
        ioService.write(nioPath, source, serviceHelper.makeCommentedOption(comment));
        dataObjectCreatedEvent.fire(new DataObjectCreatedEvent(currentModule, dataObject));
        return newPath;
    } catch (Exception e) {
        // uncommon error.
        logger.error("It was not possible to create Java file, for path: " + context.toURI() + ", fileName: " + fileName, e);
        throw new ServiceException("It was not possible to create Java file, for path: " + context.toURI() + ", fileName: " + fileName, e);
    }
}
Also used : Path(org.uberfire.backend.vfs.Path) FileAlreadyExistsException(org.uberfire.java.nio.file.FileAlreadyExistsException) 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) DataObjectImpl(org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) DataObjectCreatedEvent(org.kie.workbench.common.screens.datamodeller.events.DataObjectCreatedEvent) ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) DomainHandler(org.kie.workbench.common.screens.datamodeller.backend.server.handler.DomainHandler) Package(org.guvnor.common.services.project.model.Package) KieModule(org.kie.workbench.common.services.shared.project.KieModule)

Aggregations

DataObjectCreatedEvent (org.kie.workbench.common.screens.datamodeller.events.DataObjectCreatedEvent)5 Path (org.uberfire.backend.vfs.Path)5 DataObject (org.kie.workbench.common.services.datamodeller.core.DataObject)4 DataObjectImpl (org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl)4 Module (org.guvnor.common.services.project.model.Module)3 Metadata (org.guvnor.common.services.shared.metadata.model.Metadata)3 Test (org.junit.Test)3 PersistenceDescriptorModel (org.kie.workbench.common.screens.datamodeller.model.persistence.PersistenceDescriptorModel)3 ServiceException (org.kie.workbench.common.screens.datamodeller.service.ServiceException)2 ModelDriverException (org.kie.workbench.common.services.datamodeller.driver.ModelDriverException)2 KieModule (org.kie.workbench.common.services.shared.project.KieModule)2 FileAlreadyExistsException (org.uberfire.java.nio.file.FileAlreadyExistsException)2 Package (org.guvnor.common.services.project.model.Package)1 DomainHandler (org.kie.workbench.common.screens.datamodeller.backend.server.handler.DomainHandler)1 GenerationResult (org.kie.workbench.common.screens.datamodeller.model.GenerationResult)1 AnnotationImpl (org.kie.workbench.common.services.datamodeller.core.impl.AnnotationImpl)1