Search in sources :

Example 16 with Operation

use of com.developmentontheedge.be5.metadata.model.Operation in project be5 by DevelopmentOnTheEdge.

the class ReadModelFromXmlTest method testWriteReadCompareXmlProject.

@Test
public void testWriteReadCompareXmlProject() throws Exception {
    final Project project = new Project("TestProject");
    project.setRoles(Arrays.asList("Admin", "Guest"));
    final Module module = project.getApplication();
    final Entity table = new Entity("testTable", module, EntityType.TABLE);
    DataElementUtils.saveQuiet(table);
    table.setDisplayName("$project.Name/$module.Name/$entity.Name");
    final Operation op = Operation.createOperation("Insert", Operation.OPERATION_TYPE_JAVA, table);
    DataElementUtils.saveQuiet(op);
    final Query query = new Query("All records", table);
    DataElementUtils.saveQuiet(query);
    query.setQuery("<@distinct \"name\"/>");
    query.setParametrizingOperation(op);
// TODO
// project.getMacroCollection().optScript( FreemarkerCatalog.MAIN_MACRO_LIBRARY )
// .setSource( "<#macro distinct column>SELECT DISTINCT ${column} FROM ${entity.getName()}</#macro>" );
// final BeModelCollection<TableRef> tableRefs = table.getOrCreateTableReferences();
// final TableRef tableRef = new TableRef("ref1", "user", tableRefs);
// tableRef.setTableTo("users");
// tableRef.setColumnsTo("userName");
// DataElementUtils.saveQuiet(tableRef);
// 
// final Path tempFolder = Files.createTempDirectory("be4-temp");
// Serialization.save( project, tempFolder );
// 
// final Project project2 = Serialization.load( tempFolder );
// assertEquals(new HashSet<>(Arrays.asList( "Admin", "Guest" )), project2.getRoles());
// assertEquals( project.getMacroCollection().optScript( FreemarkerCatalog.MAIN_MACRO_LIBRARY ).getSource(),
// project2.getMacroCollection().optScript( FreemarkerCatalog.MAIN_MACRO_LIBRARY ).getSource() );
// final Entity table2 = project2.getApplication().getEntity( "testTable" );
// assertNotNull(table2);
// assertEquals(table, table2);
// assertEquals(table2, table);
// final Query query2 = table2.getQueries().get("All records");
// assertNotNull(query2);
// assertEquals("<@distinct \"name\"/>", query2.getQuery());
// final Operation op2 = table2.getOperations().get("Insert");
// assertNotNull(op2);
// assertEquals(op, op2);
// assertSame(op2, query2.getParametrizingOperation());
// 
// final BeModelCollection<TableRef> tableReferences = project.getEntity("testTable").getOrCreateTableReferences();
// final BeModelCollection<TableRef> tableReferences2 = project2.getEntity("testTable").getOrCreateTableReferences();
// assertEquals(tableReferences.getSize(), tableReferences2.getSize());
// assertEquals(tableReferences.iterator().next().getColumnsFrom(), tableReferences2.iterator().next().getColumnsFrom());
// 
// FileUtils.deleteRecursively( tempFolder );
}
Also used : Project(com.developmentontheedge.be5.metadata.model.Project) Entity(com.developmentontheedge.be5.metadata.model.Entity) Query(com.developmentontheedge.be5.metadata.model.Query) Operation(com.developmentontheedge.be5.metadata.model.Operation) Module(com.developmentontheedge.be5.metadata.model.Module) Test(org.junit.Test)

Example 17 with Operation

use of com.developmentontheedge.be5.metadata.model.Operation in project be5 by DevelopmentOnTheEdge.

the class GroovyOperationLoader method initOperationMap.

void initOperationMap() {
    Map<String, com.developmentontheedge.be5.metadata.model.Operation> newOperationMap = new HashMap<>();
    List<Entity> entities = meta.getOrderedEntities("ru");
    for (Entity entity : entities) {
        List<String> operationNames = meta.getOperationNames(entity);
        for (String operationName : operationNames) {
            com.developmentontheedge.be5.metadata.model.Operation operation = meta.getOperationIgnoringRoles(entity, operationName);
            if (operation.getType().equals(OPERATION_TYPE_GROOVY)) {
                GroovyOperation groovyOperation = (GroovyOperation) operation;
                String fileName = groovyOperation.getFileName().replace("/", ".");
                newOperationMap.put(fileName, operation);
            }
        }
    }
    groovyOperationsMap = newOperationMap;
}
Also used : Entity(com.developmentontheedge.be5.metadata.model.Entity) Operation(com.developmentontheedge.be5.metadata.model.Operation) HashMap(java.util.HashMap) Operation(com.developmentontheedge.be5.metadata.model.Operation) GroovyOperation(com.developmentontheedge.be5.metadata.model.GroovyOperation) GroovyOperation(com.developmentontheedge.be5.metadata.model.GroovyOperation)

Example 18 with Operation

use of com.developmentontheedge.be5.metadata.model.Operation in project be5 by DevelopmentOnTheEdge.

the class OperationExecutorImpl method create.

@Override
public Operation create(OperationInfo operationInfo, OperationContext operationContext) {
    Operation operation;
    switch(operationInfo.getType()) {
        case OPERATION_TYPE_GROOVY:
            try {
                Class aClass = groovyOperationLoader.get(operationInfo);
                if (aClass != null) {
                    operation = (Operation) aClass.newInstance();
                } else {
                    throw Be5Exception.internalInOperation(new Error("Class " + operationInfo.getCode() + " is null."), operationInfo.getModel());
                }
            } catch (NoClassDefFoundError | IllegalAccessException | InstantiationException e) {
                throw new UnsupportedOperationException("Groovy feature has been excluded", e);
            } catch (Throwable e) {
                throw Be5Exception.internalInOperation(e, operationInfo.getModel());
            }
            break;
        case OPERATION_TYPE_JAVA:
            try {
                operation = (Operation) Class.forName(operationInfo.getCode()).newInstance();
                break;
            } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
                throw Be5Exception.internalInOperation(new RuntimeException("It is possible to use the 'file:' instead of the 'code:' " + "in the yaml file. \n\t" + e.getMessage(), e), operationInfo.getModel());
            }
        case OPERATION_TYPE_JAVAFUNCTION:
        case OPERATION_TYPE_SQL:
        case OPERATION_TYPE_JAVASCRIPT:
        case OPERATION_TYPE_JSSERVER:
        case OPERATION_TYPE_DOTNET:
        case OPERATION_TYPE_JAVADOTNET:
            throw Be5Exception.internal("Not support operation type: " + operationInfo.getType());
        default:
            throw Be5Exception.internal("Unknown action type '" + operationInfo.getType() + "'");
    }
    operation.initialize(operationInfo, operationContext, OperationResult.create());
    injector.injectAnnotatedFields(operation);
    return operation;
}
Also used : Operation(com.developmentontheedge.be5.metadata.model.Operation) TransactionalOperation(com.developmentontheedge.be5.operation.TransactionalOperation) Operation(com.developmentontheedge.be5.operation.Operation)

Example 19 with Operation

use of com.developmentontheedge.be5.metadata.model.Operation in project be5 by DevelopmentOnTheEdge.

the class OperationServiceImpl method execute.

@Override
public Either<Object, OperationResult> execute(Operation operation, Map<String, Object> values) {
    Map<String, Object> presetValues = getPresetValues(operation.getContext(), values);
    operation.setResult(OperationResult.execute());
    Object parameters = operationExecutor.execute(operation, presetValues);
    if (OperationStatus.ERROR == operation.getStatus()) {
        try {
            validator.isError(parameters);
        } catch (RuntimeException e) {
            log.log(Level.INFO, "error on execute in parameters", e);
            operation.setResult(OperationResult.error(e));
            return replaceNullValueToEmptyStringAndReturn(parameters);
        }
        OperationResult invokeResult = operation.getResult();
        operation.setResult(OperationResult.execute());
        Object newParameters = operationExecutor.generate(operation, presetValues);
        if (OperationStatus.ERROR == operation.getStatus()) {
            return Either.second(invokeResult);
        }
        if (newParameters != null) {
            operation.setResult(invokeResult);
            return replaceNullValueToEmptyStringAndReturn(newParameters);
        } else {
            return Either.second(invokeResult);
        }
    }
    return Either.second(operation.getResult());
}
Also used : OperationResult(com.developmentontheedge.be5.operation.OperationResult)

Example 20 with Operation

use of com.developmentontheedge.be5.metadata.model.Operation in project be5 by DevelopmentOnTheEdge.

the class Menu method generateEntityOperations.

private List<OperationNode> generateEntityOperations(Entity entity, Meta meta, UserAwareMeta userAwareMeta, List<String> roles, boolean withIds) {
    List<OperationNode> operations = new ArrayList<>();
    Query allRecords = entity.getQueries().get(DatabaseConstants.ALL_RECORDS_VIEW);
    String insertOperationName = INSERT_OPERATION;
    if (allRecords != null && allRecords.getOperationNames().getFinalValues().contains(insertOperationName)) {
        Operation insertOperation = entity.getOperations().get(insertOperationName);
        if (insertOperation != null && meta.isAvailableFor(insertOperation, roles)) {
            String title = userAwareMeta.getLocalizedOperationTitle(entity.getName(), insertOperationName);
            Action action = ActionUtils.toAction(DatabaseConstants.ALL_RECORDS_VIEW, insertOperation);
            OperationId id = withIds ? new OperationId(entity.getName(), insertOperationName) : null;
            OperationNode operation = new OperationNode(id, title, action);
            operations.add(operation);
        }
    }
    return operations;
}
Also used : Action(com.developmentontheedge.be5.model.Action) Query(com.developmentontheedge.be5.metadata.model.Query) ArrayList(java.util.ArrayList) Operation(com.developmentontheedge.be5.metadata.model.Operation)

Aggregations

Operation (com.developmentontheedge.be5.metadata.model.Operation)9 Entity (com.developmentontheedge.be5.metadata.model.Entity)6 Operation (com.developmentontheedge.be5.operation.Operation)6 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 Module (com.developmentontheedge.be5.metadata.model.Module)5 Query (com.developmentontheedge.be5.metadata.model.Query)5 Project (com.developmentontheedge.be5.metadata.model.Project)4 OperationResult (com.developmentontheedge.be5.operation.OperationResult)4 Path (java.nio.file.Path)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ReadException (com.developmentontheedge.be5.metadata.exception.ReadException)2 BeModelElement (com.developmentontheedge.be5.metadata.model.base.BeModelElement)2 Action (com.developmentontheedge.be5.model.Action)2 FormPresentation (com.developmentontheedge.be5.model.FormPresentation)2 TableOperationPresentation (com.developmentontheedge.be5.model.TableOperationPresentation)2 ResourceData (com.developmentontheedge.be5.model.jsonapi.ResourceData)2 SqlMockOperationTest (com.developmentontheedge.be5.test.SqlMockOperationTest)2 HashUrl (com.developmentontheedge.be5.util.HashUrl)2