Skip to content

Commit

Permalink
olingo: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rareddy committed Oct 10, 2014
1 parent dbc516f commit 094840c
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 59 deletions.
177 changes: 118 additions & 59 deletions olingo/src/main/java/org/teiid/olingo/OData4EntitySchemaBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.apache.olingo.server.api.edm.provider.EntityContainer;
import org.apache.olingo.server.api.edm.provider.EntitySet;
import org.apache.olingo.server.api.edm.provider.EntityType;
import org.apache.olingo.server.api.edm.provider.Function;
import org.apache.olingo.server.api.edm.provider.FunctionImport;
import org.apache.olingo.server.api.edm.provider.NavigationProperty;
import org.apache.olingo.server.api.edm.provider.NavigationPropertyBinding;
import org.apache.olingo.server.api.edm.provider.Parameter;
Expand All @@ -57,14 +59,11 @@

public class OData4EntitySchemaBuilder {

public static org.apache.olingo.server.api.edm.provider.Schema buildMetadata(
org.teiid.metadata.Schema teiidSchema) {
public static org.apache.olingo.server.api.edm.provider.Schema buildMetadata(org.teiid.metadata.Schema teiidSchema) {
try {
org.apache.olingo.server.api.edm.provider.Schema edmSchema = new org.apache.olingo.server.api.edm.provider.Schema();

buildEntityTypes(teiidSchema, edmSchema);
buildActions(teiidSchema, edmSchema);

buildProcedures(teiidSchema, edmSchema);
return edmSchema;
} catch (Exception e) {
throw new TeiidRuntimeException(e);
Expand All @@ -81,13 +80,11 @@ static EntitySet findEntitySet(org.apache.olingo.server.api.edm.provider.Schema
return null;
}

static org.apache.olingo.server.api.edm.provider.Schema findSchema(
Map<String, org.apache.olingo.server.api.edm.provider.Schema> edmSchemas, String schemaName) {
static org.apache.olingo.server.api.edm.provider.Schema findSchema(Map<String, org.apache.olingo.server.api.edm.provider.Schema> edmSchemas, String schemaName) {
return edmSchemas.get(schemaName);
}

static EntityType findEntityType(Map<String, org.apache.olingo.server.api.edm.provider.Schema> edmSchemas,
String schemaName, String enitityName) {
static EntityType findEntityType(Map<String, org.apache.olingo.server.api.edm.provider.Schema> edmSchemas, String schemaName, String enitityName) {
org.apache.olingo.server.api.edm.provider.Schema schema = findSchema(edmSchemas, schemaName);
if (schema != null) {
for (EntityType type : schema.getEntityTypes()) {
Expand All @@ -99,9 +96,7 @@ static EntityType findEntityType(Map<String, org.apache.olingo.server.api.edm.pr
return null;
}

static EntityContainer findEntityContainer(
Map<String, org.apache.olingo.server.api.edm.provider.Schema> edmSchemas,
String schemaName) {
static EntityContainer findEntityContainer(Map<String, org.apache.olingo.server.api.edm.provider.Schema> edmSchemas, String schemaName) {
org.apache.olingo.server.api.edm.provider.Schema schema = edmSchemas.get(schemaName);
return schema.getEntityContainer();
}
Expand All @@ -116,9 +111,7 @@ public static void buildEntityTypes(Schema schema, org.apache.olingo.server.api.
KeyRecord primaryKey = table.getPrimaryKey();
List<KeyRecord> uniques = table.getUniqueKeys();
if (primaryKey == null && uniques.isEmpty()) {
LogManager.logDetail(LogConstants.CTX_ODATA,
ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16017,
table.getFullName()));
LogManager.logDetail(LogConstants.CTX_ODATA,ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16017,table.getFullName()));
continue;
}

Expand Down Expand Up @@ -255,66 +248,132 @@ private static void buildNavigationProperties(Table table,
entitySet.setNavigationPropertyBindings(navigationBindingProperties);
}

public static void buildActions(Schema schema,
org.apache.olingo.server.api.edm.provider.Schema edmSchema) {
public static void buildProcedures(Schema schema, org.apache.olingo.server.api.edm.provider.Schema edmSchema) {
// procedures
ArrayList<ComplexType> complexTypes = new ArrayList<ComplexType>();
ArrayList<Function> functions = new ArrayList<Function>();
ArrayList<FunctionImport> functionImports = new ArrayList<FunctionImport>();
ArrayList<Action> actions = new ArrayList<Action>();
ArrayList<ActionImport> actionImports = new ArrayList<ActionImport>();

for (Procedure proc : schema.getProcedures().values()) {
if (doesProcedureReturn(proc)) {
buildFunction(schema.getName(), proc, complexTypes, functions, functionImports);
}
else {
buildAction(schema.getName(), proc, complexTypes, actions, actionImports);
}
}
edmSchema.setComplexTypes(complexTypes);
edmSchema.setFunctions(functions);
edmSchema.setActions(actions);
edmSchema.getEntityContainer().setFunctionImports(functionImports);
edmSchema.getEntityContainer().setActionImports(actionImports);
}

Action edmAction = new Action();
edmAction.setName(proc.getName());
edmAction.setBound(false);
private static boolean doesProcedureReturn(Procedure proc) {
for (ProcedureParameter pp : proc.getParameters()) {
if (pp.getName().equals("return")) { //$NON-NLS-1$
return true;
}
}
return false;
}

ArrayList<Parameter> params = new ArrayList<Parameter>();
for (ProcedureParameter pp : proc.getParameters()) {
if (pp.getName().equals("return")) { //$NON-NLS-1$
edmAction.setReturnType(new ReturnType().setType(ODataTypeManager.odataType(pp.getRuntimeType())
.getFullQualifiedName()));
continue;
}
public static void buildFunction(String schemaName, Procedure proc, ArrayList<ComplexType> complexTypes, ArrayList<Function> functions, ArrayList<FunctionImport> functionImports) {
Function edmFunction = new Function();
edmFunction.setName(proc.getName());
edmFunction.setBound(false);

Parameter param = new Parameter();
param.setName(pp.getName());
param.setType(ODataTypeManager.odataType(pp.getRuntimeType()).getFullQualifiedName());
ArrayList<Parameter> params = new ArrayList<Parameter>();
for (ProcedureParameter pp : proc.getParameters()) {
if (pp.getName().equals("return")) { //$NON-NLS-1$
edmFunction.setReturnType(new ReturnType().setType(ODataTypeManager.odataType(pp.getRuntimeType()).getFullQualifiedName()));
continue;
}

if (DataTypeManager.isArrayType(pp.getRuntimeType())) {
param.setCollection(true);
}
param.setNullable(pp.getNullType() == NullType.Nullable);
params.add(param);
Parameter param = new Parameter();
param.setName(pp.getName());
param.setType(ODataTypeManager.odataType(pp.getRuntimeType()).getFullQualifiedName());

if (DataTypeManager.isArrayType(pp.getRuntimeType())) {
param.setCollection(true);
}
edmAction.setParameters(params);

// add a complex type for return resultset.
ColumnSet<Procedure> returnColumns = proc.getResultSet();
if (returnColumns != null) {
ComplexType complexType = new ComplexType();
String entityTypeName = proc.getName() + "_" + returnColumns.getName(); //$NON-NLS-1$
complexType.setName(entityTypeName);

ArrayList<Property> props = new ArrayList<Property>();
for (Column c : returnColumns.getColumns()) {
props.add(buildProperty(c));
}
complexType.setProperties(props);
param.setNullable(pp.getNullType() == NullType.Nullable);
params.add(param);
}
edmFunction.setParameters(params);

// add a complex type for return resultset.
ColumnSet<Procedure> returnColumns = proc.getResultSet();
if (returnColumns != null) {
ComplexType complexType = new ComplexType();
String entityTypeName = proc.getName() + "_" + returnColumns.getName(); //$NON-NLS-1$
complexType.setName(entityTypeName);

ArrayList<Property> props = new ArrayList<Property>();
for (Column c : returnColumns.getColumns()) {
props.add(buildProperty(c));
}
complexType.setProperties(props);

complexTypes.add(complexType);
edmFunction.setReturnType((new ReturnType().setType(new FullQualifiedName(schemaName, complexType.getName())).setCollection(true)));
}

complexTypes.add(complexType);
edmAction.setReturnType((new ReturnType().setType(new FullQualifiedName(schema.getName(), complexType
.getName())).setCollection(true)));
FunctionImport functionImport = new FunctionImport();
functionImport.setName(proc.getName()).setFunction(new FullQualifiedName(schemaName, proc.getName()));

functions.add(edmFunction);
functionImports.add(functionImport);
}

public static void buildAction(String schemaName, Procedure proc, ArrayList<ComplexType> complexTypes, ArrayList<Action> actions, ArrayList<ActionImport> actionImports) {
Action edmAction = new Action();
edmAction.setName(proc.getName());
edmAction.setBound(false);

ArrayList<Parameter> params = new ArrayList<Parameter>();
for (ProcedureParameter pp : proc.getParameters()) {
if (pp.getName().equals("return")) { //$NON-NLS-1$
edmAction.setReturnType(new ReturnType().setType(ODataTypeManager.odataType(pp.getRuntimeType()).getFullQualifiedName()));
continue;
}

ActionImport actionImport = new ActionImport();
actionImport.setName(proc.getName()).setAction(new FullQualifiedName(schema.getName(), proc.getName()));
Parameter param = new Parameter();
param.setName(pp.getName());
param.setType(ODataTypeManager.odataType(pp.getRuntimeType()).getFullQualifiedName());

actions.add(edmAction);
actionImports.add(actionImport);
if (DataTypeManager.isArrayType(pp.getRuntimeType())) {
param.setCollection(true);
}
param.setNullable(pp.getNullType() == NullType.Nullable);
params.add(param);
}
edmSchema.setComplexTypes(complexTypes);
edmSchema.setActions(actions);
edmSchema.getEntityContainer().setActionImports(actionImports);
edmAction.setParameters(params);

// add a complex type for return resultset.
ColumnSet<Procedure> returnColumns = proc.getResultSet();
if (returnColumns != null) {
ComplexType complexType = new ComplexType();
String entityTypeName = proc.getName() + "_" + returnColumns.getName(); //$NON-NLS-1$
complexType.setName(entityTypeName);

ArrayList<Property> props = new ArrayList<Property>();
for (Column c : returnColumns.getColumns()) {
props.add(buildProperty(c));
}
complexType.setProperties(props);

complexTypes.add(complexType);
edmAction.setReturnType((new ReturnType().setType(new FullQualifiedName(schemaName, complexType.getName())).setCollection(true)));
}

ActionImport actionImport = new ActionImport();
actionImport.setName(proc.getName()).setAction(new FullQualifiedName(schemaName, proc.getName()));

actions.add(edmAction);
actionImports.add(actionImport);
}

static List<String> getColumnNames(List<Column> columns) {
Expand Down
15 changes: 15 additions & 0 deletions olingo/src/test/java/org/teiid/olingo/TestODataIntegration.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public void tesServiceMetadata() throws Exception {

ContentResponse response = http.GET("http://localhost:"+port+"/odata4/loopy/VM1");
Assert.assertEquals(200, response.getStatus());
System.out.println(response.getContentAsString());
//TODO: match the document here.. port is being random
}

Expand Down Expand Up @@ -241,6 +242,20 @@ public void testIndividualProperty() throws Exception {
Assert.assertEquals("{\"@odata.context\":\"$metadata#Edm.String\",\"value\":\"ABCDEFGHIJ\"}", response.getContentAsString());
}

@Test
public void testProcedure() throws Exception {
// for usage see
// http://www.eclipse.org/jetty/documentation/current/http-client-api.html
HttpClientTransportOverHTTP transport = new HttpClientTransportOverHTTP();
HttpClient http = new HttpClient(transport, null);
transport.setHttpClient(http);
http.start();

ContentResponse response = http.GET("http://localhost:"+port+"/odata4/loopy/vm1/proc(x='foo')");
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("{\"@odata.context\":\"$metadata#Edm.String\",\"value\":\"ABCDEFGHIJ\"}", response.getContentAsString());
}

/*
protected Client mockClient2() {
Client client = mock(Client.class);
Expand Down

0 comments on commit 094840c

Please sign in to comment.