olingo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tbo...@apache.org
Subject [32/47] [OLINGO-99] Re-factor Package Names. Following are the changes
Date Thu, 02 Jan 2014 12:47:19 GMT
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLJoinStatementBuilder.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLJoinStatementBuilder.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLJoinStatementBuilder.java
new file mode 100644
index 0000000..40ceeba
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLJoinStatementBuilder.java
@@ -0,0 +1,145 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.jpql;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map.Entry;
+
+import org.apache.olingo.odata2.jpa.processor.api.access.JPAJoinClause;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContextType;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContextView;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLJoinContextView;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLStatement;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLStatement.JPQLStatementBuilder;
+
+public class JPQLJoinStatementBuilder extends JPQLStatementBuilder {
+
+  JPQLStatement jpqlStatement;
+  private JPQLJoinContextView context;
+
+  public JPQLJoinStatementBuilder(final JPQLContextView context) {
+    this.context = (JPQLJoinContextView) context;
+  }
+
+  @Override
+  public JPQLStatement build() throws ODataJPARuntimeException {
+    jpqlStatement = createStatement(createJPQLQuery());
+    return jpqlStatement;
+
+  }
+
+  private String createJPQLQuery() throws ODataJPARuntimeException {
+
+    StringBuilder jpqlQuery = new StringBuilder();
+    StringBuilder joinWhereCondition = null;
+
+    jpqlQuery.append(JPQLStatement.KEYWORD.SELECT).append(JPQLStatement.DELIMITER.SPACE);
+    if (context.getType().equals(JPQLContextType.JOIN_COUNT)) {// $COUNT
+      jpqlQuery.append(JPQLStatement.KEYWORD.COUNT).append(JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(JPQLStatement.DELIMITER.PARENTHESIS_LEFT).append(JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(context.getSelectExpression()).append(JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(JPQLStatement.DELIMITER.PARENTHESIS_RIGHT).append(JPQLStatement.DELIMITER.SPACE);
+    } else { // Normal
+      jpqlQuery.append(context.getSelectExpression()).append(JPQLStatement.DELIMITER.SPACE);
+    }
+
+    jpqlQuery.append(JPQLStatement.KEYWORD.FROM).append(JPQLStatement.DELIMITER.SPACE);
+
+    if (context.getJPAJoinClauses() != null && context.getJPAJoinClauses().size() > 0) {
+      List<JPAJoinClause> joinClauseList = context.getJPAJoinClauses();
+      JPAJoinClause joinClause = joinClauseList.get(0);
+      String joinCondition = joinClause.getJoinCondition();
+      joinWhereCondition = new StringBuilder();
+      if (joinCondition != null) {
+        joinWhereCondition.append(joinCondition);
+      }
+      String relationShipAlias = null;
+      joinClause = joinClauseList.get(1);
+      jpqlQuery.append(joinClause.getEntityName()).append(JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(joinClause.getEntityAlias());
+
+      int i = 1;
+      int limit = joinClauseList.size();
+      relationShipAlias = joinClause.getEntityAlias();
+      while (i < limit) {
+        jpqlQuery.append(JPQLStatement.DELIMITER.SPACE);
+        jpqlQuery.append(JPQLStatement.KEYWORD.JOIN).append(JPQLStatement.DELIMITER.SPACE);
+
+        joinClause = joinClauseList.get(i);
+        jpqlQuery.append(relationShipAlias).append(JPQLStatement.DELIMITER.PERIOD);
+        jpqlQuery.append(joinClause.getEntityRelationShip()).append(JPQLStatement.DELIMITER.SPACE);
+        jpqlQuery.append(joinClause.getEntityRelationShipAlias());
+
+        relationShipAlias = joinClause.getEntityRelationShipAlias();
+        i++;
+
+        joinCondition = joinClause.getJoinCondition();
+        if (joinCondition != null) {
+          joinWhereCondition.append(JPQLStatement.DELIMITER.SPACE + JPQLStatement.Operator.AND
+              + JPQLStatement.DELIMITER.SPACE);
+
+          joinWhereCondition.append(joinCondition);
+        }
+      }
+    } else {
+      throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.JOIN_CLAUSE_EXPECTED, null);
+    }
+    String whereExpression = context.getWhereExpression();
+    if (whereExpression != null || joinWhereCondition.length() > 0) {
+      jpqlQuery.append(JPQLStatement.DELIMITER.SPACE).append(JPQLStatement.KEYWORD.WHERE).append(
+          JPQLStatement.DELIMITER.SPACE);
+      if (whereExpression != null) {
+        jpqlQuery.append(whereExpression);
+        if (joinWhereCondition != null) {
+          jpqlQuery.append(JPQLStatement.DELIMITER.SPACE + JPQLStatement.Operator.AND + JPQLStatement.DELIMITER.SPACE);
+        }
+      }
+      if (joinWhereCondition != null) {
+        jpqlQuery.append(joinWhereCondition.toString());
+      }
+
+    }
+
+    if (context.getOrderByCollection() != null && context.getOrderByCollection().size() > 0) {
+
+      StringBuilder orderByBuilder = new StringBuilder();
+      Iterator<Entry<String, String>> orderItr = context.getOrderByCollection().entrySet().iterator();
+
+      int i = 0;
+
+      while (orderItr.hasNext()) {
+        if (i != 0) {
+          orderByBuilder.append(JPQLStatement.DELIMITER.SPACE).append(JPQLStatement.DELIMITER.COMMA).append(
+              JPQLStatement.DELIMITER.SPACE);
+        }
+        Entry<String, String> entry = orderItr.next();
+        orderByBuilder.append(entry.getKey()).append(JPQLStatement.DELIMITER.SPACE);
+        orderByBuilder.append(entry.getValue());
+        i++;
+      }
+      jpqlQuery.append(JPQLStatement.DELIMITER.SPACE).append(JPQLStatement.KEYWORD.ORDERBY).append(
+          JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(orderByBuilder);
+    }
+
+    return jpqlQuery.toString();
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectContext.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectContext.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectContext.java
new file mode 100644
index 0000000..61d2801
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectContext.java
@@ -0,0 +1,159 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.jpql;
+
+import java.util.HashMap;
+
+import org.apache.olingo.odata2.api.edm.EdmEntityType;
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmMapping;
+import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.api.uri.info.GetEntitySetUriInfo;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContext;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContextType;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLSelectContextView;
+import org.apache.olingo.odata2.jpa.processor.core.ODataExpressionParser;
+
+public class JPQLSelectContext extends JPQLContext implements JPQLSelectContextView {
+
+  protected String selectExpression;
+  protected HashMap<String, String> orderByCollection;
+  protected String whereCondition;
+
+  protected boolean isCountOnly = false;// Support for $count
+
+  public JPQLSelectContext(final boolean isCountOnly) {
+    this.isCountOnly = isCountOnly;
+  }
+
+  protected final void setOrderByCollection(final HashMap<String, String> orderByCollection) {
+    this.orderByCollection = orderByCollection;
+  }
+
+  protected final void setWhereExpression(final String filterExpression) {
+    whereCondition = filterExpression;
+  }
+
+  protected final void setSelectExpression(final String selectExpression) {
+    this.selectExpression = selectExpression;
+  }
+
+  @Override
+  public String getSelectExpression() {
+    return selectExpression;
+  }
+
+  @Override
+  public HashMap<String, String> getOrderByCollection() {
+    return orderByCollection;
+  }
+
+  @Override
+  public String getWhereExpression() {
+    return whereCondition;
+  }
+
+  public class JPQLSelectContextBuilder extends
+      org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContext.JPQLContextBuilder {
+
+    protected GetEntitySetUriInfo entitySetView;
+
+    @Override
+    public JPQLContext build() throws ODataJPAModelException, ODataJPARuntimeException {
+      if (entitySetView != null) {
+
+        try {
+
+          if (isCountOnly) {
+            setType(JPQLContextType.SELECT_COUNT);
+          } else {
+            setType(JPQLContextType.SELECT);
+          }
+          EdmEntityType entityType = entitySetView.getTargetEntitySet().getEntityType();
+          EdmMapping mapping = entityType.getMapping();
+          if (mapping != null) {
+            setJPAEntityName(mapping.getInternalName());
+          } else {
+            setJPAEntityName(entityType.getName());
+          }
+
+          setJPAEntityAlias(generateJPAEntityAlias());
+
+          setOrderByCollection(generateOrderByFileds());
+
+          setSelectExpression(generateSelectExpression());
+
+          setWhereExpression(generateWhereExpression());
+        } catch (ODataException e) {
+          throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
+        }
+      }
+
+      return JPQLSelectContext.this;
+
+    }
+
+    @Override
+    protected void setResultsView(final Object resultsView) {
+      if (resultsView instanceof GetEntitySetUriInfo) {
+        entitySetView = (GetEntitySetUriInfo) resultsView;
+      }
+
+    }
+
+    /*
+     * Generate Select Clause
+     */
+    protected String generateSelectExpression() throws EdmException {
+      return getJPAEntityAlias();
+    }
+
+    /*
+     * Generate Order By Clause Fields
+     */
+    protected HashMap<String, String> generateOrderByFileds() throws ODataJPARuntimeException, EdmException {
+
+      if (entitySetView.getOrderBy() != null) {
+
+        return ODataExpressionParser.parseToJPAOrderByExpression(entitySetView.getOrderBy(), getJPAEntityAlias());
+
+      } else if (entitySetView.getTop() != null || entitySetView.getSkip() != null) {
+
+        return ODataExpressionParser.parseKeyPropertiesToJPAOrderByExpression(entitySetView.getTargetEntitySet()
+            .getEntityType().getKeyProperties(), getJPAEntityAlias());
+      } else {
+        return null;
+      }
+
+    }
+
+    /*
+     * Generate Where Clause Expression
+     */
+    protected String generateWhereExpression() throws ODataException {
+      if (entitySetView.getFilter() != null) {
+        return ODataExpressionParser.parseToJPAWhereExpression(entitySetView.getFilter(), getJPAEntityAlias());
+      }
+      return null;
+    }
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectSingleContext.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectSingleContext.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectSingleContext.java
new file mode 100644
index 0000000..ac54d2c
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectSingleContext.java
@@ -0,0 +1,109 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.jpql;
+
+import java.util.List;
+
+import org.apache.olingo.odata2.api.edm.EdmEntityType;
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmMapping;
+import org.apache.olingo.odata2.api.uri.KeyPredicate;
+import org.apache.olingo.odata2.api.uri.info.GetEntityUriInfo;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContext;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContextType;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLSelectSingleContextView;
+
+public class JPQLSelectSingleContext extends JPQLContext implements JPQLSelectSingleContextView {
+
+  private String selectExpression;
+  private List<KeyPredicate> keyPredicates;
+
+  protected void setKeyPredicates(final List<KeyPredicate> keyPredicates) {
+    this.keyPredicates = keyPredicates;
+  }
+
+  @Override
+  public List<KeyPredicate> getKeyPredicates() {
+    return keyPredicates;
+  }
+
+  protected final void setSelectExpression(final String selectExpression) {
+    this.selectExpression = selectExpression;
+  }
+
+  @Override
+  public String getSelectExpression() {
+    return selectExpression;
+  }
+
+  public class JPQLSelectSingleContextBuilder extends
+      org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContext.JPQLContextBuilder {
+
+    protected GetEntityUriInfo entityView;
+
+    @Override
+    public JPQLContext build() throws ODataJPAModelException, ODataJPARuntimeException {
+      if (entityView != null) {
+
+        try {
+
+          setType(JPQLContextType.SELECT_SINGLE);
+
+          EdmEntityType entityType = entityView.getTargetEntitySet().getEntityType();
+          EdmMapping mapping = entityType.getMapping();
+          if (mapping != null) {
+            setJPAEntityName(mapping.getInternalName());
+          } else {
+            setJPAEntityName(entityType.getName());
+          }
+
+          setJPAEntityAlias(generateJPAEntityAlias());
+
+          setKeyPredicates(entityView.getKeyPredicates());
+
+          setSelectExpression(generateSelectExpression());
+
+        } catch (EdmException e) {
+          throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+        }
+
+      }
+
+      return JPQLSelectSingleContext.this;
+
+    }
+
+    @Override
+    protected void setResultsView(final Object resultsView) {
+      if (resultsView instanceof GetEntityUriInfo) {
+        entityView = (GetEntityUriInfo) resultsView;
+      }
+
+    }
+
+    /*
+     * Generate Select Clause
+     */
+    protected String generateSelectExpression() throws EdmException {
+      return getJPAEntityAlias();
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectSingleStatementBuilder.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectSingleStatementBuilder.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectSingleStatementBuilder.java
new file mode 100644
index 0000000..4e06b9e
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectSingleStatementBuilder.java
@@ -0,0 +1,66 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.jpql;
+
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContextView;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLSelectSingleContextView;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLStatement;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLStatement.JPQLStatementBuilder;
+import org.apache.olingo.odata2.jpa.processor.core.ODataExpressionParser;
+
+public class JPQLSelectSingleStatementBuilder extends JPQLStatementBuilder {
+
+  JPQLStatement jpqlStatement;
+  private JPQLSelectSingleContextView context;
+
+  public JPQLSelectSingleStatementBuilder(final JPQLContextView context) {
+    this.context = (JPQLSelectSingleContextView) context;
+  }
+
+  @Override
+  public JPQLStatement build() throws ODataJPARuntimeException {
+    jpqlStatement = createStatement(createJPQLQuery());
+    return jpqlStatement;
+
+  }
+
+  private String createJPQLQuery() throws ODataJPARuntimeException {
+
+    StringBuilder jpqlQuery = new StringBuilder();
+    String tableAlias = context.getJPAEntityAlias();
+    String fromClause = context.getJPAEntityName() + JPQLStatement.DELIMITER.SPACE + tableAlias;
+
+    jpqlQuery.append(JPQLStatement.KEYWORD.SELECT).append(JPQLStatement.DELIMITER.SPACE);
+    jpqlQuery.append(context.getSelectExpression()).append(JPQLStatement.DELIMITER.SPACE);
+    jpqlQuery.append(JPQLStatement.KEYWORD.FROM).append(JPQLStatement.DELIMITER.SPACE);
+    jpqlQuery.append(fromClause);
+
+    if (context.getKeyPredicates() != null && context.getKeyPredicates().size() > 0) {
+      jpqlQuery.append(JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(JPQLStatement.KEYWORD.WHERE).append(JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(ODataExpressionParser
+          .parseKeyPredicates(context.getKeyPredicates(), context.getJPAEntityAlias()));
+    }
+
+    return jpqlQuery.toString();
+
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectStatementBuilder.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectStatementBuilder.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectStatementBuilder.java
new file mode 100644
index 0000000..22dcc9a
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/jpql/JPQLSelectStatementBuilder.java
@@ -0,0 +1,98 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.jpql;
+
+import java.util.Iterator;
+import java.util.Map.Entry;
+
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContextType;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContextView;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLSelectContextView;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLStatement;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLStatement.JPQLStatementBuilder;
+
+public class JPQLSelectStatementBuilder extends JPQLStatementBuilder {
+
+  JPQLStatement jpqlStatement;
+  private JPQLSelectContextView context;
+
+  public JPQLSelectStatementBuilder(final JPQLContextView context) {
+    this.context = (JPQLSelectContextView) context;
+  }
+
+  @Override
+  public JPQLStatement build() throws ODataJPARuntimeException {
+    jpqlStatement = createStatement(createJPQLQuery());
+    return jpqlStatement;
+
+  }
+
+  private String createJPQLQuery() throws ODataJPARuntimeException {
+
+    StringBuilder jpqlQuery = new StringBuilder();
+    String tableAlias = context.getJPAEntityAlias();
+    String fromClause = context.getJPAEntityName() + JPQLStatement.DELIMITER.SPACE + tableAlias;
+
+    jpqlQuery.append(JPQLStatement.KEYWORD.SELECT).append(JPQLStatement.DELIMITER.SPACE);
+    if (context.getType().equals(JPQLContextType.SELECT_COUNT)) { // $COUNT
+      jpqlQuery.append(JPQLStatement.KEYWORD.COUNT).append(JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(JPQLStatement.DELIMITER.PARENTHESIS_LEFT).append(JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(context.getSelectExpression()).append(JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(JPQLStatement.DELIMITER.PARENTHESIS_RIGHT).append(JPQLStatement.DELIMITER.SPACE);
+    } else {// Normal
+      jpqlQuery.append(context.getSelectExpression()).append(JPQLStatement.DELIMITER.SPACE);
+    }
+
+    jpqlQuery.append(JPQLStatement.KEYWORD.FROM).append(JPQLStatement.DELIMITER.SPACE);
+    jpqlQuery.append(fromClause);
+
+    if (context.getWhereExpression() != null) {
+      jpqlQuery.append(JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(JPQLStatement.KEYWORD.WHERE).append(JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(context.getWhereExpression());
+    }
+
+    if (context.getOrderByCollection() != null && context.getOrderByCollection().size() > 0) {
+
+      StringBuilder orderByBuilder = new StringBuilder();
+      Iterator<Entry<String, String>> orderItr = context.getOrderByCollection().entrySet().iterator();
+
+      int i = 0;
+
+      while (orderItr.hasNext()) {
+        if (i != 0) {
+          orderByBuilder.append(JPQLStatement.DELIMITER.SPACE).append(JPQLStatement.DELIMITER.COMMA).append(
+              JPQLStatement.DELIMITER.SPACE);
+        }
+        Entry<String, String> entry = orderItr.next();
+        orderByBuilder.append(entry.getKey()).append(JPQLStatement.DELIMITER.SPACE);
+        orderByBuilder.append(entry.getValue());
+        i++;
+      }
+      jpqlQuery.append(JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(JPQLStatement.KEYWORD.ORDERBY).append(JPQLStatement.DELIMITER.SPACE);
+      jpqlQuery.append(orderByBuilder);
+    }
+
+    return jpqlQuery.toString();
+
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociation.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociation.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociation.java
new file mode 100644
index 0000000..537a4f8
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociation.java
@@ -0,0 +1,237 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.model;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.olingo.odata2.api.edm.provider.Association;
+import org.apache.olingo.odata2.api.edm.provider.AssociationEnd;
+import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmBuilder;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmAssociationEndView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmAssociationView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmEntityTypeView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmPropertyView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmReferentialConstraintView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmSchemaView;
+import org.apache.olingo.odata2.jpa.processor.core.access.model.JPAEdmNameBuilder;
+
+public class JPAEdmAssociation extends JPAEdmBaseViewImpl implements JPAEdmAssociationView {
+
+  private JPAEdmAssociationEndView associationEndView;
+
+  private Association currentAssociation;
+  private List<Association> consistentAssociatonList;
+  private HashMap<String, Association> associationMap;
+  private HashMap<String, JPAEdmAssociationEndView> associationEndMap;
+  private List<JPAEdmReferentialConstraintView> inconsistentRefConstraintViewList;
+  private int numberOfSimilarEndPoints;
+
+  public JPAEdmAssociation(final JPAEdmAssociationEndView associationEndview,
+      final JPAEdmEntityTypeView entityTypeView, final JPAEdmPropertyView propertyView, final int value) {
+    super(associationEndview);
+    associationEndView = associationEndview;
+    numberOfSimilarEndPoints = value;
+    init();
+  }
+
+  public JPAEdmAssociation(final JPAEdmSchemaView view) {
+    super(view);
+    init();
+  }
+
+  private void init() {
+    isConsistent = false;
+    consistentAssociatonList = new ArrayList<Association>();
+    inconsistentRefConstraintViewList = new LinkedList<JPAEdmReferentialConstraintView>();
+    associationMap = new HashMap<String, Association>();
+    associationEndMap = new HashMap<String, JPAEdmAssociationEndView>();
+  }
+
+  @Override
+  public JPAEdmBuilder getBuilder() {
+    if (builder == null) {
+      builder = new JPAEdmAssociationBuilder();
+    }
+    return builder;
+  }
+
+  @Override
+  public Association getEdmAssociation() {
+    return currentAssociation;
+  }
+
+  @Override
+  public List<Association> getConsistentEdmAssociationList() {
+    return consistentAssociatonList;
+  }
+
+  @Override
+  public Association searchAssociation(final JPAEdmAssociationEndView view) {
+    if (view != null) {
+      for (String key : associationMap.keySet()) {
+        Association association = associationMap.get(key);
+        if (association != null) {
+          if (view.compare(association.getEnd1(), association.getEnd2())) {
+            JPAEdmAssociationEndView associationEnd = associationEndMap.get(association.getName());
+            if (associationEnd.getJoinColumnName() != null && associationEnd.getJoinColumnReferenceColumnName() != null
+                && view.getJoinColumnName() != null && view.getJoinColumnReferenceColumnName() != null) {
+              if (view.getJoinColumnName().equals(associationEnd.getJoinColumnName())
+                  && view.getJoinColumnReferenceColumnName()
+                      .equals(associationEnd.getJoinColumnReferenceColumnName())) {
+                currentAssociation = association;
+                return association;
+              }
+
+            }
+            if (associationEnd.getMappedByName() != null) {
+              if (associationEnd.getMappedByName().equals(view.getOwningPropertyName())) {
+                currentAssociation = association;
+                return association;
+              }
+            }
+            if (associationEnd.getOwningPropertyName() != null) {
+              if (associationEnd.getOwningPropertyName().equals(view.getMappedByName())) {
+                currentAssociation = association;
+                return association;
+              }
+            }
+          }
+        }
+      }
+    }
+    return null;
+  }
+
+  @Override
+  public void addJPAEdmAssociationView(final JPAEdmAssociationView associationView,
+      final JPAEdmAssociationEndView associationEndView) {
+    if (associationView != null) {
+      currentAssociation = associationView.getEdmAssociation();
+      associationMap.put(currentAssociation.getName(), currentAssociation);
+      associationEndMap.put(currentAssociation.getName(), associationEndView);
+      addJPAEdmRefConstraintView(associationView.getJPAEdmReferentialConstraintView());
+    }
+  }
+
+  @Override
+  public void addJPAEdmRefConstraintView(final JPAEdmReferentialConstraintView refView) {
+    if (refView != null && refView.isExists()) {
+      inconsistentRefConstraintViewList.add(refView);
+    }
+  }
+
+  @Override
+  public JPAEdmReferentialConstraintView getJPAEdmReferentialConstraintView() {
+    if (inconsistentRefConstraintViewList.isEmpty()) {
+      return null;
+    }
+    return inconsistentRefConstraintViewList.get(0);
+  }
+
+  private class JPAEdmAssociationBuilder implements JPAEdmBuilder {
+
+    @Override
+    public void build() throws ODataJPAModelException, ODataJPARuntimeException {
+
+      if (associationEndView != null && searchAssociation(associationEndView) == null) {
+        currentAssociation = new Association();
+        currentAssociation.setEnd1(associationEndView.getEdmAssociationEnd1());
+        currentAssociation.setEnd2(associationEndView.getEdmAssociationEnd2());
+
+        JPAEdmNameBuilder.build(JPAEdmAssociation.this, numberOfSimilarEndPoints);
+
+        associationMap.put(currentAssociation.getName(), currentAssociation);
+
+      } else if (!inconsistentRefConstraintViewList.isEmpty()) {
+        int inconsistentRefConstraintViewSize = inconsistentRefConstraintViewList.size();
+        int index = 0;
+        for (int i = 0; i < inconsistentRefConstraintViewSize; i++) {
+          JPAEdmReferentialConstraintView view = inconsistentRefConstraintViewList.get(index);
+
+          if (view.isExists() && !view.isConsistent()) {
+            view.getBuilder().build();
+          }
+          if (view.isConsistent()) {
+            Association newAssociation = new Association();
+            copyAssociation(newAssociation, associationMap.get(view.getEdmRelationShipName()));
+            newAssociation.setReferentialConstraint(view.getEdmReferentialConstraint());
+            consistentAssociatonList.add(newAssociation);
+            associationMap.put(view.getEdmRelationShipName(), newAssociation);
+            inconsistentRefConstraintViewList.remove(index);
+          } else {
+            associationMap.remove(view.getEdmRelationShipName());
+            index++;
+          }
+        }
+      }
+
+      if (associationMap.size() == consistentAssociatonList.size()) {
+        isConsistent = true;
+      } else {
+        for (String key : associationMap.keySet()) {
+          Association association = associationMap.get(key);
+          if (!consistentAssociatonList.contains(association)) {
+            consistentAssociatonList.add(association);
+          }
+        }
+        isConsistent = true;
+      }
+
+    }
+
+    private void copyAssociation(final Association copyToAssociation, final Association copyFromAssociation) {
+      copyToAssociation.setEnd1(copyFromAssociation.getEnd1());
+      copyToAssociation.setEnd2(copyFromAssociation.getEnd2());
+      copyToAssociation.setName(copyFromAssociation.getName());
+      copyToAssociation.setAnnotationAttributes(copyFromAssociation.getAnnotationAttributes());
+      copyToAssociation.setAnnotationElements(copyFromAssociation.getAnnotationElements());
+      copyToAssociation.setDocumentation(copyFromAssociation.getDocumentation());
+
+    }
+  }
+
+  @Override
+  public int getNumberOfAssociationsWithSimilarEndPoints(final JPAEdmAssociationEndView view) {
+    int count = 0;
+    AssociationEnd currentAssociationEnd1 = view.getEdmAssociationEnd1();
+    AssociationEnd currentAssociationEnd2 = view.getEdmAssociationEnd2();
+    AssociationEnd end1 = null;
+    AssociationEnd end2 = null;
+    for (String key : associationMap.keySet()) {
+      Association association = associationMap.get(key);
+      if (association != null) {
+        end1 = association.getEnd1();
+        end2 = association.getEnd2();
+        if ((end1.getType().equals(currentAssociationEnd1.getType()) && end2.getType().equals(
+            currentAssociationEnd2.getType()))
+            || (end1.getType().equals(currentAssociationEnd2.getType()) && end2.getType().equals(
+                currentAssociationEnd1.getType()))) {
+          count++;
+        }
+      }
+    }
+    return count;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java
new file mode 100644
index 0000000..0fdaee0
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java
@@ -0,0 +1,183 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.model;
+
+import java.lang.reflect.AnnotatedElement;
+
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToMany;
+import javax.persistence.OneToMany;
+import javax.persistence.OneToOne;
+import javax.persistence.metamodel.Attribute.PersistentAttributeType;
+
+import org.apache.olingo.odata2.api.edm.EdmMultiplicity;
+import org.apache.olingo.odata2.api.edm.provider.AssociationEnd;
+import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmBuilder;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmAssociationEndView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmEntityTypeView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmPropertyView;
+import org.apache.olingo.odata2.jpa.processor.core.access.model.JPAEdmNameBuilder;
+
+public class JPAEdmAssociationEnd extends JPAEdmBaseViewImpl implements JPAEdmAssociationEndView {
+
+  private JPAEdmEntityTypeView entityTypeView = null;
+  private JPAEdmPropertyView propertyView = null;
+  private AssociationEnd currentAssociationEnd1 = null;
+  private AssociationEnd currentAssociationEnd2 = null;
+  private String columnName;
+  private String referencedColumnName;
+  private String mappedBy;
+  private String ownerPropertyName;
+
+  public JPAEdmAssociationEnd(final JPAEdmEntityTypeView entityTypeView, final JPAEdmPropertyView propertyView) {
+    super(entityTypeView);
+    this.entityTypeView = entityTypeView;
+    this.propertyView = propertyView;
+  }
+
+  @Override
+  public JPAEdmBuilder getBuilder() {
+    if (builder == null) {
+      builder = new JPAEdmAssociationEndBuilder();
+    }
+
+    return builder;
+  }
+
+  @Override
+  public AssociationEnd getEdmAssociationEnd1() {
+    return currentAssociationEnd1;
+  }
+
+  @Override
+  public AssociationEnd getEdmAssociationEnd2() {
+    return currentAssociationEnd2;
+  }
+
+  private class JPAEdmAssociationEndBuilder implements JPAEdmBuilder {
+
+    @Override
+    public void build() throws ODataJPAModelException {
+
+      JoinColumn joinColumn = null;
+
+      currentAssociationEnd1 = new AssociationEnd();
+      currentAssociationEnd2 = new AssociationEnd();
+
+      JPAEdmNameBuilder.build(JPAEdmAssociationEnd.this, entityTypeView, propertyView);
+
+      currentAssociationEnd1.setRole(currentAssociationEnd1.getType().getName());
+      currentAssociationEnd2.setRole(currentAssociationEnd2.getType().getName());
+
+      setEdmMultiplicity(propertyView.getJPAAttribute().getPersistentAttributeType());
+
+      AnnotatedElement annotatedElement = (AnnotatedElement) propertyView.getJPAAttribute().getJavaMember();
+      if (annotatedElement != null) {
+        joinColumn = annotatedElement.getAnnotation(JoinColumn.class);
+        if (joinColumn != null) {
+          columnName = joinColumn.name();
+          referencedColumnName = joinColumn.referencedColumnName();
+        }
+
+      }
+      ownerPropertyName = propertyView.getJPAAttribute().getName();
+
+    }
+
+    private void setEdmMultiplicity(final PersistentAttributeType type) {
+      AnnotatedElement annotatedElement = (AnnotatedElement) propertyView.getJPAAttribute().getJavaMember();
+      switch (type) {
+      case ONE_TO_MANY:
+        currentAssociationEnd1.setMultiplicity(EdmMultiplicity.ONE);
+        currentAssociationEnd2.setMultiplicity(EdmMultiplicity.MANY);
+        if (annotatedElement != null) {
+          OneToMany reln = annotatedElement.getAnnotation(OneToMany.class);
+          if (reln != null) {
+            mappedBy = reln.mappedBy();
+          }
+        }
+        break;
+      case MANY_TO_MANY:
+        currentAssociationEnd1.setMultiplicity(EdmMultiplicity.MANY);
+        currentAssociationEnd2.setMultiplicity(EdmMultiplicity.MANY);
+        if (annotatedElement != null) {
+          ManyToMany reln = annotatedElement.getAnnotation(ManyToMany.class);
+          if (reln != null) {
+            mappedBy = reln.mappedBy();
+          }
+        }
+        break;
+      case MANY_TO_ONE:
+        currentAssociationEnd1.setMultiplicity(EdmMultiplicity.MANY);
+        currentAssociationEnd2.setMultiplicity(EdmMultiplicity.ONE);
+        break;
+      case ONE_TO_ONE:
+        currentAssociationEnd1.setMultiplicity(EdmMultiplicity.ONE);
+        currentAssociationEnd2.setMultiplicity(EdmMultiplicity.ONE);
+        if (annotatedElement != null) {
+          OneToOne reln = annotatedElement.getAnnotation(OneToOne.class);
+          if (reln != null) {
+            mappedBy = reln.mappedBy();
+          }
+        }
+        break;
+      default:
+        break;
+      }
+    }
+  }
+
+  @Override
+  public boolean compare(final AssociationEnd end1, final AssociationEnd end2) {
+    if ((end1.getType().equals(currentAssociationEnd1.getType())
+        && end2.getType().equals(currentAssociationEnd2.getType())
+        && end1.getMultiplicity().equals(currentAssociationEnd1.getMultiplicity()) && end2.getMultiplicity().equals(
+        currentAssociationEnd2.getMultiplicity()))
+        || (end1.getType().equals(currentAssociationEnd2.getType())
+            && end2.getType().equals(currentAssociationEnd1.getType())
+            && end1.getMultiplicity().equals(currentAssociationEnd2.getMultiplicity()) && end2.getMultiplicity()
+            .equals(currentAssociationEnd1.getMultiplicity()))) {
+      return true;
+    }
+
+    return false;
+  }
+
+  @Override
+  public String getJoinColumnName() {
+    return columnName;
+  }
+
+  @Override
+  public String getJoinColumnReferenceColumnName() {
+    return referencedColumnName;
+  }
+
+  @Override
+  public String getMappedByName() {
+    return mappedBy;
+  }
+
+  @Override
+  public String getOwningPropertyName() {
+    return ownerPropertyName;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationSet.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationSet.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationSet.java
new file mode 100644
index 0000000..f5a0ecd
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationSet.java
@@ -0,0 +1,131 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.model;
+
+import java.util.ArrayList;
+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.EntitySet;
+import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmBuilder;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmAssociationSetView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmAssociationView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmEntitySetView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmSchemaView;
+import org.apache.olingo.odata2.jpa.processor.core.access.model.JPAEdmNameBuilder;
+
+public class JPAEdmAssociationSet extends JPAEdmBaseViewImpl implements JPAEdmAssociationSetView {
+
+  private JPAEdmSchemaView schemaView;
+  private AssociationSet currentAssociationSet;
+  private List<AssociationSet> associationSetList;
+  private Association currentAssociation;
+
+  public JPAEdmAssociationSet(final JPAEdmSchemaView view) {
+    super(view);
+    schemaView = view;
+  }
+
+  @Override
+  public JPAEdmBuilder getBuilder() {
+    if (builder == null) {
+      builder = new JPAEdmAssociationSetBuilder();
+    }
+
+    return builder;
+  }
+
+  @Override
+  public List<AssociationSet> getConsistentEdmAssociationSetList() {
+    return associationSetList;
+  }
+
+  @Override
+  public AssociationSet getEdmAssociationSet() {
+    return currentAssociationSet;
+  }
+
+  @Override
+  public Association getEdmAssociation() {
+    return currentAssociation;
+  }
+
+  private class JPAEdmAssociationSetBuilder implements JPAEdmBuilder {
+
+    @Override
+    public void build() throws ODataJPAModelException {
+
+      if (associationSetList == null) {
+        associationSetList = new ArrayList<AssociationSet>();
+      }
+
+      JPAEdmAssociationView associationView = schemaView.getJPAEdmAssociationView();
+      JPAEdmEntitySetView entitySetView = schemaView.getJPAEdmEntityContainerView().getJPAEdmEntitySetView();
+
+      List<EntitySet> entitySetList = entitySetView.getConsistentEdmEntitySetList();
+      if (associationView.isConsistent()) {
+        for (Association association : associationView.getConsistentEdmAssociationList()) {
+
+          currentAssociation = association;
+
+          FullQualifiedName fQname =
+              new FullQualifiedName(schemaView.getEdmSchema().getNamespace(), association.getName());
+          currentAssociationSet = new AssociationSet();
+          currentAssociationSet.setAssociation(fQname);
+
+          int endCount = 0;
+          short endFlag = 0;
+          for (EntitySet entitySet : entitySetList) {
+            fQname = entitySet.getEntityType();
+            endFlag = 0;
+            if (fQname.equals(association.getEnd1().getType()) || ++endFlag > 1
+                || fQname.equals(association.getEnd2().getType())) {
+
+              AssociationSetEnd end = new AssociationSetEnd();
+              end.setEntitySet(entitySet.getName());
+              if (endFlag == 0) {
+                currentAssociationSet.setEnd1(end);
+                end.setRole(association.getEnd1().getRole());
+                endCount++;
+              } else {
+                endCount++;
+                currentAssociationSet.setEnd2(end);
+                end.setRole(association.getEnd2().getRole());
+              }
+
+              if (endCount == 2) {
+                break;
+              }
+            }
+          }
+          if (endCount == 2) {
+            JPAEdmNameBuilder.build(JPAEdmAssociationSet.this);
+            associationSetList.add(currentAssociationSet);
+          }
+
+        }
+
+      }
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmBaseViewImpl.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmBaseViewImpl.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmBaseViewImpl.java
new file mode 100644
index 0000000..ee6f083
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmBaseViewImpl.java
@@ -0,0 +1,101 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.model;
+
+import javax.persistence.metamodel.Metamodel;
+
+import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
+import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmBuilder;
+import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmMappingModelAccess;
+import org.apache.olingo.odata2.jpa.processor.api.factory.ODataJPAFactory;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmBaseView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmExtension;
+
+public abstract class JPAEdmBaseViewImpl implements JPAEdmBaseView {
+
+  protected boolean skipDefaultNaming = false;
+  protected String pUnitName = null;
+  protected Metamodel metaModel = null;
+  protected boolean isConsistent = true;
+  protected JPAEdmBuilder builder = null;
+  protected JPAEdmExtension jpaEdmExtension = null;
+  private JPAEdmMappingModelAccess jpaEdmMappingModelAccess = null;
+
+  public JPAEdmBaseViewImpl(final JPAEdmBaseView view) {
+    pUnitName = view.getpUnitName();
+    metaModel = view.getJPAMetaModel();
+    jpaEdmMappingModelAccess = view.getJPAEdmMappingModelAccess();
+    jpaEdmExtension = view.getJPAEdmExtension();
+    skipDefaultNaming = view.isDefaultNamingSkipped();
+  }
+
+  public JPAEdmBaseViewImpl(final ODataJPAContext context) {
+    pUnitName = context.getPersistenceUnitName();
+    metaModel = context.getEntityManagerFactory().getMetamodel();
+    jpaEdmMappingModelAccess =
+        ODataJPAFactory.createFactory().getJPAAccessFactory().getJPAEdmMappingModelAccess(context);
+    jpaEdmExtension = context.getJPAEdmExtension();
+    jpaEdmMappingModelAccess.loadMappingModel();
+    skipDefaultNaming = !context.getDefaultNaming();
+  }
+
+  public JPAEdmBaseViewImpl(final Metamodel metaModel, final String pUnitName) {
+    this.metaModel = metaModel;
+    this.pUnitName = pUnitName;
+  }
+
+  @Override
+  public String getpUnitName() {
+    return pUnitName;
+  }
+
+  @Override
+  public Metamodel getJPAMetaModel() {
+    return metaModel;
+  }
+
+  @Override
+  public boolean isConsistent() {
+    return isConsistent;
+  }
+
+  @Override
+  public void clean() {
+    pUnitName = null;
+    metaModel = null;
+    isConsistent = false;
+  }
+
+  @Override
+  public JPAEdmMappingModelAccess getJPAEdmMappingModelAccess() {
+    return jpaEdmMappingModelAccess;
+
+  }
+
+  @Override
+  public JPAEdmExtension getJPAEdmExtension() {
+    return jpaEdmExtension;
+  }
+
+  @Override
+  public boolean isDefaultNamingSkipped() {
+    return skipDefaultNaming;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmComplexType.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmComplexType.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmComplexType.java
new file mode 100644
index 0000000..c033494
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmComplexType.java
@@ -0,0 +1,270 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.model;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.persistence.metamodel.Attribute;
+import javax.persistence.metamodel.EmbeddableType;
+
+import org.apache.olingo.odata2.api.edm.FullQualifiedName;
+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.Mapping;
+import org.apache.olingo.odata2.api.edm.provider.Property;
+import org.apache.olingo.odata2.api.edm.provider.SimpleProperty;
+import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmBuilder;
+import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmMappingModelAccess;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmComplexTypeView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmMapping;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmPropertyView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmSchemaView;
+import org.apache.olingo.odata2.jpa.processor.core.access.model.JPAEdmNameBuilder;
+
+public class JPAEdmComplexType extends JPAEdmBaseViewImpl implements JPAEdmComplexTypeView {
+
+  private JPAEdmSchemaView schemaView;
+  private ComplexType currentComplexType = null;
+  private EmbeddableType<?> currentEmbeddableType = null;
+  private HashMap<String, ComplexType> searchMap = null;
+  private List<ComplexType> consistentComplextTypes = null;
+  private boolean directBuild;
+  private EmbeddableType<?> nestedComplexType = null;
+  private List<String> nonKeyComplexList = null;
+
+  public JPAEdmComplexType(final JPAEdmSchemaView view) {
+    super(view);
+    schemaView = view;
+    directBuild = true;
+    if (nonKeyComplexList == null) {
+      nonKeyComplexList = new ArrayList<String>();
+    }
+  }
+
+  public JPAEdmComplexType(final JPAEdmSchemaView view, final Attribute<?, ?> complexAttribute) {
+    super(view);
+    schemaView = view;
+    for (EmbeddableType<?> jpaEmbeddable : schemaView.getJPAMetaModel().getEmbeddables()) {
+      if (jpaEmbeddable.getJavaType().getName().equals(complexAttribute.getJavaType().getName())) {
+        nestedComplexType = jpaEmbeddable;
+        break;
+      }
+    }
+    directBuild = false;
+    if (nonKeyComplexList == null) {
+      nonKeyComplexList = new ArrayList<String>();
+    }
+  }
+
+  @Override
+  public boolean isReferencedInKey(final String complexTypeName) {
+    return nonKeyComplexList.contains(complexTypeName);
+  }
+
+  @Override
+  public void setReferencedInKey(final String complexTypeName) {
+    nonKeyComplexList.add(complexTypeName);
+  }
+
+  @Override
+  public JPAEdmBuilder getBuilder() {
+    if (builder == null) {
+      builder = new JPAEdmComplexTypeBuilder();
+    }
+
+    return builder;
+  }
+
+  @Override
+  public ComplexType getEdmComplexType() {
+    return currentComplexType;
+  }
+
+  @Override
+  public ComplexType searchEdmComplexType(final String embeddableTypeName) {
+    return searchMap.get(embeddableTypeName);
+  }
+
+  @Override
+  public EmbeddableType<?> getJPAEmbeddableType() {
+    return currentEmbeddableType;
+  }
+
+  @Override
+  public List<ComplexType> getConsistentEdmComplexTypes() {
+    return consistentComplextTypes;
+  }
+
+  @Override
+  public ComplexType searchEdmComplexType(final FullQualifiedName type) {
+    String name = type.getName();
+    return searchComplexTypeByName(name);
+
+  }
+
+  private ComplexType searchComplexTypeByName(final String name) {
+    for (ComplexType complexType : consistentComplextTypes) {
+      if (null != complexType && null != complexType.getName() && complexType.getName().equals(name)) {
+        return complexType;
+      }
+    }
+
+    return null;
+  }
+
+  @Override
+  public void addJPAEdmCompleTypeView(final JPAEdmComplexTypeView view) {
+    String searchKey = view.getJPAEmbeddableType().getJavaType().getName();
+
+    if (!searchMap.containsKey(searchKey)) {
+      consistentComplextTypes.add(view.getEdmComplexType());
+      searchMap.put(searchKey, view.getEdmComplexType());
+    }
+  }
+
+  @Override
+  public void expandEdmComplexType(final ComplexType complexType, List<Property> expandedList,
+      final String embeddablePropertyName) {
+
+    if (expandedList == null) {
+      expandedList = new ArrayList<Property>();
+    }
+    for (Property property : complexType.getProperties()) {
+      try {
+        SimpleProperty newSimpleProperty = new SimpleProperty();
+        SimpleProperty oldSimpleProperty = (SimpleProperty) property;
+        newSimpleProperty.setAnnotationAttributes(oldSimpleProperty.getAnnotationAttributes());
+        newSimpleProperty.setAnnotationElements(oldSimpleProperty.getAnnotationElements());
+        newSimpleProperty.setCustomizableFeedMappings(oldSimpleProperty.getCustomizableFeedMappings());
+        newSimpleProperty.setDocumentation(oldSimpleProperty.getDocumentation());
+        newSimpleProperty.setFacets(oldSimpleProperty.getFacets());
+        newSimpleProperty.setMimeType(oldSimpleProperty.getMimeType());
+        newSimpleProperty.setName(oldSimpleProperty.getName());
+        newSimpleProperty.setType(oldSimpleProperty.getType());
+        JPAEdmMappingImpl newMapping = new JPAEdmMappingImpl();
+        Mapping mapping = oldSimpleProperty.getMapping();
+        JPAEdmMapping oldMapping = (JPAEdmMapping) mapping;
+        newMapping.setJPAColumnName(oldMapping.getJPAColumnName());
+        newMapping.setInternalName(embeddablePropertyName + "." + mapping.getInternalName());
+        newMapping.setObject(mapping.getObject());
+        newMapping.setJPAType(oldMapping.getJPAType());
+        newSimpleProperty.setMapping(newMapping);
+        expandedList.add(newSimpleProperty);
+      } catch (ClassCastException e) {
+        ComplexProperty complexProperty = (ComplexProperty) property;
+        String name = embeddablePropertyName + "." + complexProperty.getMapping().getInternalName();
+        expandEdmComplexType(searchComplexTypeByName(complexProperty.getName()), expandedList, name);
+      }
+    }
+
+  }
+
+  private class JPAEdmComplexTypeBuilder implements JPAEdmBuilder {
+    /*
+     * 
+     * Each call to build method creates a new Complex Type.
+     * The Complex Type is created only if it is not created
+     * earlier. A local buffer is maintained to track the list
+     * of complex types created.
+     * 
+     * ************************************************************
+     * Build EDM Complex Type - STEPS
+     * ************************************************************
+     * 1) Fetch list of embeddable types from JPA Model
+     * 2) Search local buffer if there exists already a Complex
+     * type for the embeddable type.
+     * 3) If the complex type was already been built continue with
+     * the next embeddable type, else create new EDM Complex Type.
+     * 4) Create a Property view with Complex Type
+     * 5) Get Property Builder and build the Property with Complex
+     * type.
+     * 6) Set EDM complex type with list of properties built by
+     * the property view
+     * 7) Provide name for EDM complex type.
+     * 
+     * ************************************************************
+     * Build EDM Complex Type - STEPS
+     * ************************************************************
+     */
+    @Override
+    public void build() throws ODataJPAModelException, ODataJPARuntimeException {
+      Set<EmbeddableType<?>> embeddables = new HashSet<EmbeddableType<?>>();
+
+      if (consistentComplextTypes == null) {
+        consistentComplextTypes = new ArrayList<ComplexType>();
+      }
+
+      if (searchMap == null) {
+        searchMap = new HashMap<String, ComplexType>();
+      }
+
+      if (directBuild) {
+        embeddables = schemaView.getJPAMetaModel().getEmbeddables();
+      } else {
+        embeddables.add(nestedComplexType);
+      }
+
+      for (EmbeddableType<?> embeddableType : embeddables) {
+
+        currentEmbeddableType = embeddableType;
+        String searchKey = embeddableType.getJavaType().getName();
+
+        if (searchMap.containsKey(searchKey)) {
+          continue;
+        }
+
+        // Check for need to Exclude
+        if (isExcluded(JPAEdmComplexType.this)) {
+          continue;
+        }
+
+        JPAEdmPropertyView propertyView = new JPAEdmProperty(schemaView, JPAEdmComplexType.this);
+        propertyView.getBuilder().build();
+
+        currentComplexType = new ComplexType();
+        currentComplexType.setProperties(propertyView.getEdmPropertyList());
+        JPAEdmNameBuilder.build(JPAEdmComplexType.this);
+
+        searchMap.put(searchKey, currentComplexType);
+        consistentComplextTypes.add(currentComplexType);
+
+      }
+
+    }
+
+    private boolean isExcluded(final JPAEdmComplexType jpaEdmComplexType) {
+
+      JPAEdmMappingModelAccess mappingModelAccess = jpaEdmComplexType.getJPAEdmMappingModelAccess();
+      if (mappingModelAccess != null
+          && mappingModelAccess.isMappingModelExists()
+          && mappingModelAccess.checkExclusionOfJPAEmbeddableType(jpaEdmComplexType.getJPAEmbeddableType()
+              .getJavaType().getSimpleName())) {
+        return true;
+      }
+      return false;
+    }
+
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntityContainer.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntityContainer.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntityContainer.java
new file mode 100644
index 0000000..81d6c4f
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntityContainer.java
@@ -0,0 +1,145 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.model;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.olingo.odata2.api.edm.provider.EntityContainer;
+import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmBuilder;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmAssociationSetView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmEntityContainerView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmEntitySetView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmSchemaView;
+import org.apache.olingo.odata2.jpa.processor.core.access.model.JPAEdmNameBuilder;
+
+public class JPAEdmEntityContainer extends JPAEdmBaseViewImpl implements JPAEdmEntityContainerView {
+
+  private JPAEdmEntitySetView entitySetView;
+  private JPAEdmSchemaView schemaView;
+  private JPAEdmAssociationSetView associationSetView;
+
+  private EntityContainer currentEntityContainer;
+  private List<EntityContainer> consistentEntityContainerList;
+
+  public JPAEdmEntityContainer(final JPAEdmSchemaView view) {
+    super(view);
+    schemaView = view;
+  }
+
+  @Override
+  public JPAEdmBuilder getBuilder() {
+    if (builder == null) {
+      builder = new JPAEdmEntityContainerBuilder();
+    }
+
+    return builder;
+  }
+
+  @Override
+  public EntityContainer getEdmEntityContainer() {
+    return currentEntityContainer;
+  }
+
+  @Override
+  public List<EntityContainer> getConsistentEdmEntityContainerList() {
+    return consistentEntityContainerList;
+  }
+
+  @Override
+  public JPAEdmEntitySetView getJPAEdmEntitySetView() {
+    return entitySetView;
+  }
+
+  @Override
+  public JPAEdmAssociationSetView getEdmAssociationSetView() {
+    return associationSetView;
+  }
+
+  @Override
+  public void clean() {
+    super.clean();
+    entitySetView = null;
+    associationSetView = null;
+    currentEntityContainer = null;
+    consistentEntityContainerList = null;
+  }
+
+  private class JPAEdmEntityContainerBuilder implements JPAEdmBuilder {
+    /*
+     * 
+     * Each call to build method creates a new Entity Container and builds
+     * the entity container with Association Sets and Entity Sets. The newly
+     * created and built entity container is added to the exiting Entity
+     * Container List.
+     * 
+     * ************************************************************ Build
+     * EDM Entity Container - STEPS
+     * ************************************************************ 1)
+     * Instantiate New EDM Entity Container 2) Build Name for EDM Entity
+     * Container 2) Create Entity Container List (if does not exists) 3)
+     * Build EDM Entity Set 4) Add EDM Entity Set to EDM Entity Container 6)
+     * Build EDM Association Set 7) Add EDM Association Set to EDM Entity
+     * Container 8) Add EDM Entity Container to the Container List
+     * ************************************************************ Build
+     * EDM Entity Container - STEPS
+     * ************************************************************
+     */
+    @Override
+    public void build() throws ODataJPAModelException, ODataJPARuntimeException {
+
+      currentEntityContainer = new EntityContainer();
+
+      if (consistentEntityContainerList == null) {
+        currentEntityContainer.setDefaultEntityContainer(true);
+        consistentEntityContainerList = new ArrayList<EntityContainer>();
+      }
+
+      entitySetView = new JPAEdmEntitySet(schemaView);
+      entitySetView.getBuilder().build();
+      if (entitySetView.isConsistent()) {
+        currentEntityContainer.setEntitySets(entitySetView.getConsistentEdmEntitySetList());
+      } else {
+        isConsistent = false;
+        return;
+      }
+
+      if (!schemaView.getJPAEdmAssociationView().isConsistent()) {
+        schemaView.getJPAEdmAssociationView().getBuilder().build();
+      }
+
+      associationSetView = new JPAEdmAssociationSet(schemaView);
+      associationSetView.getBuilder().build();
+      if (associationSetView.isConsistent()) {
+        currentEntityContainer.setAssociationSets(associationSetView.getConsistentEdmAssociationSetList());
+      } else {
+        isConsistent = false;
+        return;
+      }
+
+      JPAEdmNameBuilder.build(JPAEdmEntityContainer.this);
+      consistentEntityContainerList.add(currentEntityContainer);
+      isConsistent = true;
+
+    }
+
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntitySet.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntitySet.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntitySet.java
new file mode 100644
index 0000000..5558165
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntitySet.java
@@ -0,0 +1,112 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.model;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.olingo.odata2.api.edm.FullQualifiedName;
+import org.apache.olingo.odata2.api.edm.provider.EntitySet;
+import org.apache.olingo.odata2.api.edm.provider.EntityType;
+import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmBuilder;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmEntitySetView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmEntityTypeView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmSchemaView;
+import org.apache.olingo.odata2.jpa.processor.core.access.model.JPAEdmNameBuilder;
+
+public class JPAEdmEntitySet extends JPAEdmBaseViewImpl implements JPAEdmEntitySetView {
+
+  private EntitySet currentEntitySet = null;
+  private List<EntitySet> consistentEntitySetList = null;
+  private JPAEdmEntityTypeView entityTypeView = null;
+  private JPAEdmSchemaView schemaView;
+
+  public JPAEdmEntitySet(final JPAEdmSchemaView view) {
+    super(view);
+    schemaView = view;
+  }
+
+  @Override
+  public JPAEdmBuilder getBuilder() {
+    if (builder == null) {
+      builder = new JPAEdmEntitySetBuilder();
+    }
+
+    return builder;
+  }
+
+  @Override
+  public EntitySet getEdmEntitySet() {
+    return currentEntitySet;
+  }
+
+  @Override
+  public List<EntitySet> getConsistentEdmEntitySetList() {
+    return consistentEntitySetList;
+  }
+
+  @Override
+  public JPAEdmEntityTypeView getJPAEdmEntityTypeView() {
+    return entityTypeView;
+  }
+
+  @Override
+  public void clean() {
+    currentEntitySet = null;
+    consistentEntitySetList = null;
+    entityTypeView = null;
+    isConsistent = false;
+  }
+
+  private class JPAEdmEntitySetBuilder implements JPAEdmBuilder {
+
+    @Override
+    public void build() throws ODataJPAModelException, ODataJPARuntimeException {
+
+      if (consistentEntitySetList == null) {
+        consistentEntitySetList = new ArrayList<EntitySet>();
+      }
+
+      entityTypeView = new JPAEdmEntityType(schemaView);
+      entityTypeView.getBuilder().build();
+
+      if (entityTypeView.isConsistent() && entityTypeView.getConsistentEdmEntityTypes() != null) {
+
+        String nameSpace = schemaView.getEdmSchema().getNamespace();
+        for (EntityType entityType : entityTypeView.getConsistentEdmEntityTypes()) {
+
+          currentEntitySet = new EntitySet();
+          currentEntitySet.setEntityType(new FullQualifiedName(nameSpace, entityType.getName()));
+          JPAEdmNameBuilder.build(JPAEdmEntitySet.this, entityTypeView);
+          consistentEntitySetList.add(currentEntitySet);
+
+        }
+        isConsistent = true;
+      } else {
+        isConsistent = false;
+        return;
+      }
+
+    }
+
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntityType.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntityType.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntityType.java
new file mode 100644
index 0000000..a127c1e
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntityType.java
@@ -0,0 +1,230 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.model;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Set;
+
+import org.apache.olingo.odata2.api.edm.provider.EntityType;
+import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmBuilder;
+import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmMappingModelAccess;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmEntityTypeView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmKeyView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmNavigationPropertyView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmPropertyView;
+import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmSchemaView;
+import org.apache.olingo.odata2.jpa.processor.core.access.model.JPAEdmNameBuilder;
+
+public class JPAEdmEntityType extends JPAEdmBaseViewImpl implements JPAEdmEntityTypeView {
+
+  private JPAEdmSchemaView schemaView = null;
+  private EntityType currentEdmEntityType = null;
+  private javax.persistence.metamodel.EntityType<?> currentJPAEntityType = null;
+  private EntityTypeList<EntityType> consistentEntityTypes = null;
+
+  private HashMap<String, EntityType> consistentEntityTypeMap;
+
+  public JPAEdmEntityType(final JPAEdmSchemaView view) {
+    super(view);
+    schemaView = view;
+    consistentEntityTypeMap = new HashMap<String, EntityType>();
+  }
+
+  @Override
+  public JPAEdmBuilder getBuilder() {
+    if (builder == null) {
+      builder = new JPAEdmEntityTypeBuilder();
+    }
+
+    return builder;
+  }
+
+  @Override
+  public EntityType getEdmEntityType() {
+    return currentEdmEntityType;
+  }
+
+  @Override
+  public javax.persistence.metamodel.EntityType<?> getJPAEntityType() {
+    return currentJPAEntityType;
+  }
+
+  @Override
+  public List<EntityType> getConsistentEdmEntityTypes() {
+    return consistentEntityTypes;
+  }
+
+  @Override
+  public EntityType searchEdmEntityType(final String jpaEntityTypeName) {
+    return consistentEntityTypeMap.get(jpaEntityTypeName);
+  }
+
+  private class JPAEdmEntityTypeBuilder implements JPAEdmBuilder {
+
+    @Override
+    public void build() throws ODataJPAModelException, ODataJPARuntimeException {
+
+      Set<javax.persistence.metamodel.EntityType<?>> jpaEntityTypes = metaModel.getEntities();
+
+      if (jpaEntityTypes == null || jpaEntityTypes.isEmpty() == true) {
+        return;
+      } else if (consistentEntityTypes == null) {
+        consistentEntityTypes = new EntityTypeList<EntityType>();
+
+      }
+
+      for (javax.persistence.metamodel.EntityType<?> jpaEntityType : jpaEntityTypes) {
+        currentEdmEntityType = new EntityType();
+        currentJPAEntityType = jpaEntityType;
+
+        // Check for need to Exclude
+        if (isExcluded(JPAEdmEntityType.this)) {
+          continue;
+        }
+
+        JPAEdmNameBuilder.build(JPAEdmEntityType.this);
+
+        JPAEdmPropertyView propertyView = new JPAEdmProperty(schemaView);
+        propertyView.getBuilder().build();
+
+        currentEdmEntityType.setProperties(propertyView.getEdmPropertyList());
+        if (propertyView.getJPAEdmNavigationPropertyView() != null) {
+          JPAEdmNavigationPropertyView navPropView = propertyView.getJPAEdmNavigationPropertyView();
+          if (navPropView.getConsistentEdmNavigationProperties() != null
+              && !navPropView.getConsistentEdmNavigationProperties().isEmpty()) {
+            currentEdmEntityType.setNavigationProperties(navPropView.getConsistentEdmNavigationProperties());
+          }
+        }
+        JPAEdmKeyView keyView = propertyView.getJPAEdmKeyView();
+        currentEdmEntityType.setKey(keyView.getEdmKey());
+
+        consistentEntityTypes.add(currentEdmEntityType);
+        consistentEntityTypeMap.put(currentJPAEntityType.getName(), currentEdmEntityType);
+      }
+
+    }
+
+    private boolean isExcluded(final JPAEdmEntityType jpaEdmEntityType) {
+      JPAEdmMappingModelAccess mappingModelAccess = jpaEdmEntityType.getJPAEdmMappingModelAccess();
+      if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()
+          && mappingModelAccess.checkExclusionOfJPAEntityType(jpaEdmEntityType.getJPAEntityType().getName())) {
+        return true;
+      }
+      return false;
+    }
+
+  }
+
+  private class EntityTypeList<X> extends ArrayList<EntityType> {
+
+    /**
+     * 
+     */
+    private static final long serialVersionUID = 719079109608251592L;
+
+    @Override
+    public Iterator<EntityType> iterator() {
+      return new EntityTypeListIterator<X>(size());
+    }
+
+  }
+
+  private class EntityTypeListIterator<E> implements ListIterator<EntityType> {
+
+    private int size = 0;
+    private int pos = 0;
+
+    public EntityTypeListIterator(final int listSize) {
+      this.size = listSize;
+    }
+
+    @Override
+    public void add(final EntityType e) {
+      consistentEntityTypes.add(e);
+      size++;
+    }
+
+    @Override
+    public boolean hasNext() {
+      if (pos < size) {
+        return true;
+      }
+
+      return false;
+    }
+
+    @Override
+    public boolean hasPrevious() {
+      if (pos > 0) {
+        return true;
+      }
+      return false;
+    }
+
+    @Override
+    public EntityType next() {
+      if (pos < size) {
+        currentEdmEntityType = consistentEntityTypes.get(pos++);
+        return currentEdmEntityType;
+      }
+
+      return null;
+    }
+
+    @Override
+    public int nextIndex() {
+      return pos;
+    }
+
+    @Override
+    public EntityType previous() {
+      if (pos > 0 && pos < size) {
+        currentEdmEntityType = consistentEntityTypes.get(--pos);
+        return currentEdmEntityType;
+      }
+      return null;
+    }
+
+    @Override
+    public int previousIndex() {
+      if (pos > 0) {
+        return pos - 1;
+      }
+
+      return 0;
+    }
+
+    @Override
+    public void remove() {
+      consistentEntityTypes.remove(pos);
+    }
+
+    @Override
+    public void set(final EntityType e) {
+      consistentEntityTypes.set(pos, e);
+    }
+
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/63b621a8/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmFacets.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmFacets.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmFacets.java
new file mode 100644
index 0000000..916b49c
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmFacets.java
@@ -0,0 +1,94 @@
+/*******************************************************************************
+ * 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.jpa.processor.core.model;
+
+import java.lang.reflect.AnnotatedElement;
+
+import javax.persistence.Column;
+import javax.persistence.metamodel.Attribute;
+
+import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind;
+import org.apache.olingo.odata2.api.edm.provider.Facets;
+import org.apache.olingo.odata2.api.edm.provider.SimpleProperty;
+
+public class JPAEdmFacets {
+  public static void setFacets(final Attribute<?, ?> jpaAttribute, final SimpleProperty edmProperty) {
+    EdmSimpleTypeKind edmTypeKind = edmProperty.getType();
+    Facets facets = new Facets();
+    edmProperty.setFacets(facets);
+
+    Column column = null;
+    if (jpaAttribute.getJavaMember() instanceof AnnotatedElement) {
+      column = ((AnnotatedElement) jpaAttribute
+          .getJavaMember()).getAnnotation(Column.class);
+    }
+
+    if (column == null) {
+      return;
+    }
+
+    setNullable(column, edmProperty);
+
+    switch (edmTypeKind) {
+    case Binary:
+      setMaxLength(column, edmProperty);
+      break;
+    case DateTime:
+      setPrecision(column, edmProperty);
+      break;
+    case DateTimeOffset:
+      setPrecision(column, edmProperty);
+      break;
+    case Time:
+      setPrecision(column, edmProperty);
+      break;
+    case Decimal:
+      setPrecision(column, edmProperty);
+      setScale(column, edmProperty);
+      break;
+    case String:
+      setMaxLength(column, edmProperty);
+      break;
+    default:
+      break;
+    }
+  }
+
+  private static void setNullable(final Column column, final SimpleProperty edmProperty) {
+    ((Facets) edmProperty.getFacets()).setNullable(column.nullable());
+  }
+
+  private static void setMaxLength(final Column column, final SimpleProperty edmProperty) {
+    if (column.length() > 0) {
+      ((Facets) edmProperty.getFacets()).setMaxLength(column.length());
+    }
+  }
+
+  private static void setPrecision(final Column column, final SimpleProperty edmProperty) {
+    if (column.precision() > 0) {
+      ((Facets) edmProperty.getFacets()).setPrecision(column.precision());
+    }
+  }
+
+  private static void setScale(final Column column, final SimpleProperty edmProperty) {
+    if (column.scale() > 0) {
+      ((Facets) edmProperty.getFacets()).setScale(column.scale());
+    }
+  }
+}


Mime
View raw message