Example #1
0
  /**
   * Method to add a Collection to a position in the ArrayList.
   *
   * @param index Position to insert the collection.
   * @param elements The collection
   * @return Whether it was added ok.
   */
  public boolean addAll(int index, Collection elements) {
    if (useCache) {
      loadFromStore();
    }

    boolean backingSuccess = true;
    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(queued, ownerOP)) {
        int pos = index;
        for (Object element : elements) {
          ownerOP
              .getExecutionContext()
              .addOperationToQueue(new ListAddAtOperation(ownerOP, backingStore, pos++, element));
        }
      } else {
        try {
          backingSuccess =
              backingStore.addAll(ownerOP, elements, index, (useCache ? delegate.size() : -1));
        } catch (NucleusDataStoreException dse) {
          throw new IllegalArgumentException(
              Localiser.msg("023013", "addAll", ownerMmd.getName(), dse), dse);
        }
      }
    }

    // Only make it dirty after adding the element(s) to the datastore so we give it time
    // to be inserted - otherwise jdoPreStore on this object would have been called before
    // completing the addition
    makeDirty();

    boolean delegateSuccess = delegate.addAll(index, elements);

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return (backingStore != null ? backingSuccess : delegateSuccess);
  }
Example #2
0
  /**
   * Method to initialise the SCO from an existing value.
   *
   * @param c The object to set from
   * @param forInsert Whether the object needs inserting in the datastore with this value
   * @param forUpdate Whether to update the SCO in the datastore with this value
   */
  public void initialise(java.util.ArrayList c, boolean forInsert, boolean forUpdate) {
    if (c != null) {
      // Check for the case of serialised PC elements, and assign ObjectProviders to the elements
      // without
      if (SCOUtils.collectionHasSerialisedElements(ownerMmd)
          && ownerMmd.getCollection().elementIsPersistent()) {
        ExecutionContext ec = ownerOP.getExecutionContext();
        Iterator iter = c.iterator();
        while (iter.hasNext()) {
          Object pc = iter.next();
          ObjectProvider objOP = ec.findObjectProvider(pc);
          if (objOP == null) {
            objOP =
                ec.getNucleusContext()
                    .getObjectProviderFactory()
                    .newForEmbedded(ec, pc, false, ownerOP, ownerMmd.getAbsoluteFieldNumber());
          }
        }
      }

      if (backingStore != null && useCache && !isCacheLoaded) {
        // Mark as loaded so we just use the value
        isCacheLoaded = true;
      }

      if (forInsert) {
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
          NucleusLogger.PERSISTENCE.debug(
              Localiser.msg(
                  "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size()));
        }
        if (useCache) {
          loadFromStore();
        }
        if (backingStore != null) {
          if (SCOUtils.useQueuedUpdate(queued, ownerOP)) {
            for (Object element : c) {
              ownerOP
                  .getExecutionContext()
                  .addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element));
            }
          } else {
            try {
              backingStore.addAll(ownerOP, c, (useCache ? delegate.size() : -1));
            } catch (NucleusDataStoreException dse) {
              NucleusLogger.PERSISTENCE.warn(
                  Localiser.msg("023013", "addAll", ownerMmd.getName(), dse));
            }
          }
        }
        makeDirty();
        delegate.addAll(c);
      } else if (forUpdate) {
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
          NucleusLogger.PERSISTENCE.debug(
              Localiser.msg(
                  "023008", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size()));
        }

        // TODO This does clear+addAll : Improve this and work out which elements are added and
        // which deleted
        if (backingStore != null) {
          if (SCOUtils.useQueuedUpdate(queued, ownerOP)) {
            ownerOP
                .getExecutionContext()
                .addOperationToQueue(new CollectionClearOperation(ownerOP, backingStore));
          } else {
            backingStore.clear(ownerOP);
          }
        }

        if (useCache) {
          loadFromStore();
        }
        if (backingStore != null) {
          if (SCOUtils.useQueuedUpdate(queued, ownerOP)) {
            for (Object element : c) {
              ownerOP
                  .getExecutionContext()
                  .addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element));
            }
          } else {
            try {
              backingStore.addAll(ownerOP, c, (useCache ? delegate.size() : -1));
            } catch (NucleusDataStoreException dse) {
              NucleusLogger.PERSISTENCE.warn(
                  Localiser.msg("023013", "addAll", ownerMmd.getName(), dse));
            }
          }
        }
        delegate.addAll(c);
        makeDirty();
      } else {
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
          NucleusLogger.PERSISTENCE.debug(
              Localiser.msg(
                  "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size()));
        }
        delegate.clear();
        delegate.addAll(c);
      }
    }
  }