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);
}
}
}
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());
}
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()));
}
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());
}
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);
}
}
Aggregations