Search in sources :

Example 1 with IObjectSubstitutor

use of weborb.writer.IObjectSubstitutor in project Android-SDK by Backendless.

the class Persistence method save.

public <E> E save(final E entity) throws BackendlessException {
    if (entity == null)
        throw new IllegalArgumentException(ExceptionMessage.NULL_ENTITY);
    checkDeclaredType(entity.getClass());
    final Map<String, Object> serializedEntity = BackendlessSerializer.serializeToMap(entity);
    MapEntityUtil.removeNullsAndRelations(serializedEntity);
    MessageWriter.setObjectSubstitutor(new IObjectSubstitutor() {

        @Override
        public Object substitute(Object o) {
            if (o == entity)
                return serializedEntity;
            else
                return o;
        }
    });
    try {
        String method = "create";
        if (serializedEntity.containsKey(Persistence.DEFAULT_OBJECT_ID_FIELD) && serializedEntity.get(Persistence.DEFAULT_OBJECT_ID_FIELD) != null)
            method = "update";
        E newEntity = Invoker.invokeSync(PERSISTENCE_MANAGER_SERVER_ALIAS, method, new Object[] { BackendlessSerializer.getSimpleName(entity.getClass()), serializedEntity }, ResponderHelper.getPOJOAdaptingResponder(entity.getClass()));
        if (serializedEntity.get(Persistence.DEFAULT_OBJECT_ID_FIELD) == null)
            FootprintsManager.getInstance().Inner.duplicateFootprintForObject(serializedEntity, newEntity, entity);
        else
            FootprintsManager.getInstance().Inner.updateFootprintForObject(serializedEntity, newEntity, entity);
        // put or update footprint's properties to user's properties, if exists
        Footprint footprint = FootprintsManager.getInstance().getEntityFootprint(newEntity);
        if (footprint != null)
            footprint.initObjectId(entity);
        return newEntity;
    } finally {
        MessageWriter.setObjectSubstitutor(null);
    }
}
Also used : IObjectSubstitutor(weborb.writer.IObjectSubstitutor)

Example 2 with IObjectSubstitutor

use of weborb.writer.IObjectSubstitutor in project Android-SDK by Backendless.

the class Persistence method save.

public <E> void save(final E entity, final AsyncCallback<E> responder) {
    try {
        if (entity == null)
            throw new IllegalArgumentException(ExceptionMessage.NULL_ENTITY);
        checkDeclaredType(entity.getClass());
        final Map<String, Object> serializedEntity = BackendlessSerializer.serializeToMap(entity);
        MapEntityUtil.removeNullsAndRelations(serializedEntity);
        MessageWriter.setObjectSubstitutor(new IObjectSubstitutor() {

            @Override
            public Object substitute(Object o) {
                if (o == entity)
                    return serializedEntity;
                else
                    return o;
            }
        });
        AsyncCallback<E> callbackOverrider;
        if (serializedEntity.get(Persistence.DEFAULT_OBJECT_ID_FIELD) == null) {
            callbackOverrider = new AsyncCallback<E>() {

                @Override
                public void handleResponse(E newEntity) {
                    MessageWriter.setObjectSubstitutor(null);
                    FootprintsManager.getInstance().Inner.duplicateFootprintForObject(serializedEntity, newEntity, entity);
                    Footprint footprint = FootprintsManager.getInstance().getEntityFootprint(newEntity);
                    if (footprint != null)
                        footprint.initObjectId(entity);
                    if (responder != null)
                        responder.handleResponse(newEntity);
                }

                @Override
                public void handleFault(BackendlessFault fault) {
                    MessageWriter.setObjectSubstitutor(null);
                    if (responder != null)
                        responder.handleFault(fault);
                }
            };
        } else {
            callbackOverrider = new AsyncCallback<E>() {

                @Override
                public void handleResponse(E newEntity) {
                    FootprintsManager.getInstance().Inner.updateFootprintForObject(serializedEntity, newEntity, entity);
                    Footprint footprint = FootprintsManager.getInstance().getEntityFootprint(newEntity);
                    if (footprint != null)
                        footprint.initObjectId(entity);
                    if (responder != null)
                        responder.handleResponse(newEntity);
                }

                @Override
                public void handleFault(BackendlessFault fault) {
                    if (responder != null)
                        responder.handleFault(fault);
                }
            };
        }
        String method = "create";
        if (serializedEntity.containsKey(Persistence.DEFAULT_OBJECT_ID_FIELD) && serializedEntity.get(Persistence.DEFAULT_OBJECT_ID_FIELD) != null)
            method = "save";
        Invoker.invokeAsync(PERSISTENCE_MANAGER_SERVER_ALIAS, method, new Object[] { BackendlessSerializer.getSimpleName(entity.getClass()), entity }, callbackOverrider, ResponderHelper.getPOJOAdaptingResponder(entity.getClass()));
    } catch (Throwable e) {
        if (responder != null)
            responder.handleFault(new BackendlessFault(e));
    }
}
Also used : IObjectSubstitutor(weborb.writer.IObjectSubstitutor) BackendlessFault(com.backendless.exceptions.BackendlessFault)

Aggregations

IObjectSubstitutor (weborb.writer.IObjectSubstitutor)2 BackendlessFault (com.backendless.exceptions.BackendlessFault)1