Search in sources :

Example 6 with ServiceException

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

the class DataModelerServiceImpl method loadModel.

private Pair<DataModel, ModelDriverResult> loadModel(final KieModule module, boolean processErrors) {
    if (logger.isDebugEnabled()) {
        logger.debug("Loading data model from path: " + module.getRootPath());
    }
    Long startTime = System.currentTimeMillis();
    DataModel dataModel = null;
    Path modulePath = null;
    Package defaultPackage = null;
    try {
        modulePath = module.getRootPath();
        defaultPackage = moduleService.resolveDefaultPackage(module);
        if (logger.isDebugEnabled()) {
            logger.debug("Current module path is: " + modulePath);
        }
        ClassLoader classLoader = classLoaderHelper.getModuleClassLoader(module);
        ModelDriver modelDriver = new JavaRoasterModelDriver(ioService, Paths.convert(defaultPackage.getPackageMainSrcPath()), classLoader, filterHolder);
        ModelDriverResult result = modelDriver.loadModel();
        dataModel = result.getDataModel();
        if (processErrors && result.hasErrors()) {
            processErrors(module, result);
        }
        // by now we still use the DMO to calculate module external dependencies.
        ModuleDataModelOracle moduleDataModelOracle = dataModelService.getModuleDataModel(modulePath);
        ModuleDataModelOracleUtils.loadExternalDependencies(dataModel, moduleDataModelOracle, classLoader);
        Long endTime = System.currentTimeMillis();
        if (logger.isDebugEnabled()) {
            logger.debug("Time elapsed when loading " + modulePath.getFileName() + ": " + (endTime - startTime) + " ms");
        }
        return new Pair<DataModel, ModelDriverResult>(dataModel, result);
    } catch (Exception e) {
        logger.error("Data model couldn't be loaded, path: " + modulePath + ", modulePath: " + modulePath + ".", e);
        throw new ServiceException("Data model couldn't be loaded, path: " + modulePath + ", modulePath: " + modulePath + ".", e);
    }
}
Also used : Path(org.uberfire.backend.vfs.Path) ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) DataModel(org.kie.workbench.common.services.datamodeller.core.DataModel) JavaRoasterModelDriver(org.kie.workbench.common.services.datamodeller.driver.impl.JavaRoasterModelDriver) ModelDriver(org.kie.workbench.common.services.datamodeller.driver.ModelDriver) ModuleDataModelOracle(org.kie.soup.project.datamodel.oracle.ModuleDataModelOracle) Package(org.guvnor.common.services.project.model.Package) ModelDriverResult(org.kie.workbench.common.services.datamodeller.driver.model.ModelDriverResult) 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) Pair(org.uberfire.commons.data.Pair)

Example 7 with ServiceException

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

the class DataModelerServiceImpl method updateDataObject.

/**
 * Updates data object provided in the dataObject parameter with the Java code provided in the source parameter.
 * This method does not write changes in the file system.
 * @param dataObject Data object definition to be updated.
 * @param source Java code to use for the update.
 * @param path Path to the java file. (used for error messages adf)
 * @return returns a GenerationResult object with the updated data object and the source and path parameter as is.
 */
@Override
public GenerationResult updateDataObject(final DataObject dataObject, final String source, final Path path) {
    // Resolve the dataobject update in memory
    GenerationResult result = new GenerationResult();
    KieModule module;
    try {
        result.setSource(source);
        module = moduleService.resolveModule(path);
        if (module == null) {
            logger.warn("File : " + path.toURI() + " do not belong to a valid module");
            result.setSource(source);
            return result;
        }
        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());
            }
        }
        return result;
    } catch (Exception e) {
        logger.error("Source file for data object: " + dataObject.getClassName() + ", couldn't be parsed", e);
        throw new ServiceException("Source file for data object: " + dataObject.getClassName() + ", couldn't be parsed", 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)

Example 8 with ServiceException

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

the class FormGenerationIntegrationTest method testNestedFormsGeneration.

@Test
public void testNestedFormsGeneration() {
    when(module.getRootPath()).thenReturn(rootPathWithoutNestedForms);
    ModelDriver modelDriver = new JavaRoasterModelDriver(ioService, Paths.convert(rootPathWithoutNestedForms), moduleClassLoader, filterHolder);
    try {
        ModelDriverResult result = modelDriver.loadModel();
        dataModel = result.getDataModel();
    } catch (ModelDriverException e) {
        throw new ServiceException("It was not possible to load model for URI: " + rootPathWithoutNestedForms.toURI(), e);
    }
    when(dataModelerService.loadModel(module)).thenReturn(dataModel);
    when(commentedOptionFactory.makeCommentedOption(any())).thenReturn(commentedOption);
    final FormGenerationResult formGenerationResult = generateForm("FormGenerationTest_TwinTasks-taskform.frm", taskFormModels.get(4));
    final List<String> expectedDataObjectFormFields = Arrays.asList("nestedObjectForSubform", SUBFORM_FIELDTYPE, "nestedObjectListForMultipleSubform", MULTIPLESUBFORM_FIELDTYPE, "bigDecimalDecimalBox", DECIMALBOX_FIELDTYPE, "bigIntegerIntegerBox", INTEGERBOX_FIELDTYPE, "booleanCheckBox", CHECKBOX_FIELDTYPE, "byteIntegerBox", INTEGERBOX_FIELDTYPE, "charTextBox", TEXTBOX_FIELDTYPE, "dateDatePicker", DATEPICKER_FIELDTYPE, "doubleDecimalBox", DECIMALBOX_FIELDTYPE, "floatDecimalBox", DECIMALBOX_FIELDTYPE, "longIntegerBox", INTEGERBOX_FIELDTYPE, "stringListBox", TEXTBOX_FIELDTYPE, "intIntegerBox", INTEGERBOX_FIELDTYPE, "shortIntegerBox", INTEGERBOX_FIELDTYPE, "bigDecimalRadioGroup", DECIMALBOX_FIELDTYPE, "bigIntegerRadioGroup", INTEGERBOX_FIELDTYPE, "byteRadioGroup", INTEGERBOX_FIELDTYPE, "charRadioGroup", TEXTBOX_FIELDTYPE, "doubleSlider", DECIMALBOX_FIELDTYPE, "doubleRadioGroup", DECIMALBOX_FIELDTYPE, "floatRadioGroup", DECIMALBOX_FIELDTYPE, "intRadioGroup", INTEGERBOX_FIELDTYPE, "intSlider", INTEGERBOX_FIELDTYPE, "longRadioGroup", INTEGERBOX_FIELDTYPE, "stringPicture", TEXTBOX_FIELDTYPE, "stringRadioGroup", TEXTBOX_FIELDTYPE, "stringTextArea", TEXTBOX_FIELDTYPE, "stringTextBox", TEXTBOX_FIELDTYPE, "shortRadioGroup", INTEGERBOX_FIELDTYPE);
    final List<String> expectedNestedDataObjectFormFields = Arrays.asList("stringListBox", TEXBOX_FIELDTYPE, "doubleDecimalBox", DECIMALBOX_FIELDTYPE, "booleanCheckBox", CHECKBOX_FIELDTYPE, "dateDatePicker", DATEPICKER_FIELDTYPE, "charRadioGroup", TEXTBOX_FIELDTYPE, "doubleSlider", DECIMALBOX_FIELDTYPE, "intIntegerBox", INTEGERBOX_FIELDTYPE, "stringPicture", TEXBOX_FIELDTYPE, "stringTextArea", TEXTBOX_FIELDTYPE, "stringTextBox", TEXTBOX_FIELDTYPE);
    final FormDefinition rootForm = formGenerationResult.getRootForm();
    final List<FormDefinition> nestedForms = formGenerationResult.getNestedForms();
    final FormDefinition dataObjectForm = nestedForms.get(0);
    final FormDefinition nestedDataObjectForm = nestedForms.get(1);
    final String dataObjectFormID = dataObjectForm.getId();
    testNestedFormsHaveCorrectId(rootForm, "_dataObject1", dataObjectFormID);
    testNestedFormsHaveCorrectId(rootForm, "_dataObject2", dataObjectFormID);
    testNestedFormsHaveCorrectId(rootForm, "dataObject_", dataObjectFormID);
    final String nestedDataObjectFormID = nestedDataObjectForm.getId();
    testFormDefinition(dataObjectForm, expectedDataObjectFormFields);
    testNestedFormsHaveCorrectId(dataObjectForm, "nestedObjectForSubform", nestedDataObjectFormID);
    testFormDefinition(nestedDataObjectForm, expectedNestedDataObjectFormFields);
}
Also used : ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) FormGenerationResult(org.kie.workbench.common.forms.jbpm.server.service.formGeneration.FormGenerationResult) JavaRoasterModelDriver(org.kie.workbench.common.services.datamodeller.driver.impl.JavaRoasterModelDriver) ModelDriver(org.kie.workbench.common.services.datamodeller.driver.ModelDriver) ModelDriverResult(org.kie.workbench.common.services.datamodeller.driver.model.ModelDriverResult) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) FormDefinition(org.kie.workbench.common.forms.model.FormDefinition) JavaRoasterModelDriver(org.kie.workbench.common.services.datamodeller.driver.impl.JavaRoasterModelDriver) Test(org.junit.Test)

Example 9 with ServiceException

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

Example 10 with ServiceException

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

the class DataModelerServiceImpl method constructContent.

@Override
protected EditorModelContent constructContent(Path path, Overview overview) {
    if (logger.isDebugEnabled()) {
        logger.debug("Loading editor model from path: " + path.toURI());
    }
    Long startTime = System.currentTimeMillis();
    EditorModelContent editorModelContent = new EditorModelContent();
    try {
        KieModule module = moduleService.resolveModule(path);
        if (module == null) {
            logger.warn("File : " + path.toURI() + " do not belong to a valid module");
            return editorModelContent;
        }
        Pair<DataModel, ModelDriverResult> resultPair = loadModel(module, false);
        String className = calculateClassName(module, path);
        editorModelContent.setCurrentModule(module);
        editorModelContent.setPath(path);
        editorModelContent.setCurrentModulePackages(serviceHelper.resolvePackages(module));
        editorModelContent.setDataModel(resultPair.getK1());
        editorModelContent.setDataObject(resultPair.getK1().getDataObject(className));
        editorModelContent.setDataObjectPaths(resultPair.getK2().getClassPaths());
        editorModelContent.setOriginalClassName(className);
        editorModelContent.setOriginalPackageName(NamingUtils.extractPackageName(className));
        // Read the sources for the file being edited.
        if (ioService.exists(Paths.convert(path))) {
            String source = ioService.readAllString(Paths.convert(path));
            editorModelContent.setSource(source);
        }
        if (resultPair.getK2().hasErrors()) {
            editorModelContent.setErrors(serviceHelper.toDataModelerError(resultPair.getK2().getErrors()));
        }
        editorModelContent.setOverview(overview);
        editorModelContent.setElapsedTime(System.currentTimeMillis() - startTime);
        if (logger.isDebugEnabled()) {
            logger.debug("Time elapsed when loading editor model from:" + path + " : " + editorModelContent.getElapsedTime() + " ms");
        }
        return editorModelContent;
    } catch (Exception e) {
        logger.error("Editor model couldn't be loaded from path: " + (path != null ? path.toURI() : path) + ".", e);
        throw new ServiceException("Editor model couldn't be loaded from path: " + (path != null ? path.toURI() : path) + ".", e);
    }
}
Also used : ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) DataModel(org.kie.workbench.common.services.datamodeller.core.DataModel) EditorModelContent(org.kie.workbench.common.screens.datamodeller.model.EditorModelContent) ModelDriverResult(org.kie.workbench.common.services.datamodeller.driver.model.ModelDriverResult) 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)

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