All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xapi.gwt.model.service.ModelServiceGwt Maven / Gradle / Ivy

Go to download

Everything needed to run a comprehensive dev environment. Just type X_ and pick a service from autocomplete; new dev modules will be added as they are built. The only dev service not included in the uber jar is xapi-dev-maven, as it includes all runtime dependencies of maven, adding ~4 seconds to build time, and 6 megabytes to the final output jar size (without xapi-dev-maven, it's ~1MB).

The newest version!
package xapi.gwt.model.service;

import com.google.gwt.core.client.GWT;

import xapi.annotation.inject.SingletonOverride;
import xapi.collect.X_Collect;
import xapi.collect.api.ClassTo;
import xapi.collect.api.StringDictionary;
import xapi.dev.source.CharBuffer;
import xapi.except.NotConfiguredCorrectly;
import xapi.io.X_IO;
import xapi.io.api.DelegatingIOCallback;
import xapi.log.X_Log;
import xapi.model.X_Model;
import xapi.model.api.Model;
import xapi.model.api.ModelKey;
import xapi.model.api.ModelSerializer;
import xapi.model.api.PrimitiveSerializer;
import xapi.model.impl.AbstractModelService;
import xapi.model.impl.ModelSerializerDefault;
import xapi.model.service.ModelService;
import xapi.platform.GwtPlatform;
import xapi.source.impl.StringCharIterator;
import xapi.util.api.ProvidesValue;
import xapi.util.api.SuccessHandler;

@GwtPlatform
@SuppressWarnings({"rawtypes"})
@SingletonOverride(implFor=ModelService.class)
public class ModelServiceGwt extends AbstractModelService
{


  @SuppressWarnings("unchecked")
  private static ClassTo> PROVIDERS = X_Collect.newClassMap(
      Class.class.cast(ProvidesValue.class)
  );

  public static String REGISTER_CREATOR_METHOD = "registerCreator";

  private static Class implClassRef;
  public static  String registerCreator(final Class cls, final String type, final Class implClass, final ProvidesValue provider) {
    PROVIDERS.put(cls, provider);
    implClassRef = implClass;
    final ModelService modelService = X_Model.getService();
    if (modelService instanceof ModelServiceGwt) {
      final ModelServiceGwt service = (ModelServiceGwt) modelService;
      service.classToTypeName.put(cls, type);
      service.classToTypeName.put(implClass, type);
      service.typeNameToClass.put(type, cls);
    } else {
      return modelService.register(cls);
    }
    implClassRef = null;
    return type;
  }

  @Override
  public  T create(final Class key) {
    final ProvidesValue provider = PROVIDERS.get(key);
    if (provider == null) {
      throw new NotConfiguredCorrectly("Could not find provider for "+key+"; did you forget to call X_Model.register()?");
    }
    if (!classToTypeName.containsKey(key)) {
      final T model = (T) provider.get();
      classToTypeName.put(key, model.getType());
      return model;
    }
    return (T) provider.get();
  }

  /**
   * @see xapi.model.impl.AbstractModelService#getDefaultSerializer(java.lang.Class)
   */
  @Override
  protected  ModelSerializer getDefaultSerializer(final String typeName) {

    return new ModelSerializerDefault() {
      @Override
      protected boolean isModelType(final Class propertyType) {
        return PROVIDERS.containsKey(propertyType);
      }
    };
  }

  @Override
  public String register(final Class model) {
    super.register(model);
    final String type = classToTypeName.get(model);
    if (implClassRef != null) {
      if (type != null) {
        classToTypeName.put(implClassRef, type);
      }
    }
    return type;
  }

  @Override
  protected  void doPersist(final String type, final M model, final SuccessHandler callback) {
    final String url = getUrlBase()+"model/persist";
    final StringDictionary headers = X_Collect.newDictionary();
    headers.setValue("X-Model-Type", model.getType());
    final CharBuffer serialized =  serialize(type, model);
    X_IO.getIOService().post(url, serialized.toString(), headers, new DelegatingIOCallback<>((resp) -> {
      final M deserialized = deserialize(type, new StringCharIterator(resp.body()));
      callback.onSuccess(deserialized);
    }));
  }

  protected String getUrlBase() {
    return GWT.getHostPageBaseURL();
  }

  @Override
  public  void load(final Class type, final ModelKey modelKey, final SuccessHandler callback) {
    final String url = getUrlBase()+"model/load";
    final String typeName = getTypeName(type);
    final StringDictionary headers = X_Collect.newDictionary();
    headers.setValue("X-Model-Type", typeName);
    final PrimitiveSerializer primitives = primitiveSerializer();
    String ns = modelKey.getNamespace().length() == 0 ? "" : modelKey.getNamespace();
    // The namespace might be empty, so we use the serialized form that can transmit "" without
    // constructing an invalid uri.
    ns = primitives.serializeString(ns);
    final String kind = modelKey.getKind();
    final String id = primitives.serializeInt(modelKey.getKeyType()) + modelKey.getId();
    final String serialized = "/" + ns + "/"+kind+"/"+id;
    X_IO.getIOService().get(url+""+serialized, headers, new DelegatingIOCallback<>(resp -> {
      X_Log.error("Got response! "+resp.body());
      final M deserialized = deserialize(type, new StringCharIterator(resp.body()));
      callback.onSuccess(deserialized);
    }));

  }

  @Override
  protected boolean isClientToServer() {
    return true;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy