Search in sources :

Example 1 with ExchangeOption

use of com.bluenimble.platform.db.Database.ExchangeOption in project serverless by bluenimble.

the class ExportDatabaseSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    String[] aEntities = Lang.split((String) request.get(Spec.Entities), Lang.COMMA, true);
    Set<String> entities = null;
    if (aEntities != null && aEntities.length > 0) {
        entities = new HashSet<String>();
        for (String entity : aEntities) {
            entities.add(entity.toUpperCase());
        }
    }
    String[] aOptions = Lang.split((String) request.get(Spec.Options), Lang.COMMA, true);
    Map<ExchangeOption, Boolean> options = null;
    if (aOptions != null && aOptions.length > 0) {
        for (String o : aOptions) {
            try {
                options.put(ExchangeOption.valueOf(o.toLowerCase()), true);
            } catch (Exception ex) {
            // ignore malformed options
            }
        }
    }
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    String file = (String) request.get(Spec.File) + DotGz;
    JsonObject result = new JsonObject();
    result.set(Spec.File, file);
    final StringBuilder sb = new StringBuilder();
    try {
        space.feature(Database.class, provider, request).exp(entities, space.feature(Storage.class, provider, request).root().get(file).writer(request), options, new Database.ExchangeListener() {

            @Override
            public void onMessage(String message) {
                sb.append(message).append(Lang.ENDLN);
            }
        });
    } catch (Exception e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    result.set(Output.Feedback, sb.toString());
    sb.setLength(0);
    return new JsonApiOutput(result);
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput) ExchangeOption(com.bluenimble.platform.db.Database.ExchangeOption)

Example 2 with ExchangeOption

use of com.bluenimble.platform.db.Database.ExchangeOption in project serverless by bluenimble.

the class ImportDatabaseSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    String[] aEntities = Lang.split((String) request.get(Spec.Entities), Lang.COMMA, true);
    Set<String> entities = null;
    if (aEntities != null && aEntities.length > 0) {
        entities = new HashSet<String>();
        for (String entity : aEntities) {
            entities.add(entity.toUpperCase());
        }
    }
    String[] aOptions = Lang.split((String) request.get(Spec.Options), Lang.COMMA, true);
    Map<ExchangeOption, Boolean> options = null;
    if (aOptions != null && aOptions.length > 0) {
        for (String o : aOptions) {
            try {
                options.put(ExchangeOption.valueOf(o.toLowerCase()), true);
            } catch (Exception ex) {
            // ignore malformed options
            }
        }
    }
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    String file = (String) request.get(Spec.File);
    JsonObject result = new JsonObject();
    result.set(Spec.File, file);
    final StringBuilder sb = new StringBuilder();
    try {
        space.feature(Database.class, provider, request).imp(entities, space.feature(Storage.class, provider, request).root().get(file).reader(request), options, new Database.ExchangeListener() {

            @Override
            public void onMessage(String message) {
                sb.append(message).append(Lang.ENDLN);
            }
        });
    } catch (Exception e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    result.set(Output.Feedback, sb.toString());
    sb.setLength(0);
    return new JsonApiOutput(result);
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput) ExchangeOption(com.bluenimble.platform.db.Database.ExchangeOption)

Aggregations

ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)2 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)2 ApiSpace (com.bluenimble.platform.api.ApiSpace)2 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)2 Database (com.bluenimble.platform.db.Database)2 ExchangeOption (com.bluenimble.platform.db.Database.ExchangeOption)2 JsonObject (com.bluenimble.platform.json.JsonObject)2