示例#1
0
  /** @param subs The sub configurations for this configuration */
  public void setSubConfigs(MutableConfig[] subs) {
    prisms.util.ArrayUtils.adjust(
        theSubConfigs,
        subs,
        new prisms.util.ArrayUtils.DifferenceListener<MutableConfig, MutableConfig>() {
          @Override
          public boolean identity(MutableConfig o1, MutableConfig o2) {
            return o1 == o2;
          }

          @Override
          public MutableConfig added(MutableConfig o, int mIdx, int retIdx) {
            o.setParent(MutableConfig.this);
            o.addListener(getSubConfigListener());
            return null;
          }

          @Override
          public MutableConfig removed(MutableConfig o, int oIdx, int incMod, int retIdx) {
            if (o.getParent() == MutableConfig.this) o.setParent(null);
            o.removeListener(getSubConfigListener());
            return null;
          }

          @Override
          public MutableConfig set(
              MutableConfig o1, int idx1, int incMod, MutableConfig o2, int idx2, int retIdx) {
            return null;
          }
        });
    theSubConfigs = subs;
  }
示例#2
0
 /**
  * Sets the value of a property for this listener
  *
  * @param propertyName The name of the property to set
  * @param value The value to associated with the property
  */
 public void set(String propertyName, Object value) {
   if (value == null) { // Delete the property
     if (thePropertyNames == null) return;
     int idx = ArrayUtils.indexOf(thePropertyNames, propertyName);
     if (idx < 0) return;
     if (thePropertyNames.length == 1) {
       thePropertyNames = null;
       thePropertyValues = null;
     } else {
       thePropertyNames = ArrayUtils.remove(thePropertyNames, idx);
       thePropertyValues = ArrayUtils.remove(thePropertyValues, idx);
     }
   } else if (thePropertyNames == null) {
     thePropertyNames = new String[] {propertyName};
     thePropertyValues = new Object[] {value};
   } else {
     int idx = ArrayUtils.indexOf(thePropertyNames, propertyName);
     if (idx < 0) {
       thePropertyNames = ArrayUtils.add(thePropertyNames, propertyName);
       thePropertyValues = ArrayUtils.add(thePropertyValues, value);
     } else thePropertyValues[idx] = value;
   }
 }
示例#3
0
 /** @param listener The listener to stop notification for */
 public void removeListener(ConfigListener listener) {
   theListeners = prisms.util.ArrayUtils.remove(theListeners, listener);
 }
示例#4
0
 /**
  * @param listener The listener to be notified when this configuration or any of its children
  *     change
  */
 public void addListener(ConfigListener listener) {
   if (listener != null) theListeners = prisms.util.ArrayUtils.add(theListeners, listener);
 }
示例#5
0
 /** @param sub The sub configuration to remove from this configuration */
 public void removeSubConfig(MutableConfig sub) {
   theSubConfigs = prisms.util.ArrayUtils.remove(theSubConfigs, sub);
   if (sub.theParent == this) sub.theParent = null;
   sub.removeListener(theSubConfigListener);
 }
示例#6
0
 /**
  * @param sub The sub configuration to add to this configuration
  * @return The added config, for chaining
  */
 public MutableConfig addSubConfig(MutableConfig sub) {
   theSubConfigs = prisms.util.ArrayUtils.add(theSubConfigs, sub);
   sub.theParent = this;
   sub.addListener(theSubConfigListener);
   return sub;
 }
示例#7
0
 /**
  * Gets the value of a property for this listener
  *
  * @param propertyName The name of the property to get
  * @return The value associated with the property or none if the property has not been set
  */
 public Object get(String propertyName) {
   if (thePropertyNames == null) return null;
   int idx = ArrayUtils.indexOf(thePropertyNames, propertyName);
   if (idx < 0) return null;
   return thePropertyValues[idx];
 }
    void importData(
        PrismsApplication app,
        PrismsSynchronizer sync,
        String version,
        JsonSerialReader jsr,
        StringBuilder message)
        throws java.io.IOException, SAJParser.ParseException {
      SyncRecord syncRecord =
          new SyncRecord(
              new PrismsCenter("Export"),
              SyncRecord.Type.AUTOMATIC,
              System.currentTimeMillis(),
              false);
      PrismsSynchronizer.SyncTransaction trans =
          sync.transact(
              version, true, syncRecord, false, new prisms.ui.UI.DefaultProgressInformer());

      // Items
      PrismsSynchronizer.PS2ItemReader itemReader = new PrismsSynchronizer.PS2ItemReader(trans);
      if (!jsr.goToProperty("items")) {
        message.append("\n\t\tNo items property in sync data");
        return;
      }
      int items = 0;
      org.json.simple.JSONObject obj;
      jsr.startArray();
      obj = jsr.parseObject();
      while (obj != null) {
        items++;
        try {
          itemReader.read(obj);
        } catch (PrismsRecordException e) {
          log.error("Error parsing item", e);
        }
        obj = jsr.parseObject();
      }
      jsr.endArray(null);
      message.append("\n\t\t").append(items).append(" items");

      PrismsCenter[] centers;
      try {
        centers = sync.getKeeper().getCenters();
      } catch (PrismsRecordException e) {
        message.append("\n\t\t").append("Could not get centers");
        log.error("Could not get centers", e);
        centers = null;
      }
      if (centers != null && trans.getImpl().getCentersProperty() != null) {
        PrismsCenter[] appCenters = centers;
        for (int i = 0; i < appCenters.length; i++)
          if (centers[i].getID() == 0) {
            // Don't include the "Here" center in the UI value
            appCenters = prisms.util.ArrayUtils.remove(appCenters, i);
            break;
          }
        app.setGlobalProperty(
            trans.getImpl().getCentersProperty(),
            appCenters,
            RecordUtils.TRANSACTION_EVENT_PROP,
            new prisms.records.RecordsTransaction());
      }

      // Changes
      PrismsSynchronizer.ChangeReader changeReader =
          new PrismsSynchronizer.ChangeReader(trans, null, itemReader, 0);
      if (!jsr.goToProperty("changes")) {
        message.append("\n\t\tNo changes property in sync data");
        return;
      }
      int changes = 0;
      jsr.startArray();
      obj = jsr.parseObject();
      while (obj != null) {
        changes++;
        changeReader.readChange(obj);
        obj = jsr.parseObject();
      }
      jsr.endArray(null);
      message.append(", ").append(changes).append(" changes");
      changeReader = null;
      itemReader = null;

      // Sync records
      if (centers != null) {
        if (jsr.goToProperty("syncRecords")) {
          int syncRecords = 0;
          jsr.startArray();
          while (jsr.getNextItem(true, false) instanceof JsonSerialReader.ObjectItem) {
            JsonSerialReader.StructState centerState = jsr.save();
            try {
              if (!jsr.goToProperty("id")) {
                message.append("\n\t\tID missing in sync records group");
                break;
              }
              int centerID = jsr.parseInt();
              PrismsCenter center = null;
              for (PrismsCenter c : centers)
                if (c.getID() == centerID) {
                  center = c;
                  break;
                }
              if (center == null) {
                log.warn("No such center with ID " + centerID);
                continue;
              }
              if (!jsr.goToProperty("syncRecords")) {
                message.append("\n\t\tsyncRecords missing in sync records group");
                continue;
              }
              jsr.startArray();
              while (jsr.getNextItem(true, false) instanceof JsonSerialReader.ObjectItem) {
                JsonSerialReader.StructState srState = jsr.save();
                try {
                  if (!jsr.goToProperty("id")) {
                    log.warn("No id property in sync record");
                    continue;
                  }
                  int srID = jsr.parseInt();
                  if (!jsr.goToProperty("parallelID")) {
                    log.warn("No parallelID property in sync record");
                    continue;
                  }
                  int parallelID = jsr.parseInt();
                  if (!jsr.goToProperty("syncType")) {
                    log.warn("No syncType property in sync record");
                    continue;
                  }
                  String typeName = jsr.parseString();
                  SyncRecord.Type type = SyncRecord.Type.byName(typeName);
                  if (type == null) {
                    log.warn("Unrecognized sync type in sync record: " + typeName);
                    continue;
                  }
                  if (!jsr.goToProperty("time")) {
                    log.warn("No time property in sync record");
                    continue;
                  }
                  long time = jsr.parseLong();
                  if (!jsr.goToProperty("isImport")) {
                    log.warn("No isImport property in sync record");
                    continue;
                  }
                  boolean isImport = jsr.parseBoolean();
                  if (!jsr.goToProperty("syncError")) {
                    log.warn("No syncError property in sync record");
                    continue;
                  }
                  Object syncError = jsr.parseNext(false);
                  SyncRecord sr = new SyncRecord(center, type, time, isImport);
                  sr.setID(srID);
                  sr.setParallelID(parallelID);
                  if (syncError instanceof String) sr.setSyncError((String) syncError);
                  else if (syncError == JsonSerialReader.NULL) sr.setSyncError(null);
                  else String.class.cast(syncError);
                  try {
                    sync.getKeeper().putSyncRecord(sr);
                  } catch (PrismsRecordException e) {
                    log.error("Could not persist sync record", e);
                    continue;
                  }
                  syncRecords++;
                  if (!jsr.goToProperty("associated")) {
                    log.warn("No associated property in sync record");
                    continue;
                  }
                  prisms.util.LongList assocIDs = new prisms.util.LongList();
                  prisms.util.BooleanList assocErrors = new prisms.util.BooleanList();
                  jsr.startArray();
                  while (jsr.getNextItem(true, false) instanceof JsonSerialReader.ObjectItem) {
                    if (!jsr.goToProperty("id")) {
                      log.warn("No id for associated change");
                      jsr.endObject(null);
                      continue;
                    }
                    assocIDs.add(jsr.parseLong());
                    if (jsr.goToProperty("error")) assocErrors.add(jsr.parseBoolean());
                    else assocErrors.add(false);
                    jsr.endObject(null);
                  }
                  jsr.endArray(null);
                  ChangeRecord[] assocChanges;
                  try {
                    assocChanges = sync.getKeeper().getItems(assocIDs.toArray());
                  } catch (PrismsRecordException e) {
                    log.error("Could not get associated changes", e);
                    continue;
                  }
                  for (int i = 0; i < assocChanges.length; i++) {
                    if (assocChanges[i] != null)
                      try {
                        sync.getKeeper().associate(assocChanges[i], sr, assocErrors.get(i));
                      } catch (PrismsRecordException e) {
                        log.error("Could not associated change", e);
                      }
                  }
                } finally {
                  jsr.endObject(srState);
                }
              }
              jsr.endArray(null);
            } finally {
              jsr.endObject(centerState);
            }
          }
          jsr.endArray(null);
          message.append(", ").append(syncRecords).append(" sync records");
        }
      }

      // Latest changes
      if (jsr.goToProperty("latestChanges")) {
        JsonSerialReader.StructState rootState = jsr.startArray();
        while (jsr.getNextItem(true, false) instanceof JsonSerialReader.ObjectItem) {
          JsonSerialReader.StructState lcState = jsr.save();
          try {
            if (!jsr.goToProperty("center")) {
              log.warn("No center for latest change entry");
              continue;
            }
            int centerID = jsr.parseInt();
            if (!jsr.goToProperty("subjectCenter")) {
              log.warn("No subjectCenter for latest change entry");
              continue;
            }
            int subjectCenter = jsr.parseInt();
            if (!jsr.goToProperty("latestChange")) {
              log.warn("No latestChange for latest change entry");
              continue;
            }
            long changeTime = jsr.parseLong();
            try {
              sync.getKeeper().setLatestChange(centerID, subjectCenter, changeTime);
            } catch (PrismsRecordException e) {
              log.error("Could not set latest change", e);
            }
          } finally {
            jsr.endObject(lcState);
          }
        }
        jsr.endArray(rootState);
      }
    }