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.