How to create a bounded action in Olingo V4 (java)

我与影子孤独终老i 提交于 2019-12-10 15:44:17

问题


I have tried looking everywhere but couldn't figure out how to implement bounded actions in olingo V4 java.

Everywhere unbounded action tutorial is given.

I tried tweaking this code.

  final CsdlAction action = new CsdlAction();
  action.setName("testAction");
  action.setBound(true);

This gives me error when I try to access $metadata API.

If anyone can point me towards a good tutorial on how to go about it then it would be great.


回答1:


I looked into the source code of olingo and debugged their code. After a lot of research work I was able to implement bounded actions in Olingo.

Suppose we want to implement a bounded action which is bounded to an entity type X and which return an entity Y.

Changes which needs to be made are:

Metadata Document: In your java class (custom class) which extends CsdlAbstractEdmProvider or implements CsdlEdmProvider,

implement getActions(...) functions

// Action Names
public static final String ACTION_EXECUTE_NAME = "Execute";

// FullyQualified Action Names
public static final FullQualifiedName ACTION_EXECUTE_FQN = new FullQualifiedName("StackOverflow", ACTION_EXECUTE_NAME);

@Override
public List<CsdlAction> getActions(FullQualifiedName actionName) throws ODataException {
    if (actionName.equals(ACTION_EXECUTE_FQN)) {
        // It is allowed to overload actions, so we have to provide a list
        // of Actions for each action name
        final List<CsdlAction> actions = new ArrayList<CsdlAction>();

        // Create the Csdl Action
        final CsdlAction action = new CsdlAction();
        action.setName(ACTION_EXECUTE_FQN.getName());
        action.setBound(true);

        // Creating Parameter the first one being binding parameter
        final List<CsdlParameter> parameters = new ArrayList<CsdlParameter>();
        final CsdlParameter parameter = new CsdlParameter();
        parameter.setName("Parameter1");
        parameter.setType(X);
        parameter.setNullable(true);
        parameter.setCollection(false);
        parameters.add(parameter);
        action.setParameters(parameters);

        action.setReturnType(new CsdlReturnType().setCollection(false).setType(Y));

        actions.add(action);
        return actions;
    }
    return null;
}

and in the getSchemas(...) of the same metadata provider class cal the getActions(...) method.

@Override
public List<CsdlSchema> getSchemas() throws ODataException {
    // create Schema
    CsdlSchema schema = new CsdlSchema();
    schema.setNamespace("Stackoverflow");

    // add EntityTypes
    List<CsdlEntityType> entityTypes = new ArrayList<CsdlEntityType>();
    entityTypes.add(getEntityType(X));
    entityTypes.add(getEntityType(Y));

    schema.setEntityTypes(entityTypes);

    // add EntityContainer
    schema.setEntityContainer(getEntityContainer());

    // add bounded actions
    List<CsdlAction> actions = new ArrayList<CsdlAction>();
    schema.setActions(actions);
    actions.addAll(getActions(ACTION_EXECUTE_FQN));

    List<CsdlSchema> schemas = new ArrayList<CsdlSchema>();
    schemas.add(schema);
    return schemas;
}

what we have done is that, created a bounded a action named ACTION_EXECUTE_FQN with parameter as first argument of action, in our case being the entity X and return type is entity Y.

Service Implementation: Now, service implementation is also needed. According to the use case which has been taken, we need a create a class implementing ActionEntityProcessor .

After this, everything is same. I hope this will help. There are other ActionProcessors depending on the return type of actions and the type of parameter to which action is bounded.



来源:https://stackoverflow.com/questions/38736115/how-to-create-a-bounded-action-in-olingo-v4-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!