if(token != JsonToken.VALUE_NULL) { String id = parser.getText(); entity.setInternalId(id);
private void runIdGeneration(Entity entity) throws JasDBStorageException { if(entity.getInternalId() == null || entity.getInternalId().isEmpty()) { entity.setInternalId(generator.generateNewId()); } }
private void handleValues(JsonParser parser, String field, boolean isCollection, Entity entity) throws IOException, MetadataParseException { JsonToken token = parser.getCurrentToken(); while(token != JsonToken.END_ARRAY && token != JsonToken.FIELD_NAME && token != JsonToken.END_OBJECT) { if(token.isNumeric()) { long value = parser.getLongValue(); entity.addProperty(field, isCollection, value); } else if(token == JsonToken.VALUE_TRUE || token == JsonToken.VALUE_FALSE) { entity.addProperty(field, parser.getBooleanValue()); } else if(token == JsonToken.START_OBJECT) { EmbeddedEntity embeddedEntity = new EmbeddedEntity(); if(handleProperties(parser, embeddedEntity) != null) { entity.addEntity(field, embeddedEntity); } } else if(token == JsonToken.VALUE_STRING) { String value = parser.getText(); if(SimpleEntity.DOCUMENT_ID.equals(field)) { entity.setInternalId(value); } else { entity.addProperty(field, isCollection, value); } } else if(token != JsonToken.VALUE_NULL) { throw new MetadataParseException("Unexpected token in entity: " + token); } token = parser.nextToken(); } }
@Override public Entity insertEntity(RemotingContext context, String instance, String bag, Entity entity) throws RemoteException { String connectionString = new RestConnectionBuilder().instance(instance).bag(bag).entities().getConnectionString(); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); new JsonRestResponseHandler().serialize(new StreamedEntity(entity), bos); ClientResponse clientResponse = doInternalRequest(context, connectionString, new HashMap<>(), bos.toByteArray(), REQUEST_MODE.POST); try { StreamedEntity returnedEntity = new JsonRestResponseHandler().deserialize(StreamedEntity.class, clientResponse.getEntityInputStream()); entity.setInternalId(returnedEntity.getEntity().getInternalId()); return returnedEntity.getEntity(); } finally { clientResponse.close(); } } catch(RestException e) { throw new RemoteException("Unable to insert entity on remote destination", e); } }