http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmEntityTypeImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmEntityTypeImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmEntityTypeImpl.java new file mode 100644 index 0000000..e6fefca --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmEntityTypeImpl.java @@ -0,0 +1,146 @@ +/* + * 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.commons.core.edm; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmKeyPropertyRef; +import org.apache.olingo.commons.api.edm.EdmStructuredType; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.constants.EdmTypeKind; +import org.apache.olingo.commons.api.edm.provider.EntityType; +import org.apache.olingo.commons.api.edm.provider.PropertyRef; + +public class EdmEntityTypeImpl extends AbstractEdmStructuredType implements EdmEntityType { + + private EntityType entityType; + private boolean baseTypeChecked = false; + private final boolean hasStream; + protected EdmEntityType entityBaseType; + private final List<String> keyPredicateNames = Collections.synchronizedList(new ArrayList<String>()); + private final Map<String, EdmKeyPropertyRef> keyPropertyRefs = + Collections.synchronizedMap(new LinkedHashMap<String, EdmKeyPropertyRef>()); + private List<EdmKeyPropertyRef> keyPropertyRefsList; + + public EdmEntityTypeImpl(final Edm edm, final FullQualifiedName name, final EntityType entityType) { + super(edm, name, EdmTypeKind.ENTITY, entityType); + this.entityType = entityType; + hasStream = entityType.hasStream(); + } + + @Override + protected void checkBaseType() { + if (!baseTypeChecked) { + if (baseTypeName != null) { + baseType = buildBaseType(baseTypeName); + entityBaseType = (EdmEntityType) baseType; + } + if (baseType == null + || (baseType.isAbstract() && ((EdmEntityType) baseType).getKeyPropertyRefs().size() == 0)) { + final List<PropertyRef> key = entityType.getKey(); + if (key != null) { + final List<EdmKeyPropertyRef> edmKey = new ArrayList<EdmKeyPropertyRef>(); + for (PropertyRef ref : key) { + edmKey.add(new EdmKeyPropertyRefImpl(this, ref)); + } + setEdmKeyPropertyRef(edmKey); + } + } + baseTypeChecked = true; + } + } + + protected void setEdmKeyPropertyRef(final List<EdmKeyPropertyRef> edmKey) { + for (EdmKeyPropertyRef ref : edmKey) { + if (ref.getAlias() == null) { + keyPredicateNames.add(ref.getName()); + keyPropertyRefs.put(ref.getName(), ref); + } else { + keyPredicateNames.add(ref.getAlias()); + keyPropertyRefs.put(ref.getAlias(), ref); + } + } + } + + @Override + protected EdmStructuredType buildBaseType(final FullQualifiedName baseTypeName) { + EdmEntityType baseType = null; + if (baseTypeName != null) { + baseType = edm.getEntityType(baseTypeName); + if (baseType == null) { + throw new EdmException("Cannot find base type with name: " + baseTypeName + " for entity type: " + getName()); + } + } + return baseType; + } + + @Override + public EdmEntityType getBaseType() { + checkBaseType(); + return entityBaseType; + } + + @Override + public List<String> getKeyPredicateNames() { + checkBaseType(); + if (keyPredicateNames.isEmpty() && baseType != null) { + return entityBaseType.getKeyPredicateNames(); + } + return Collections.unmodifiableList(keyPredicateNames); + } + + @Override + public List<EdmKeyPropertyRef> getKeyPropertyRefs() { + checkBaseType(); + if (keyPropertyRefsList == null) { + keyPropertyRefsList = new ArrayList<EdmKeyPropertyRef>(keyPropertyRefs.values()); + } + if (keyPropertyRefsList.isEmpty() && entityBaseType != null) { + return entityBaseType.getKeyPropertyRefs(); + } + return Collections.unmodifiableList(keyPropertyRefsList); + } + + @Override + public EdmKeyPropertyRef getKeyPropertyRef(final String keyPredicateName) { + checkBaseType(); + final EdmKeyPropertyRef edmKeyPropertyRef = keyPropertyRefs.get(keyPredicateName); + if (edmKeyPropertyRef == null && entityBaseType != null) { + return entityBaseType.getKeyPropertyRef(keyPredicateName); + } + return edmKeyPropertyRef; + } + + @Override + public boolean hasStream() { + return hasStream; + } + + @Override + public TargetType getAnnotationsTargetType() { + return TargetType.EntityType; + } +}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmEnumTypeImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmEnumTypeImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmEnumTypeImpl.java new file mode 100644 index 0000000..95866fd --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmEnumTypeImpl.java @@ -0,0 +1,268 @@ +/* + * 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.commons.core.edm; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Set; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmEnumType; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmMember; +import org.apache.olingo.commons.api.edm.EdmPrimitiveType; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.constants.EdmTypeKind; +import org.apache.olingo.commons.api.edm.provider.EnumMember; +import org.apache.olingo.commons.api.edm.provider.EnumType; +import org.apache.olingo.commons.core.edm.primitivetype.EdmInt64; +import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory; + +public class EdmEnumTypeImpl extends EdmTypeImpl implements EdmEnumType { + + private static final Set<EdmPrimitiveTypeKind> VALID_UNDERLYING_TYPES = new HashSet<EdmPrimitiveTypeKind>(); + static { + VALID_UNDERLYING_TYPES.add(EdmPrimitiveTypeKind.Byte); + VALID_UNDERLYING_TYPES.add(EdmPrimitiveTypeKind.SByte); + VALID_UNDERLYING_TYPES.add(EdmPrimitiveTypeKind.Int16); + VALID_UNDERLYING_TYPES.add(EdmPrimitiveTypeKind.Int32); + VALID_UNDERLYING_TYPES.add(EdmPrimitiveTypeKind.Int64); + } + + private final EdmPrimitiveType underlyingType; + private final EnumType enumType; + private final String uriPrefix; + private final String uriSuffix; + private List<String> memberNames; + private LinkedHashMap<String, EdmMember> membersMap; + + public EdmEnumTypeImpl(final Edm edm, final FullQualifiedName enumName, final EnumType enumType) { + super(edm, enumName, EdmTypeKind.ENUM, enumType); + + if (enumType.getUnderlyingType() == null) { + underlyingType = EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Int32); + } else { + EdmPrimitiveTypeKind underlyingTypeKind = EdmPrimitiveTypeKind.valueOfFQN(enumType.getUnderlyingType()); + + if (!VALID_UNDERLYING_TYPES.contains(underlyingTypeKind)) { + throw new EdmException("Not allowed as underlying type: " + underlyingTypeKind); + } + underlyingType = EdmPrimitiveTypeFactory.getInstance(underlyingTypeKind); + } + + this.enumType = enumType; + this.uriPrefix = enumName.getFullQualifiedNameAsString() + '\''; + this.uriSuffix = "'"; + } + + @Override + public EdmPrimitiveType getUnderlyingType() { + return underlyingType; + } + + @Override + public EdmMember getMember(final String name) { + if (membersMap == null) { + createEdmMembers(); + } + return membersMap.get(name); + } + + @Override + public List<String> getMemberNames() { + if (memberNames == null) { + createEdmMembers(); + } + return Collections.unmodifiableList(memberNames); + } + + private void createEdmMembers() { + final LinkedHashMap<String, EdmMember> membersMapLocal = new LinkedHashMap<String, EdmMember>(); + final List<String> memberNamesLocal = new ArrayList<String>(); + if (enumType.getMembers() != null) { + for (final EnumMember member : enumType.getMembers()) { + membersMapLocal.put(member.getName(), new EdmMemberImpl(edm, getFullQualifiedName(), member)); + memberNamesLocal.add(member.getName()); + } + + membersMap = membersMapLocal; + memberNames = memberNamesLocal; + } + } + + @Override + public boolean isCompatible(final EdmPrimitiveType primitiveType) { + return equals(primitiveType); + } + + @Override + public Class<?> getDefaultType() { + return getUnderlyingType().getDefaultType(); + } + + @Override + public boolean validate(final String value, final Boolean isNullable, final Integer maxLength, + final Integer precision, final Integer scale, final Boolean isUnicode) { + + try { + valueOfString(value, isNullable, maxLength, precision, scale, isUnicode, getDefaultType()); + return true; + } catch (final EdmPrimitiveTypeException e) { + return false; + } + } + + private Long parseEnumValue(final String value) throws EdmPrimitiveTypeException { + Long result = null; + for (final String memberValue : value.split(",", isFlags() ? -1 : 1)) { + Long memberValueLong = null; + for (final EdmMember member : getMembers()) { + if (member.getName().equals(memberValue) || member.getValue().equals(memberValue)) { + memberValueLong = Long.decode(member.getValue()); + } + } + if (memberValueLong == null) { + throw new EdmPrimitiveTypeException("The literal '" + value + "' has illegal content."); + } + result = result == null ? memberValueLong : result | memberValueLong; + } + return result; + } + + @Override + public <T> T valueOfString(final String value, final Boolean isNullable, final Integer maxLength, + final Integer precision, final Integer scale, final Boolean isUnicode, final Class<T> returnType) + throws EdmPrimitiveTypeException { + + if (value == null) { + if (isNullable != null && !isNullable) { + throw new EdmPrimitiveTypeException("The literal 'null' is not allowed."); + } + return null; + } + + try { + return EdmInt64.convertNumber(parseEnumValue(value), returnType); + } catch (final IllegalArgumentException e) { + throw new EdmPrimitiveTypeException("The literal '" + value + + "' cannot be converted to value type " + returnType + ".", e); + } catch (final ClassCastException e) { + throw new EdmPrimitiveTypeException("The value type " + returnType + " is not supported.", e); + } + } + + private String constructEnumValue(final long value) + throws EdmPrimitiveTypeException { + long remaining = value; + final StringBuilder result = new StringBuilder(); + + final boolean flags = isFlags(); + for (final EdmMember member : getMembers()) { + final long memberValue = Long.parseLong(member.getValue()); + if (flags) { + if ((memberValue & remaining) == memberValue) { + if (result.length() > 0) { + result.append(','); + } + result.append(member.getName()); + remaining ^= memberValue; + } + } else { + if (value == memberValue) { + return member.getName(); + } + } + } + + if (remaining != 0) { + throw new EdmPrimitiveTypeException("The value '" + value + "' is not valid."); + } + return result.toString(); + } + + private Collection<EdmMember> getMembers() { + if(membersMap == null){ + createEdmMembers(); + } + return membersMap.values(); + } + + @Override + public String valueToString(final Object value, final Boolean isNullable, final Integer maxLength, + final Integer precision, final Integer scale, final Boolean isUnicode) throws EdmPrimitiveTypeException { + + if (value == null) { + if (isNullable != null && !isNullable) { + throw new EdmPrimitiveTypeException("The value NULL is not allowed."); + } + return null; + } + if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long) { + return constructEnumValue(((Number) value).longValue()); + } else { + throw new EdmPrimitiveTypeException("The value type " + value.getClass() + " is not supported."); + } + } + + @Override + public String toUriLiteral(final String literal) { + return literal == null ? null + : uriPrefix.isEmpty() && uriSuffix.isEmpty() ? literal : uriPrefix + literal + uriSuffix; + } + + @Override + public String fromUriLiteral(final String literal) throws EdmPrimitiveTypeException { + if (literal == null) { + return null; + } else if (uriPrefix.isEmpty() && uriSuffix.isEmpty()) { + return literal; + } else if (literal.length() >= uriPrefix.length() + uriSuffix.length() + && literal.startsWith(uriPrefix) && literal.endsWith(uriSuffix)) { + return literal.substring(uriPrefix.length(), literal.length() - uriSuffix.length()); + } else { + throw new EdmPrimitiveTypeException("The literal '" + literal + "' has illegal content."); + } + } + + @Override + public boolean isFlags() { + return enumType.isFlags(); + } + + @Override + public TargetType getAnnotationsTargetType() { + return TargetType.EnumType; + } + + @Override + public String getAnnotationsTargetPath() { + return null; + } + + @Override + public FullQualifiedName getAnnotationsTargetFQN() { + return getFullQualifiedName(); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmFunctionImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmFunctionImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmFunctionImpl.java new file mode 100644 index 0000000..01d20ea --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmFunctionImpl.java @@ -0,0 +1,52 @@ +/* + * 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.commons.core.edm; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmFunction; +import org.apache.olingo.commons.api.edm.EdmReturnType; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.constants.EdmTypeKind; +import org.apache.olingo.commons.api.edm.provider.Function; + +public class EdmFunctionImpl extends AbstractEdmOperation implements EdmFunction { + + private final Function function; + + public EdmFunctionImpl(final Edm edm, final FullQualifiedName name, final Function function) { + super(edm, name, function, EdmTypeKind.FUNCTION); + this.function = function; + } + + @Override + public boolean isComposable() { + return function.isComposable(); + } + + @Override + public EdmReturnType getReturnType() { + final EdmReturnType returnType = super.getReturnType(); + if (returnType == null) { + throw new EdmException("ReturnType for a function must not be null"); + } + return returnType; + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmFunctionImportImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmFunctionImportImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmFunctionImportImpl.java new file mode 100644 index 0000000..ea9037d --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmFunctionImportImpl.java @@ -0,0 +1,63 @@ +/* + * 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.commons.core.edm; + +import java.util.List; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmEntityContainer; +import org.apache.olingo.commons.api.edm.EdmFunction; +import org.apache.olingo.commons.api.edm.EdmFunctionImport; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.FunctionImport; + +public class EdmFunctionImportImpl extends AbstractEdmOperationImport implements EdmFunctionImport { + + private final FunctionImport functionImport; + + public EdmFunctionImportImpl(final Edm edm, final EdmEntityContainer container, final FunctionImport functionImport) { + super(edm, container, functionImport); + this.functionImport = functionImport; + } + + @Override + public FullQualifiedName getFunctionFqn() { + return functionImport.getFunctionFQN(); + } + + @Override + public EdmFunction getUnboundFunction(final List<String> parameterNames) { + return edm.getUnboundFunction(getFunctionFqn(), parameterNames); + } + + @Override + public List<EdmFunction> getUnboundFunctions() { + return edm.getUnboundFunctions(getFunctionFqn()); + } + + @Override + public boolean isIncludeInServiceDocument() { + return functionImport.isIncludeInServiceDocument(); + } + + @Override + public TargetType getAnnotationsTargetType() { + return TargetType.FunctionImport; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmKeyPropertyRefImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmKeyPropertyRefImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmKeyPropertyRefImpl.java new file mode 100644 index 0000000..0f69deb --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmKeyPropertyRefImpl.java @@ -0,0 +1,83 @@ +/* + * 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.commons.core.edm; + +import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmKeyPropertyRef; +import org.apache.olingo.commons.api.edm.EdmProperty; +import org.apache.olingo.commons.api.edm.EdmStructuredType; +import org.apache.olingo.commons.api.edm.provider.PropertyRef; + +public class EdmKeyPropertyRefImpl implements EdmKeyPropertyRef { + + private final PropertyRef ref; + private EdmEntityType edmEntityType; + private EdmProperty property; + + public EdmKeyPropertyRefImpl(final EdmEntityType edmEntityType, final PropertyRef ref) { + this.edmEntityType = edmEntityType; + this.ref = ref; + } + + @Override + public String getName() { + return ref.getName(); + } + + @Override + public String getAlias() { + return ref.getAlias(); + } + + @Override + public EdmProperty getProperty() { + if (property == null) { + if (getAlias() == null) { + property = edmEntityType.getStructuralProperty(getName()); + if (property == null) { + throw new EdmException("Invalid key property ref specified. Can´t find property with name: " + + getName()); + } + } else { + if (getName() == null || getName().isEmpty()) { + throw new EdmException("Alias but no path specified for propertyRef"); + } + final String[] splitPath = getName().split("/"); + EdmStructuredType structType = edmEntityType; + for (int i = 0; i < splitPath.length - 1; i++) { + final EdmProperty _property = structType.getStructuralProperty(splitPath[i]); + if (_property == null) { + throw new EdmException("Invalid property ref specified. Can´t find property with name: " + splitPath[i] + + " at type: " + structType.getNamespace() + "." + structType.getName()); + } + structType = (EdmStructuredType) _property.getType(); + } + property = structType.getStructuralProperty(splitPath[splitPath.length - 1]); + if (property == null) { + throw new EdmException("Invalid property ref specified. Can´t find property with name: " + + splitPath[splitPath.length - 1] + " at type: " + structType.getNamespace() + "." + + structType.getName()); + } + } + } + + return property; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmMemberImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmMemberImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmMemberImpl.java new file mode 100644 index 0000000..362dd72 --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmMemberImpl.java @@ -0,0 +1,56 @@ +/* + * 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.commons.core.edm; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmMember; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.EnumMember; + +public class EdmMemberImpl extends AbstractEdmNamed implements EdmMember { + + private final FullQualifiedName enumFQN; + private final EnumMember member; + + public EdmMemberImpl(final Edm edm, final FullQualifiedName enumFQN, final EnumMember member) { + super(edm, member.getName(), member); + this.enumFQN = enumFQN; + this.member = member; + } + + @Override + public TargetType getAnnotationsTargetType() { + return TargetType.Member; + } + + @Override + public FullQualifiedName getAnnotationsTargetFQN() { + return enumFQN; + } + + @Override + public String getAnnotationsTargetPath() { + return getName(); + } + + @Override + public String getValue() { + return member.getValue(); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmNavigationPropertyBindingImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmNavigationPropertyBindingImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmNavigationPropertyBindingImpl.java new file mode 100644 index 0000000..5815a6c --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmNavigationPropertyBindingImpl.java @@ -0,0 +1,43 @@ +/* + * 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.commons.core.edm; + +import org.apache.olingo.commons.api.edm.EdmNavigationPropertyBinding; + +public class EdmNavigationPropertyBindingImpl implements EdmNavigationPropertyBinding { + + private final String path; + private final String target; + + public EdmNavigationPropertyBindingImpl(final String path, final String target) { + this.path = path; + this.target = target; + } + + @Override + public String getPath() { + return path; + } + + @Override + public String getTarget() { + return target; + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmNavigationPropertyImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmNavigationPropertyImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmNavigationPropertyImpl.java new file mode 100644 index 0000000..71f0a57 --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmNavigationPropertyImpl.java @@ -0,0 +1,142 @@ +/* + * 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.commons.core.edm; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmElement; +import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmNavigationProperty; +import org.apache.olingo.commons.api.edm.EdmReferentialConstraint; +import org.apache.olingo.commons.api.edm.EdmStructuredType; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.NavigationProperty; +import org.apache.olingo.commons.api.edm.provider.ReferentialConstraint; + +public class EdmNavigationPropertyImpl extends AbstractEdmNamed implements EdmElement, EdmNavigationProperty { + + private final FullQualifiedName structuredTypeName; + private final NavigationProperty navigationProperty; + private List<EdmReferentialConstraint> referentialConstraints; + private EdmEntityType typeImpl; + private EdmNavigationProperty partnerNavigationProperty; + + public EdmNavigationPropertyImpl( + final Edm edm, final FullQualifiedName structuredTypeName, final NavigationProperty navigationProperty) { + super(edm, navigationProperty.getName(), navigationProperty); + this.structuredTypeName = structuredTypeName; + this.navigationProperty = navigationProperty; + } + + @Override + public boolean isCollection() { + return navigationProperty.isCollection(); + } + + @Override + public boolean isNullable() { + return navigationProperty.isNullable(); + } + + @Override + public boolean containsTarget() { + return navigationProperty.isContainsTarget(); + } + + @Override + public EdmEntityType getType() { + if (typeImpl == null) { + typeImpl = edm.getEntityType(navigationProperty.getTypeFQN()); + if (typeImpl == null) { + throw new EdmException("Cannot find type with name: " + navigationProperty.getTypeFQN()); + } + } + return typeImpl; + } + + @Override + public EdmNavigationProperty getPartner() { + if (partnerNavigationProperty == null) { + String partner = navigationProperty.getPartner(); + if (partner != null) { + EdmStructuredType type = getType(); + EdmNavigationProperty property = null; + final String[] split = partner.split("/"); + for (String element : split) { + property = type.getNavigationProperty(element); + if (property == null) { + throw new EdmException("Cannot find navigation property with name: " + element + + " at type " + type.getName()); + } + type = property.getType(); + } + partnerNavigationProperty = property; + } + } + return partnerNavigationProperty; + } + + @Override + public String getReferencingPropertyName(final String referencedPropertyName) { + final List<ReferentialConstraint> referentialConstraints = navigationProperty.getReferentialConstraints(); + if (referentialConstraints != null) { + for (ReferentialConstraint constraint : referentialConstraints) { + if (constraint.getReferencedProperty().equals(referencedPropertyName)) { + return constraint.getProperty(); + } + } + } + return null; + } + + @Override + public List<EdmReferentialConstraint> getReferentialConstraints() { + if (referentialConstraints == null) { + final List<ReferentialConstraint> providerConstraints = navigationProperty.getReferentialConstraints(); + final List<EdmReferentialConstraint> referentialConstraintsLocal = new ArrayList<EdmReferentialConstraint>(); + if (providerConstraints != null) { + for (ReferentialConstraint constraint : providerConstraints) { + referentialConstraintsLocal.add(new EdmReferentialConstraintImpl(edm, constraint)); + } + } + + referentialConstraints = referentialConstraintsLocal; + } + return Collections.unmodifiableList(referentialConstraints); + } + + @Override + public TargetType getAnnotationsTargetType() { + return TargetType.NavigationProperty; + } + + @Override + public String getAnnotationsTargetPath() { + return getName(); + } + + @Override + public FullQualifiedName getAnnotationsTargetFQN() { + return structuredTypeName; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmParameterImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmParameterImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmParameterImpl.java new file mode 100644 index 0000000..e32bc6f --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmParameterImpl.java @@ -0,0 +1,88 @@ +/* + * 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.commons.core.edm; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmElement; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmMapping; +import org.apache.olingo.commons.api.edm.EdmParameter; +import org.apache.olingo.commons.api.edm.EdmType; +import org.apache.olingo.commons.api.edm.geo.SRID; +import org.apache.olingo.commons.api.edm.provider.Parameter; + +public class EdmParameterImpl extends AbstractEdmNamed implements EdmParameter, EdmElement { + + private final Parameter parameter; + private final EdmTypeInfo typeInfo; + private EdmType typeImpl; + + public EdmParameterImpl(final Edm edm, final Parameter parameter) { + super(edm, parameter.getName(), parameter); + this.parameter = parameter; + this.typeInfo = new EdmTypeInfo.Builder().setEdm(edm).setTypeExpression(parameter.getType()).build(); + } + + @Override + public boolean isCollection() { + return parameter.isCollection(); + } + + @Override + public EdmMapping getMapping() { + return parameter.getMapping(); + } + + @Override + public boolean isNullable() { + return parameter.isNullable(); + } + + @Override + public Integer getMaxLength() { + return parameter.getMaxLength(); + } + + @Override + public Integer getPrecision() { + return parameter.getPrecision(); + } + + @Override + public Integer getScale() { + return parameter.getScale(); + } + + @Override + public SRID getSrid() { + return parameter.getSrid(); + } + + @Override + public EdmType getType() { + if (typeImpl == null) { + typeImpl = typeInfo.getType(); + if (typeImpl == null) { + throw new EdmException("Cannot find type with name: " + typeInfo.getFullQualifiedName()); + } + } + + return typeImpl; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmPropertyImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmPropertyImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmPropertyImpl.java new file mode 100644 index 0000000..494744b --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmPropertyImpl.java @@ -0,0 +1,127 @@ +/* + * 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.commons.core.edm; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmElement; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmMapping; +import org.apache.olingo.commons.api.edm.EdmProperty; +import org.apache.olingo.commons.api.edm.EdmType; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.geo.SRID; +import org.apache.olingo.commons.api.edm.provider.Property; + +public class EdmPropertyImpl extends AbstractEdmNamed implements EdmProperty, EdmElement { + + private final FullQualifiedName structuredTypeName; + private final Property property; + private final EdmTypeInfo typeInfo; + private EdmType propertyType; + + public EdmPropertyImpl(final Edm edm, final FullQualifiedName structuredTypeName, final Property property) { + super(edm, property.getName(), property); + + this.structuredTypeName = structuredTypeName; + this.property = property; + typeInfo = new EdmTypeInfo.Builder().setEdm(edm).setTypeExpression(property.getType().toString()).build(); + } + + @Override + public EdmType getType() { + if (propertyType == null) { + propertyType = typeInfo.getType(); + if (propertyType == null) { + throw new EdmException("Cannot find type with name: " + typeInfo.getFullQualifiedName()); + } + } + + return propertyType; + } + + @Override + public boolean isCollection() { + return property.isCollection(); + } + + @Override + public EdmMapping getMapping() { + return property.getMapping(); + } + + @Override + public String getMimeType() { + return property.getMimeType(); + } + + @Override + public boolean isNullable() { + return property.isNullable(); + } + + @Override + public Integer getMaxLength() { + return property.getMaxLength(); + } + + @Override + public Integer getPrecision() { + return property.getPrecision(); + } + + @Override + public Integer getScale() { + return property.getScale(); + } + + @Override + public SRID getSrid() { + return property.getSrid(); + } + + @Override + public boolean isUnicode() { + return property.isUnicode(); + } + + @Override + public String getDefaultValue() { + return property.getDefaultValue(); + } + + @Override + public TargetType getAnnotationsTargetType() { + return TargetType.Property; + } + + @Override + public String getAnnotationsTargetPath() { + return getName(); + } + + @Override + public FullQualifiedName getAnnotationsTargetFQN() { + return structuredTypeName; + } + + @Override + public boolean isPrimitive() { + return typeInfo.isPrimitiveType(); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmProviderImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmProviderImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmProviderImpl.java new file mode 100644 index 0000000..62efd32 --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmProviderImpl.java @@ -0,0 +1,377 @@ +/* + * 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.commons.core.edm; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.olingo.commons.api.ODataException; +import org.apache.olingo.commons.api.edm.EdmAction; +import org.apache.olingo.commons.api.edm.EdmAnnotation; +import org.apache.olingo.commons.api.edm.EdmAnnotations; +import org.apache.olingo.commons.api.edm.EdmComplexType; +import org.apache.olingo.commons.api.edm.EdmEntityContainer; +import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.EdmEnumType; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmFunction; +import org.apache.olingo.commons.api.edm.EdmSchema; +import org.apache.olingo.commons.api.edm.EdmTerm; +import org.apache.olingo.commons.api.edm.EdmTypeDefinition; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.Action; +import org.apache.olingo.commons.api.edm.provider.AliasInfo; +import org.apache.olingo.commons.api.edm.provider.Annotatable; +import org.apache.olingo.commons.api.edm.provider.Annotation; +import org.apache.olingo.commons.api.edm.provider.Annotations; +import org.apache.olingo.commons.api.edm.provider.ComplexType; +import org.apache.olingo.commons.api.edm.provider.EdmProvider; +import org.apache.olingo.commons.api.edm.provider.EntityContainerInfo; +import org.apache.olingo.commons.api.edm.provider.EntityType; +import org.apache.olingo.commons.api.edm.provider.EnumType; +import org.apache.olingo.commons.api.edm.provider.Function; +import org.apache.olingo.commons.api.edm.provider.Parameter; +import org.apache.olingo.commons.api.edm.provider.Schema; +import org.apache.olingo.commons.api.edm.provider.Term; +import org.apache.olingo.commons.api.edm.provider.TypeDefinition; + +public class EdmProviderImpl extends AbstractEdm { + + private final EdmProvider provider; + private final Map<FullQualifiedName, List<Action>> actionsMap = + Collections.synchronizedMap(new HashMap<FullQualifiedName, List<Action>>()); + private final Map<FullQualifiedName, List<Function>> functionsMap = + Collections.synchronizedMap(new HashMap<FullQualifiedName, List<Function>>()); + + public EdmProviderImpl(final EdmProvider provider) { + this.provider = provider; + } + + @Override + public EdmEntityContainer createEntityContainer(final FullQualifiedName containerName) { + try { + EntityContainerInfo entityContainerInfo = provider.getEntityContainerInfo(containerName); + if (entityContainerInfo != null) { + return new EdmEntityContainerImpl(this, provider, entityContainerInfo); + } + return null; + } catch (ODataException e) { + throw new EdmException(e); + } + } + + @Override + public EdmEnumType createEnumType(final FullQualifiedName enumName) { + try { + EnumType enumType = provider.getEnumType(enumName); + if (enumType != null) { + return new EdmEnumTypeImpl(this, enumName, enumType); + } + return null; + } catch (ODataException e) { + throw new EdmException(e); + } + } + + @Override + public EdmTypeDefinition createTypeDefinition(final FullQualifiedName typeDefinitionName) { + try { + TypeDefinition typeDefinition = provider.getTypeDefinition(typeDefinitionName); + if (typeDefinition != null) { + return new EdmTypeDefinitionImpl(this, typeDefinitionName, typeDefinition); + } + return null; + } catch (ODataException e) { + throw new EdmException(e); + } + } + + @Override + public EdmEntityType createEntityType(final FullQualifiedName entityTypeName) { + try { + EntityType entityType = provider.getEntityType(entityTypeName); + if (entityType != null) { + return new EdmEntityTypeImpl(this, entityTypeName, entityType); + } + return null; + } catch (ODataException e) { + throw new EdmException(e); + } + } + + @Override + public EdmComplexType createComplexType(final FullQualifiedName complexTypeName) { + try { + final ComplexType complexType = provider.getComplexType(complexTypeName); + if (complexType != null) { + return new EdmComplexTypeImpl(this, complexTypeName, complexType); + } + return null; + } catch (ODataException e) { + throw new EdmException(e); + } + } + + @Override + public EdmAction createBoundAction(final FullQualifiedName actionName, + final FullQualifiedName bindingParameterTypeName, final Boolean isBindingParameterCollection) { + + try { + List<Action> actions = actionsMap.get(actionName); + if (actions == null) { + actions = provider.getActions(actionName); + if (actions == null) { + return null; + } else { + actionsMap.put(actionName, actions); + } + } + // Search for bound action where binding parameter matches + for (Action action : actions) { + if (action.isBound()) { + final List<Parameter> parameters = action.getParameters(); + final Parameter parameter = parameters.get(0); + if (bindingParameterTypeName.equals(parameter.getTypeFQN()) + && isBindingParameterCollection.booleanValue() == parameter.isCollection()) { + + return new EdmActionImpl(this, actionName, action); + } + + } + } + return null; + } catch (ODataException e) { + throw new EdmException(e); + } + } + + @Override + public EdmFunction createBoundFunction(final FullQualifiedName functionName, + final FullQualifiedName bindingParameterTypeName, final Boolean isBindingParameterCollection, + final List<String> parameterNames) { + + try { + List<Function> functions = functionsMap.get(functionName); + if (functions == null) { + functions = provider.getFunctions(functionName); + if (functions == null) { + return null; + } else { + functionsMap.put(functionName, functions); + } + } + final List<String> parameterNamesCopy = + parameterNames == null ? Collections.<String> emptyList() : parameterNames; + for (Function function : functions) { + if (function.isBound()) { + List<Parameter> providerParameters = function.getParameters(); + if (providerParameters == null || providerParameters.size() == 0) { + throw new EdmException("No parameter specified for bound function: " + functionName); + } + final Parameter bindingParameter = providerParameters.get(0); + if (bindingParameterTypeName.equals(bindingParameter.getTypeFQN()) + && isBindingParameterCollection.booleanValue() == bindingParameter.isCollection()) { + + if (parameterNamesCopy.size() == providerParameters.size() - 1) { + final List<String> providerParameterNames = new ArrayList<String>(); + for (int i = 1; i < providerParameters.size(); i++) { + providerParameterNames.add(providerParameters.get(i).getName()); + } + if (parameterNamesCopy.containsAll(providerParameterNames)) { + return new EdmFunctionImpl(this, functionName, function); + } + } + } + } + } + return null; + } catch (ODataException e) { + throw new EdmException(e); + } + } + + @Override + protected Map<String, String> createAliasToNamespaceInfo() { + final Map<String, String> aliasToNamespaceInfos = new HashMap<String, String>(); + try { + final List<AliasInfo> aliasInfos = provider.getAliasInfos(); + if (aliasInfos != null) { + for (AliasInfo info : aliasInfos) { + aliasToNamespaceInfos.put(info.getAlias(), info.getNamespace()); + } + } + } catch (ODataException e) { + throw new EdmException(e); + } + return aliasToNamespaceInfos; + } + + @Override + protected EdmAction createUnboundAction(final FullQualifiedName actionName) { + try { + List<Action> actions = actionsMap.get(actionName); + if (actions == null) { + actions = provider.getActions(actionName); + if (actions == null) { + return null; + } else { + actionsMap.put(actionName, actions); + } + } + // Search for first unbound action + for (Action action : actions) { + if (!action.isBound()) { + return new EdmActionImpl(this, actionName, action); + } + } + return null; + } catch (ODataException e) { + throw new EdmException(e); + } + } + + @Override + protected List<EdmFunction> createUnboundFunctions(final FullQualifiedName functionName) { + List<EdmFunction> result = new ArrayList<EdmFunction>(); + + try { + List<Function> functions = functionsMap.get(functionName); + if (functions == null) { + functions = provider.getFunctions(functionName); + if (functions != null) { + functionsMap.put(functionName, functions); + } + } + if (functions != null) { + for (Function function : functions) { + if (!function.isBound()) { + result.add(new EdmFunctionImpl(this, functionName, function)); + } + } + } + } catch (ODataException e) { + throw new EdmException(e); + } + + return result; + } + + @Override + protected EdmFunction createUnboundFunction(final FullQualifiedName functionName, final List<String> parameterNames) { + try { + List<Function> functions = functionsMap.get(functionName); + if (functions == null) { + functions = provider.getFunctions(functionName); + if (functions == null) { + return null; + } else { + functionsMap.put(functionName, functions); + } + } + + final List<String> parameterNamesCopy = + parameterNames == null ? Collections.<String> emptyList() : parameterNames; + for (Function function : functions) { + if (!function.isBound()) { + List<Parameter> providerParameters = function.getParameters(); + if (providerParameters == null) { + providerParameters = Collections.emptyList(); + } + if (parameterNamesCopy.size() == providerParameters.size()) { + final List<String> functionParameterNames = new ArrayList<String>(); + for (Parameter parameter : providerParameters) { + functionParameterNames.add(parameter.getName()); + } + + if (parameterNamesCopy.containsAll(functionParameterNames)) { + return new EdmFunctionImpl(this, functionName, function); + } + } + } + } + return null; + } catch (ODataException e) { + throw new EdmException(e); + } + } + + @Override + protected Map<String, EdmSchema> createSchemas() { + try { + final Map<String, EdmSchema> providerSchemas = new LinkedHashMap<String, EdmSchema>(); + for (Schema schema : provider.getSchemas()) { + providerSchemas.put(schema.getNamespace(), new EdmSchemaImpl(this, provider, schema)); + } + return providerSchemas; + } catch (ODataException e) { + throw new EdmException(e); + } + } + + @Override + protected EdmTerm createTerm(final FullQualifiedName termName) { + try { + Term providerTerm = provider.getTerm(termName); + if (providerTerm != null) { + return new EdmTermImpl(this, termName.getNamespace(), providerTerm); + } + return null; + } catch (ODataException e) { + throw new EdmException(e); + } + } + + //TODO: Check Provider annotations implementation + @Override + protected EdmAnnotations createAnnotationGroup(final FullQualifiedName targetName) { + try { + EdmSchema schema = getSchema(targetName.getNamespace()); + Annotations providerGroup = provider.getAnnotationsGroup(targetName); + if (providerGroup != null) { + return new EdmAnnotationsImpl(this, schema, providerGroup); + } + return null; + } catch (ODataException e) { + throw new EdmException(e); + } + } + + @Override + protected List<EdmAnnotation> createAnnotations(final FullQualifiedName annotatedName) { + try { + Annotatable providerAnnotatable = provider.getAnnoatatable(annotatedName); + if (providerAnnotatable != null && providerAnnotatable.getAnnotations() != null) { + List<EdmAnnotation> result = new ArrayList<EdmAnnotation>(); + for(Annotation annotation : providerAnnotatable.getAnnotations()){ + //Load Term + getTerm(new FullQualifiedName(annotation.getTerm())); + result.add(new EdmAnnotationImpl(this, annotation)); + } + return result; + } + return null; + } catch (ODataException e) { + throw new EdmException(e); + } + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmReferentialConstraintImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmReferentialConstraintImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmReferentialConstraintImpl.java new file mode 100644 index 0000000..e0e8f85 --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmReferentialConstraintImpl.java @@ -0,0 +1,43 @@ +/* + * 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.commons.core.edm; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmReferentialConstraint; +import org.apache.olingo.commons.api.edm.provider.ReferentialConstraint; + +public class EdmReferentialConstraintImpl extends AbstractEdmAnnotatable implements EdmReferentialConstraint { + + private final ReferentialConstraint constraint; + + public EdmReferentialConstraintImpl(final Edm edm, final ReferentialConstraint constraint) { + super(edm, constraint); + this.constraint = constraint; + } + + @Override + public String getPropertyName() { + return constraint.getProperty(); + } + + @Override + public String getReferencedPropertyName() { + return constraint.getReferencedProperty(); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmReturnTypeImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmReturnTypeImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmReturnTypeImpl.java new file mode 100644 index 0000000..04f6f90 --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmReturnTypeImpl.java @@ -0,0 +1,80 @@ +/* + * 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.commons.core.edm; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmReturnType; +import org.apache.olingo.commons.api.edm.EdmType; +import org.apache.olingo.commons.api.edm.geo.SRID; +import org.apache.olingo.commons.api.edm.provider.ReturnType; + +public class EdmReturnTypeImpl implements EdmReturnType { + + private final ReturnType returnType; + private final EdmTypeInfo typeInfo; + private EdmType typeImpl; + + public EdmReturnTypeImpl(final Edm edm, final ReturnType returnType) { + this.returnType = returnType; + this.typeInfo = new EdmTypeInfo.Builder().setEdm(edm).setTypeExpression(returnType.getType()).build(); + } + + @Override + public boolean isCollection() { + return returnType.isCollection(); + } + + @Override + public boolean isNullable() { + return returnType.isNullable(); + } + + @Override + public Integer getMaxLength() { + return returnType.getMaxLength(); + } + + @Override + public Integer getPrecision() { + return returnType.getPrecision(); + } + + @Override + public Integer getScale() { + return returnType.getScale(); + } + + @Override + public SRID getSrid() { + return returnType.getSrid(); + } + + @Override + public EdmType getType() { + if (typeImpl == null) { + typeImpl = typeInfo.getType(); + if (typeImpl == null) { + throw new EdmException("Cannot find type with name: " + typeInfo.getFullQualifiedName()); + } + } + + return typeImpl; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmSchemaImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmSchemaImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmSchemaImpl.java new file mode 100644 index 0000000..5521b92 --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmSchemaImpl.java @@ -0,0 +1,304 @@ +/* + * 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.commons.core.edm; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.apache.olingo.commons.api.edm.EdmAction; +import org.apache.olingo.commons.api.edm.EdmAnnotation; +import org.apache.olingo.commons.api.edm.EdmAnnotations; +import org.apache.olingo.commons.api.edm.EdmComplexType; +import org.apache.olingo.commons.api.edm.EdmEntityContainer; +import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.EdmEnumType; +import org.apache.olingo.commons.api.edm.EdmFunction; +import org.apache.olingo.commons.api.edm.EdmSchema; +import org.apache.olingo.commons.api.edm.EdmTerm; +import org.apache.olingo.commons.api.edm.EdmTypeDefinition; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.Action; +import org.apache.olingo.commons.api.edm.provider.Annotation; +import org.apache.olingo.commons.api.edm.provider.Annotations; +import org.apache.olingo.commons.api.edm.provider.ComplexType; +import org.apache.olingo.commons.api.edm.provider.EdmProvider; +import org.apache.olingo.commons.api.edm.provider.EntityType; +import org.apache.olingo.commons.api.edm.provider.EnumType; +import org.apache.olingo.commons.api.edm.provider.Function; +import org.apache.olingo.commons.api.edm.provider.Schema; +import org.apache.olingo.commons.api.edm.provider.Term; +import org.apache.olingo.commons.api.edm.provider.TypeDefinition; + +public class EdmSchemaImpl implements EdmSchema { + + private final Schema schema; + private final EdmProviderImpl edm; + private final EdmProvider provider; + + protected final String namespace; + private final String alias; + private List<EdmEnumType> enumTypes; + private List<EdmEntityType> entityTypes; + private List<EdmComplexType> complexTypes; + private List<EdmAction> actions; + private List<EdmFunction> functions; + private List<EdmTypeDefinition> typeDefinitions; + private List<EdmTerm> terms; + private List<EdmAnnotations> annotationGroups; + private List<EdmAnnotation> annotations; + private EdmEntityContainer entityContainer; + + public EdmSchemaImpl(final EdmProviderImpl edm, final EdmProvider provider, final Schema schema) { + this.edm = edm; + this.provider = provider; + this.schema = schema; + this.namespace = schema.getNamespace(); + this.alias = schema.getAlias(); + + if (alias != null) { + edm.cacheAliasNamespaceInfo(alias, namespace); + } + + enumTypes = createEnumTypes(); + typeDefinitions = createTypeDefinitions(); + entityTypes = createEntityTypes(); + complexTypes = createComplexTypes(); + actions = createActions(); + functions = createFunctions(); + entityContainer = createEntityContainer(); + annotationGroups = createAnnotationGroups(); + annotations = createAnnotations(); + terms = createTerms(); + } + + @Override + public List<EdmEnumType> getEnumTypes() { + return Collections.unmodifiableList(enumTypes); + } + + @Override + public List<EdmEntityType> getEntityTypes() { + return Collections.unmodifiableList(entityTypes); + } + + @Override + public List<EdmComplexType> getComplexTypes() { + return Collections.unmodifiableList(complexTypes); + } + + @Override + public List<EdmAction> getActions() { + return Collections.unmodifiableList(actions); + } + + @Override + public List<EdmFunction> getFunctions() { + return Collections.unmodifiableList(functions); + } + + @Override + public List<EdmTypeDefinition> getTypeDefinitions() { + return Collections.unmodifiableList(typeDefinitions); + } + + @Override + public List<EdmTerm> getTerms() { + return Collections.unmodifiableList(terms); + } + + @Override + public List<EdmAnnotations> getAnnotationGroups() { + return Collections.unmodifiableList(annotationGroups); + } + + @Override + public List<EdmAnnotation> getAnnotations() { + return Collections.unmodifiableList(annotations); + } + + @Override + public EdmEntityContainer getEntityContainer() { + return entityContainer; + } + + @Override + public String getNamespace() { + return namespace; + } + + @Override + public String getAlias() { + return alias; + } + + protected EdmEntityContainer createEntityContainer() { + if (schema.getEntityContainer() != null) { + FullQualifiedName containerFQN = new FullQualifiedName(namespace, schema.getEntityContainer().getName()); + EdmEntityContainer impl = new EdmEntityContainerImpl(edm, provider, containerFQN, schema.getEntityContainer()); + edm.cacheEntityContainer(containerFQN, impl); + edm.cacheEntityContainer(null, impl); + return impl; + } + return null; + } + + protected List<EdmTypeDefinition> createTypeDefinitions() { + final List<EdmTypeDefinition> typeDefinitions = new ArrayList<EdmTypeDefinition>(); + final List<TypeDefinition> providerTypeDefinitions = schema.getTypeDefinitions(); + if (providerTypeDefinitions != null) { + for (TypeDefinition def : providerTypeDefinitions) { + FullQualifiedName typeDefName = new FullQualifiedName(namespace, def.getName()); + EdmTypeDefinitionImpl typeDefImpl = new EdmTypeDefinitionImpl(edm, typeDefName, def); + typeDefinitions.add(typeDefImpl); + edm.cacheTypeDefinition(typeDefName, typeDefImpl); + } + } + return typeDefinitions; + } + + protected List<EdmEnumType> createEnumTypes() { + final List<EdmEnumType> enumTypes = new ArrayList<EdmEnumType>(); + final List<EnumType> providerEnumTypes = schema.getEnumTypes(); + if (providerEnumTypes != null) { + for (EnumType enumType : providerEnumTypes) { + FullQualifiedName enumName = new FullQualifiedName(namespace, enumType.getName()); + EdmEnumType enumTypeImpl = new EdmEnumTypeImpl(edm, enumName, enumType); + enumTypes.add(enumTypeImpl); + edm.cacheEnumType(enumName, enumTypeImpl); + } + } + return enumTypes; + } + + protected List<EdmEntityType> createEntityTypes() { + final List<EdmEntityType> entityTypes = new ArrayList<EdmEntityType>(); + final List<EntityType> providerEntityTypes = schema.getEntityTypes(); + if (providerEntityTypes != null) { + for (EntityType entityType : providerEntityTypes) { + FullQualifiedName entityTypeName = new FullQualifiedName(namespace, entityType.getName()); + EdmEntityTypeImpl entityTypeImpl = new EdmEntityTypeImpl(edm, entityTypeName, entityType); + entityTypes.add(entityTypeImpl); + edm.cacheEntityType(entityTypeName, entityTypeImpl); + } + } + return entityTypes; + } + + protected List<EdmComplexType> createComplexTypes() { + final List<EdmComplexType> complexTypes = new ArrayList<EdmComplexType>(); + final List<ComplexType> providerComplexTypes = schema.getComplexTypes(); + if (providerComplexTypes != null) { + for (ComplexType complexType : providerComplexTypes) { + FullQualifiedName comlexTypeName = new FullQualifiedName(namespace, complexType.getName()); + EdmComplexTypeImpl complexTypeImpl = new EdmComplexTypeImpl(edm, comlexTypeName, complexType); + complexTypes.add(complexTypeImpl); + edm.cacheComplexType(comlexTypeName, complexTypeImpl); + } + } + return complexTypes; + } + + protected List<EdmAction> createActions() { + final List<EdmAction> actions = new ArrayList<EdmAction>(); + final List<Action> providerActions = schema.getActions(); + if (providerActions != null) { + for (Action action : providerActions) { + FullQualifiedName actionName = new FullQualifiedName(namespace, action.getName()); + EdmActionImpl edmActionImpl = new EdmActionImpl(edm, actionName, action); + actions.add(edmActionImpl); + edm.cacheAction(actionName, edmActionImpl); + } + } + return actions; + } + + protected List<EdmFunction> createFunctions() { + final List<EdmFunction> functions = new ArrayList<EdmFunction>(); + final List<Function> providerFunctions = schema.getFunctions(); + if (providerFunctions != null) { + for (Function function : providerFunctions) { + FullQualifiedName functionName = new FullQualifiedName(namespace, function.getName()); + EdmFunctionImpl functionImpl = new EdmFunctionImpl(edm, functionName, function); + functions.add(functionImpl); + edm.cacheFunction(functionName, functionImpl); + } + } + return functions; + } + + protected List<EdmTerm> createTerms() { + final List<EdmTerm> terms = new ArrayList<EdmTerm>(); + final List<Term> providerTerms = schema.getTerms(); + if (providerTerms != null) { + for (Term term : providerTerms) { + FullQualifiedName termName = new FullQualifiedName(namespace, term.getName()); + EdmTermImpl termImpl = new EdmTermImpl(edm, getNamespace(), term); + terms.add(termImpl); + edm.cacheTerm(termName, termImpl); + } + } + return terms; + } + + protected List<EdmAnnotations> createAnnotationGroups() { + final List<EdmAnnotations> annotationGroups = new ArrayList<EdmAnnotations>(); + final List<Annotations> providerAnnotations = + schema.getAnnotationGroups(); + if (providerAnnotations != null) { + for (Annotations annotationGroup : providerAnnotations) { + FullQualifiedName annotationsGroupName; + if (annotationGroup.getTarget().contains(".")) { + annotationsGroupName = new FullQualifiedName(annotationGroup.getTarget()); + } else { + annotationsGroupName = new FullQualifiedName(namespace, annotationGroup.getTarget()); + } + EdmAnnotationsImpl annotationsImpl = new EdmAnnotationsImpl(edm, this, annotationGroup); + annotationGroups.add(annotationsImpl); + edm.cacheAnnotationGroup(annotationsGroupName, annotationsImpl); + } + } + return annotationGroups; + } + + protected List<EdmAnnotation> createAnnotations() { + final List<EdmAnnotation> annotations = new ArrayList<EdmAnnotation>(); + final List<Annotation> providerAnnotations = + schema.getAnnotations(); + if (providerAnnotations != null) { + for (Annotation annotation : providerAnnotations) { + EdmAnnotationImpl annotationImpl = new EdmAnnotationImpl(edm, annotation); + annotations.add(annotationImpl); + } + } + return annotations; + } + + @Override + public EdmAnnotation getAnnotation(final EdmTerm term) { + EdmAnnotation result = null; + for (EdmAnnotation annotation : getAnnotations()) { + if (term.getFullQualifiedName().equals(annotation.getTerm().getFullQualifiedName())) { + result = annotation; + } + } + + return result; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmSingletonImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmSingletonImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmSingletonImpl.java new file mode 100644 index 0000000..afb900f --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmSingletonImpl.java @@ -0,0 +1,36 @@ +/* + * 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.commons.core.edm; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmEntityContainer; +import org.apache.olingo.commons.api.edm.EdmSingleton; +import org.apache.olingo.commons.api.edm.provider.Singleton; + +public class EdmSingletonImpl extends AbstractEdmBindingTarget implements EdmSingleton { + + public EdmSingletonImpl(final Edm edm, final EdmEntityContainer container, final Singleton singleton) { + super(edm, container, singleton); + } + + @Override + public TargetType getAnnotationsTargetType() { + return TargetType.Singleton; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmTermImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmTermImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmTermImpl.java new file mode 100644 index 0000000..8d72c83 --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmTermImpl.java @@ -0,0 +1,148 @@ +/* + * 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.commons.core.edm; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.ClassUtils; +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmTerm; +import org.apache.olingo.commons.api.edm.EdmType; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.geo.SRID; +import org.apache.olingo.commons.api.edm.provider.Term; +import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EdmTermImpl extends AbstractEdmNamed implements EdmTerm { + + private static final Logger LOG = LoggerFactory.getLogger(EdmTermImpl.class); + private final Term term; + private final FullQualifiedName fqn; + private final EdmTypeInfo typeInfo; + private EdmType termType; + private EdmTerm baseTerm; + private List<Class<?>> appliesTo; + + public EdmTermImpl(final Edm edm, final String namespace, final Term term) { + super(edm, term.getName(), term); + + this.term = term; + this.fqn = new FullQualifiedName(namespace, term.getName()); + this.typeInfo = new EdmTypeInfo.Builder().setEdm(edm).setTypeExpression(term.getType()).build(); + } + + @Override + public FullQualifiedName getFullQualifiedName() { + return fqn; + } + + @Override + public EdmType getType() { + if (termType == null) { + termType = typeInfo.isPrimitiveType() + ? EdmPrimitiveTypeFactory.getInstance(typeInfo.getPrimitiveTypeKind()) + : typeInfo.isTypeDefinition() + ? typeInfo.getTypeDefinition() + : typeInfo.isEnumType() + ? typeInfo.getEnumType() + : typeInfo.isComplexType() + ? typeInfo.getComplexType() + : null; + if (termType == null) { + throw new EdmException("Cannot find type with name: " + typeInfo.getFullQualifiedName()); + } + } + + return termType; + } + + @Override + public EdmTerm getBaseTerm() { + if (baseTerm == null && term.getBaseTerm() != null) { + baseTerm = edm.getTerm(new FullQualifiedName(term.getBaseTerm())); + } + return baseTerm; + } + + @Override + public List<Class<?>> getAppliesTo() { + if (appliesTo == null) { + final List<Class<?>> appliesToLocal = new ArrayList<Class<?>>(); + for (String element : term.getAppliesTo()) { + try { + appliesToLocal.add(ClassUtils.getClass(EdmTerm.class.getPackage().getName() + ".Edm" + element)); + } catch (ClassNotFoundException e) { + LOG.error("Could not load Edm class for {}", element, e); + } + } + + appliesTo = appliesToLocal; + } + return appliesTo; + } + + @Override + public Boolean isNullable() { + return term.isNullable(); + } + + @Override + public Integer getMaxLength() { + return term.getMaxLength(); + } + + @Override + public Integer getPrecision() { + return term.getPrecision(); + } + + @Override + public Integer getScale() { + return term.getScale(); + } + + @Override + public SRID getSrid() { + return term.getSrid(); + } + + @Override + public String getDefaultValue() { + return term.getDefaultValue(); + } + + @Override + public TargetType getAnnotationsTargetType() { + return TargetType.Term; + } + + @Override + public FullQualifiedName getAnnotationsTargetFQN() { + return getFullQualifiedName(); + } + + @Override + public String getAnnotationsTargetPath() { + return null; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac32d236/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmTypeDefinitionImpl.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmTypeDefinitionImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmTypeDefinitionImpl.java new file mode 100644 index 0000000..b42f6c6 --- /dev/null +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmTypeDefinitionImpl.java @@ -0,0 +1,163 @@ +/* + * 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.commons.core.edm; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmPrimitiveType; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; +import org.apache.olingo.commons.api.edm.EdmTypeDefinition; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.constants.EdmTypeKind; +import org.apache.olingo.commons.api.edm.geo.SRID; +import org.apache.olingo.commons.api.edm.provider.TypeDefinition; +import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory; + +public class EdmTypeDefinitionImpl extends AbstractEdmNamed implements EdmTypeDefinition { + + private TypeDefinition typeDefinition; + private FullQualifiedName typeDefinitionName; + private EdmPrimitiveType edmPrimitiveTypeInstance; + + public EdmTypeDefinitionImpl(final Edm edm, final FullQualifiedName typeDefinitionName, + final TypeDefinition typeDefinition) { + super(edm, typeDefinitionName.getName(), typeDefinition); + this.typeDefinitionName = typeDefinitionName; + this.typeDefinition = typeDefinition; + } + + @Override + public FullQualifiedName getFullQualifiedName() { + return typeDefinitionName; + } + + @Override + public String getNamespace() { + return typeDefinitionName.getNamespace(); + } + + @Override + public EdmTypeKind getKind() { + return EdmTypeKind.DEFINITION; + } + + @Override + public EdmPrimitiveType getUnderlyingType() { + if(edmPrimitiveTypeInstance == null){ + try { + if (typeDefinition.getUnderlyingType() == null) { + throw new EdmException("Underlying Type for type definition: " + + typeDefinitionName.getFullQualifiedNameAsString() + " must not be null."); + } + this.edmPrimitiveTypeInstance = EdmPrimitiveTypeFactory.getInstance( + EdmPrimitiveTypeKind.valueOfFQN(typeDefinition.getUnderlyingType())); + } catch (IllegalArgumentException e) { + throw new EdmException("Invalid underlying type: " + typeDefinition.getUnderlyingType(), e); + } + } + return edmPrimitiveTypeInstance; + } + + @Override + public Integer getMaxLength() { + return typeDefinition.getMaxLength(); + } + + @Override + public Integer getPrecision() { + return typeDefinition.getPrecision(); + } + + @Override + public Integer getScale() { + return typeDefinition.getScale(); + } + + @Override + public SRID getSrid() { + return typeDefinition.getSrid(); + } + + @Override + public Boolean isUnicode() { + return typeDefinition.isUnicode(); + } + + @Override + public boolean isCompatible(final EdmPrimitiveType primitiveType) { + return getUnderlyingType().isCompatible(primitiveType); + } + + @Override + public Class<?> getDefaultType() { + return getUnderlyingType().getDefaultType(); + } + + @Override + public boolean validate(final String value, final Boolean isNullable, final Integer maxLength, + final Integer precision, final Integer scale, + final Boolean isUnicode) { + + return getUnderlyingType().validate(value, isNullable, maxLength, precision, scale, isUnicode); + } + + @Override + public <T> T valueOfString(final String value, final Boolean isNullable, final Integer maxLength, + final Integer precision, final Integer scale, + final Boolean isUnicode, final Class<T> returnType) throws EdmPrimitiveTypeException { + + return getUnderlyingType(). + valueOfString(value, isNullable, maxLength, precision, scale, isUnicode, returnType); + } + + @Override + public String valueToString(final Object value, final Boolean isNullable, final Integer maxLength, + final Integer precision, final Integer scale, + final Boolean isUnicode) throws EdmPrimitiveTypeException { + + return getUnderlyingType().valueToString(value, isNullable, maxLength, precision, scale, isUnicode); + } + + @Override + public String toUriLiteral(final String literal) { + return getUnderlyingType().toUriLiteral(literal); + } + + @Override + public String fromUriLiteral(final String literal) throws EdmPrimitiveTypeException { + return getUnderlyingType().fromUriLiteral(literal); + } + + + @Override + public TargetType getAnnotationsTargetType() { + return TargetType.TypeDefinition; + } + + @Override + public FullQualifiedName getAnnotationsTargetFQN() { + return getFullQualifiedName(); + } + + @Override + public String getAnnotationsTargetPath() { + return getName(); + } +}
