You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ch...@apache.org on 2013/12/18 10:12:30 UTC
[32/37] [OLINGO-82] Renamed the project folder name to
odata2-jpa-processor
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/1b479e6c/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/data/JPALink.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/data/JPALink.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/data/JPALink.java
new file mode 100644
index 0000000..872a99b
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/data/JPALink.java
@@ -0,0 +1,225 @@
+/*******************************************************************************
+ * 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.odata2.processor.core.jpa.access.data;
+
+import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+
+import org.apache.olingo.odata2.api.edm.EdmEntitySet;
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmNavigationProperty;
+import org.apache.olingo.odata2.api.uri.UriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntityUriInfo;
+import org.apache.olingo.odata2.api.uri.info.PostUriInfo;
+import org.apache.olingo.odata2.api.uri.info.PutMergePatchUriInfo;
+import org.apache.olingo.odata2.processor.api.jpa.ODataJPAContext;
+import org.apache.olingo.odata2.processor.api.jpa.access.JPAProcessor;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.processor.api.jpa.factory.ODataJPAFactory;
+import org.apache.olingo.odata2.processor.core.jpa.ODataEntityParser;
+
+public class JPALink {
+
+ private ODataJPAContext context;
+ private JPAProcessor jpaProcessor;
+ private ODataEntityParser parser;
+ private Object targetJPAEntity;
+ private Object sourceJPAEntity;
+
+ public JPALink(final ODataJPAContext context) {
+ this.context = context;
+ jpaProcessor = ODataJPAFactory.createFactory().getJPAAccessFactory().getJPAProcessor(this.context);
+ parser = new ODataEntityParser(this.context);
+ }
+
+ public void setSourceJPAEntity(final Object jpaEntity) {
+ sourceJPAEntity = jpaEntity;
+ }
+
+ public void create(final PostUriInfo uriInfo, final InputStream content, final String requestContentType,
+ final String contentType) throws ODataJPARuntimeException, ODataJPAModelException {
+
+ EdmEntitySet targetEntitySet = uriInfo.getTargetEntitySet();
+ String targerEntitySetName;
+ EdmNavigationProperty navigationProperty = null;
+ try {
+ targerEntitySetName = targetEntitySet.getName();
+ } catch (EdmException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ }
+
+ List<UriInfo> uriInfoList = new ArrayList<UriInfo>();
+
+ if (((UriInfo) uriInfo).isLinks()) {
+ UriInfo getUriInfo = parser.parseLink(targetEntitySet, content, requestContentType);
+ uriInfoList = new ArrayList<UriInfo>();
+ uriInfoList.add(getUriInfo);
+ navigationProperty = uriInfo.getNavigationSegments().get(0).getNavigationProperty();
+ } else {
+ uriInfoList = parser.parseLinks(targetEntitySet, content, contentType);
+ }
+
+ if (uriInfoList == null) {
+ return;
+ }
+ try {
+ for (UriInfo getUriInfo : uriInfoList) {
+
+ if (!getUriInfo.getTargetEntitySet().getName().equals(targerEntitySetName)) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.RELATIONSHIP_INVALID, null);
+ }
+ if (!((UriInfo) uriInfo).isLinks()) {
+ navigationProperty = getUriInfo.getNavigationSegments().get(0).getNavigationProperty();
+ }
+
+ targetJPAEntity = jpaProcessor.process((GetEntityUriInfo) getUriInfo);
+ if (targetJPAEntity != null && ((UriInfo) uriInfo).isLinks()) {
+ getUriInfo = parser.parseLinkURI();
+ sourceJPAEntity = jpaProcessor.process((GetEntityUriInfo) getUriInfo);
+ if (sourceJPAEntity == null) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.RESOURCE_X_NOT_FOUND
+ .addContent(getUriInfo.getTargetEntitySet().getName()), null);
+ }
+ }
+
+ JPAEntityParser entityParser = new JPAEntityParser();
+ Method setMethod =
+ entityParser.getAccessModifier(sourceJPAEntity, navigationProperty, JPAEntityParser.ACCESS_MODIFIER_SET);
+
+ Method getMethod =
+ entityParser.getAccessModifier(sourceJPAEntity, navigationProperty, JPAEntityParser.ACCESS_MODIFIER_GET);
+
+ if (getMethod.getReturnType().getTypeParameters() != null) {
+ @SuppressWarnings("unchecked")
+ List<Object> relatedEntities = (List<Object>) getMethod.invoke(sourceJPAEntity);
+ relatedEntities.add(targetJPAEntity);
+ setMethod.invoke(sourceJPAEntity, relatedEntities);
+ } else {
+ setMethod.invoke(sourceJPAEntity, targetJPAEntity);
+ }
+ }
+ } catch (IllegalAccessException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ } catch (IllegalArgumentException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ } catch (InvocationTargetException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ } catch (EdmException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ }
+ }
+
+ public void delete() {}
+
+ public void save() {
+ EntityManager em = context.getEntityManager();
+ EntityTransaction tx = em.getTransaction();
+
+ if (!tx.isActive()) {
+ em.getTransaction().begin();
+ em.persist(sourceJPAEntity);
+ em.getTransaction().commit();
+ }
+
+ }
+
+ public void update(final PutMergePatchUriInfo putUriInfo, final InputStream content, final String requestContentType,
+ final String contentType) throws ODataJPARuntimeException, ODataJPAModelException {
+ UriInfo uriInfo = (UriInfo) putUriInfo;
+
+ EdmEntitySet targetEntitySet = uriInfo.getTargetEntitySet();
+ String targerEntitySetName;
+ EdmNavigationProperty navigationProperty = null;
+ try {
+ targerEntitySetName = targetEntitySet.getName();
+ } catch (EdmException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ }
+
+ List<UriInfo> uriInfoList = new ArrayList<UriInfo>();
+
+ if (((UriInfo) uriInfo).isLinks()) {
+ UriInfo getUriInfo = parser.parseLink(targetEntitySet, content, requestContentType);
+ uriInfoList = new ArrayList<UriInfo>();
+ uriInfoList.add(getUriInfo);
+ navigationProperty = uriInfo.getNavigationSegments().get(0).getNavigationProperty();
+ } else {
+ uriInfoList = parser.parseLinks(targetEntitySet, content, contentType);
+ }
+
+ if (uriInfoList == null) {
+ return;
+ }
+ try {
+ for (UriInfo getUriInfo : uriInfoList) {
+
+ if (!getUriInfo.getTargetEntitySet().getName().equals(targerEntitySetName)) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.RELATIONSHIP_INVALID, null);
+ }
+ if (!((UriInfo) uriInfo).isLinks()) {
+ navigationProperty = getUriInfo.getNavigationSegments().get(0).getNavigationProperty();
+ }
+
+ targetJPAEntity = jpaProcessor.process((GetEntityUriInfo) getUriInfo);
+ if (targetJPAEntity != null && ((UriInfo) uriInfo).isLinks()) {
+ getUriInfo = parser.parseLinkURI();
+ sourceJPAEntity = jpaProcessor.process((GetEntityUriInfo) getUriInfo);
+ if (sourceJPAEntity == null) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.RESOURCE_X_NOT_FOUND
+ .addContent(getUriInfo.getTargetEntitySet().getName()), null);
+ }
+ }
+
+ JPAEntityParser entityParser = new JPAEntityParser();
+ Method setMethod =
+ entityParser.getAccessModifier(sourceJPAEntity, navigationProperty, JPAEntityParser.ACCESS_MODIFIER_SET);
+
+ Method getMethod =
+ entityParser.getAccessModifier(sourceJPAEntity, navigationProperty, JPAEntityParser.ACCESS_MODIFIER_GET);
+
+ if (getMethod.getReturnType().getTypeParameters() != null
+ && getMethod.getReturnType().getTypeParameters().length != 0) {
+ @SuppressWarnings("unchecked")
+ List<Object> relatedEntities = (List<Object>) getMethod.invoke(sourceJPAEntity);
+ relatedEntities.add(targetJPAEntity);
+ setMethod.invoke(sourceJPAEntity, relatedEntities);
+ } else {
+ setMethod.invoke(sourceJPAEntity, targetJPAEntity);
+ }
+ }
+ } catch (IllegalAccessException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ } catch (IllegalArgumentException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ } catch (InvocationTargetException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ } catch (EdmException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ }
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/1b479e6c/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/data/JPAProcessorImpl.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/data/JPAProcessorImpl.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/data/JPAProcessorImpl.java
new file mode 100644
index 0000000..8501b77
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/data/JPAProcessorImpl.java
@@ -0,0 +1,491 @@
+/*******************************************************************************
+ * 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.odata2.processor.core.jpa.access.data;
+
+import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.olingo.odata2.api.edm.EdmEntitySet;
+import org.apache.olingo.odata2.api.edm.EdmEntityType;
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmMultiplicity;
+import org.apache.olingo.odata2.api.ep.entry.ODataEntry;
+import org.apache.olingo.odata2.api.uri.info.DeleteUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntityCountUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntityLinkUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntitySetCountUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntitySetLinksUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntitySetUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntityUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetFunctionImportUriInfo;
+import org.apache.olingo.odata2.api.uri.info.PostUriInfo;
+import org.apache.olingo.odata2.api.uri.info.PutMergePatchUriInfo;
+import org.apache.olingo.odata2.processor.api.jpa.ODataJPAContext;
+import org.apache.olingo.odata2.processor.api.jpa.access.JPAFunction;
+import org.apache.olingo.odata2.processor.api.jpa.access.JPAMethodContext;
+import org.apache.olingo.odata2.processor.api.jpa.access.JPAProcessor;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.processor.api.jpa.jpql.JPQLContext;
+import org.apache.olingo.odata2.processor.api.jpa.jpql.JPQLContextType;
+import org.apache.olingo.odata2.processor.api.jpa.jpql.JPQLStatement;
+import org.apache.olingo.odata2.processor.core.jpa.ODataEntityParser;
+
+public class JPAProcessorImpl implements JPAProcessor {
+
+ ODataJPAContext oDataJPAContext;
+ EntityManager em;
+
+ public JPAProcessorImpl(final ODataJPAContext oDataJPAContext) {
+ this.oDataJPAContext = oDataJPAContext;
+ em = oDataJPAContext.getEntityManager();
+ }
+
+ /* Process Function Import Request */
+ @SuppressWarnings("unchecked")
+ @Override
+ public List<Object> process(final GetFunctionImportUriInfo uriParserResultView)
+ throws ODataJPAModelException, ODataJPARuntimeException {
+
+ JPAMethodContext jpaMethodContext = JPAMethodContext.createBuilder(
+ JPQLContextType.FUNCTION, uriParserResultView).build();
+
+ List<Object> resultObj = null;
+
+ try {
+
+ JPAFunction jpaFunction = jpaMethodContext.getJPAFunctionList()
+ .get(0);
+ Method method = jpaFunction.getFunction();
+ Object[] args = jpaFunction.getArguments();
+
+ if (uriParserResultView.getFunctionImport().getReturnType()
+ .getMultiplicity().equals(EdmMultiplicity.MANY)) {
+
+ resultObj = (List<Object>) method.invoke(
+ jpaMethodContext.getEnclosingObject(), args);
+ } else {
+ resultObj = new ArrayList<Object>();
+ Object result = method.invoke(
+ jpaMethodContext.getEnclosingObject(), args);
+ resultObj.add(result);
+ }
+
+ } catch (EdmException e) {
+ throw ODataJPARuntimeException
+ .throwException(ODataJPARuntimeException.GENERAL
+ .addContent(e.getMessage()), e);
+ } catch (IllegalAccessException e) {
+ throw ODataJPARuntimeException
+ .throwException(ODataJPARuntimeException.GENERAL
+ .addContent(e.getMessage()), e);
+ } catch (IllegalArgumentException e) {
+ throw ODataJPARuntimeException
+ .throwException(ODataJPARuntimeException.GENERAL
+ .addContent(e.getMessage()), e);
+ } catch (InvocationTargetException e) {
+ throw ODataJPARuntimeException
+ .throwException(ODataJPARuntimeException.GENERAL
+ .addContent(e.getTargetException().getMessage()), e.getTargetException());
+ }
+
+ return resultObj;
+ }
+
+ /* Process Get Entity Set Request (Query) */
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T> List<T> process(final GetEntitySetUriInfo uriParserResultView)
+ throws ODataJPAModelException, ODataJPARuntimeException {
+
+ if (uriParserResultView.getFunctionImport() != null) {
+ return (List<T>) process((GetFunctionImportUriInfo) uriParserResultView);
+ }
+ JPQLContextType contextType = null;
+ try {
+ if (!uriParserResultView.getStartEntitySet().getName()
+ .equals(uriParserResultView.getTargetEntitySet().getName())) {
+ contextType = JPQLContextType.JOIN;
+ } else {
+ contextType = JPQLContextType.SELECT;
+ }
+
+ } catch (EdmException e) {
+ ODataJPARuntimeException.throwException(
+ ODataJPARuntimeException.GENERAL, e);
+ }
+
+ JPQLContext jpqlContext = JPQLContext.createBuilder(contextType,
+ uriParserResultView).build();
+
+ JPQLStatement jpqlStatement = JPQLStatement.createBuilder(jpqlContext)
+ .build();
+ Query query = null;
+ try {
+ query = em.createQuery(jpqlStatement.toString());
+ // $top/$skip with $inlinecount case handled in response builder to avoid multiple DB call
+ if (uriParserResultView.getSkip() != null && uriParserResultView.getInlineCount() == null) {
+ query.setFirstResult(uriParserResultView.getSkip());
+ }
+
+ if (uriParserResultView.getTop() != null && uriParserResultView.getInlineCount() == null) {
+ if (uriParserResultView.getTop() == 0) {
+ List<T> resultList = new ArrayList<T>();
+ return resultList;
+ } else {
+ query.setMaxResults(uriParserResultView.getTop());
+ }
+ }
+ return query.getResultList();
+ } catch (Exception e) {
+ throw ODataJPARuntimeException.throwException(
+ ODataJPARuntimeException.ERROR_JPQL_QUERY_CREATE, e);
+
+ }
+ }
+
+ /* Process Get Entity Request (Read) */
+ @Override
+ public <T> Object process(GetEntityUriInfo uriParserResultView)
+ throws ODataJPAModelException, ODataJPARuntimeException {
+
+ JPQLContextType contextType = null;
+ try {
+ if (uriParserResultView instanceof GetEntityUriInfo) {
+ uriParserResultView = ((GetEntityUriInfo) uriParserResultView);
+ if (!((GetEntityUriInfo) uriParserResultView).getStartEntitySet().getName()
+ .equals(((GetEntityUriInfo) uriParserResultView).getTargetEntitySet().getName())) {
+ contextType = JPQLContextType.JOIN_SINGLE;
+ } else {
+ contextType = JPQLContextType.SELECT_SINGLE;
+ }
+ }
+ } catch (EdmException e) {
+ ODataJPARuntimeException.throwException(
+ ODataJPARuntimeException.GENERAL, e);
+ }
+
+ return readEntity(uriParserResultView, contextType);
+ }
+
+ /* Process $count for Get Entity Set Request */
+ @Override
+ public long process(final GetEntitySetCountUriInfo resultsView)
+ throws ODataJPAModelException, ODataJPARuntimeException {
+
+ JPQLContextType contextType = null;
+ try {
+ if (!resultsView.getStartEntitySet().getName()
+ .equals(resultsView.getTargetEntitySet().getName())) {
+ contextType = JPQLContextType.JOIN_COUNT;
+ } else {
+ contextType = JPQLContextType.SELECT_COUNT;
+ }
+ } catch (EdmException e) {
+ ODataJPARuntimeException.throwException(
+ ODataJPARuntimeException.GENERAL, e);
+ }
+
+ JPQLContext jpqlContext = JPQLContext.createBuilder(contextType,
+ resultsView).build();
+
+ JPQLStatement jpqlStatement = JPQLStatement.createBuilder(jpqlContext)
+ .build();
+ Query query = null;
+ try {
+
+ query = em.createQuery(jpqlStatement.toString());
+ List<?> resultList = query.getResultList();
+ if (resultList != null && resultList.size() == 1) {
+ return Long.valueOf(resultList.get(0).toString());
+ }
+ } catch (IllegalArgumentException e) {
+ throw ODataJPARuntimeException.throwException(
+ ODataJPARuntimeException.ERROR_JPQL_QUERY_CREATE, e);
+ }
+ return 0;
+ }
+
+ /* Process $count for Get Entity Request */
+ @Override
+ public long process(final GetEntityCountUriInfo resultsView) throws ODataJPAModelException, ODataJPARuntimeException {
+
+ JPQLContextType contextType = null;
+ try {
+ if (!resultsView.getStartEntitySet().getName()
+ .equals(resultsView.getTargetEntitySet().getName())) {
+ contextType = JPQLContextType.JOIN_COUNT;
+ } else {
+ contextType = JPQLContextType.SELECT_COUNT;
+ }
+ } catch (EdmException e) {
+ ODataJPARuntimeException.throwException(
+ ODataJPARuntimeException.GENERAL, e);
+ }
+
+ JPQLContext jpqlContext = JPQLContext.createBuilder(contextType,
+ resultsView).build();
+
+ JPQLStatement jpqlStatement = JPQLStatement.createBuilder(jpqlContext)
+ .build();
+ Query query = null;
+ try {
+
+ query = em.createQuery(jpqlStatement.toString());
+ List<?> resultList = query.getResultList();
+ if (resultList != null && resultList.size() == 1) {
+ return Long.valueOf(resultList.get(0).toString());
+ }
+ } catch (IllegalArgumentException e) {
+ throw ODataJPARuntimeException.throwException(
+ ODataJPARuntimeException.ERROR_JPQL_QUERY_CREATE, e);
+ }
+
+ return 0;
+ }
+
+ /* Process Create Entity Request */
+ @Override
+ public <T> List<T> process(final PostUriInfo createView, final InputStream content,
+ final String requestedContentType) throws ODataJPAModelException,
+ ODataJPARuntimeException {
+ return processCreate(createView, content, null, requestedContentType);
+ }
+
+ @Override
+ public <T> List<T> process(final PostUriInfo createView, final Map<String, Object> content)
+ throws ODataJPAModelException, ODataJPARuntimeException {
+ return processCreate(createView, null, content, null);
+ }
+
+ /* Process Update Entity Request */
+ @Override
+ public <T> Object process(final PutMergePatchUriInfo updateView,
+ final InputStream content, final String requestContentType)
+ throws ODataJPAModelException, ODataJPARuntimeException {
+ return processUpdate(updateView, content, null, requestContentType);
+ }
+
+ @Override
+ public <T> Object process(final PutMergePatchUriInfo updateView, final Map<String, Object> content)
+ throws ODataJPAModelException, ODataJPARuntimeException {
+ return processUpdate(updateView, null, content, null);
+ }
+
+ @SuppressWarnings("unchecked")
+ private <T> List<T> processCreate(final PostUriInfo createView, final InputStream content,
+ final Map<String, Object> properties,
+ final String requestedContentType) throws ODataJPAModelException,
+ ODataJPARuntimeException {
+ try {
+
+ final EdmEntitySet oDataEntitySet = createView.getTargetEntitySet();
+ final EdmEntityType oDataEntityType = oDataEntitySet.getEntityType();
+ final JPAEntity virtualJPAEntity = new JPAEntity(oDataEntityType, oDataEntitySet);
+ final List<Object> createList = new ArrayList<Object>();
+ Object jpaEntity = null;
+
+ if (content != null) {
+ final ODataEntityParser oDataEntityParser = new ODataEntityParser(oDataJPAContext);
+ final ODataEntry oDataEntry =
+ oDataEntityParser.parseEntry(oDataEntitySet, content, requestedContentType, false);
+ virtualJPAEntity.create(oDataEntry);
+ JPALink link = new JPALink(oDataJPAContext);
+ link.setSourceJPAEntity(jpaEntity);
+ link.create(createView, content, requestedContentType, requestedContentType);
+ } else if (properties != null) {
+ virtualJPAEntity.create(properties);
+ } else {
+ return null;
+ }
+
+ em.getTransaction().begin();
+ jpaEntity = virtualJPAEntity.getJPAEntity();
+
+ em.persist(jpaEntity);
+ if (em.contains(jpaEntity)) {
+ em.getTransaction().commit();
+
+ createList.add(virtualJPAEntity.getJPAEntity());
+ createList.add(virtualJPAEntity.getInlineJPAEntities());
+
+ return (List<T>) createList;
+ }
+ } catch (Exception e) {
+ throw ODataJPARuntimeException.throwException(
+ ODataJPARuntimeException.ERROR_JPQL_CREATE_REQUEST, e);
+ }
+ return null;
+ }
+
+ public <T> Object processUpdate(PutMergePatchUriInfo updateView,
+ final InputStream content, final Map<String, Object> properties, final String requestContentType)
+ throws ODataJPAModelException, ODataJPARuntimeException {
+ JPQLContextType contextType = null;
+ Object jpaEntity = null;
+ try {
+ em.getTransaction().begin();
+ if (updateView instanceof PutMergePatchUriInfo) {
+ updateView = ((PutMergePatchUriInfo) updateView);
+ if (!((PutMergePatchUriInfo) updateView).getStartEntitySet().getName()
+ .equals(((PutMergePatchUriInfo) updateView).getTargetEntitySet().getName())) {
+ contextType = JPQLContextType.JOIN_SINGLE;
+ } else {
+ contextType = JPQLContextType.SELECT_SINGLE;
+ }
+ }
+
+ jpaEntity = readEntity(updateView, contextType);
+
+ if (jpaEntity == null) {
+ throw ODataJPARuntimeException
+ .throwException(ODataJPARuntimeException.RESOURCE_NOT_FOUND, null);
+ }
+
+ final EdmEntitySet oDataEntitySet = updateView.getTargetEntitySet();
+ final EdmEntityType oDataEntityType = oDataEntitySet.getEntityType();
+ final JPAEntity virtualJPAEntity = new JPAEntity(oDataEntityType, oDataEntitySet);
+ virtualJPAEntity.setJPAEntity(jpaEntity);
+
+ if (content != null) {
+ final ODataEntityParser oDataEntityParser = new ODataEntityParser(oDataJPAContext);
+ final ODataEntry oDataEntry = oDataEntityParser.parseEntry(oDataEntitySet, content, requestContentType, false);
+ virtualJPAEntity.update(oDataEntry);
+ } else if (properties != null) {
+ virtualJPAEntity.update(properties);
+ } else {
+ return null;
+ }
+ em.flush();
+ em.getTransaction().commit();
+ } catch (Exception e) {
+ throw ODataJPARuntimeException.throwException(
+ ODataJPARuntimeException.ERROR_JPQL_UPDATE_REQUEST, e);
+ }
+
+ return jpaEntity;
+ }
+
+ /* Process Delete Entity Request */
+ @Override
+ public Object process(DeleteUriInfo uriParserResultView, final String contentType)
+ throws ODataJPAModelException, ODataJPARuntimeException {
+ JPQLContextType contextType = null;
+ try {
+ if (uriParserResultView instanceof DeleteUriInfo) {
+ uriParserResultView = ((DeleteUriInfo) uriParserResultView);
+ if (!((DeleteUriInfo) uriParserResultView).getStartEntitySet().getName()
+ .equals(((DeleteUriInfo) uriParserResultView).getTargetEntitySet().getName())) {
+ contextType = JPQLContextType.JOIN_SINGLE;
+ } else {
+ contextType = JPQLContextType.SELECT_SINGLE;
+ }
+ }
+ } catch (EdmException e) {
+ ODataJPARuntimeException.throwException(
+ ODataJPARuntimeException.GENERAL, e);
+ }
+
+ // First read the entity with read operation.
+ Object selectedObject = readEntity(uriParserResultView, contextType);
+ // Read operation done. This object would be passed on to entity manager for delete
+ if (selectedObject != null) {
+ try {
+ em.getTransaction().begin();
+ em.remove(selectedObject);
+ em.flush();
+ em.getTransaction().commit();
+ } catch (Exception e) {
+ throw ODataJPARuntimeException.throwException(
+ ODataJPARuntimeException.ERROR_JPQL_DELETE_REQUEST, e);
+ }
+ }
+ return selectedObject;
+ }
+
+ /* Process Get Entity Link Request */
+ @Override
+ public Object process(final GetEntityLinkUriInfo uriParserResultView)
+ throws ODataJPAModelException, ODataJPARuntimeException {
+
+ return this.process((GetEntityUriInfo) uriParserResultView);
+ }
+
+ /* Process Get Entity Set Link Request */
+ @Override
+ public <T> List<T> process(final GetEntitySetLinksUriInfo uriParserResultView)
+ throws ODataJPAModelException, ODataJPARuntimeException {
+ return this.process((GetEntitySetUriInfo) uriParserResultView);
+ }
+
+ @Override
+ public void process(final PostUriInfo uriInfo,
+ final InputStream content, final String requestContentType, final String contentType)
+ throws ODataJPARuntimeException, ODataJPAModelException {
+ JPALink link = new JPALink(oDataJPAContext);
+ link.create(uriInfo, content, requestContentType, contentType);
+ link.save();
+ }
+
+ /* Common method for Read and Delete */
+ private Object readEntity(final Object uriParserResultView, final JPQLContextType contextType)
+ throws ODataJPAModelException, ODataJPARuntimeException {
+
+ Object selectedObject = null;
+
+ if (uriParserResultView instanceof DeleteUriInfo || uriParserResultView instanceof GetEntityUriInfo
+ || uriParserResultView instanceof PutMergePatchUriInfo) {
+
+ JPQLContext selectJPQLContext = JPQLContext.createBuilder(
+ contextType, uriParserResultView).build();
+
+ JPQLStatement selectJPQLStatement = JPQLStatement.createBuilder(
+ selectJPQLContext).build();
+ Query query = null;
+ try {
+ query = em.createQuery(selectJPQLStatement.toString());
+ if (!query.getResultList().isEmpty()) {
+ selectedObject = query.getResultList().get(0);
+ }
+ } catch (IllegalArgumentException e) {
+ throw ODataJPARuntimeException.throwException(
+ ODataJPARuntimeException.ERROR_JPQL_QUERY_CREATE, e);
+ }
+ }
+ return selectedObject;
+ }
+
+ @Override
+ public void process(final PutMergePatchUriInfo putUriInfo,
+ final InputStream content, final String requestContentType, final String contentType)
+ throws ODataJPARuntimeException, ODataJPAModelException {
+
+ JPALink link = new JPALink(oDataJPAContext);
+ link.update(putUriInfo, content, requestContentType, contentType);
+ link.save();
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/1b479e6c/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/EdmTypeConvertor.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/EdmTypeConvertor.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/EdmTypeConvertor.java
new file mode 100644
index 0000000..f3c3e88
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/EdmTypeConvertor.java
@@ -0,0 +1,69 @@
+/*******************************************************************************
+ * 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.odata2.processor.core.jpa.access.model;
+
+import java.math.BigDecimal;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.UUID;
+
+import org.apache.olingo.odata2.api.edm.EdmSimpleType;
+import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind;
+import org.apache.olingo.odata2.api.edm.EdmType;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException;
+
+public class EdmTypeConvertor {
+
+ public static Class<?> convertToJavaType(final EdmType edmType) throws ODataJPAModelException,
+ ODataJPARuntimeException {
+ if (edmType instanceof EdmSimpleType) {
+ EdmSimpleType edmSimpleType = (EdmSimpleType) edmType;
+ if (edmSimpleType == EdmSimpleTypeKind.String.getEdmSimpleTypeInstance()) {
+ return String.class;
+ } else if (edmSimpleType == EdmSimpleTypeKind.Int64.getEdmSimpleTypeInstance()) {
+ return Long.TYPE;
+ } else if (edmSimpleType == EdmSimpleTypeKind.Int16.getEdmSimpleTypeInstance()) {
+ return Short.TYPE;
+ } else if (edmSimpleType == EdmSimpleTypeKind.Int32.getEdmSimpleTypeInstance()) {
+ return Integer.TYPE;
+ } else if (edmSimpleType == EdmSimpleTypeKind.Double.getEdmSimpleTypeInstance()) {
+ return Double.TYPE;
+ } else if (edmSimpleType == EdmSimpleTypeKind.Single.getEdmSimpleTypeInstance()) {
+ return Float.TYPE;
+ } else if (edmSimpleType == EdmSimpleTypeKind.Decimal.getEdmSimpleTypeInstance()) {
+ return BigDecimal.class;
+ } else if (edmSimpleType == EdmSimpleTypeKind.Binary.getEdmSimpleTypeInstance()) {
+ return byte[].class;
+ } else if (edmSimpleType == EdmSimpleTypeKind.Byte.getEdmSimpleTypeInstance()) {
+ return Byte.TYPE;
+ } else if (edmSimpleType == EdmSimpleTypeKind.Boolean.getEdmSimpleTypeInstance()) {
+ return Boolean.TYPE;
+ } else if (edmSimpleType == EdmSimpleTypeKind.DateTime.getEdmSimpleTypeInstance()) {
+ return Date.class;
+ } else if (edmSimpleType == EdmSimpleTypeKind.DateTimeOffset.getEdmSimpleTypeInstance()) {
+ return Calendar.class;
+ } else if (edmSimpleType == EdmSimpleTypeKind.Guid.getEdmSimpleTypeInstance()) {
+ return UUID.class;
+ }
+ }
+ throw ODataJPAModelException.throwException(ODataJPAModelException.TYPE_NOT_SUPPORTED
+ .addContent(edmType.toString()), null);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/1b479e6c/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmMappingModelService.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmMappingModelService.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmMappingModelService.java
new file mode 100644
index 0000000..d00638e
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmMappingModelService.java
@@ -0,0 +1,247 @@
+/*******************************************************************************
+ * 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.odata2.processor.core.jpa.access.model;
+
+import java.io.InputStream;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
+
+import org.apache.olingo.odata2.processor.api.jpa.ODataJPAContext;
+import org.apache.olingo.odata2.processor.api.jpa.access.JPAEdmMappingModelAccess;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.processor.api.jpa.model.mapping.JPAAttributeMapType.JPAAttribute;
+import org.apache.olingo.odata2.processor.api.jpa.model.mapping.JPAEdmMappingModel;
+import org.apache.olingo.odata2.processor.api.jpa.model.mapping.JPAEmbeddableTypeMapType;
+import org.apache.olingo.odata2.processor.api.jpa.model.mapping.JPAEntityTypeMapType;
+import org.apache.olingo.odata2.processor.api.jpa.model.mapping.JPAPersistenceUnitMapType;
+import org.apache.olingo.odata2.processor.api.jpa.model.mapping.JPARelationshipMapType.JPARelationship;
+
+public class JPAEdmMappingModelService implements JPAEdmMappingModelAccess {
+
+ boolean mappingModelExists = true;
+ private JPAEdmMappingModel mappingModel;
+ private String mappingModelName;
+
+ public JPAEdmMappingModelService(final ODataJPAContext ctx) {
+ mappingModelName = ctx.getJPAEdmMappingModel();
+ if (mappingModelName == null) {
+ mappingModelExists = false;
+ }
+ }
+
+ @Override
+ public void loadMappingModel() {
+
+ if (mappingModelExists) {
+ JAXBContext context;
+ try {
+ context = JAXBContext.newInstance(JPAEdmMappingModel.class);
+
+ Unmarshaller unmarshaller = context.createUnmarshaller();
+ InputStream is = loadMappingModelInputStream();
+ if (is == null) {
+ mappingModelExists = false;
+ return;
+ }
+
+ mappingModel = (JPAEdmMappingModel) unmarshaller.unmarshal(is);
+
+ if (mappingModel != null) {
+ mappingModelExists = true;
+ }
+
+ } catch (JAXBException e) {
+ mappingModelExists = false;
+ ODataJPAModelException.throwException(ODataJPAModelException.GENERAL, e);
+ }
+ }
+ }
+
+ @Override
+ public boolean isMappingModelExists() {
+ return mappingModelExists;
+ }
+
+ @Override
+ public JPAEdmMappingModel getJPAEdmMappingModel() {
+ return mappingModel;
+ }
+
+ @Override
+ public String mapJPAPersistenceUnit(final String persistenceUnitName) {
+
+ JPAPersistenceUnitMapType persistenceUnit = mappingModel.getPersistenceUnit();
+ if (persistenceUnit.getName().equals(persistenceUnitName)) {
+ return persistenceUnit.getEDMSchemaNamespace();
+ }
+
+ return null;
+ }
+
+ @Override
+ public String mapJPAEntityType(final String jpaEntityTypeName) {
+
+ JPAEntityTypeMapType jpaEntityTypeMap = searchJPAEntityTypeMapType(jpaEntityTypeName);
+ if (jpaEntityTypeMap != null) {
+ return jpaEntityTypeMap.getEDMEntityType();
+ } else {
+ return null;
+ }
+ }
+
+ @Override
+ public String mapJPAEntitySet(final String jpaEntityTypeName) {
+ JPAEntityTypeMapType jpaEntityTypeMap = searchJPAEntityTypeMapType(jpaEntityTypeName);
+ if (jpaEntityTypeMap != null) {
+ return jpaEntityTypeMap.getEDMEntitySet();
+ } else {
+ return null;
+ }
+ }
+
+ @Override
+ public String mapJPAAttribute(final String jpaEntityTypeName, final String jpaAttributeName) {
+ JPAEntityTypeMapType jpaEntityTypeMap = searchJPAEntityTypeMapType(jpaEntityTypeName);
+ if (jpaEntityTypeMap != null && jpaEntityTypeMap.getJPAAttributes() != null) {
+ // fixing attributes
+ // removal issue
+ // from mapping
+ for (JPAAttribute jpaAttribute : jpaEntityTypeMap.getJPAAttributes().getJPAAttribute()) {
+ if (jpaAttribute.getName().equals(jpaAttributeName)) {
+ return jpaAttribute.getValue();
+ }
+ }
+ }
+
+ return null;
+ }
+
+ @Override
+ public String mapJPARelationship(final String jpaEntityTypeName, final String jpaRelationshipName) {
+ JPAEntityTypeMapType jpaEntityTypeMap = searchJPAEntityTypeMapType(jpaEntityTypeName);
+ if (jpaEntityTypeMap != null && jpaEntityTypeMap.getJPARelationships() != null) {
+ for (JPARelationship jpaRealtionship : jpaEntityTypeMap.getJPARelationships().getJPARelationship()) {
+ if (jpaRealtionship.getName().equals(jpaRelationshipName)) {
+ return jpaRealtionship.getValue();
+ }
+ }
+ }
+
+ return null;
+ }
+
+ @Override
+ public String mapJPAEmbeddableType(final String jpaEmbeddableTypeName) {
+ JPAEmbeddableTypeMapType jpaEmbeddableType = searchJPAEmbeddableTypeMapType(jpaEmbeddableTypeName);
+ if (jpaEmbeddableType != null) {
+ return jpaEmbeddableType.getEDMComplexType();
+ } else {
+ return null;
+ }
+ }
+
+ @Override
+ public String mapJPAEmbeddableTypeAttribute(final String jpaEmbeddableTypeName, final String jpaAttributeName) {
+ JPAEmbeddableTypeMapType jpaEmbeddableType = searchJPAEmbeddableTypeMapType(jpaEmbeddableTypeName);
+ if (jpaEmbeddableType != null && jpaEmbeddableType.getJPAAttributes() != null) {
+ for (JPAAttribute jpaAttribute : jpaEmbeddableType.getJPAAttributes().getJPAAttribute()) {
+ if (jpaAttribute.getName().equals(jpaAttributeName)) {
+ return jpaAttribute.getValue();
+ }
+ }
+ }
+ return null;
+ }
+
+ private JPAEntityTypeMapType searchJPAEntityTypeMapType(final String jpaEntityTypeName) {
+ for (JPAEntityTypeMapType jpaEntityType : mappingModel.getPersistenceUnit().getJPAEntityTypes()
+ .getJPAEntityType()) {
+ if (jpaEntityType.getName().equals(jpaEntityTypeName)) {
+ return jpaEntityType;
+ }
+ }
+
+ return null;
+ }
+
+ private JPAEmbeddableTypeMapType searchJPAEmbeddableTypeMapType(final String jpaEmbeddableTypeName) {
+ for (JPAEmbeddableTypeMapType jpaEmbeddableType : mappingModel.getPersistenceUnit().getJPAEmbeddableTypes()
+ .getJPAEmbeddableType()) {
+ if (jpaEmbeddableType.getName().equals(jpaEmbeddableTypeName)) {
+ return jpaEmbeddableType;
+ }
+ }
+
+ return null;
+ }
+
+ protected InputStream loadMappingModelInputStream() {
+ InputStream is = JPAEdmMappingModelService.class.getClassLoader().getResourceAsStream("../../" + mappingModelName);
+
+ return is;
+
+ }
+
+ @Override
+ public boolean checkExclusionOfJPAEntityType(final String jpaEntityTypeName) {
+ JPAEntityTypeMapType type = searchJPAEntityTypeMapType(jpaEntityTypeName);
+ if (type != null) {
+ return type.isExclude();
+ }
+ return false;
+ }
+
+ @Override
+ public boolean checkExclusionOfJPAAttributeType(final String jpaEntityTypeName, final String jpaAttributeName) {
+ JPAEntityTypeMapType type = searchJPAEntityTypeMapType(jpaEntityTypeName);
+ if (type != null && type.getJPAAttributes() != null) {
+ for (JPAAttribute jpaAttribute : type.getJPAAttributes().getJPAAttribute()) {
+ if (jpaAttribute.getName().equals(jpaAttributeName)) {
+ return jpaAttribute.isExclude();
+ }
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public boolean checkExclusionOfJPAEmbeddableType(final String jpaEmbeddableTypeName) {
+ JPAEmbeddableTypeMapType type = searchJPAEmbeddableTypeMapType(jpaEmbeddableTypeName);
+ if (type != null) {
+ return type.isExclude();
+ }
+ return false;
+ }
+
+ @Override
+ public boolean checkExclusionOfJPAEmbeddableAttributeType(final String jpaEmbeddableTypeName,
+ final String jpaAttributeName) {
+ JPAEmbeddableTypeMapType type = searchJPAEmbeddableTypeMapType(jpaEmbeddableTypeName);
+ if (type != null && type.getJPAAttributes() != null) {
+ for (JPAAttribute jpaAttribute : type.getJPAAttributes().getJPAAttribute()) {
+ if (jpaAttribute.getName().equals(jpaAttributeName)) {
+ return jpaAttribute.isExclude();
+ }
+ }
+ }
+ return false;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/1b479e6c/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmNameBuilder.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmNameBuilder.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmNameBuilder.java
new file mode 100644
index 0000000..2a96ac9
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmNameBuilder.java
@@ -0,0 +1,499 @@
+/*******************************************************************************
+ * 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.odata2.processor.core.jpa.access.model;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Field;
+
+import javax.persistence.Column;
+import javax.persistence.metamodel.Attribute;
+import javax.persistence.metamodel.ManagedType;
+import javax.persistence.metamodel.PluralAttribute;
+
+import org.apache.olingo.odata2.api.edm.FullQualifiedName;
+import org.apache.olingo.odata2.api.edm.provider.Association;
+import org.apache.olingo.odata2.api.edm.provider.AssociationSet;
+import org.apache.olingo.odata2.api.edm.provider.ComplexProperty;
+import org.apache.olingo.odata2.api.edm.provider.ComplexType;
+import org.apache.olingo.odata2.api.edm.provider.EntityType;
+import org.apache.olingo.odata2.api.edm.provider.Mapping;
+import org.apache.olingo.odata2.api.edm.provider.NavigationProperty;
+import org.apache.olingo.odata2.processor.api.jpa.access.JPAEdmMappingModelAccess;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmAssociationEndView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmAssociationSetView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmAssociationView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmBaseView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmComplexPropertyView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmEntityContainerView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmEntitySetView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmEntityTypeView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmMapping;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmNavigationPropertyView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmPropertyView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmSchemaView;
+import org.apache.olingo.odata2.processor.core.jpa.model.JPAEdmComplexType;
+import org.apache.olingo.odata2.processor.core.jpa.model.JPAEdmMappingImpl;
+
+public class JPAEdmNameBuilder {
+ private static final String ENTITY_CONTAINER_SUFFIX = "Container";
+ private static final String ENTITY_SET_SUFFIX = "s";
+ private static final String ASSOCIATIONSET_SUFFIX = "Set";
+ private static final String NAVIGATION_NAME = "Details";
+ private static final String UNDERSCORE = "_";
+
+ public static FullQualifiedName build(final JPAEdmBaseView view, final String name) {
+ FullQualifiedName fqName = new FullQualifiedName(buildNamespace(view), name);
+ return fqName;
+ }
+
+ /*
+ * ************************************************************************
+ * EDM EntityType Name - RULES
+ * ************************************************************************
+ * EDM Entity Type Name = JPA Entity Name EDM Entity Type Internal Name =
+ * JPA Entity Name
+ * ************************************************************************
+ * EDM Entity Type Name - RULES
+ * ************************************************************************
+ */
+ public static void build(final JPAEdmEntityTypeView view) {
+
+ EntityType edmEntityType = view.getEdmEntityType();
+ String jpaEntityName = view.getJPAEntityType().getName();
+ JPAEdmMappingModelAccess mappingModelAccess = view.getJPAEdmMappingModelAccess();
+ String edmEntityTypeName = null;
+ if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()) {
+ edmEntityTypeName = mappingModelAccess.mapJPAEntityType(jpaEntityName);
+ }
+
+ JPAEdmMapping mapping = new JPAEdmMappingImpl();
+ mapping.setJPAType(view.getJPAEntityType().getJavaType());
+
+ if (edmEntityTypeName == null) {
+ edmEntityTypeName = jpaEntityName;
+ }
+ // Setting the mapping object
+ edmEntityType.setMapping(((Mapping) mapping).setInternalName(jpaEntityName));
+
+ edmEntityType.setName(edmEntityTypeName);
+
+ }
+
+ /*
+ * ************************************************************************
+ * EDM Schema Name - RULES
+ * ************************************************************************
+ * Java Persistence Unit name is set as Schema's Namespace
+ * ************************************************************************
+ * EDM Schema Name - RULES
+ * ************************************************************************
+ */
+ public static void build(final JPAEdmSchemaView view) throws ODataJPAModelException {
+ view.getEdmSchema().setNamespace(buildNamespace(view));
+ }
+
+ /*
+ * ************************************************************************
+ * EDM Property Name - RULES
+ * ************************************************************************
+ * OData Property Names are represented in Camel Case. The first character
+ * of JPA Attribute Name is converted to an UpperCase Character and set as
+ * OData Property Name. JPA Attribute Name is set as Internal Name for OData
+ * Property. The Column name (annotated as @Column(name="x")) is set as
+ * column name in the mapping object.
+ * ************************************************************************
+ * EDM Property Name - RULES
+ * ************************************************************************
+ */
+ public static void build(final JPAEdmPropertyView view, final boolean isComplexMode,
+ final boolean skipDefaultNaming) {
+ Attribute<?, ?> jpaAttribute = view.getJPAAttribute();
+ String jpaAttributeName = jpaAttribute.getName();
+ String propertyName = null;
+
+ JPAEdmMappingModelAccess mappingModelAccess = view.getJPAEdmMappingModelAccess();
+ if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()) {
+ if (isComplexMode) {
+ propertyName =
+ mappingModelAccess.mapJPAEmbeddableTypeAttribute(view.getJPAEdmComplexTypeView().getJPAEmbeddableType()
+ .getJavaType().getSimpleName(), jpaAttributeName);
+ } else {
+ propertyName =
+ mappingModelAccess.mapJPAAttribute(view.getJPAEdmEntityTypeView().getJPAEntityType().getName(),
+ jpaAttributeName);
+ }
+ }
+ if (skipDefaultNaming == false && propertyName == null) {
+ propertyName = Character.toUpperCase(jpaAttributeName.charAt(0)) + jpaAttributeName.substring(1);
+ } else if (propertyName == null) {
+ propertyName = jpaAttributeName;
+ }
+
+ view.getEdmSimpleProperty().setName(propertyName);
+
+ JPAEdmMapping mapping = new JPAEdmMappingImpl();
+ ((Mapping) mapping).setInternalName(jpaAttributeName);
+ mapping.setJPAType(jpaAttribute.getJavaType());
+
+ AnnotatedElement annotatedElement = (AnnotatedElement) jpaAttribute.getJavaMember();
+ if (annotatedElement != null) {
+ Column column = annotatedElement.getAnnotation(Column.class);
+ if (column != null) {
+ mapping.setJPAColumnName(column.name());
+ }
+ } else {
+ ManagedType<?> managedType = jpaAttribute.getDeclaringType();
+ if (managedType != null) {
+ Class<?> clazz = managedType.getJavaType();
+ try {
+ Field field = clazz.getField(jpaAttributeName);
+ Column column = field.getAnnotation(Column.class);
+ if (column != null) {
+ mapping.setJPAColumnName(column.name());
+ }
+ } catch (SecurityException e) {
+
+ } catch (NoSuchFieldException e) {
+
+ }
+ }
+
+ }
+ view.getEdmSimpleProperty().setMapping((Mapping) mapping);
+ }
+
+ /*
+ * ************************************************************************
+ * EDM EntityContainer Name - RULES
+ * ************************************************************************
+ * Entity Container Name = EDM Namespace + Literal "Container"
+ * ************************************************************************
+ * EDM EntityContainer Name - RULES
+ * ************************************************************************
+ */
+ public static void build(final JPAEdmEntityContainerView view) {
+ view.getEdmEntityContainer().setName(buildNamespace(view) + ENTITY_CONTAINER_SUFFIX);
+ }
+
+ /*
+ * ************************************************************************
+ * EDM EntitySet Name - RULES
+ * ************************************************************************
+ * Entity Set Name = JPA Entity Type Name + Literal "s"
+ * ************************************************************************
+ * EDM EntitySet Name - RULES
+ * ************************************************************************
+ */
+ public static void build(final JPAEdmEntitySetView view, final JPAEdmEntityTypeView entityTypeView) {
+ FullQualifiedName fQname = view.getEdmEntitySet().getEntityType();
+ JPAEdmMappingModelAccess mappingModelAccess = view.getJPAEdmMappingModelAccess();
+ String entitySetName = null;
+ if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()) {
+ Mapping mapping = entityTypeView.getEdmEntityType().getMapping();
+ if (mapping != null) {
+ entitySetName = mappingModelAccess.mapJPAEntitySet(mapping.getInternalName());
+ }
+ }
+
+ if (entitySetName == null) {
+ entitySetName = fQname.getName() + ENTITY_SET_SUFFIX;
+ }
+
+ view.getEdmEntitySet().setName(entitySetName);
+ }
+
+ /*
+ * ************************************************************************
+ * EDM Complex Type Name - RULES
+ * ************************************************************************
+ * Complex Type Name = JPA Embeddable Type Simple Name.
+ * ************************************************************************
+ * EDM Complex Type Name - RULES
+ * ************************************************************************
+ */
+ public static void build(final JPAEdmComplexType view) {
+
+ JPAEdmMappingModelAccess mappingModelAccess = view.getJPAEdmMappingModelAccess();
+ String jpaEmbeddableTypeName = view.getJPAEmbeddableType().getJavaType().getSimpleName();
+ String edmComplexTypeName = null;
+ if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()) {
+ edmComplexTypeName = mappingModelAccess.mapJPAEmbeddableType(jpaEmbeddableTypeName);
+ }
+
+ if (edmComplexTypeName == null) {
+ edmComplexTypeName = jpaEmbeddableTypeName;
+ }
+
+ view.getEdmComplexType().setName(edmComplexTypeName);
+ ComplexType complexType = view.getEdmComplexType();
+ complexType.setName(edmComplexTypeName);
+ JPAEdmMapping mapping = new JPAEdmMappingImpl();
+ mapping.setJPAType(view.getJPAEmbeddableType().getJavaType());
+ complexType.setMapping((Mapping) mapping);
+
+ }
+
+ /*
+ * ************************************************************************
+ * EDM Complex Property Name - RULES
+ * ************************************************************************
+ * The first character of JPA complex attribute name is converted to
+ * uppercase. The modified JPA complex attribute name is assigned as EDM
+ * complex property name. The unmodified JPA complex attribute name is
+ * assigned as internal name.
+ * ************************************************************************
+ * EDM Complex Property Name - RULES
+ * ************************************************************************
+ */
+ public static void build(final JPAEdmComplexPropertyView complexView,
+ final JPAEdmPropertyView propertyView, final boolean skipDefaultNaming) {
+
+ ComplexProperty complexProperty = complexView.getEdmComplexProperty();
+
+ String jpaAttributeName = propertyView.getJPAAttribute().getName();
+ String jpaEntityTypeName = propertyView.getJPAEdmEntityTypeView().getJPAEntityType().getName();
+
+ JPAEdmMappingModelAccess mappingModelAccess = complexView.getJPAEdmMappingModelAccess();
+ String propertyName = null;
+
+ if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()) {
+ propertyName = mappingModelAccess.mapJPAAttribute(jpaEntityTypeName, jpaAttributeName);
+ }
+
+ if (skipDefaultNaming == false && propertyName == null) {
+ propertyName = Character.toUpperCase(jpaAttributeName.charAt(0)) + jpaAttributeName.substring(1);
+ } else if (propertyName == null) {
+ propertyName = jpaAttributeName;
+ }
+
+ // change for navigation property issue
+ JPAEdmMapping mapping = new JPAEdmMappingImpl();
+ ((Mapping) mapping).setInternalName(jpaAttributeName);
+ mapping.setJPAType(propertyView.getJPAAttribute().getJavaType());
+ complexProperty.setMapping((Mapping) mapping);
+
+ complexProperty.setName(propertyName);
+
+ }
+
+ public static void build(final JPAEdmComplexPropertyView complexView,
+ final String parentComplexTypeName, final boolean skipDefaultNaming) {
+ ComplexProperty complexProperty = complexView.getEdmComplexProperty();
+
+ JPAEdmMappingModelAccess mappingModelAccess = complexView.getJPAEdmMappingModelAccess();
+ JPAEdmPropertyView propertyView = ((JPAEdmPropertyView) complexView);
+ String jpaAttributeName = propertyView.getJPAAttribute().getName();
+ String propertyName = null;
+ if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()) {
+ propertyName = mappingModelAccess.mapJPAEmbeddableTypeAttribute(parentComplexTypeName, jpaAttributeName);
+ }
+ if (skipDefaultNaming == false && propertyName == null) {
+ propertyName = Character.toUpperCase(jpaAttributeName.charAt(0)) + jpaAttributeName.substring(1);
+ } else if (propertyName == null) {
+ propertyName = jpaAttributeName;
+ }
+
+ JPAEdmMapping mapping = new JPAEdmMappingImpl();
+ ((Mapping) mapping).setInternalName(jpaAttributeName);
+ mapping.setJPAType(propertyView.getJPAAttribute().getJavaType());
+ complexProperty.setMapping((Mapping) mapping);
+ complexProperty.setName(propertyName);
+
+ }
+
+ /*
+ * ************************************************************************
+ * EDM Association End Name - RULES
+ * ************************************************************************
+ * Association End name = Namespace + Entity Type Name
+ * ************************************************************************
+ * EDM Association End Name - RULES
+ * ************************************************************************
+ */
+ public static void build(final JPAEdmAssociationEndView assocaitionEndView,
+ final JPAEdmEntityTypeView entityTypeView, final JPAEdmPropertyView propertyView) {
+
+ String namespace = buildNamespace(assocaitionEndView);
+
+ String name = entityTypeView.getEdmEntityType().getName();
+ FullQualifiedName fQName = new FullQualifiedName(namespace, name);
+ assocaitionEndView.getEdmAssociationEnd1().setType(fQName);
+
+ name = null;
+ String jpaEntityTypeName = null;
+ Attribute<?, ?> jpaAttribute = propertyView.getJPAAttribute();
+ if (jpaAttribute.isCollection()) {
+ jpaEntityTypeName = ((PluralAttribute<?, ?, ?>) jpaAttribute).getElementType().getJavaType()
+ .getSimpleName();
+ } else {
+ jpaEntityTypeName = propertyView.getJPAAttribute().getJavaType()
+ .getSimpleName();
+ }
+
+ JPAEdmMappingModelAccess mappingModelAccess = assocaitionEndView.getJPAEdmMappingModelAccess();
+
+ if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()) {
+ name = mappingModelAccess.mapJPAEntityType(jpaEntityTypeName);
+ }
+
+ if (name == null) {
+ name = jpaEntityTypeName;
+ }
+
+ fQName = new FullQualifiedName(namespace, name);
+ assocaitionEndView.getEdmAssociationEnd2().setType(fQName);
+
+ }
+
+ private static String buildNamespace(final JPAEdmBaseView view) {
+ JPAEdmMappingModelAccess mappingModelAccess = view.getJPAEdmMappingModelAccess();
+ String namespace = null;
+ if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()) {
+ namespace = mappingModelAccess.mapJPAPersistenceUnit(view.getpUnitName());
+ }
+ if (namespace == null) {
+ namespace = view.getpUnitName();
+ }
+
+ return namespace;
+ }
+
+ /*
+ * ************************************************************************
+ * EDM Association Name - RULES
+ * ************************************************************************
+ * Association name = Association + End1 Name + End2 Name
+ * ************************************************************************
+ * EDM Association Name - RULES
+ * ************************************************************************
+ */
+
+ public static void build(final JPAEdmAssociationView view, final int count) {
+ Association association = view.getEdmAssociation();
+ String associationName = null;
+ String end1Name = association.getEnd1().getType().getName();
+ String end2Name = association.getEnd2().getType().getName();
+
+ if (end1Name.compareToIgnoreCase(end2Name) > 0) {
+ associationName = end2Name + UNDERSCORE + end1Name;
+ } else {
+ associationName = end1Name + UNDERSCORE + end2Name;
+ }
+ if (count > 1) {
+ associationName = associationName + Integer.toString(count - 1);
+ }
+ association.setName(associationName);
+
+ }
+
+ /*
+ * ************************************************************************
+ * EDM Association Set Name - RULES
+ * ************************************************************************
+ * Association Set name = Association Name + "Set"
+ * ************************************************************************
+ * EDM Association Set Name - RULES
+ * ************************************************************************
+ */
+ public static void build(final JPAEdmAssociationSetView view) {
+ AssociationSet associationSet = view.getEdmAssociationSet();
+
+ String name = view.getEdmAssociation().getName();
+ associationSet.setName(name + ASSOCIATIONSET_SUFFIX);
+
+ }
+
+ public static void build(final JPAEdmAssociationView associationView,
+ final JPAEdmPropertyView propertyView,
+ final JPAEdmNavigationPropertyView navPropertyView, final boolean skipDefaultNaming, final int count) {
+
+ String toName = null;
+ String fromName = null;
+ String navPropName = null;
+ NavigationProperty navProp = navPropertyView.getEdmNavigationProperty();
+ String namespace = buildNamespace(associationView);
+
+ Association association = associationView.getEdmAssociation();
+ navProp.setRelationship(new FullQualifiedName(namespace, association
+ .getName()));
+
+ FullQualifiedName associationEndTypeOne = association.getEnd1()
+ .getType();
+ FullQualifiedName associationEndTypeTwo = association.getEnd2()
+ .getType();
+
+ Attribute<?, ?> jpaAttribute = propertyView.getJPAAttribute();
+ JPAEdmMapping mapping = new JPAEdmMappingImpl();
+ ((Mapping) mapping).setInternalName(jpaAttribute.getName());
+ mapping.setJPAType(jpaAttribute.getJavaType());
+ navProp.setMapping((Mapping) mapping);
+
+ String jpaEntityTypeName = propertyView.getJPAEdmEntityTypeView()
+ .getJPAEntityType().getName();
+ JPAEdmMappingModelAccess mappingModelAccess = navPropertyView
+ .getJPAEdmMappingModelAccess();
+
+ String targetEntityTypeName = null;
+ if (jpaAttribute.isCollection()) {
+ targetEntityTypeName = ((PluralAttribute<?, ?, ?>) jpaAttribute).getElementType().getJavaType().getSimpleName();
+ } else {
+ targetEntityTypeName = jpaAttribute.getJavaType().getSimpleName();
+ }
+
+ if (mappingModelAccess != null
+ && mappingModelAccess.isMappingModelExists()) {
+ navPropName = mappingModelAccess.mapJPARelationship(
+ jpaEntityTypeName, jpaAttribute.getName());
+ toName = mappingModelAccess.mapJPAEntityType(targetEntityTypeName);
+ fromName = mappingModelAccess
+ .mapJPAEntityType(jpaEntityTypeName);
+ }
+ if (toName == null) {
+ toName = targetEntityTypeName;
+ }
+
+ if (fromName == null) {
+ fromName = jpaEntityTypeName;
+ }
+
+ if (skipDefaultNaming == false) {
+ if (navPropName == null) {
+ navPropName = toName.concat(NAVIGATION_NAME);
+ }
+ if (count > 1) {
+ navPropName = navPropName + Integer.toString(count - 1);
+ }
+ } else if (navPropName == null) {
+ navPropName = jpaAttribute.getName();
+ }
+
+ navProp.setName(navPropName);
+
+ if (toName.equals(associationEndTypeOne.getName())) {
+ navProp.setFromRole(association.getEnd2().getRole());
+ navProp.setToRole(association.getEnd1().getRole());
+ } else if (toName.equals(associationEndTypeTwo.getName())) {
+
+ navProp.setToRole(association.getEnd2().getRole());
+ navProp.setFromRole(association.getEnd1().getRole());
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/1b479e6c/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPATypeConvertor.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPATypeConvertor.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPATypeConvertor.java
new file mode 100644
index 0000000..e7b8a6e
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPATypeConvertor.java
@@ -0,0 +1,119 @@
+/*******************************************************************************
+ * 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.odata2.processor.core.jpa.access.model;
+
+import java.lang.reflect.Field;
+import java.math.BigDecimal;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.UUID;
+
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+import javax.persistence.metamodel.Attribute;
+
+import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException;
+
+/**
+ * This class holds utility methods for Type conversions between JPA and OData Types.
+ *
+ *
+ *
+ */
+public class JPATypeConvertor {
+
+ /**
+ * This utility method converts a given jpa Type to equivalent
+ * EdmSimpleTypeKind for maintaining compatibility between Java and OData
+ * Types.
+ *
+ * @param jpaType
+ * The JPA Type input.
+ * @return The corresponding EdmSimpleTypeKind.
+ * @throws ODataJPAModelException
+ * @throws org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException
+ *
+ * @see EdmSimpleTypeKind
+ */
+
+ private static final String OBJECT_TYPE = "java.lang.Object";
+
+ public static EdmSimpleTypeKind
+ convertToEdmSimpleType(final Class<?> jpaType, final Attribute<?, ?> currentAttribute)
+ throws ODataJPAModelException {
+ if (jpaType.equals(String.class) || jpaType.equals(Character.class) || jpaType.equals(char.class)
+ || jpaType.equals(char[].class) ||
+ jpaType.equals(Character[].class)) {
+ return EdmSimpleTypeKind.String;
+ } else if (jpaType.equals(Long.class) || jpaType.equals(long.class)) {
+ return EdmSimpleTypeKind.Int64;
+ } else if (jpaType.equals(Short.class) || jpaType.equals(short.class)) {
+ return EdmSimpleTypeKind.Int16;
+ } else if (jpaType.equals(Integer.class) || jpaType.equals(int.class)) {
+ return EdmSimpleTypeKind.Int32;
+ } else if (jpaType.equals(Double.class) || jpaType.equals(double.class)) {
+ return EdmSimpleTypeKind.Double;
+ } else if (jpaType.equals(Float.class) || jpaType.equals(float.class)) {
+ return EdmSimpleTypeKind.Single;
+ } else if (jpaType.equals(BigDecimal.class)) {
+ return EdmSimpleTypeKind.Decimal;
+ } else if (jpaType.equals(byte[].class)) {
+ return EdmSimpleTypeKind.Binary;
+ } else if (jpaType.equals(Byte.class) || jpaType.equals(byte.class)) {
+ return EdmSimpleTypeKind.Byte;
+ } else if (jpaType.equals(Byte[].class)) {
+ return EdmSimpleTypeKind.Binary;
+ } else if (jpaType.equals(Boolean.class) || jpaType.equals(boolean.class)) {
+ return EdmSimpleTypeKind.Boolean;
+ } else if ((jpaType.equals(Date.class)) || (jpaType.equals(Calendar.class))) {
+ try {
+ if ((currentAttribute != null)
+ && (determineTemporalType(currentAttribute.getDeclaringType().getJavaType(), currentAttribute.getName())
+ == TemporalType.TIME)) {
+ return EdmSimpleTypeKind.Time;
+ } else {
+ return EdmSimpleTypeKind.DateTime;
+ }
+ } catch (SecurityException e) {
+ throw ODataJPAModelException.throwException(ODataJPAModelException.GENERAL.addContent(e.getMessage()), e);
+ }
+ } else if (jpaType.equals(UUID.class)) {
+ return EdmSimpleTypeKind.Guid;
+ }
+ throw ODataJPAModelException.throwException(ODataJPAModelException.TYPE_NOT_SUPPORTED
+ .addContent(jpaType.toString()), null);
+ }
+
+ private static TemporalType determineTemporalType(final Class<?> type, final String fieldName)
+ throws ODataJPAModelException {
+ if (type != null && !type.getName().equals(OBJECT_TYPE)) {
+ try {
+ Field field = type.getField(fieldName);
+ return field.getAnnotation(Temporal.class).value();
+ } catch (NoSuchFieldException e) {
+ determineTemporalType(type.getSuperclass(), fieldName);
+ } catch (SecurityException e) {
+ throw ODataJPAModelException.throwException(ODataJPAModelException.GENERAL.addContent(e.getMessage()), e);
+ }
+ }
+ return null;
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/1b479e6c/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/edm/ODataJPAEdmProvider.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/edm/ODataJPAEdmProvider.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/edm/ODataJPAEdmProvider.java
new file mode 100644
index 0000000..e0014e5
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/edm/ODataJPAEdmProvider.java
@@ -0,0 +1,295 @@
+/*******************************************************************************
+ * 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.odata2.processor.core.jpa.edm;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import org.apache.olingo.odata2.api.edm.FullQualifiedName;
+import org.apache.olingo.odata2.api.edm.provider.Association;
+import org.apache.olingo.odata2.api.edm.provider.AssociationSet;
+import org.apache.olingo.odata2.api.edm.provider.AssociationSetEnd;
+import org.apache.olingo.odata2.api.edm.provider.ComplexType;
+import org.apache.olingo.odata2.api.edm.provider.EdmProvider;
+import org.apache.olingo.odata2.api.edm.provider.EntityContainer;
+import org.apache.olingo.odata2.api.edm.provider.EntityContainerInfo;
+import org.apache.olingo.odata2.api.edm.provider.EntitySet;
+import org.apache.olingo.odata2.api.edm.provider.EntityType;
+import org.apache.olingo.odata2.api.edm.provider.FunctionImport;
+import org.apache.olingo.odata2.api.edm.provider.Schema;
+import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.processor.api.jpa.ODataJPAContext;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAException;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.processor.api.jpa.factory.ODataJPAFactory;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmModelView;
+
+public class ODataJPAEdmProvider extends EdmProvider {
+
+ private ODataJPAContext oDataJPAContext;
+ private JPAEdmModelView jpaEdmModel;
+
+ private List<Schema> schemas;
+ private HashMap<String, EntityType> entityTypes;
+ private HashMap<String, EntityContainerInfo> entityContainerInfos;
+ private HashMap<String, ComplexType> complexTypes;
+ private HashMap<String, Association> associations;
+ private HashMap<String, FunctionImport> functionImports;
+
+ public ODataJPAEdmProvider() {
+ entityTypes = new HashMap<String, EntityType>();
+ entityContainerInfos = new HashMap<String, EntityContainerInfo>();
+ complexTypes = new HashMap<String, ComplexType>();
+ associations = new HashMap<String, Association>();
+ functionImports = new HashMap<String, FunctionImport>();
+ }
+
+ public ODataJPAEdmProvider(final ODataJPAContext oDataJPAContext) {
+ if (oDataJPAContext == null) {
+ throw new IllegalArgumentException(ODataJPAException.ODATA_JPACTX_NULL);
+ }
+ entityTypes = new HashMap<String, EntityType>();
+ entityContainerInfos = new HashMap<String, EntityContainerInfo>();
+ complexTypes = new HashMap<String, ComplexType>();
+ associations = new HashMap<String, Association>();
+ functionImports = new HashMap<String, FunctionImport>();
+ jpaEdmModel = ODataJPAFactory.createFactory().getJPAAccessFactory().getJPAEdmModelView(oDataJPAContext);
+ }
+
+ public ODataJPAContext getODataJPAContext() {
+ return oDataJPAContext;
+ }
+
+ public void setODataJPAContext(final ODataJPAContext jpaContext) {
+ oDataJPAContext = jpaContext;
+ }
+
+ @Override
+ public EntityContainerInfo getEntityContainerInfo(final String name) throws ODataException {
+
+ if (entityContainerInfos.containsKey(name)) {
+ return entityContainerInfos.get(name);
+ } else {
+
+ if (schemas == null) {
+ getSchemas();
+ }
+ List<EntityContainer> containerList = schemas.get(0).getEntityContainers();
+ if (containerList == null) {
+ return null;
+ }
+ for (EntityContainer container : containerList) {
+ if (name == null && container.isDefaultEntityContainer()) {
+ entityContainerInfos.put(name, container);
+ return container;
+ } else if (name != null && name.equals(container.getName())) {
+ return container;
+ }
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public EntityType getEntityType(final FullQualifiedName edmFQName) throws ODataException {
+
+ String strEdmFQName = edmFQName.toString();
+
+ if (edmFQName != null) {
+ if (entityTypes.containsKey(strEdmFQName)) {
+ return entityTypes.get(strEdmFQName);
+ } else if (schemas == null) {
+ getSchemas();
+ }
+
+ String entityTypeNamespace = edmFQName.getNamespace();
+ String entityTypeName = edmFQName.getName();
+
+ for (Schema schema : schemas) {
+ String schemaNamespace = schema.getNamespace();
+ if (schemaNamespace.equals(entityTypeNamespace)) {
+ if (schema.getEntityTypes() == null) {
+ return null;
+ }
+ for (EntityType et : schema.getEntityTypes()) {
+ if (et.getName().equals(entityTypeName)) {
+ entityTypes.put(strEdmFQName, et);
+ return et;
+ }
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+
+ @Override
+ public ComplexType getComplexType(final FullQualifiedName edmFQName) throws ODataException {
+
+ if (edmFQName != null) {
+ if (complexTypes.containsKey(edmFQName.toString())) {
+ return complexTypes.get(edmFQName.toString());
+ } else if (schemas == null) {
+ getSchemas();
+ }
+
+ for (Schema schema : schemas) {
+ if (schema.getNamespace().equals(edmFQName.getNamespace())) {
+ if (schema.getComplexTypes() == null) {
+ return null;
+ }
+ for (ComplexType ct : schema.getComplexTypes()) {
+ if (ct.getName().equals(edmFQName.getName())) {
+ complexTypes.put(edmFQName.toString(), ct);
+ return ct;
+ }
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+
+ @Override
+ public Association getAssociation(final FullQualifiedName edmFQName) throws ODataException {
+ if (edmFQName != null) {
+ if (associations.containsKey(edmFQName.toString())) {
+ return associations.get(edmFQName.toString());
+ } else if (schemas == null) {
+ getSchemas();
+ }
+
+ for (Schema schema : schemas) {
+ if (schema.getNamespace().equals(edmFQName.getNamespace())) {
+ if (schema.getAssociations() == null) {
+ return null;
+ }
+ for (Association association : schema.getAssociations()) {
+ if (association.getName().equals(edmFQName.getName())) {
+ associations.put(edmFQName.toString(), association);
+ return association;
+ }
+ }
+ }
+ }
+
+ }
+ return null;
+ }
+
+ @Override
+ public EntitySet getEntitySet(final String entityContainer, final String name) throws ODataException {
+
+ EntitySet returnedSet = null;
+ EntityContainer container = null;
+ if (!entityContainerInfos.containsKey(entityContainer)) {
+ container = (EntityContainer) getEntityContainerInfo(entityContainer);
+ } else {
+ container = (EntityContainer) entityContainerInfos.get(entityContainer);
+ }
+
+ if (container != null && name != null) {
+ for (EntitySet es : container.getEntitySets()) {
+ if (name.equals(es.getName())) {
+ returnedSet = es;
+ break;
+ }
+ }
+ }
+
+ return returnedSet;
+ }
+
+ @Override
+ public AssociationSet getAssociationSet(final String entityContainer, final FullQualifiedName association,
+ final String sourceEntitySetName, final String sourceEntitySetRole) throws ODataException {
+
+ EntityContainer container = null;
+ if (!entityContainerInfos.containsKey(entityContainer)) {
+ container = (EntityContainer) getEntityContainerInfo(entityContainer);
+ } else {
+ container = (EntityContainer) entityContainerInfos.get(entityContainer);
+ }
+
+ if (container != null && association != null && container.getAssociationSets() != null) {
+ for (AssociationSet as : container.getAssociationSets()) {
+ if (association.equals(as.getAssociation())) {
+ AssociationSetEnd end = as.getEnd1();
+ if (sourceEntitySetName.equals(end.getEntitySet()) && sourceEntitySetRole.equals(end.getRole())) {
+ return as;
+ } else {
+ end = as.getEnd2();
+ if (sourceEntitySetName.equals(end.getEntitySet()) && sourceEntitySetRole.equals(end.getRole())) {
+ return as;
+ }
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public FunctionImport getFunctionImport(final String entityContainer, final String name) throws ODataException {
+
+ if (functionImports.containsKey(name)) {
+ return functionImports.get(name);
+ }
+
+ EntityContainer container = null;
+ if (!entityContainerInfos.containsKey(entityContainer)) {
+ container = (EntityContainer) getEntityContainerInfo(entityContainer);
+ } else {
+ container = (EntityContainer) entityContainerInfos.get(entityContainer);
+ }
+
+ if (container != null && name != null) {
+ if (container.getFunctionImports() == null) {
+ return null;
+ }
+ for (FunctionImport fi : container.getFunctionImports()) {
+ if (name.equals(fi.getName())) {
+ functionImports.put(name, fi);
+ return fi;
+ }
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public List<Schema> getSchemas() throws ODataException {
+ if (schemas == null && jpaEdmModel != null) {
+ jpaEdmModel.getBuilder().build();
+ schemas = new ArrayList<Schema>();
+ schemas.add(jpaEdmModel.getEdmSchemaView().getEdmSchema());
+ }
+ if (jpaEdmModel == null) {
+
+ throw ODataJPAModelException.throwException(ODataJPAModelException.BUILDER_NULL, null);
+ }
+
+ return schemas;
+
+ }
+
+}