Package org.persvr.data

Examples of org.persvr.data.Persistable


   */
  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


   * persisted in the class's table (when the current transaction is committed).
   * @param queryId The id of the table
   * @return the newly created instance object
   */
  public static Persistable newObject(ObjectId queryId) {
    Persistable object = newObject(queryId.source);
    ((PersistableClass)object.getSchema()).doConstruction(PersevereContextFactory.getContext(), GlobalData.getGlobalScope(), object, new Object[]{});
    return object;
  }
View Full Code Here

    if(!UserSecurity.hasPermission(SystemPermission.createTables)){
      throw new SecurityException("You do not have permission to create new tables");
    }
    if (name.matches("[^\\w$_]"))
      throw new RuntimeException("Illegal character in table");
    Persistable newSource = Persevere.newObject(DataSourceManager.getMetaClassSource());
    newSource.set("id", name);
    newSource.set("extends", superType instanceof String ? ObjectId.idForObject(DataSourceManager.getMetaClassSource(), superType) : ObjectId.idForObject(DataSourceManager.getMetaClassSource(),"Object"));

  }
View Full Code Here

    if (value instanceof Identification)
      return ((Identification<? extends Object>)value).getTarget();
    return value;
  }
  public void handleRPC(Object targetObject,  Map rpcObject) {
    Persistable target = (targetObject instanceof ObjectId) ? ((ObjectId)targetObject).getTarget() : (Persistable)targetObject;
    List params = (List) rpcObject.get("params");
    Object[] paramValues;
    if (params == null) {
      paramValues = new Object[0];
    }
View Full Code Here

  }
  public Identification<? extends Object> idFromJSPONObject(Map<String,Object> object, ObjectId targetId, boolean mustMatchId)  {
    //TODO: This needs be rearranged so that when you do a put (specifically an alteration), that we use the object
    // returned by the put instead of what the id indicates, because it is possible for a aliasId to indicate that we
    // should use a new object, when really we should use an existing object (from the childMods list)
      Persistable target;
    try {
      String key;
      target = null;
      Date changesSince=null;
      if (object.containsKey("update") && ((Map) object.get("update")).containsKey("changesSince")) {
        String value = (String) ((Map) object.get("update")).get("changesSince");
        changesSince = new Date(Long.parseLong(value.substring(1,value.length()-1))); // handle dates     
      }
      if (object.containsKey("$ref")) {
       
        return Identification.idForRelativeString(path, (String) object.get("$ref"));
      }
      if (object.containsKey("id")) {
        Identification currentId = Identification.idForRelativeString(path, object.remove("id").toString());

        if (currentId.source instanceof ClientData) // TODO: Surely we can do this more consistently
          target = Client.getCurrentObjectResponse().getConnection().clientSideObject(currentId.toString(),createInitialObject(object));
        else {
          if (currentId instanceof ObjectId){
            if(mustMatchId){
              if(targetId != currentId){
                throw new RuntimeException("id does not match location");
              }
            }
            else {
              targetId = (ObjectId) currentId;
            }
           
          }
          else {
            target = (Persistable) currentId.getTarget();
            if(mustMatchId && target.getId() != targetId){
              throw new RuntimeException("id does not match location");
            }
          }
        }
      }
      if (targetId == null) {
        if (target == null)
          target = createInitialObject(object);
      }
      else {
        target= targetId.getOrCreateTarget();
      }
      PersistableObject.checkSecurity(target, PermissionLevel.WRITE_LEVEL.level);

      for (Map.Entry<String,Object> oldEntry : target.entrySet(0)) {
        String oldKey = oldEntry.getKey();
        if (!object.containsKey(oldKey) && !oldKey.equals("parent"))
          target.delete(oldKey);
      }
      for (Map.Entry<String,Object> entry : object.entrySet()) {
        key = entry.getKey()// TODO: This needs to be limited to alteration lists, so we don't get a conflict with fields that start with c$.  This may need to be identified on the client side
        Object value = entry.getValue();
        if (key.startsWith("client/")) // This is a client id alteration which needs be changed a
        {
          key = Client.getCurrentObjectResponse().getConnection().clientSideObject(key,object.containsKey("array") ?
                Persevere.newArray() : Persevere.newObject()).getId().toString();
        }
        /*String valueModOriginal = null;
        if (GlobalData.CHILDMODS_FIELD.equals(key))
          valueModOriginal = GlobalData.CHILDMODS_FIELD;

        if (target!=null && (GlobalData.CHILDMODS_FIELD.equals(childModOriginalId) || target.isChildMods()) && !GlobalData.PARENT_FIELD.equals(key))
          valueModOriginal = key;*/
/*        if ("update".equals(key)) {
          if ("delete".equals(value))
            return getErasureEntity();
          if (value instanceof Map)
            updateList(target,(Map) value);
        }
        else {*/
        Object oldValue = target.get(key);
          value = idOrValueFromJSON(value, oldValue instanceof Persistable ? ((Persistable)oldValue).getId() : null);
          if (value instanceof ObjectNotFoundId)
            throw new RuntimeException("Can not set value to an undefined id");
          if (key.equals(FUNCTION_CODE_KEY)) {
            value = functionCompression((String) value);
            }
          if (target != null) {
              value = convertIdIfNeeded(value);
              if (!(oldValue == null ? value == null : oldValue.equals(value))){
                PersistableObject.checkSecurity(target, PermissionLevel.WRITE_LEVEL.level);
                target.set(key,value);
              }
          }
        //}
      }
    } catch (JSONException e) {
      throw new RuntimeException(e);
    }
/*    if (parentValue != null) // we will do it at the end so that if it is an append list entry it can be done without security problems
      target.set(GlobalData.PARENT_FIELD, parentValue);*/
      return target.getId();
  }
View Full Code Here

        public Object run() {
          List correctUsers = (List) JsonPath.query(usersTable(),"$[?(@.name=$1)]", username);
              if (correctUsers.size() == 0) {
                  return new LoginException("user " + username + " not found");
              }
              Persistable userObject = (Persistable) correctUsers.get(0);
              boolean alreadyHashed = false;
              boolean passwordMatch = password.equals(userObject.get(PASSWORD_FIELD));
              if (!passwordMatch) {
                  try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(((String) userObject.get(PASSWORD_FIELD)).getBytes());
            passwordMatch = password.equals(new String(new Base64().encode(md.digest())));
            } catch (NoSuchAlgorithmException e) {
              throw new RuntimeException(e);
            }
            alreadyHashed = true;         
View Full Code Here

            throw new RuntimeException("You are not a part of the su capable group, so you can not su");
        List correctUsers = (List) JsonPath.query(usersTable(),"$[?(@.name=$1)]", username);
        if (correctUsers.size() == 0) {
            throw new RuntimeException("user " + username + " not found");
        }
        Persistable userObject = (Persistable) correctUsers.get(0);
        return (CapabilityUser) userObject;
    }
View Full Code Here

  public void execute() {
    try {
      List customers = (List) Persevere.load("Customer/");
      Transaction transaction = Transaction.startTransaction();
      if(customers.size() == 0){
        Persistable customer1 = Persevere.newObject("Customer");
        customer1.set("firstName", "John");
        customer1.set("lastName", "Doe");
        customer1.set("age", 41);
        Persistable customer2 = Persevere.newObject("Customer");
        customer2.set("firstName", "Jim");
        customer2.set("lastName", "Jones");
        customer2.set("age", 33);
      }
      transaction.commit();
    }
    catch (ObjectNotFoundException e){
     
View Full Code Here

  public void grantCapability(Persistable target, final String levelName) {
   
    if(target.getId().source == DataSourceManager.getMetaClassSource() && target.getId() instanceof Query){
      target = DataSourceManager.getRootObject();
    }
    final Persistable persistable = target;
    int level = permissionNameLevelMap.get(levelName);
    if (UserSecurity.getPermissionLevel(persistable) < FULL_ACCESS)
      throw new SecurityException("You do not have permission to grant this authorization to this resource");
    computedPermissions.clear(); // clear the cache first
      UserSecurity.doPriviledgedAction(new PrivilegedAction() {

      public Object run() {
        for (Entry<String,Integer> permissionEntry : permissionNameLevelMap.entrySet()){
          Object grantedObject = noCheckGet(permissionEntry.getKey());
          if (grantedObject instanceof PersistableList) {
            PersistableList<Persistable> granted = (PersistableList<Persistable>) grantedObject;
            if(permissionEntry.getValue() == 7) {
              // if it is granted, we want to clean out bad entries from the previous version of Persevere
              for (int i = granted.size(); i > 0;){
                i--;
                Object grantee = granted.get(i);
                if (!(grantee instanceof Capability)){
                  granted.remove(i);
                }
              }
            }
            if (granted.contains(persistable)){
              if (permissionEntry.getKey().equals(levelName)) {
                // already has the right permission
                return null;
              }
              else {
                granted.remove(persistable);
              }
            }
          }
        }
        Object granted = noCheckGet(levelName);
        if (!(granted instanceof PersistableList)){
          granted = Persevere.newArray();
          set(levelName, granted);
        }
        ((PersistableList)granted).add(persistable);
        return null;
      }
      });     
   
    if (allGranted != null)
      allGranted.put(persistable.getId(), level);
  }
View Full Code Here

       */
    @Override
    public String handle(ClientImpl client, Transport transport, Message message) throws IOException {
      if (!message.getChannel().startsWith("/meta")){
        // These are published messages
        Persistable target = (Persistable) Identification.idForString((String) message.getChannel()).getTarget();
        Function messageFunc = (Function) target.get("message");
        messageFunc.call(PersevereContextFactory.getContext(), GlobalData.getGlobalScope(), target, new Object[]{message.getData()});
        // don't actually publish it, let Persevere handle it
        return null;
      }
      if (message.getChannel().startsWith("/meta/subscribe")){
View Full Code Here

TOP

Related Classes of org.persvr.data.Persistable

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.