use of org.kie.workbench.common.screens.datamodeller.model.GenerationResult in project kie-wb-common by kiegroup.
the class DataModelerScreenPresenterTest method testSaveSuccessCallbackRejectingPathChange.
@Test
public void testSaveSuccessCallbackRejectingPathChange() {
final String commitMessage = "testCommitMessage";
final GenerationResult result = setupSave();
when(modelerService.saveSource(eq("testSource"), Mockito.<Path>any(), eq(testObject1), Mockito.<Metadata>any(), eq(commitMessage))).thenReturn(result);
presenter.save();
/* when package or file name is changed YesNoCancel Popup should show up
*/
verify(view).showYesNoCancelPopup(Mockito.<String>any(), Mockito.<String>any(), Mockito.<Command>any(), Mockito.<String>any(), eq(ButtonType.PRIMARY), noCommandCaptor.capture(), Mockito.<String>any(), eq(ButtonType.DANGER));
/* Execute command when package or file name change rejected
*/
noCommandCaptor.getValue().execute();
verify(savePopUpPresenter).show(Mockito.<Path>any(), parameterizedCommandCaptor.capture());
/* Execute saveCommand at save with comment pop up
*/
parameterizedCommandCaptor.getValue().execute(commitMessage);
verify(dataModelerEvent, times(2)).fire(Mockito.<DataModelStatusChangeEvent>any());
verify(versionRecordManager).reloadVersions(Mockito.<Path>any());
}
use of org.kie.workbench.common.screens.datamodeller.model.GenerationResult in project kie-wb-common by kiegroup.
the class DataModelerScreenPresenterTest method testSaveSuccessCallbackWithPathChange.
@Test
public void testSaveSuccessCallbackWithPathChange() {
final String commitMessage = "testCommitMessage";
final GenerationResult result = setupSave();
when(modelerService.saveSource(eq("testSource"), Mockito.<Path>any(), eq(testObject1), Mockito.<Metadata>any(), eq(commitMessage), Mockito.<String>any(), Mockito.<String>any())).thenReturn(result);
presenter.save();
/* when package or file name is changed YesNoCancel Popup should show up
*/
verify(view).showYesNoCancelPopup(Mockito.<String>any(), Mockito.<String>any(), yesCommandCaptor.capture(), Mockito.<String>any(), eq(ButtonType.PRIMARY), Mockito.<Command>any(), Mockito.<String>any(), eq(ButtonType.DANGER));
/* Execute command when package or file name changed
*/
yesCommandCaptor.getValue().execute();
verify(savePopUpPresenter).show(Mockito.<Path>any(), parameterizedCommandCaptor.capture());
/* Execute saveCommand at save with comment pop up
*/
parameterizedCommandCaptor.getValue().execute(commitMessage);
verify(dataModelerEvent, times(2)).fire(Mockito.<DataModelStatusChangeEvent>any());
verify(versionRecordManager).reloadVersions(Mockito.<Path>any());
}
use of org.kie.workbench.common.screens.datamodeller.model.GenerationResult 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 {
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;
}
/* Modify data object with updated fileName and packageName
* or by resolving from @path before resolving source to handle
* inconsistencies in source code and data object
* */
if (dataObject != null) {
dataObject.setName(targetName.replace(".java", ""));
dataObject.setPackageName(targetPackage.getPackageName());
}
GenerationResult result = resolveSaveSource(source, path, dataObject);
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();
}
}
}
use of org.kie.workbench.common.screens.datamodeller.model.GenerationResult 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);
}
}
Aggregations