private static String replaceContentId( Edm edm, String entityReference, Map<String, String> contentIdMap) throws EdmException { final int pathSeparator = entityReference.indexOf('/'); final StringBuilder referencedEntity; if (pathSeparator == -1) { referencedEntity = new StringBuilder(contentIdMap.get(entityReference)); } else { referencedEntity = new StringBuilder(contentIdMap.get(entityReference.substring(0, pathSeparator))); } // create a dummy entity location by adding a dummy key predicate // look for a Container name if available String referencedEntityName = referencedEntity.toString(); final int containerSeparator = referencedEntityName.lastIndexOf('.'); final EdmEntityContainer entityContainer; if (containerSeparator != -1) { final String containerName = referencedEntityName.substring(0, containerSeparator); referencedEntityName = referencedEntityName.substring(containerSeparator + 1); entityContainer = edm.getEntityContainer(containerName); if (entityContainer == null) { throw new IllegalArgumentException("EDM does not have entity container " + containerName); } } else { entityContainer = edm.getDefaultEntityContainer(); if (entityContainer == null) { throw new IllegalArgumentException( "EDM does not have a default entity container" + ", use a fully qualified entity set name"); } } final EdmEntitySet entitySet = entityContainer.getEntitySet(referencedEntityName); final List<EdmProperty> keyProperties = entitySet.getEntityType().getKeyProperties(); if (keyProperties.size() == 1) { referencedEntity.append("('dummy')"); } else { referencedEntity.append("("); for (EdmProperty keyProperty : keyProperties) { referencedEntity.append(keyProperty.getName()).append('=').append("'dummy',"); } referencedEntity.deleteCharAt(referencedEntity.length() - 1); referencedEntity.append(')'); } return pathSeparator == -1 ? referencedEntityName : referencedEntity.append(entityReference.substring(pathSeparator)).toString(); }
private static EdmProperty mockEdmPropertyOfSource1() { EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class); EdmType type = EasyMock.createMock(EdmType.class); EasyMock.expect(type.getKind()).andStubReturn(EdmTypeKind.SIMPLE); EasyMock.replay(type); EdmMapping mapping = EasyMock.createMock(EdmMapping.class); EasyMock.expect(mapping.getInternalName()).andStubReturn("id"); EasyMock.replay(mapping); try { EasyMock.expect(edmProperty.getName()).andStubReturn("id"); EasyMock.expect(edmProperty.getType()).andStubReturn(type); EasyMock.expect(edmProperty.getMapping()).andStubReturn(mapping); } catch (EdmException e) { fail( ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); } EasyMock.replay(edmProperty); return edmProperty; }
private int getKeyValue(KeyPredicate key) throws ODataException { EdmProperty property = key.getProperty(); EdmSimpleType type = (EdmSimpleType) property.getType(); return type.valueOfString( key.getLiteral(), EdmLiteralKind.DEFAULT, property.getFacets(), Integer.class); }
private ODataResponse writeContent(Edm edm, UriInfoWithType uriInfo, Object content) throws ODataApplicationException, EdmException, EntityProviderException, URISyntaxException, IOException { String responseContentType = getContentType(); ODataResponse response; switch (uriInfo.getUriType()) { case URI4: case URI5: // simple property final List<EdmProperty> simplePropertyPath = uriInfo.getPropertyPath(); final EdmProperty simpleProperty = simplePropertyPath.get(simplePropertyPath.size() - 1); responseContentType = simpleProperty.getMimeType(); if (uriInfo.isValue()) { response = EntityProvider.writePropertyValue(simpleProperty, content); responseContentType = TEXT_PLAIN_WITH_CS_UTF_8.toString(); } else { response = EntityProvider.writeProperty(getContentType(), simpleProperty, content); } break; case URI3: // complex property final List<EdmProperty> complexPropertyPath = uriInfo.getPropertyPath(); final EdmProperty complexProperty = complexPropertyPath.get(complexPropertyPath.size() - 1); response = EntityProvider.writeProperty(responseContentType, complexProperty, content); break; case URI7A: // $links with 0..1 cardinality property final EdmEntitySet targetLinkEntitySet = uriInfo.getTargetEntitySet(); EntityProviderWriteProperties linkProperties = EntityProviderWriteProperties.serviceRoot(new URI(serviceUri + SEPARATOR)).build(); @SuppressWarnings("unchecked") final Map<String, Object> linkMap = (Map<String, Object>) content; response = EntityProvider.writeLink( responseContentType, targetLinkEntitySet, linkMap, linkProperties); break; case URI7B: // $links with * cardinality property final EdmEntitySet targetLinksEntitySet = uriInfo.getTargetEntitySet(); EntityProviderWriteProperties linksProperties = EntityProviderWriteProperties.serviceRoot(new URI(serviceUri + SEPARATOR)).build(); @SuppressWarnings("unchecked") final List<Map<String, Object>> linksMap = (List<Map<String, Object>>) content; response = EntityProvider.writeLinks( responseContentType, targetLinksEntitySet, linksMap, linksProperties); break; case URI1: case URI2: case URI6A: case URI6B: // Entity final EdmEntitySet targetEntitySet = uriInfo.getTargetEntitySet(); EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(new URI(serviceUri + SEPARATOR)).build(); @SuppressWarnings("unchecked") final Map<String, Object> objectMap = (Map<String, Object>) content; response = EntityProvider.writeEntry(responseContentType, targetEntitySet, objectMap, properties); break; case URI9: // $batch @SuppressWarnings("unchecked") final List<Olingo2BatchRequest> batchParts = (List<Olingo2BatchRequest>) content; response = parseBatchRequest(edm, batchParts); break; default: // notify exception and return!!! throw new ODataApplicationException( "Unsupported resource type " + uriInfo.getTargetType(), Locale.ENGLISH); } return response.getContentHeader() != null ? response : ODataResponse.fromResponse(response).contentHeader(responseContentType).build(); }