[OLINGO-187] Using commons EdmType hierarchy everywhere

This commit is contained in:
Francesco Chicchiriccò 2014-03-05 16:12:53 +01:00
parent bae3d847a8
commit cce0ba5c01
7 changed files with 2 additions and 539 deletions

View File

@ -1,111 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.odata4.client.api.edm;
import org.apache.olingo.odata4.client.api.data.EdmSimpleType;
import org.apache.olingo.odata4.client.api.edm.xml.ComplexType;
import org.apache.olingo.odata4.client.api.edm.xml.EntityType;
import org.apache.olingo.odata4.client.api.edm.xml.EnumType;
public interface EdmType {
/**
* Checks if is a collection.
*
* @return 'TRUE' if is a collection; 'FALSE' otherwise.
*/
boolean isCollection();
/**
* Checks if is a simple type.
*
* @return 'TRUE' if is a simple type; 'FALSE' otherwise.
*/
boolean isSimpleType();
/**
* Gets type as a simple type.
*
* @return simple type. An <tt>EdmTypeNotFoundException</tt> will be raised if it is not a simple type.
*/
EdmSimpleType getSimpleType();
/**
* Checks if is an enum type.
*
* @return 'TRUE' if is an enum type; 'FALSE' otherwise.
*/
boolean isEnumType();
/**
* Gets type as enum type.
*
* @return enum type. An <tt>EdmTypeNotFoundException</tt> will be raised if it is not an enum type.
*/
EnumType getEnumType();
/**
* Checks if is a complex type.
*
* @return 'TRUE' if is a complex type; 'FALSE' otherwise.
*/
boolean isComplexType();
/**
* Gets type as complex type.
*
* @return complex type. An <tt>EdmTypeNotFoundException</tt> will be raised if it is not a complex type.
*/
ComplexType getComplexType();
/**
* Checks if is an entity type.
*
* @return 'TRUE' if is an entity type; 'FALSE' otherwise.
*/
boolean isEntityType();
/**
* Gets type as entity type.
*
* @return entity type. An <tt>EdmTypeNotFoundException</tt> will be raised if it is not an entity type.
*/
EntityType getEntityType();
/**
* Gets base type.
*
* @return base type.
*/
String getBaseType();
/**
* Gets type expression.
*
* @return type expression.
*/
String getTypeExpression();
/**
* Gets namespace or alias retrieved from the provided type expression.
*
* @return namespace or alias.
*/
String getNamespaceOrAlias();
}

View File

@ -1,37 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.odata4.client.api.edm;
/**
* This exception indicates that a certain type is not found.
*/
public class EdmTypeNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1685118875699966611L;
/**
* Constructor.
*
* @param type type in object.
* @param typeExpression type expression.
*/
public EdmTypeNotFoundException(final Class<?> type, final String typeExpression) {
super("No " + type.getSimpleName() + " found in " + typeExpression);
}
}

View File

@ -1,52 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.odata4.client.core.edm.v3;
import org.apache.olingo.odata4.client.api.edm.xml.XMLMetadata;
import org.apache.olingo.odata4.client.core.edm.xml.AbstractEdmType;
import org.apache.olingo.odata4.client.core.edm.xml.v3.ComplexTypeImpl;
import org.apache.olingo.odata4.client.core.edm.xml.v3.EntityTypeImpl;
import org.apache.olingo.odata4.client.core.edm.xml.v3.EnumTypeImpl;
public class EdmTypeImpl extends AbstractEdmType {
public EdmTypeImpl(final String typeExpression) {
super(typeExpression);
}
public EdmTypeImpl(final XMLMetadata metadata, final String typeExpression) {
super(metadata, typeExpression);
}
@Override
public EnumTypeImpl getEnumType() {
return (EnumTypeImpl) super.getEnumType();
}
@Override
public ComplexTypeImpl getComplexType() {
return (ComplexTypeImpl) super.getComplexType();
}
@Override
public EntityTypeImpl getEntityType() {
return (EntityTypeImpl) super.getEntityType();
}
}

View File

@ -1,51 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.odata4.client.core.edm.v4;
import org.apache.olingo.odata4.client.api.edm.xml.XMLMetadata;
import org.apache.olingo.odata4.client.core.edm.xml.AbstractEdmType;
import org.apache.olingo.odata4.client.core.edm.xml.v4.ComplexTypeImpl;
import org.apache.olingo.odata4.client.core.edm.xml.v4.EntityTypeImpl;
import org.apache.olingo.odata4.client.core.edm.xml.v4.EnumTypeImpl;
public class EdmTypeImpl extends AbstractEdmType {
public EdmTypeImpl(final String typeExpression) {
super(typeExpression);
}
public EdmTypeImpl(final XMLMetadata metadata, final String typeExpression) {
super(metadata, typeExpression);
}
@Override
public EnumTypeImpl getEnumType() {
return (EnumTypeImpl) super.getEnumType();
}
@Override
public ComplexTypeImpl getComplexType() {
return (ComplexTypeImpl) super.getComplexType();
}
@Override
public EntityTypeImpl getEntityType() {
return (EntityTypeImpl) super.getEntityType();
}
}

View File

@ -1,262 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.odata4.client.core.edm.xml;
import org.apache.commons.lang3.StringUtils;
import org.apache.olingo.odata4.client.api.data.EdmSimpleType;
import org.apache.olingo.odata4.client.api.edm.xml.ComplexType;
import org.apache.olingo.odata4.client.api.edm.EdmType;
import org.apache.olingo.odata4.client.api.edm.EdmTypeNotFoundException;
import org.apache.olingo.odata4.client.api.edm.xml.EntityType;
import org.apache.olingo.odata4.client.api.edm.xml.EnumType;
import org.apache.olingo.odata4.client.api.edm.xml.Schema;
import org.apache.olingo.odata4.client.api.edm.xml.XMLMetadata;
/**
* Parse type information from metadata into semantic data.
*/
public abstract class AbstractEdmType implements EdmType {
private final String typeExpression;
private final String baseType;
private final String namespaceOrAlias;
private boolean collection;
private EdmSimpleType simpleType;
private EnumType enumType;
private ComplexType complexType;
private EntityType entityType;
/**
* Constructor.
*
* @param typeExpression type expression.
*/
public AbstractEdmType(final String typeExpression) {
this(null, typeExpression);
}
/**
* Constructor.
*
* @param metadata metadata.
* @param typeExpression type expression.
*/
public AbstractEdmType(final XMLMetadata metadata, final String typeExpression) {
this.typeExpression = typeExpression;
final int collectionStartIdx = typeExpression.indexOf("Collection(");
final int collectionEndIdx = typeExpression.lastIndexOf(')');
if (collectionStartIdx == -1) {
baseType = typeExpression;
} else {
if (collectionEndIdx == -1) {
throw new IllegalArgumentException("Malformed type: " + typeExpression);
}
this.collection = true;
baseType = typeExpression.substring(collectionStartIdx + 11, collectionEndIdx);
}
final int lastDotIdx = baseType.lastIndexOf('.');
if (lastDotIdx == -1) {
throw new IllegalArgumentException("Cannot find namespace or alias in " + typeExpression);
}
namespaceOrAlias = baseType.substring(0, lastDotIdx);
final String typeName = baseType.substring(lastDotIdx + 1);
if (StringUtils.isBlank(typeName)) {
throw new IllegalArgumentException("Null or empty type name in " + typeExpression);
}
if (namespaceOrAlias.equals(EdmSimpleType.namespace())) {
this.simpleType = EdmSimpleType.fromValue(EdmSimpleType.namespace() + "." + typeName);
} else if (metadata != null) {
if (!metadata.isNsOrAlias(namespaceOrAlias)) {
throw new IllegalArgumentException("Illegal namespace or alias: " + namespaceOrAlias);
}
final Schema schema = metadata.getSchema(namespaceOrAlias);
for (EnumType type : schema.getEnumTypes()) {
if (typeName.equals(type.getName())) {
this.enumType = type;
}
}
if (this.enumType == null) {
for (ComplexType type : schema.getComplexTypes()) {
if (typeName.equals(type.getName())) {
this.complexType = type;
}
}
if (this.complexType == null) {
for (EntityType type : schema.getEntityTypes()) {
if (typeName.equals(type.getName())) {
this.entityType = type;
}
}
}
}
if (!isSimpleType() && !isEnumType() && !isComplexType() && !isEntityType()) {
throw new IllegalArgumentException("Could not parse type information out of " + typeExpression);
}
}
}
/**
* Checks if is a collection.
*
* @return 'TRUE' if is a collection; 'FALSE' otherwise.
*/
@Override
public final boolean isCollection() {
return this.collection;
}
/**
* Checks if is a simple type.
*
* @return 'TRUE' if is a simple type; 'FALSE' otherwise.
*/
@Override
public final boolean isSimpleType() {
return this.simpleType != null;
}
/**
* Gets type as a simple type.
*
* @return simple type. An <tt>EdmTypeNotFoundException</tt> will be raised if it is not a simple type.
*/
@Override
public final EdmSimpleType getSimpleType() {
if (!isSimpleType()) {
throw new EdmTypeNotFoundException(EdmSimpleType.class, this.typeExpression);
}
return this.simpleType;
}
/**
* Checks if is an enum type.
*
* @return 'TRUE' if is an enum type; 'FALSE' otherwise.
*/
@Override
public final boolean isEnumType() {
return this.enumType != null;
}
/**
* Gets type as enum type.
*
* @return enum type. An <tt>EdmTypeNotFoundException</tt> will be raised if it is not an enum type.
*/
@Override
public EnumType getEnumType() {
if (!isEnumType()) {
throw new EdmTypeNotFoundException(AbstractEnumType.class, this.typeExpression);
}
return this.enumType;
}
/**
* Checks if is a complex type.
*
* @return 'TRUE' if is a complex type; 'FALSE' otherwise.
*/
@Override
public final boolean isComplexType() {
return this.complexType != null;
}
/**
* Gets type as complex type.
*
* @return complex type. An <tt>EdmTypeNotFoundException</tt> will be raised if it is not a complex type.
*/
@Override
public ComplexType getComplexType() {
if (!isComplexType()) {
throw new EdmTypeNotFoundException(AbstractComplexType.class, this.typeExpression);
}
return this.complexType;
}
/**
* Checks if is an entity type.
*
* @return 'TRUE' if is an entity type; 'FALSE' otherwise.
*/
@Override
public final boolean isEntityType() {
return this.entityType != null;
}
/**
* Gets type as entity type.
*
* @return entity type. An <tt>EdmTypeNotFoundException</tt> will be raised if it is not an entity type.
*/
@Override
public EntityType getEntityType() {
if (!isEntityType()) {
throw new EdmTypeNotFoundException(AbstractEntityType.class, this.typeExpression);
}
return this.entityType;
}
/**
* Gets base type.
*
* @return base type.
*/
@Override
public String getBaseType() {
return baseType;
}
/**
* Gets type expression.
*
* @return type expression.
*/
@Override
public String getTypeExpression() {
return typeExpression;
}
/**
* Gets namespace or alias retrieved from the provided type expression.
*
* @return namespace or alias.
*/
@Override
public String getNamespaceOrAlias() {
return namespaceOrAlias;
}
}

View File

@ -21,15 +21,12 @@ package org.apache.olingo.odata4.client.core.v3;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.apache.olingo.odata4.client.api.edm.EdmType;
import org.apache.olingo.odata4.client.api.http.HttpMethod;
import org.apache.olingo.odata4.client.core.AbstractTest;
import org.apache.olingo.odata4.client.core.ODataV3Client;
import org.apache.olingo.odata4.client.core.edm.xml.v3.ComplexTypeImpl;
import org.apache.olingo.odata4.client.core.edm.v3.EdmTypeImpl;
import org.apache.olingo.odata4.client.core.edm.xml.v3.EntityContainerImpl;
import org.apache.olingo.odata4.client.core.edm.xml.v3.EntityTypeImpl;
import org.apache.olingo.odata4.client.core.edm.xml.v3.FunctionImportImpl;
@ -50,27 +47,10 @@ public class MetadataTest extends AbstractTest {
toMetadata(getClass().getResourceAsStream("metadata.xml"));
assertNotNull(metadata);
final EdmTypeImpl orderCollection = new EdmTypeImpl(metadata,
"Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.Order)");
assertNotNull(orderCollection);
assertTrue(orderCollection.isCollection());
assertFalse(orderCollection.isSimpleType());
assertFalse(orderCollection.isEnumType());
assertFalse(orderCollection.isComplexType());
assertTrue(orderCollection.isEntityType());
final EntityTypeImpl order = orderCollection.getEntityType();
final EntityTypeImpl order = metadata.getSchemas().get(0).getEntityType("Order");
assertNotNull(order);
assertEquals("Order", order.getName());
final EdmType stream = new EdmTypeImpl(metadata, "Edm.Stream");
assertNotNull(stream);
assertFalse(stream.isCollection());
assertTrue(stream.isSimpleType());
assertFalse(stream.isEnumType());
assertFalse(stream.isComplexType());
assertFalse(stream.isEntityType());
final List<FunctionImportImpl> functionImports = metadata.getSchemas().get(0).
getDefaultEntityContainer().getFunctionImports();
int legacyGetters = 0;

View File

@ -25,7 +25,6 @@ import static org.junit.Assert.assertTrue;
import org.apache.olingo.odata4.client.core.AbstractTest;
import org.apache.olingo.odata4.client.core.ODataV4Client;
import org.apache.olingo.odata4.client.core.edm.v4.EdmTypeImpl;
import org.apache.olingo.odata4.client.core.edm.xml.v4.XMLMetadataImpl;
import org.apache.olingo.odata4.client.core.edm.xml.v4.AnnotationImpl;
import org.apache.olingo.odata4.client.core.edm.xml.v4.AnnotationsImpl;
@ -177,10 +176,7 @@ public class MetadataTest extends AbstractTest {
assertEquals("Products", product.getNavigationProperty("Supplier").getPartner());
final EntityTypeImpl category = metadata.getSchema(0).getEntityType("Category");
final EdmTypeImpl type = new EdmTypeImpl(metadata, category.getNavigationProperty("Products").getType());
assertNotNull(type);
assertTrue(type.isCollection());
assertFalse(type.isSimpleType());
assertNotNull(category);
final ComplexTypeImpl address = metadata.getSchema(0).getComplexType("Address");
assertFalse(address.getNavigationProperty("Country").getReferentialConstraints().isEmpty());