/* (non-Javadoc)
  * @see org.javarosa.core.services.storage.IStorageUtility#write(org.javarosa.core.services.storage.Persistable)
  */
 public void write(Persistable p) throws StorageFullException {
   if (p.getID() != -1) {
     this.data.put(DataUtil.integer(p.getID()), (T) p);
     syncMeta();
   } else {
     p.setID(curCount);
     this.add((T) p);
   }
 }
Esempio n. 2
0
  public static byte[] toBlob(Persistable p) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    OutputStream out = bos;

    try {
      p.writeExternal(new DataOutputStream(out));
      out.close();
    } catch (IOException e1) {
      e1.printStackTrace();
      throw new RuntimeException("Failed to serialize externalizable for content values");
    }
    return bos.toByteArray();
  }
  public static void importRMS(FormInstance dm, IStorageUtility storage, Class type, String path) {
    if (!Externalizable.class.isAssignableFrom(type) || !Restorable.class.isAssignableFrom(type)) {
      return;
    }

    boolean idMatters = Persistable.class.isAssignableFrom(type);

    String childName = ((Restorable) PrototypeFactory.getInstance(type)).getRestorableType();
    TreeElement e = dm.resolveReference(absRef(path, dm));
    Vector children = e.getChildrenWithName(childName);

    for (int i = 0; i < children.size(); i++) {
      FormInstance child = subDataModel((TreeElement) children.elementAt(i));

      Restorable inst = (Restorable) PrototypeFactory.getInstance(type);

      // restore record id first so 'importData' has access to it
      int recID = -1;
      if (idMatters) {
        recID = ((Integer) getValue(RECORD_ID_TAG, child)).intValue();
        ((Persistable) inst).setID(recID);
      }

      inst.importData(child);

      try {
        if (idMatters) {
          storage.write((Persistable) inst);
        } else {
          storage.add((Externalizable) inst);
        }
      } catch (Exception ex) {
        throw new RuntimeException(
            "Error importing RMS during restore! ["
                + type.getName()
                + ":"
                + recID
                + "]; "
                + ex.getMessage());
      }
    }
  }
 /* (non-Javadoc)
  * @see org.javarosa.core.services.storage.IStorageUtility#remove(org.javarosa.core.services.storage.Persistable)
  */
 public void remove(Persistable p) {
   this.read(p.getID());
 }