Package org.persvr.datasource

Examples of org.persvr.datasource.DataSource


   * persisted in the class's table (when the current transaction is committed).
   * @param className  The name of the class from which to instantiate.
   * @return the newly created instance object
   */
  public static Persistable newObject(String className) {
    DataSource source = DataSourceManager.getSource(className);
    if(source == null)
      throw new RuntimeException("The table " + className + " was not found");
    Persistable object = newObject(source);
    ((PersistableClass)object.getSchema()).doConstruction(PersevereContextFactory.getContext(), GlobalData.getGlobalScope(), object, new Object[]{});
    return object;
View Full Code Here


      sourceClass = (String) schema.get("sourceClass");
    }
    if(sourceClass == null && schema == null && dataSourceElement.get("schema") instanceof Map && ((Map)dataSourceElement.get("schema")).get("sourceClass") instanceof String){
      sourceClass = (String) ((Map)dataSourceElement.get("schema")).get("sourceClass");
    }
    DataSource source;
    if ("Class".equals(name))
      source = metaClassSource;
    else{
      if(dataSources.get(name) != null)
        throw new RuntimeException("Duplicate table name " + name);
      source = ((Class<DataSource>) (Class.forName(sourceClass == null ? defaultSourceClass : sourceClass))).newInstance();
    }
    source.setId(name);
    source.initParameters(dataSourceElement);   
    dataSources.put(name, source);
    SourceInfo info = new SourceInfo();
    boolean visible = true;
    Scriptable global = GlobalData.getGlobalScope();
    visible = !Boolean.TRUE.equals(dataSourceElement.get("hidden"));
View Full Code Here

    idForSourceDefinition.targetRef = null; // force a reload
    Transaction.addAffectedSource((WritableDataSource) idForSourceDefinition.source);
    ((LocalJsonFileSource) idForSourceDefinition.source).makeDirty(idForSourceDefinition.subObjectId);
    String configId = AUTO_GENERATED_SOURCE_CONFIG_FILE + LocalJsonFileSource.pathSeparator + "sources"
      + LocalJsonFileSource.pathSeparator + (dataSourceConfigs.size() - 1);
    DataSource newSource = initSource(dataSourceElement, configId , schema, null);
    // must make the object look dirty
    ObjectId.idForObject((WritableDataSource) idForSourceDefinition.source, configId, true).getTarget().set("schema",null);
    ObjectId.idForObject((WritableDataSource) idForSourceDefinition.source, configId, true).getTarget().set("schema",
        ObjectId.idForObject((WritableDataSource) idForSourceDefinition.source, configId  + LocalJsonFileSource.pathSeparator + "schema", true).getTarget());
    return newSource;
View Full Code Here

   * saveConfig(config); }
   */

  public static void deleteSource(String name) throws Exception {
    Map config = loadConfig();
    DataSource deletingSource = getSource(name);
    if (deletingSource instanceof SourceDeleteAware)
      ((SourceDeleteAware) deletingSource).onDelete();
    List dataSourceConfigs = (List) config.get("sources");
    List newDataSourceConfigs = new ArrayList();
    for (int i = 0; i < dataSourceConfigs.size(); i++) {
View Full Code Here

      if(value.indexOf(LocalJsonFileSource.pathSeparator) != -1){
        pathIndex = 10000;
      }
    }
    String subObjectId;
    DataSource source = null;
    String path = null;
    int slashIndex = value.length();
    if (value.indexOf("/") > 0 && value.indexOf("/") < pathIndex){
      while((slashIndex = value.substring(0,slashIndex).lastIndexOf('/')) > -1){
        String sourcePart = value.substring(0,slashIndex);
        source = DataSourceManager.getSource(sourcePart);
        if (source != null)
          break;
      }
      if (source == null)
        return new ObjectNotFoundId(null,value);
      String idPart = value.substring(slashIndex + 1);
      subObjectId = idPart;
      /*if (pathIndex < 10000 && !(source instanceof HttpJsonSource)) {
        pathIndex = pathIndex - sourcePart.length() - 1;
        path = subObjectId.substring(pathIndex);
        subObjectId = subObjectId.substring(0,pathIndex);
      }*/
    }
    else {
      subObjectId = value;
      if (pathIndex < 10000) {
        path = subObjectId.substring(pathIndex);
        subObjectId = subObjectId.substring(0,pathIndex);
      }
      source = DataSourceManager.getSource(subObjectId);
      if (source == null) {
        AliasHandler handler = AliasIds.getAliasHandler(subObjectId);
        ObjectId objId;
        if (handler == null) {
          return new ObjectNotFoundId(source,value);
        }
        objId = handler.getTarget().getId();
        source = objId.getSource();
        if (source == null && handler != null)
          return handler;
        subObjectId = objId.getSubObjectId();
      }
      else
        subObjectId = ""; // this is a direct reference to the source, the id should be null
    }
    String slashedSubObjectId = null;
    if (subObjectId != null) {
      /*while (!subObjectId.equals(slashedSubObjectId)) {
        slashedSubObjectId = subObjectId;
        subObjectId = subObjectId.replaceAll("^([^\\[]+)/", "$1."); // allow slashes to indicate property references
      }*/
      int legalDotStart = 0;
      if (source instanceof HttpJsonSource && source.getId().length() < 8) {
        // if we are using a remote address, the first dots can be part of the hostname and should be ignored
        legalDotStart = subObjectId.indexOf('/', 2);
      }
      pathIndex = Math.min(Math.min(subObjectId.indexOf('[') > -1 ? subObjectId.indexOf('[') : 10000, subObjectId.indexOf('.', legalDotStart) > -1 ? subObjectId.indexOf('.', legalDotStart) : 10000),subObjectId.indexOf('?') > -1 ? subObjectId.indexOf('?') : 10000);
      if (pathIndex < 10000 &&
View Full Code Here

                      }
                    }
                  } else {
                    if (safeContentType)
                      requestHelper.authorizeCookieAuthentication();
                    DataSource source = ((Persistable) target).getId().source;
                    String contentDisposition = request.getHeader("Content-Disposition");
                    if (postBytes == null)
                      postBytes = IOUtils.toByteArray(request.getInputStream());
                    // create a File using the provided file/binary data
                    Persistable fileTarget = createFile(contentType, contentDisposition, postBytes);
                    if (!(source instanceof DynaFileDBSource)) {
                      Persistable resourceTarget = Persevere.newObject(((Persistable) target).getId());
                      resourceTarget.put("representation:" + contentType, resourceTarget, fileTarget);
                      ((PersistableObject) resourceTarget).setAttributes("representation:" + contentType,
                          ScriptableObject.DONTENUM);
                    }
                    target = fileTarget;
                    newContent = true;
                    force204 = true;
                  }
                }

                response.setStatus(201);
              } else if ("PUT".equals(method)) {
                boolean isJson = false;
                Identification<? extends Object> id = Identification.idForString(path);
                try {
                  if (couldBeJson(contentType)) {
                    if (postBytes == null)
                      postBytes = IOUtils.toByteArray(request.getInputStream());

                    String postBody = new String(postBytes, "UTF-8");
                    if (id instanceof ObjectPath) {
                      Object value = id.getTarget();
                      if (value instanceof Persistable) {
                        id = ((Persistable) value).getId();
                      }
                    }
                    target = requestHelper.convertWithKnownId(postBody, id);

                    isJson = true;
                  }
                } catch (JSONParser.JSONException e) {
                  if (contentType == null || contentType.indexOf("json") > 0 || contentType.indexOf("javascript") > 0) {
                    throw e;
                  }
                }
                if (!isJson) {
                  // create an alternate representation for the target object using the provided file/binary data
                  DataSource source = target instanceof Persistable ? ((Persistable) target).getId().source : null;
                  String contentDisposition = request.getHeader("Content-Disposition");
                  if (postBytes == null)
                    postBytes = IOUtils.toByteArray(request.getInputStream());
                  Persistable fileTarget = createFile(contentType, contentDisposition, postBytes);
                  if (!(source instanceof DynaFileDBSource) && !(id instanceof ObjectPath)) {
View Full Code Here

  }
  public Scriptable assignId(Scriptable newObject){
    ObjectId objId = ((Persistable)newObject).getId();
    if(objId instanceof NewObjectId && !objId.isPersisted()){
      String id = objId.subObjectId;
      DataSource source = DataSourceManager.getSource(this.id.subObjectId);
      if(source instanceof UserAssignableIdSource && newObject instanceof PersistableObject){
        if(id.startsWith("s$")){
          id = ((UserAssignableIdSource)source).newId();
        }
        else {
View Full Code Here

        map.put(name, get(name));
      }
      return map.entrySet();
    }
    public Object get(String key) {     
      DataSource source = DataSourceManager.getSource(key);
      if (source != null) {
        //Object schema = ObjectPath.idForObject(source, "", ".schema").getTarget();
        Persistable sourceDescription = new PersistableObject();
        sourceDescription.set("target",key + '/');
        PersistableList<Persistable> params = (PersistableList<Persistable>) sourceDescription.set("parameters",Persevere.newArray());
        Persistable idParam = new PersistableObject();
        params.add(idParam);
        idParam.set("type", "string");
        sourceDescription.set("returns", ObjectId.idForObject(DataSourceManager.getMetaClassSource(), source.getId()).getTarget());
        return sourceDescription;
      }
      return Scriptable.NOT_FOUND;
    }
View Full Code Here

        }
        sourceValue = convertToDateJavaDate(sourceValue);
      Identification id = target.getId();
      if (id instanceof ObjectId && id.source != null) { // it should always be persisted right?
        if (change.key != null) {
          DataSource initialSource = id.source;
          WritableDataSource source;
          String subObjectId = id.subObjectId;
          if (subObjectId == null) {
            subObjectId = initialSource.getId();
            source = DataSourceManager.metaClassSource;
          }
          else {
            source = (WritableDataSource) initialSource;
          }
View Full Code Here

      ScriptableObject objectProto = (ScriptableObject) ScriptableObject.getClassPrototype(GlobalData.getGlobalScope(), "Object");

      if ("".equals(((Persistable) target).getId().subObjectId)
          && (((Persistable) bodyData).getPrototype() == objectProto || ((Persistable) bodyData).getPrototype() == arrayProto)) {
        // this means it is a generic object or list, we need to make it be the right class
        DataSource thisSource = ((Persistable) target).getId().source;
        Class targetClass = DataSourceManager.getObjectsClass(thisSource).objectsClass;
        if (bodyData instanceof List) {
          // this means that an array was provided for a data source that takes objects; we
          //  will assume this means that the user wants to create multiple objects
          int i = 0;
View Full Code Here

TOP

Related Classes of org.persvr.datasource.DataSource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.