line ending issue again
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/b155bda5 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/b155bda5 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/b155bda5 Branch: refs/heads/master Commit: b155bda551bc6f8980684d2b2b44e29accb4c524 Parents: f0dc0f7 Author: Christian Amend <[email protected]> Authored: Thu Oct 9 16:40:52 2014 +0200 Committer: Christian Amend <[email protected]> Committed: Thu Oct 9 16:40:52 2014 +0200 ---------------------------------------------------------------------- odata2-jpa-processor/jpa-core/.gitignore | 14 +- .../processor/core/access/data/JPAEntity.java | 896 +++++++------- .../jpa/processor/core/access/data/JPAPage.java | 422 +++---- .../core/callback/JPATombstoneCallBack.java | 120 +- .../jpa/processor/core/model/JPAEdmFacets.java | 188 +-- .../processor/core/ODataEntityParserTest.java | 488 ++++---- .../JPAEntityParserForStaticMethodTest.java | 660 +++++------ .../core/access/data/JPAEntityTest.java | 492 ++++---- .../processor/core/access/data/JPALinkTest.java | 46 +- .../access/data/JPAPageBuilderDeltaTest.java | 714 +++++------ .../core/access/data/JPAPageBuilderTest.java | 1120 +++++++++--------- .../processor/core/mock/ODataContextMock.java | 124 +- .../core/mock/ODataJPAServiceFactoryMock.java | 94 +- .../processor/core/mock/ODataServiceMock.java | 212 ++-- .../core/mock/OnJPAWriteContentMock.java | 116 +- .../jpa/processor/core/mock/PathInfoMock.java | 102 +- .../processor/core/mock/PathSegmentMock.java | 88 +- .../processor/core/mock/data/EdmMockUtilV2.java | 806 ++++++------- .../processor/core/mock/data/JPATypeMock.java | 536 ++++----- .../core/mock/data/ODataEntryMockUtil.java | 328 ++--- .../core/mock/model/JPAPluralAttributeMock.java | 188 +-- odata2-jpa-processor/jpa-ref/.gitignore | 14 +- .../ref/converter/BlobToByteConverter.java | 140 +-- .../ref/factory/JPAEntityManagerFactory.java | 92 +- .../SalesOrderItemTombstoneListener.java | 186 +-- .../listeners/SalesOrderTombstoneListener.java | 114 +- .../odata2/jpa/processor/ref/model/Address.java | 188 +-- .../jpa/processor/ref/model/Category.java | 160 +-- .../jpa/processor/ref/model/Customer.java | 186 +-- .../jpa/processor/ref/model/Material.java | 272 ++--- .../odata2/jpa/processor/ref/model/Note.java | 242 ++-- .../odata2/jpa/processor/ref/model/NoteKey.java | 166 +-- .../jpa/processor/ref/model/SalesOrderItem.java | 270 ++--- .../processor/ref/model/SalesOrderItemKey.java | 178 +-- .../odata2/jpa/processor/ref/model/Store.java | 182 +-- .../src/main/resources/META-INF/persistence.xml | 88 +- odata2-jpa-processor/jpa-web/.gitignore | 14 +- .../ref/extension/OnDBWriteContent.java | 110 +- .../jpa/processor/ref/extension/OrderValue.java | 84 +- .../jpa/processor/ref/util/DataGenerator.java | 316 ++--- 40 files changed, 5378 insertions(+), 5378 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/b155bda5/odata2-jpa-processor/jpa-core/.gitignore ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/.gitignore b/odata2-jpa-processor/jpa-core/.gitignore index fe5d89b..a7ffe90 100644 --- a/odata2-jpa-processor/jpa-core/.gitignore +++ b/odata2-jpa-processor/jpa-core/.gitignore @@ -1,8 +1,8 @@ -.project -.classpath -.settings -target -bin -*.bak -classes +.project +.classpath +.settings +target +bin +*.bak +classes .DS_Store \ No newline at end of file http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/b155bda5/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntity.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntity.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntity.java index 19ca88b..847e171 100644 --- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntity.java +++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntity.java @@ -1,449 +1,449 @@ -/******************************************************************************* - * 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.access.data; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.sql.Blob; -import java.sql.Clob; -import java.sql.SQLException; -import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -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.EdmNavigationProperty; -import org.apache.olingo.odata2.api.edm.EdmProperty; -import org.apache.olingo.odata2.api.edm.EdmSimpleType; -import org.apache.olingo.odata2.api.edm.EdmStructuralType; -import org.apache.olingo.odata2.api.edm.EdmTypeKind; -import org.apache.olingo.odata2.api.edm.EdmTyped; -import org.apache.olingo.odata2.api.ep.entry.EntryMetadata; -import org.apache.olingo.odata2.api.ep.entry.ODataEntry; -import org.apache.olingo.odata2.api.ep.feed.ODataFeed; -import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext; -import org.apache.olingo.odata2.jpa.processor.api.OnJPAWriteContent; -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.JPAEdmMapping; - -public class JPAEntity { - - private Object jpaEntity = null; - private JPAEntity parentJPAEntity = null; - private EdmEntityType oDataEntityType = null; - private EdmEntitySet oDataEntitySet = null; - private Class<?> jpaType = null; - private HashMap<String, Method> accessModifiersWrite = null; - private JPAEntityParser jpaEntityParser = null; - private ODataJPAContext oDataJPAContext; - private OnJPAWriteContent onJPAWriteContent = null; - public HashMap<String, List<Object>> relatedJPAEntityMap = null; - - public JPAEntity(final EdmEntityType oDataEntityType, final EdmEntitySet oDataEntitySet, - final ODataJPAContext context) { - this.oDataEntityType = oDataEntityType; - this.oDataEntitySet = oDataEntitySet; - oDataJPAContext = context; - try { - JPAEdmMapping mapping = (JPAEdmMapping) oDataEntityType.getMapping(); - jpaType = mapping.getJPAType(); - } catch (EdmException e) { - return; - } - jpaEntityParser = new JPAEntityParser(); - onJPAWriteContent = oDataJPAContext.getODataContext().getServiceFactory().getCallback(OnJPAWriteContent.class); - } - - public void setAccessModifersWrite(final HashMap<String, Method> accessModifiersWrite) { - this.accessModifiersWrite = accessModifiersWrite; - } - - public void setParentJPAEntity(final JPAEntity jpaEntity) { - parentJPAEntity = jpaEntity; - } - - public JPAEntity getParentJPAEntity() { - return parentJPAEntity; - } - - public Object getJPAEntity() { - return jpaEntity; - } - - @SuppressWarnings("unchecked") - private void write(final Map<String, Object> oDataEntryProperties, - final boolean isCreate) - throws ODataJPARuntimeException { - try { - - EdmStructuralType structuralType = null; - final List<String> keyNames = oDataEntityType.getKeyPropertyNames(); - - if (isCreate) { - jpaEntity = instantiateJPAEntity(); - } else if (jpaEntity == null) { - throw ODataJPARuntimeException - .throwException(ODataJPARuntimeException.RESOURCE_NOT_FOUND, null); - } - - if (accessModifiersWrite == null) { - accessModifiersWrite = - jpaEntityParser.getAccessModifiers(jpaEntity, oDataEntityType, JPAEntityParser.ACCESS_MODIFIER_SET); - } - - if (oDataEntityType == null || oDataEntryProperties == null) { - throw ODataJPARuntimeException - .throwException(ODataJPARuntimeException.GENERAL, null); - } - - final HashMap<String, String> embeddableKeys = - jpaEntityParser.getJPAEmbeddableKeyMap(jpaEntity.getClass().getName()); - Set<String> propertyNames = null; - if (embeddableKeys != null) { - setEmbeddableKeyProperty(embeddableKeys, oDataEntityType.getKeyProperties(), oDataEntryProperties, - jpaEntity); - - propertyNames = new HashSet<String>(); - propertyNames.addAll(oDataEntryProperties.keySet()); - for (String key : embeddableKeys.keySet()) { - propertyNames.remove(key); - } - } else { - propertyNames = oDataEntryProperties.keySet(); - } - - for (String propertyName : propertyNames) { - EdmTyped edmTyped = (EdmTyped) oDataEntityType.getProperty(propertyName); - - Method accessModifier = null; - - switch (edmTyped.getType().getKind()) { - case SIMPLE: - if (isCreate == false) { - if (keyNames.contains(edmTyped.getName())) { - continue; - } - } - accessModifier = accessModifiersWrite.get(propertyName); - setProperty(accessModifier, jpaEntity, oDataEntryProperties.get(propertyName), (EdmSimpleType) edmTyped - .getType()); - - break; - case COMPLEX: - structuralType = (EdmStructuralType) edmTyped.getType(); - accessModifier = accessModifiersWrite.get(propertyName); - setComplexProperty(accessModifier, jpaEntity, - structuralType, - (HashMap<String, Object>) oDataEntryProperties.get(propertyName)); - break; - case NAVIGATION: - case ENTITY: - if (isCreate) { - structuralType = (EdmStructuralType) edmTyped.getType(); - EdmNavigationProperty navProperty = (EdmNavigationProperty) edmTyped; - EdmEntitySet edmRelatedEntitySet = oDataEntitySet.getRelatedEntitySet(navProperty); - List<ODataEntry> relatedEntries = (List<ODataEntry>) oDataEntryProperties.get(propertyName); - if (relatedJPAEntityMap == null) { - relatedJPAEntityMap = new HashMap<String, List<Object>>(); - } - List<Object> relatedJPAEntities = new ArrayList<Object>(); - JPAEntity relatedEntity = - new JPAEntity((EdmEntityType) structuralType, edmRelatedEntitySet, oDataJPAContext); - for (ODataEntry oDataEntry : relatedEntries) { - relatedEntity.setParentJPAEntity(this); - relatedEntity.create(oDataEntry); - relatedJPAEntities.add(relatedEntity.getJPAEntity()); - } - relatedJPAEntityMap.put(navProperty.getName(), relatedJPAEntities); - } - default: - continue; - } - } - } catch (Exception e) { - if (e instanceof ODataJPARuntimeException) { - throw (ODataJPARuntimeException) e; - } - throw ODataJPARuntimeException - .throwException(ODataJPARuntimeException.GENERAL - .addContent(e.getMessage()), e); - } - } - - public void create(final ODataEntry oDataEntry) throws ODataJPARuntimeException { - if (oDataEntry == null) { - throw ODataJPARuntimeException - .throwException(ODataJPARuntimeException.GENERAL, null); - } - Map<String, Object> oDataEntryProperties = oDataEntry.getProperties(); - if (oDataEntry.containsInlineEntry()) { - normalizeInlineEntries(oDataEntryProperties); - } - write(oDataEntryProperties, true); - - EntryMetadata entryMetadata = oDataEntry.getMetadata(); - List<String> leftNavPrpNames = new ArrayList<String>(); - try { - for (String navigationPropertyName : oDataEntityType.getNavigationPropertyNames()) { - List<String> links = entryMetadata.getAssociationUris(navigationPropertyName); - if (links.isEmpty()) { - continue; - } else { - EdmNavigationProperty navProperty = - (EdmNavigationProperty) oDataEntityType.getProperty(navigationPropertyName); - if (relatedJPAEntityMap != null && relatedJPAEntityMap.containsKey(navigationPropertyName)) { - JPALink.linkJPAEntities(relatedJPAEntityMap.get(navigationPropertyName), jpaEntity, - navProperty); - } else if (parentJPAEntity != null - && - parentJPAEntity.getEdmEntitySet().getName().equals( - oDataEntitySet.getRelatedEntitySet(navProperty).getName())) { - List<Object> targetJPAEntities = new ArrayList<Object>(); - targetJPAEntities.add(parentJPAEntity.getJPAEntity()); - JPALink.linkJPAEntities(targetJPAEntities, jpaEntity, navProperty); - } else { - leftNavPrpNames.add(navigationPropertyName); - } - } - } - if (!leftNavPrpNames.isEmpty()) { - JPALink link = new JPALink(oDataJPAContext); - link.setSourceJPAEntity(jpaEntity); - link.create(oDataEntitySet, oDataEntry, leftNavPrpNames); - } - } catch (EdmException e) { - throw ODataJPARuntimeException - .throwException(ODataJPARuntimeException.GENERAL - .addContent(e.getMessage()), e); - } catch (ODataJPAModelException e) { - throw ODataJPARuntimeException - .throwException(ODataJPARuntimeException.GENERAL - .addContent(e.getMessage()), e); - } - } - - public EdmEntitySet getEdmEntitySet() { - return oDataEntitySet; - } - - public void create(final Map<String, Object> oDataEntryProperties) throws ODataJPARuntimeException { - normalizeInlineEntries(oDataEntryProperties); - write(oDataEntryProperties, true); - } - - public void update(final ODataEntry oDataEntry) throws ODataJPARuntimeException { - if (oDataEntry == null) { - throw ODataJPARuntimeException - .throwException(ODataJPARuntimeException.GENERAL, null); - } - Map<String, Object> oDataEntryProperties = oDataEntry.getProperties(); - if (oDataEntry.containsInlineEntry()) { - normalizeInlineEntries(oDataEntryProperties); - } - write(oDataEntryProperties, false); - JPALink link = new JPALink(oDataJPAContext); - link.setSourceJPAEntity(jpaEntity); - try { - link.create(oDataEntitySet, oDataEntry, oDataEntityType.getNavigationPropertyNames()); - } catch (EdmException e) { - throw ODataJPARuntimeException - .throwException(ODataJPARuntimeException.GENERAL - .addContent(e.getMessage()), e); - } catch (ODataJPAModelException e) { - throw ODataJPARuntimeException - .throwException(ODataJPARuntimeException.GENERAL - .addContent(e.getMessage()), e); - } - } - - public void update(final Map<String, Object> oDataEntryProperties) throws ODataJPARuntimeException { - normalizeInlineEntries(oDataEntryProperties); - write(oDataEntryProperties, false); - } - - public void setJPAEntity(final Object jpaEntity) { - this.jpaEntity = jpaEntity; - } - - @SuppressWarnings("unchecked") - protected void setComplexProperty(Method accessModifier, final Object jpaEntity, - final EdmStructuralType edmComplexType, final HashMap<String, Object> propertyValue) - throws EdmException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, - InstantiationException, ODataJPARuntimeException, NoSuchMethodException, SecurityException, SQLException { - - JPAEdmMapping mapping = (JPAEdmMapping) edmComplexType.getMapping(); - Object embeddableObject = mapping.getJPAType().newInstance(); - accessModifier.invoke(jpaEntity, embeddableObject); - - HashMap<String, Method> accessModifiers = - jpaEntityParser.getAccessModifiers(embeddableObject, edmComplexType, JPAEntityParser.ACCESS_MODIFIER_SET); - - for (String edmPropertyName : edmComplexType.getPropertyNames()) { - EdmTyped edmTyped = (EdmTyped) edmComplexType.getProperty(edmPropertyName); - accessModifier = accessModifiers.get(edmPropertyName); - if (edmTyped.getType().getKind().toString().equals(EdmTypeKind.COMPLEX.toString())) { - EdmStructuralType structualType = (EdmStructuralType) edmTyped.getType(); - setComplexProperty(accessModifier, embeddableObject, structualType, (HashMap<String, Object>) propertyValue - .get(edmPropertyName)); - } else { - setProperty(accessModifier, embeddableObject, propertyValue.get(edmPropertyName), (EdmSimpleType) edmTyped - .getType()); - } - } - } - - protected void setProperty(final Method method, final Object entity, final Object entityPropertyValue, - final EdmSimpleType type) throws - IllegalAccessException, IllegalArgumentException, InvocationTargetException, ODataJPARuntimeException { - if (entityPropertyValue != null) { - Class<?> parameterType = method.getParameterTypes()[0]; - if (type != null && type.getDefaultType().equals(String.class)) { - if (parameterType.equals(String.class)) { - method.invoke(entity, entityPropertyValue); - } else if (parameterType.equals(char[].class)) { - char[] characters = ((String) entityPropertyValue).toCharArray(); - method.invoke(entity, characters); - } else if (parameterType.equals(char.class)) { - char c = ((String) entityPropertyValue).charAt(0); - method.invoke(entity, c); - } else if (parameterType.equals(Character[].class)) { - Character[] characters = JPAEntityParser.toCharacterArray((String) entityPropertyValue); - method.invoke(entity, (Object) characters); - } else if (parameterType.equals(Character.class)) { - Character c = Character.valueOf(((String) entityPropertyValue).charAt(0)); - method.invoke(entity, c); - } - } else if (parameterType.equals(Blob.class)) { - if (onJPAWriteContent == null) { - throw ODataJPARuntimeException - .throwException(ODataJPARuntimeException.ERROR_JPA_BLOB_NULL, null); - } else { - method.invoke(entity, onJPAWriteContent.getJPABlob((byte[]) entityPropertyValue)); - } - } else if (parameterType.equals(Clob.class)) { - if (onJPAWriteContent == null) { - throw ODataJPARuntimeException - .throwException(ODataJPARuntimeException.ERROR_JPA_CLOB_NULL, null); - } else { - method.invoke(entity, onJPAWriteContent.getJPAClob(((String) entityPropertyValue).toCharArray())); - } - } else if (parameterType.equals(Timestamp.class)) { - Timestamp ts = new Timestamp(((Calendar) entityPropertyValue).getTimeInMillis()); - method.invoke(entity, ts); - } else if (parameterType.equals(java.util.Date.class)) { - method.invoke(entity, ((Calendar) entityPropertyValue).getTime()); - } else if (parameterType.equals(java.sql.Date.class)) { - long timeInMs = ((Calendar) entityPropertyValue).getTimeInMillis(); - method.invoke(entity, new java.sql.Date(timeInMs)); - } else if (parameterType.equals(java.sql.Time.class)) { - long timeInMs = ((Calendar) entityPropertyValue).getTimeInMillis(); - method.invoke(entity, new java.sql.Time(timeInMs)); - } else { - method.invoke(entity, entityPropertyValue); - } - } - } - - protected void setEmbeddableKeyProperty(final HashMap<String, String> embeddableKeys, - final List<EdmProperty> oDataEntryKeyProperties, - final Map<String, Object> oDataEntryProperties, final Object entity) - throws ODataJPARuntimeException, EdmException, IllegalAccessException, IllegalArgumentException, - InvocationTargetException, InstantiationException { - - HashMap<String, Object> embeddableObjMap = new HashMap<String, Object>(); - List<EdmProperty> leftODataEntryKeyProperties = new ArrayList<EdmProperty>(); - HashMap<String, String> leftEmbeddableKeys = new HashMap<String, String>(); - - for (EdmProperty edmProperty : oDataEntryKeyProperties) { - if (oDataEntryProperties.containsKey(edmProperty.getName()) == false) { - continue; - } - - String edmPropertyName = edmProperty.getName(); - String embeddableKeyNameComposite = embeddableKeys.get(edmPropertyName); - if (embeddableKeyNameComposite == null) { - continue; - } - String embeddableKeyNameSplit[] = embeddableKeyNameComposite.split("\\."); - String methodPartName = null; - Method method = null; - Object embeddableObj = null; - - if (embeddableObjMap.containsKey(embeddableKeyNameSplit[0]) == false) { - methodPartName = embeddableKeyNameSplit[0]; - method = jpaEntityParser.getAccessModifierSet(entity, methodPartName); - embeddableObj = method.getParameterTypes()[0].newInstance(); - method.invoke(entity, embeddableObj); - embeddableObjMap.put(embeddableKeyNameSplit[0], embeddableObj); - } else { - embeddableObj = embeddableObjMap.get(embeddableKeyNameSplit[0]); - } - - if (embeddableKeyNameSplit.length == 2) { - methodPartName = embeddableKeyNameSplit[1]; - method = jpaEntityParser.getAccessModifierSet(embeddableObj, methodPartName); - Object simpleObj = oDataEntryProperties.get(edmProperty.getName()); - method.invoke(embeddableObj, simpleObj); - } else if (embeddableKeyNameSplit.length > 2) { // Deeply nested - leftODataEntryKeyProperties.add(edmProperty); - leftEmbeddableKeys - .put(edmPropertyName, embeddableKeyNameComposite.split(embeddableKeyNameSplit[0] + ".", 2)[1]); - } - } - } - - protected Object instantiateJPAEntity() throws InstantiationException, IllegalAccessException { - if (jpaType == null) { - throw new InstantiationException(); - } - - return jpaType.newInstance(); - } - - private void normalizeInlineEntries(final Map<String, Object> oDataEntryProperties) throws ODataJPARuntimeException { - List<ODataEntry> entries = null; - try { - for (String navigationPropertyName : oDataEntityType.getNavigationPropertyNames()) { - Object inline = oDataEntryProperties.get(navigationPropertyName); - if (inline instanceof ODataFeed) { - entries = ((ODataFeed) inline).getEntries(); - } else if (inline instanceof ODataEntry) { - entries = new ArrayList<ODataEntry>(); - entries.add((ODataEntry) inline); - } - if (entries != null) { - oDataEntryProperties.put(navigationPropertyName, entries); - entries = null; - } - } - } catch (EdmException e) { - throw ODataJPARuntimeException - .throwException(ODataJPARuntimeException.GENERAL - .addContent(e.getMessage()), e); - } - } +/******************************************************************************* + * 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.access.data; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +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.EdmNavigationProperty; +import org.apache.olingo.odata2.api.edm.EdmProperty; +import org.apache.olingo.odata2.api.edm.EdmSimpleType; +import org.apache.olingo.odata2.api.edm.EdmStructuralType; +import org.apache.olingo.odata2.api.edm.EdmTypeKind; +import org.apache.olingo.odata2.api.edm.EdmTyped; +import org.apache.olingo.odata2.api.ep.entry.EntryMetadata; +import org.apache.olingo.odata2.api.ep.entry.ODataEntry; +import org.apache.olingo.odata2.api.ep.feed.ODataFeed; +import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext; +import org.apache.olingo.odata2.jpa.processor.api.OnJPAWriteContent; +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.JPAEdmMapping; + +public class JPAEntity { + + private Object jpaEntity = null; + private JPAEntity parentJPAEntity = null; + private EdmEntityType oDataEntityType = null; + private EdmEntitySet oDataEntitySet = null; + private Class<?> jpaType = null; + private HashMap<String, Method> accessModifiersWrite = null; + private JPAEntityParser jpaEntityParser = null; + private ODataJPAContext oDataJPAContext; + private OnJPAWriteContent onJPAWriteContent = null; + public HashMap<String, List<Object>> relatedJPAEntityMap = null; + + public JPAEntity(final EdmEntityType oDataEntityType, final EdmEntitySet oDataEntitySet, + final ODataJPAContext context) { + this.oDataEntityType = oDataEntityType; + this.oDataEntitySet = oDataEntitySet; + oDataJPAContext = context; + try { + JPAEdmMapping mapping = (JPAEdmMapping) oDataEntityType.getMapping(); + jpaType = mapping.getJPAType(); + } catch (EdmException e) { + return; + } + jpaEntityParser = new JPAEntityParser(); + onJPAWriteContent = oDataJPAContext.getODataContext().getServiceFactory().getCallback(OnJPAWriteContent.class); + } + + public void setAccessModifersWrite(final HashMap<String, Method> accessModifiersWrite) { + this.accessModifiersWrite = accessModifiersWrite; + } + + public void setParentJPAEntity(final JPAEntity jpaEntity) { + parentJPAEntity = jpaEntity; + } + + public JPAEntity getParentJPAEntity() { + return parentJPAEntity; + } + + public Object getJPAEntity() { + return jpaEntity; + } + + @SuppressWarnings("unchecked") + private void write(final Map<String, Object> oDataEntryProperties, + final boolean isCreate) + throws ODataJPARuntimeException { + try { + + EdmStructuralType structuralType = null; + final List<String> keyNames = oDataEntityType.getKeyPropertyNames(); + + if (isCreate) { + jpaEntity = instantiateJPAEntity(); + } else if (jpaEntity == null) { + throw ODataJPARuntimeException + .throwException(ODataJPARuntimeException.RESOURCE_NOT_FOUND, null); + } + + if (accessModifiersWrite == null) { + accessModifiersWrite = + jpaEntityParser.getAccessModifiers(jpaEntity, oDataEntityType, JPAEntityParser.ACCESS_MODIFIER_SET); + } + + if (oDataEntityType == null || oDataEntryProperties == null) { + throw ODataJPARuntimeException + .throwException(ODataJPARuntimeException.GENERAL, null); + } + + final HashMap<String, String> embeddableKeys = + jpaEntityParser.getJPAEmbeddableKeyMap(jpaEntity.getClass().getName()); + Set<String> propertyNames = null; + if (embeddableKeys != null) { + setEmbeddableKeyProperty(embeddableKeys, oDataEntityType.getKeyProperties(), oDataEntryProperties, + jpaEntity); + + propertyNames = new HashSet<String>(); + propertyNames.addAll(oDataEntryProperties.keySet()); + for (String key : embeddableKeys.keySet()) { + propertyNames.remove(key); + } + } else { + propertyNames = oDataEntryProperties.keySet(); + } + + for (String propertyName : propertyNames) { + EdmTyped edmTyped = (EdmTyped) oDataEntityType.getProperty(propertyName); + + Method accessModifier = null; + + switch (edmTyped.getType().getKind()) { + case SIMPLE: + if (isCreate == false) { + if (keyNames.contains(edmTyped.getName())) { + continue; + } + } + accessModifier = accessModifiersWrite.get(propertyName); + setProperty(accessModifier, jpaEntity, oDataEntryProperties.get(propertyName), (EdmSimpleType) edmTyped + .getType()); + + break; + case COMPLEX: + structuralType = (EdmStructuralType) edmTyped.getType(); + accessModifier = accessModifiersWrite.get(propertyName); + setComplexProperty(accessModifier, jpaEntity, + structuralType, + (HashMap<String, Object>) oDataEntryProperties.get(propertyName)); + break; + case NAVIGATION: + case ENTITY: + if (isCreate) { + structuralType = (EdmStructuralType) edmTyped.getType(); + EdmNavigationProperty navProperty = (EdmNavigationProperty) edmTyped; + EdmEntitySet edmRelatedEntitySet = oDataEntitySet.getRelatedEntitySet(navProperty); + List<ODataEntry> relatedEntries = (List<ODataEntry>) oDataEntryProperties.get(propertyName); + if (relatedJPAEntityMap == null) { + relatedJPAEntityMap = new HashMap<String, List<Object>>(); + } + List<Object> relatedJPAEntities = new ArrayList<Object>(); + JPAEntity relatedEntity = + new JPAEntity((EdmEntityType) structuralType, edmRelatedEntitySet, oDataJPAContext); + for (ODataEntry oDataEntry : relatedEntries) { + relatedEntity.setParentJPAEntity(this); + relatedEntity.create(oDataEntry); + relatedJPAEntities.add(relatedEntity.getJPAEntity()); + } + relatedJPAEntityMap.put(navProperty.getName(), relatedJPAEntities); + } + default: + continue; + } + } + } catch (Exception e) { + if (e instanceof ODataJPARuntimeException) { + throw (ODataJPARuntimeException) e; + } + throw ODataJPARuntimeException + .throwException(ODataJPARuntimeException.GENERAL + .addContent(e.getMessage()), e); + } + } + + public void create(final ODataEntry oDataEntry) throws ODataJPARuntimeException { + if (oDataEntry == null) { + throw ODataJPARuntimeException + .throwException(ODataJPARuntimeException.GENERAL, null); + } + Map<String, Object> oDataEntryProperties = oDataEntry.getProperties(); + if (oDataEntry.containsInlineEntry()) { + normalizeInlineEntries(oDataEntryProperties); + } + write(oDataEntryProperties, true); + + EntryMetadata entryMetadata = oDataEntry.getMetadata(); + List<String> leftNavPrpNames = new ArrayList<String>(); + try { + for (String navigationPropertyName : oDataEntityType.getNavigationPropertyNames()) { + List<String> links = entryMetadata.getAssociationUris(navigationPropertyName); + if (links.isEmpty()) { + continue; + } else { + EdmNavigationProperty navProperty = + (EdmNavigationProperty) oDataEntityType.getProperty(navigationPropertyName); + if (relatedJPAEntityMap != null && relatedJPAEntityMap.containsKey(navigationPropertyName)) { + JPALink.linkJPAEntities(relatedJPAEntityMap.get(navigationPropertyName), jpaEntity, + navProperty); + } else if (parentJPAEntity != null + && + parentJPAEntity.getEdmEntitySet().getName().equals( + oDataEntitySet.getRelatedEntitySet(navProperty).getName())) { + List<Object> targetJPAEntities = new ArrayList<Object>(); + targetJPAEntities.add(parentJPAEntity.getJPAEntity()); + JPALink.linkJPAEntities(targetJPAEntities, jpaEntity, navProperty); + } else { + leftNavPrpNames.add(navigationPropertyName); + } + } + } + if (!leftNavPrpNames.isEmpty()) { + JPALink link = new JPALink(oDataJPAContext); + link.setSourceJPAEntity(jpaEntity); + link.create(oDataEntitySet, oDataEntry, leftNavPrpNames); + } + } catch (EdmException e) { + throw ODataJPARuntimeException + .throwException(ODataJPARuntimeException.GENERAL + .addContent(e.getMessage()), e); + } catch (ODataJPAModelException e) { + throw ODataJPARuntimeException + .throwException(ODataJPARuntimeException.GENERAL + .addContent(e.getMessage()), e); + } + } + + public EdmEntitySet getEdmEntitySet() { + return oDataEntitySet; + } + + public void create(final Map<String, Object> oDataEntryProperties) throws ODataJPARuntimeException { + normalizeInlineEntries(oDataEntryProperties); + write(oDataEntryProperties, true); + } + + public void update(final ODataEntry oDataEntry) throws ODataJPARuntimeException { + if (oDataEntry == null) { + throw ODataJPARuntimeException + .throwException(ODataJPARuntimeException.GENERAL, null); + } + Map<String, Object> oDataEntryProperties = oDataEntry.getProperties(); + if (oDataEntry.containsInlineEntry()) { + normalizeInlineEntries(oDataEntryProperties); + } + write(oDataEntryProperties, false); + JPALink link = new JPALink(oDataJPAContext); + link.setSourceJPAEntity(jpaEntity); + try { + link.create(oDataEntitySet, oDataEntry, oDataEntityType.getNavigationPropertyNames()); + } catch (EdmException e) { + throw ODataJPARuntimeException + .throwException(ODataJPARuntimeException.GENERAL + .addContent(e.getMessage()), e); + } catch (ODataJPAModelException e) { + throw ODataJPARuntimeException + .throwException(ODataJPARuntimeException.GENERAL + .addContent(e.getMessage()), e); + } + } + + public void update(final Map<String, Object> oDataEntryProperties) throws ODataJPARuntimeException { + normalizeInlineEntries(oDataEntryProperties); + write(oDataEntryProperties, false); + } + + public void setJPAEntity(final Object jpaEntity) { + this.jpaEntity = jpaEntity; + } + + @SuppressWarnings("unchecked") + protected void setComplexProperty(Method accessModifier, final Object jpaEntity, + final EdmStructuralType edmComplexType, final HashMap<String, Object> propertyValue) + throws EdmException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, + InstantiationException, ODataJPARuntimeException, NoSuchMethodException, SecurityException, SQLException { + + JPAEdmMapping mapping = (JPAEdmMapping) edmComplexType.getMapping(); + Object embeddableObject = mapping.getJPAType().newInstance(); + accessModifier.invoke(jpaEntity, embeddableObject); + + HashMap<String, Method> accessModifiers = + jpaEntityParser.getAccessModifiers(embeddableObject, edmComplexType, JPAEntityParser.ACCESS_MODIFIER_SET); + + for (String edmPropertyName : edmComplexType.getPropertyNames()) { + EdmTyped edmTyped = (EdmTyped) edmComplexType.getProperty(edmPropertyName); + accessModifier = accessModifiers.get(edmPropertyName); + if (edmTyped.getType().getKind().toString().equals(EdmTypeKind.COMPLEX.toString())) { + EdmStructuralType structualType = (EdmStructuralType) edmTyped.getType(); + setComplexProperty(accessModifier, embeddableObject, structualType, (HashMap<String, Object>) propertyValue + .get(edmPropertyName)); + } else { + setProperty(accessModifier, embeddableObject, propertyValue.get(edmPropertyName), (EdmSimpleType) edmTyped + .getType()); + } + } + } + + protected void setProperty(final Method method, final Object entity, final Object entityPropertyValue, + final EdmSimpleType type) throws + IllegalAccessException, IllegalArgumentException, InvocationTargetException, ODataJPARuntimeException { + if (entityPropertyValue != null) { + Class<?> parameterType = method.getParameterTypes()[0]; + if (type != null && type.getDefaultType().equals(String.class)) { + if (parameterType.equals(String.class)) { + method.invoke(entity, entityPropertyValue); + } else if (parameterType.equals(char[].class)) { + char[] characters = ((String) entityPropertyValue).toCharArray(); + method.invoke(entity, characters); + } else if (parameterType.equals(char.class)) { + char c = ((String) entityPropertyValue).charAt(0); + method.invoke(entity, c); + } else if (parameterType.equals(Character[].class)) { + Character[] characters = JPAEntityParser.toCharacterArray((String) entityPropertyValue); + method.invoke(entity, (Object) characters); + } else if (parameterType.equals(Character.class)) { + Character c = Character.valueOf(((String) entityPropertyValue).charAt(0)); + method.invoke(entity, c); + } + } else if (parameterType.equals(Blob.class)) { + if (onJPAWriteContent == null) { + throw ODataJPARuntimeException + .throwException(ODataJPARuntimeException.ERROR_JPA_BLOB_NULL, null); + } else { + method.invoke(entity, onJPAWriteContent.getJPABlob((byte[]) entityPropertyValue)); + } + } else if (parameterType.equals(Clob.class)) { + if (onJPAWriteContent == null) { + throw ODataJPARuntimeException + .throwException(ODataJPARuntimeException.ERROR_JPA_CLOB_NULL, null); + } else { + method.invoke(entity, onJPAWriteContent.getJPAClob(((String) entityPropertyValue).toCharArray())); + } + } else if (parameterType.equals(Timestamp.class)) { + Timestamp ts = new Timestamp(((Calendar) entityPropertyValue).getTimeInMillis()); + method.invoke(entity, ts); + } else if (parameterType.equals(java.util.Date.class)) { + method.invoke(entity, ((Calendar) entityPropertyValue).getTime()); + } else if (parameterType.equals(java.sql.Date.class)) { + long timeInMs = ((Calendar) entityPropertyValue).getTimeInMillis(); + method.invoke(entity, new java.sql.Date(timeInMs)); + } else if (parameterType.equals(java.sql.Time.class)) { + long timeInMs = ((Calendar) entityPropertyValue).getTimeInMillis(); + method.invoke(entity, new java.sql.Time(timeInMs)); + } else { + method.invoke(entity, entityPropertyValue); + } + } + } + + protected void setEmbeddableKeyProperty(final HashMap<String, String> embeddableKeys, + final List<EdmProperty> oDataEntryKeyProperties, + final Map<String, Object> oDataEntryProperties, final Object entity) + throws ODataJPARuntimeException, EdmException, IllegalAccessException, IllegalArgumentException, + InvocationTargetException, InstantiationException { + + HashMap<String, Object> embeddableObjMap = new HashMap<String, Object>(); + List<EdmProperty> leftODataEntryKeyProperties = new ArrayList<EdmProperty>(); + HashMap<String, String> leftEmbeddableKeys = new HashMap<String, String>(); + + for (EdmProperty edmProperty : oDataEntryKeyProperties) { + if (oDataEntryProperties.containsKey(edmProperty.getName()) == false) { + continue; + } + + String edmPropertyName = edmProperty.getName(); + String embeddableKeyNameComposite = embeddableKeys.get(edmPropertyName); + if (embeddableKeyNameComposite == null) { + continue; + } + String embeddableKeyNameSplit[] = embeddableKeyNameComposite.split("\\."); + String methodPartName = null; + Method method = null; + Object embeddableObj = null; + + if (embeddableObjMap.containsKey(embeddableKeyNameSplit[0]) == false) { + methodPartName = embeddableKeyNameSplit[0]; + method = jpaEntityParser.getAccessModifierSet(entity, methodPartName); + embeddableObj = method.getParameterTypes()[0].newInstance(); + method.invoke(entity, embeddableObj); + embeddableObjMap.put(embeddableKeyNameSplit[0], embeddableObj); + } else { + embeddableObj = embeddableObjMap.get(embeddableKeyNameSplit[0]); + } + + if (embeddableKeyNameSplit.length == 2) { + methodPartName = embeddableKeyNameSplit[1]; + method = jpaEntityParser.getAccessModifierSet(embeddableObj, methodPartName); + Object simpleObj = oDataEntryProperties.get(edmProperty.getName()); + method.invoke(embeddableObj, simpleObj); + } else if (embeddableKeyNameSplit.length > 2) { // Deeply nested + leftODataEntryKeyProperties.add(edmProperty); + leftEmbeddableKeys + .put(edmPropertyName, embeddableKeyNameComposite.split(embeddableKeyNameSplit[0] + ".", 2)[1]); + } + } + } + + protected Object instantiateJPAEntity() throws InstantiationException, IllegalAccessException { + if (jpaType == null) { + throw new InstantiationException(); + } + + return jpaType.newInstance(); + } + + private void normalizeInlineEntries(final Map<String, Object> oDataEntryProperties) throws ODataJPARuntimeException { + List<ODataEntry> entries = null; + try { + for (String navigationPropertyName : oDataEntityType.getNavigationPropertyNames()) { + Object inline = oDataEntryProperties.get(navigationPropertyName); + if (inline instanceof ODataFeed) { + entries = ((ODataFeed) inline).getEntries(); + } else if (inline instanceof ODataEntry) { + entries = new ArrayList<ODataEntry>(); + entries.add((ODataEntry) inline); + } + if (entries != null) { + oDataEntryProperties.put(navigationPropertyName, entries); + entries = null; + } + } + } catch (EdmException e) { + throw ODataJPARuntimeException + .throwException(ODataJPARuntimeException.GENERAL + .addContent(e.getMessage()), e); + } + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/b155bda5/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPage.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPage.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPage.java index 7aff111..07d7dd9 100644 --- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPage.java +++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPage.java @@ -1,211 +1,211 @@ -/******************************************************************************* - * 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.access.data; - -import java.util.ArrayList; -import java.util.List; - -import javax.persistence.Query; - -import org.apache.olingo.odata2.jpa.processor.api.access.JPAPaging; - -public class JPAPage implements JPAPaging { - - private int pageSize; - private int startPage; - private int nextPage; - private List<Object> pagedEntries; - - protected JPAPage(final int startPage, final int nextPage, final List<Object> pagedEntities, final int pageSize) { - this.pageSize = pageSize; - this.startPage = startPage; - this.nextPage = nextPage; - pagedEntries = pagedEntities; - } - - @Override - public int getPageSize() { - return pageSize; - } - - @Override - public List<Object> getPagedEntities() { - return pagedEntries; - } - - @Override - public int getNextPage() { - return nextPage; - } - - @Override - public int getStartPage() { - return startPage; - } - - public static class JPAPageBuilder { - - private int pageSize; - private int startPage; - private int nextPage; - private int top = -1; - private int skip; - private int skipToken; - private Query query; - private List<Object> entities; - private List<Object> pagedEntities; - - private static class TopSkip { - public int top; - public int skip; - } - - public JPAPageBuilder() {} - - public JPAPageBuilder pageSize(final int pageSize) { - this.pageSize = pageSize; - return this; - } - - public JPAPageBuilder query(final Query query) { - this.query = query; - return this; - } - - public JPAPage build() { - if (entities != null) { - return buildFromEntities(); - } else { - return buildFromQuery(); - } - } - - private JPAPage buildFromEntities() { - TopSkip topSkip = formulateTopSkip(); - pagedEntities = new ArrayList<Object>(); - if (topSkip.skip <= 0) { - topSkip.skip = 1; - } - for (int i = topSkip.skip - 1, j = 0; (j < topSkip.top && i < entities.size()); j++) { - pagedEntities.add(entities.get(i++)); - } - formulateNextPage(); - return new JPAPage(startPage, nextPage, pagedEntities, pageSize); - } - - @SuppressWarnings("unchecked") - private JPAPage buildFromQuery() { - TopSkip topSkip = formulateTopSkip(); - query.setFirstResult(topSkip.skip); - query.setMaxResults(topSkip.top); - pagedEntities = query.getResultList(); - formulateNextPage(); - return new JPAPage(startPage, nextPage, pagedEntities, pageSize); - } - - private TopSkip formulateTopSkip() { - TopSkip topSkip = new TopSkip(); - int size = 0; - if (pageSize <= 0) { - if (skip > 0) { - topSkip.skip = skip; - } - if (top > 0) { - topSkip.top = top; - } - } else { - if (skip >= pageSize) { // No Records to fetch - startPage = skipToken; - nextPage = 0; - } else { - // Max Results - size = top + skip; - if (size > pageSize) { - if (skip == 0) { - topSkip.top = pageSize; - } else { - topSkip.top = pageSize - skip; - } - } else { - if (top > 0) { - topSkip.top = top; - } else { - topSkip.top = pageSize; - } - } - - startPage = skipToken; - if (skip > 0) { - topSkip.skip = startPage + skip; - } else { - topSkip.skip = startPage; - } - } - } - return topSkip; - } - - private void formulateNextPage() { - if (pagedEntities.size() == 0) { - nextPage = 0; - } else if (pagedEntities.size() < pageSize) { - nextPage = 0; - } else { - nextPage = startPage + pageSize; - } - } - - public JPAPageBuilder skip(final int skip) { - this.skip = skip; - if (skip < 0) { - this.skip = 0; - } else { - this.skip = skip; - } - return this; - } - - public JPAPageBuilder skipToken(final String skipToken) throws NumberFormatException { - if (skipToken == null) { - this.skipToken = 0; - } else { - this.skipToken = new Integer(skipToken).intValue(); - if (this.skipToken < 0) { - this.skipToken = 0; - } - } - - return this; - } - - public JPAPageBuilder top(final int top) { - if (top < 0) { - this.top = 0; - } else { - this.top = top; - } - return this; - } - - public JPAPageBuilder entities(final List<Object> result) { - entities = result; - return this; - } - } -} +/******************************************************************************* + * 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.access.data; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Query; + +import org.apache.olingo.odata2.jpa.processor.api.access.JPAPaging; + +public class JPAPage implements JPAPaging { + + private int pageSize; + private int startPage; + private int nextPage; + private List<Object> pagedEntries; + + protected JPAPage(final int startPage, final int nextPage, final List<Object> pagedEntities, final int pageSize) { + this.pageSize = pageSize; + this.startPage = startPage; + this.nextPage = nextPage; + pagedEntries = pagedEntities; + } + + @Override + public int getPageSize() { + return pageSize; + } + + @Override + public List<Object> getPagedEntities() { + return pagedEntries; + } + + @Override + public int getNextPage() { + return nextPage; + } + + @Override + public int getStartPage() { + return startPage; + } + + public static class JPAPageBuilder { + + private int pageSize; + private int startPage; + private int nextPage; + private int top = -1; + private int skip; + private int skipToken; + private Query query; + private List<Object> entities; + private List<Object> pagedEntities; + + private static class TopSkip { + public int top; + public int skip; + } + + public JPAPageBuilder() {} + + public JPAPageBuilder pageSize(final int pageSize) { + this.pageSize = pageSize; + return this; + } + + public JPAPageBuilder query(final Query query) { + this.query = query; + return this; + } + + public JPAPage build() { + if (entities != null) { + return buildFromEntities(); + } else { + return buildFromQuery(); + } + } + + private JPAPage buildFromEntities() { + TopSkip topSkip = formulateTopSkip(); + pagedEntities = new ArrayList<Object>(); + if (topSkip.skip <= 0) { + topSkip.skip = 1; + } + for (int i = topSkip.skip - 1, j = 0; (j < topSkip.top && i < entities.size()); j++) { + pagedEntities.add(entities.get(i++)); + } + formulateNextPage(); + return new JPAPage(startPage, nextPage, pagedEntities, pageSize); + } + + @SuppressWarnings("unchecked") + private JPAPage buildFromQuery() { + TopSkip topSkip = formulateTopSkip(); + query.setFirstResult(topSkip.skip); + query.setMaxResults(topSkip.top); + pagedEntities = query.getResultList(); + formulateNextPage(); + return new JPAPage(startPage, nextPage, pagedEntities, pageSize); + } + + private TopSkip formulateTopSkip() { + TopSkip topSkip = new TopSkip(); + int size = 0; + if (pageSize <= 0) { + if (skip > 0) { + topSkip.skip = skip; + } + if (top > 0) { + topSkip.top = top; + } + } else { + if (skip >= pageSize) { // No Records to fetch + startPage = skipToken; + nextPage = 0; + } else { + // Max Results + size = top + skip; + if (size > pageSize) { + if (skip == 0) { + topSkip.top = pageSize; + } else { + topSkip.top = pageSize - skip; + } + } else { + if (top > 0) { + topSkip.top = top; + } else { + topSkip.top = pageSize; + } + } + + startPage = skipToken; + if (skip > 0) { + topSkip.skip = startPage + skip; + } else { + topSkip.skip = startPage; + } + } + } + return topSkip; + } + + private void formulateNextPage() { + if (pagedEntities.size() == 0) { + nextPage = 0; + } else if (pagedEntities.size() < pageSize) { + nextPage = 0; + } else { + nextPage = startPage + pageSize; + } + } + + public JPAPageBuilder skip(final int skip) { + this.skip = skip; + if (skip < 0) { + this.skip = 0; + } else { + this.skip = skip; + } + return this; + } + + public JPAPageBuilder skipToken(final String skipToken) throws NumberFormatException { + if (skipToken == null) { + this.skipToken = 0; + } else { + this.skipToken = new Integer(skipToken).intValue(); + if (this.skipToken < 0) { + this.skipToken = 0; + } + } + + return this; + } + + public JPAPageBuilder top(final int top) { + if (top < 0) { + this.top = 0; + } else { + this.top = top; + } + return this; + } + + public JPAPageBuilder entities(final List<Object> result) { + entities = result; + return this; + } + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/b155bda5/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/callback/JPATombstoneCallBack.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/callback/JPATombstoneCallBack.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/callback/JPATombstoneCallBack.java index 6ae9b2b..a462696 100644 --- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/callback/JPATombstoneCallBack.java +++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/callback/JPATombstoneCallBack.java @@ -1,60 +1,60 @@ -/******************************************************************************* - * 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.callback; - -import org.apache.olingo.odata2.api.edm.EdmException; -import org.apache.olingo.odata2.api.ep.callback.TombstoneCallback; -import org.apache.olingo.odata2.api.ep.callback.TombstoneCallbackResult; -import org.apache.olingo.odata2.api.uri.info.GetEntitySetUriInfo; - -public class JPATombstoneCallBack implements TombstoneCallback { - - private static final String DELTA_TOKEN_STRING = "?!deltatoken="; - private String baseUri; - private String deltaTokenValue; - private GetEntitySetUriInfo resultsView; - - public JPATombstoneCallBack(final String baseUri, final GetEntitySetUriInfo resultsView, - final String deltaTokenValue) { - this.baseUri = baseUri; - this.deltaTokenValue = deltaTokenValue; - this.resultsView = resultsView; - } - - @Override - public TombstoneCallbackResult getTombstoneCallbackResult() { - TombstoneCallbackResult jpaTombstoneCallBackResult = new TombstoneCallbackResult(); - - jpaTombstoneCallBackResult.setDeltaLink(buildToken()); - return jpaTombstoneCallBackResult; - } - - private String buildToken() { - StringBuilder tokenBuilder = new StringBuilder(); - tokenBuilder.append(baseUri); - try { - tokenBuilder.append(resultsView.getTargetEntitySet().getName()); - } catch (EdmException e) { - // Nothing - } - tokenBuilder.append(DELTA_TOKEN_STRING); - tokenBuilder.append(deltaTokenValue); - return tokenBuilder.toString(); - } -} +/******************************************************************************* + * 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.callback; + +import org.apache.olingo.odata2.api.edm.EdmException; +import org.apache.olingo.odata2.api.ep.callback.TombstoneCallback; +import org.apache.olingo.odata2.api.ep.callback.TombstoneCallbackResult; +import org.apache.olingo.odata2.api.uri.info.GetEntitySetUriInfo; + +public class JPATombstoneCallBack implements TombstoneCallback { + + private static final String DELTA_TOKEN_STRING = "?!deltatoken="; + private String baseUri; + private String deltaTokenValue; + private GetEntitySetUriInfo resultsView; + + public JPATombstoneCallBack(final String baseUri, final GetEntitySetUriInfo resultsView, + final String deltaTokenValue) { + this.baseUri = baseUri; + this.deltaTokenValue = deltaTokenValue; + this.resultsView = resultsView; + } + + @Override + public TombstoneCallbackResult getTombstoneCallbackResult() { + TombstoneCallbackResult jpaTombstoneCallBackResult = new TombstoneCallbackResult(); + + jpaTombstoneCallBackResult.setDeltaLink(buildToken()); + return jpaTombstoneCallBackResult; + } + + private String buildToken() { + StringBuilder tokenBuilder = new StringBuilder(); + tokenBuilder.append(baseUri); + try { + tokenBuilder.append(resultsView.getTargetEntitySet().getName()); + } catch (EdmException e) { + // Nothing + } + tokenBuilder.append(DELTA_TOKEN_STRING); + tokenBuilder.append(deltaTokenValue); + return tokenBuilder.toString(); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/b155bda5/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 index 916b49c..51cd37b 100644 --- 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 @@ -1,94 +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()); - } - } -} +/******************************************************************************* + * 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()); + } + } +}
