Beispiel #1
0
  /**
   * Method to remove a collection of elements from the List.
   *
   * @param elements Collection of elements to remove
   * @return Whether it was successful.
   */
  public boolean removeAll(Collection elements) {
    makeDirty();

    if (useCache) {
      loadFromStore();
    }

    int size = (useCache ? delegate.size() : -1);
    Collection contained = null;
    if (backingStore != null && SCOUtils.useQueuedUpdate(queued, ownerOP)) {
      // Check which are contained before updating the delegate
      contained = new java.util.HashSet();
      for (Object elem : elements) {
        if (contains(elem)) {
          contained.add(elem);
        }
      }
    }
    boolean delegateSuccess = delegate.removeAll(elements);

    if (backingStore != null) {
      boolean backingSuccess = true;
      if (SCOUtils.useQueuedUpdate(queued, ownerOP)) {
        backingSuccess = false;
        for (Object element : contained) {
          backingSuccess = true;
          ownerOP
              .getExecutionContext()
              .addOperationToQueue(
                  new CollectionRemoveOperation(ownerOP, backingStore, element, true));
        }
      } else {
        try {
          backingSuccess = backingStore.removeAll(ownerOP, elements, size);
        } catch (NucleusDataStoreException dse) {
          NucleusLogger.PERSISTENCE.warn(
              Localiser.msg("023013", "removeAll", ownerMmd.getName(), dse));
          backingSuccess = false;
        }
      }

      if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
      }

      return backingSuccess;
    }

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return delegateSuccess;
  }
  /**
   * Method to remove an element from the List
   *
   * @param element The Element to remove
   * @return Whether it was removed successfully.
   */
  public boolean remove(Object element, boolean allowCascadeDelete) {
    boolean success = delegate.remove(element);
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) {
      ownerOP
          .getExecutionContext()
          .getRelationshipManager(ownerOP)
          .relationRemove(ownerMmd.getAbsoluteFieldNumber(), element);
    }

    if (ownerOP != null && allowCascadeDelete) {
      // Cascade delete
      if (SCOUtils.useQueuedUpdate(ownerOP)) {
        // Queue the cascade delete
        ownerOP
            .getExecutionContext()
            .addOperationToQueue(
                new CollectionRemoveOperation(
                    ownerOP, ownerMmd.getAbsoluteFieldNumber(), element, allowCascadeDelete));
      } else if (SCOUtils.hasDependentElement(ownerMmd)) {
        // Perform the cascade delete
        ownerOP.getExecutionContext().deleteObjectInternal(element);
      }
    }

    if (success) {
      makeDirty();
      if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
      }
    }

    return success;
  }
 /**
  * Method to add a collection to the LinkedHashSet.
  *
  * @param elements The collection
  * @return Whether it was added ok.
  */
 public boolean addAll(Collection elements) {
   boolean success = delegate.addAll(elements);
   if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) {
     // Relationship management
     Iterator iter = elements.iterator();
     while (iter.hasNext()) {
       ownerOP
           .getExecutionContext()
           .getRelationshipManager(ownerOP)
           .relationAdd(ownerMmd.getAbsoluteFieldNumber(), iter.next());
     }
   }
   if (success) {
     if (SCOUtils.useQueuedUpdate(ownerOP)) {
       for (Object element : elements) {
         ownerOP
             .getExecutionContext()
             .addOperationToQueue(
                 new CollectionAddOperation(ownerOP, ownerMmd.getAbsoluteFieldNumber(), element));
       }
     }
     makeDirty();
     if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
       ownerOP.getExecutionContext().processNontransactionalUpdate();
     }
   }
   return success;
 }
Beispiel #4
0
  /**
   * Wrapper addition that allows turning off of the dependent-field checks when doing the position
   * setting. This means that we can prevent the deletion of the object that was previously in that
   * position. This particular feature is used when attaching a list field and where some elements
   * have changed positions.
   *
   * @param index The position
   * @param element The new element
   * @return The element previously at that position
   */
  public Object set(int index, Object element, boolean allowDependentField) {
    // Reject inappropriate elements
    if (!allowNulls && element == null) {
      throw new NullPointerException(
          "Nulls not allowed for collection at field "
              + ownerMmd.getName()
              + " but element is null");
    }

    makeDirty();

    if (useCache) {
      loadFromStore();
    }

    Object delegateReturn = delegate.set(index, element);
    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(queued, ownerOP)) {
        ownerOP
            .getExecutionContext()
            .addOperationToQueue(
                new ListSetOperation(ownerOP, backingStore, index, element, allowDependentField));
      } else {
        backingStore.set(ownerOP, index, element, allowDependentField);
      }
    }

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return delegateReturn;
  }
Beispiel #5
0
  /**
   * Method to remove an element from the ArrayList.
   *
   * @param index The element position.
   * @return The object that was removed
   */
  public synchronized Object remove(int index) {
    makeDirty();

    if (useCache) {
      loadFromStore();
    }

    int size = (useCache ? delegate.size() : -1);
    Object delegateObject = (useCache ? delegate.remove(index) : null);

    Object backingObject = null;
    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(queued, ownerOP)) {
        backingObject = delegateObject;
        ownerOP
            .getExecutionContext()
            .addOperationToQueue(new ListRemoveAtOperation(ownerOP, backingStore, index));
      } else {
        try {
          backingObject = backingStore.remove(ownerOP, index, size);
        } catch (NucleusDataStoreException dse) {
          NucleusLogger.PERSISTENCE.warn(
              Localiser.msg("023013", "remove", ownerMmd.getName(), dse));
          backingObject = null;
        }
      }
    }

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }

    return (backingStore != null ? backingObject : delegateObject);
  }
Beispiel #6
0
  /**
   * Method to add a Map of values to this map.
   *
   * @param m The Map to add
   */
  public synchronized void putAll(java.util.Map m) {
    makeDirty();

    if (useCache) {
      // Make sure we have all values loaded (e.g if in optimistic tx and we put new entry)
      loadFromStore();
    }

    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(ownerOP)) {
        Iterator iter = m.entrySet().iterator();
        while (iter.hasNext()) {
          java.util.Map.Entry entry = (java.util.Map.Entry) iter.next();
          ownerOP
              .getExecutionContext()
              .addOperationToQueue(
                  new MapPutOperation(ownerOP, backingStore, entry.getKey(), entry.getValue()));
        }
      } else {
        backingStore.putAll(ownerOP, m);
      }
    }
    delegate.putAll(m);

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
  }
Beispiel #7
0
  /**
   * Method to remove a value from the Map.
   *
   * @param key The key for the value.
   * @return The value removed.
   */
  public synchronized Object remove(Object key) {
    makeDirty();

    if (useCache) {
      // Make sure we have all values loaded (e.g if in optimistic tx and we put new entry)
      loadFromStore();
    }

    Object removed = null;
    Object delegateRemoved = delegate.remove(key);
    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(ownerOP)) {
        ownerOP
            .getExecutionContext()
            .addOperationToQueue(
                new MapRemoveOperation(ownerOP, backingStore, key, delegateRemoved));
        removed = delegateRemoved;
      } else {
        removed = backingStore.remove(ownerOP, key);
      }
    } else {
      removed = delegateRemoved;
    }

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return removed;
  }
Beispiel #8
0
  /**
   * Method to add a value to the Map.
   *
   * @param key The key for the value.
   * @param value The value
   * @return The previous value against this key (if any).
   */
  public synchronized Object put(Object key, Object value) {
    // Reject inappropriate values
    if (!allowNulls) {
      if (value == null) {
        throw new NullPointerException(
            "Nulls not allowed for map at field " + ownerMmd.getName() + " but value is null");
      }
      if (key == null) {
        throw new NullPointerException(
            "Nulls not allowed for map at field " + ownerMmd.getName() + " but key is null");
      }
    }

    if (useCache) {
      // Make sure we have all values loaded (e.g if in optimistic tx and we put new entry)
      loadFromStore();
    }

    makeDirty();

    Object oldValue = null;
    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(ownerOP)) {
        ownerOP
            .getExecutionContext()
            .addOperationToQueue(new MapPutOperation(ownerOP, backingStore, key, value));
      } else {
        oldValue = backingStore.put(ownerOP, key, value);
      }
    }
    Object delegateOldValue = delegate.put(key, value);
    if (backingStore == null) {
      oldValue = delegateOldValue;
    } else if (SCOUtils.useQueuedUpdate(ownerOP)) {
      oldValue = delegateOldValue;
    }

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return oldValue;
  }
Beispiel #9
0
  /**
   * Method to add an element to the TreeSet.
   *
   * @param element The new element
   * @return Whether it was added ok.
   */
  public boolean add(Object element) {
    // Reject inappropriate elements
    if (!allowNulls && element == null) {
      throw new NullPointerException(
          "Nulls not allowed for collection at field "
              + ownerMmd.getName()
              + " but element is null");
    }

    if (useCache) {
      loadFromStore();
    }
    if (contains(element)) {
      return false;
    }

    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) {
      // Relationship management
      ownerOP
          .getExecutionContext()
          .getRelationshipManager(ownerOP)
          .relationAdd(ownerMmd.getAbsoluteFieldNumber(), element);
    }

    boolean backingSuccess = true;
    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(queued, ownerOP)) {
        ownerOP
            .getExecutionContext()
            .addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element));
      } else {
        try {
          backingStore.add(ownerOP, element, (useCache ? delegate.size() : -1));
        } catch (NucleusDataStoreException dse) {
          NucleusLogger.PERSISTENCE.warn(LOCALISER.msg("023013", "add", ownerMmd.getName(), dse));
          backingSuccess = false;
        }
      }
    }

    // Only make it dirty after adding the field 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.add(element);

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return (backingStore != null ? backingSuccess : delegateSuccess);
  }
Beispiel #10
0
  /**
   * Method to remove an element from the collection, and observe the flag for whether to allow
   * cascade delete.
   *
   * @param element The element
   * @param allowCascadeDelete Whether to allow cascade delete
   */
  public boolean remove(Object element, boolean allowCascadeDelete) {
    makeDirty();

    if (useCache) {
      loadFromStore();
    }

    int size = (useCache ? delegate.size() : -1);
    boolean contained = delegate.contains(element);
    boolean delegateSuccess = delegate.remove(element);
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) {
      ownerOP
          .getExecutionContext()
          .getRelationshipManager(ownerOP)
          .relationRemove(ownerMmd.getAbsoluteFieldNumber(), element);
    }

    boolean backingSuccess = true;
    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(queued, ownerOP)) {
        backingSuccess = contained;
        if (backingSuccess) {
          ownerOP
              .getExecutionContext()
              .addOperationToQueue(
                  new CollectionRemoveOperation(
                      ownerOP, backingStore, element, allowCascadeDelete));
        }
      } else {
        try {
          backingSuccess = backingStore.remove(ownerOP, element, size, allowCascadeDelete);
        } catch (NucleusDataStoreException dse) {
          NucleusLogger.PERSISTENCE.warn(
              LOCALISER.msg("023013", "remove", ownerMmd.getName(), dse));
          backingSuccess = false;
        }
      }
    }

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }

    return (backingStore != null ? backingSuccess : delegateSuccess);
  }
Beispiel #11
0
  /**
   * Method to add a collection to the TreeSet.
   *
   * @param elements The collection
   * @return Whether it was added ok.
   */
  public boolean addAll(Collection elements) {
    if (useCache) {
      loadFromStore();
    }
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) {
      // Relationship management
      Iterator iter = elements.iterator();
      RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP);
      while (iter.hasNext()) {
        relMgr.relationAdd(ownerMmd.getAbsoluteFieldNumber(), iter.next());
      }
    }

    boolean backingSuccess = true;
    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(queued, ownerOP)) {
        for (Object element : elements) {
          ownerOP
              .getExecutionContext()
              .addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element));
        }
      } else {
        try {
          backingSuccess =
              backingStore.addAll(ownerOP, elements, (useCache ? delegate.size() : -1));
        } catch (NucleusDataStoreException dse) {
          NucleusLogger.PERSISTENCE.warn(
              LOCALISER.msg("023013", "addAll", ownerMmd.getName(), dse));
          backingSuccess = false;
        }
      }
    }

    // Only make it dirty after adding the field 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(elements);

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return (backingStore != null ? backingSuccess : delegateSuccess);
  }
Beispiel #12
0
  /** Method to clear the Map. */
  public synchronized void clear() {
    makeDirty();
    delegate.clear();

    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(ownerOP)) {
        ownerOP
            .getExecutionContext()
            .addOperationToQueue(new MapClearOperation(ownerOP, backingStore));
      } else {
        backingStore.clear(ownerOP);
      }
    }

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
  }
  /**
   * Method to remove all elements from the collection from the LinkedHashSet.
   *
   * @param elements The collection of elements to remove
   * @return Whether it was removed ok.
   */
  public boolean removeAll(java.util.Collection elements) {
    boolean success = delegate.removeAll(elements);
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) {
      // Relationship management
      Iterator iter = elements.iterator();
      RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP);
      while (iter.hasNext()) {
        relMgr.relationRemove(ownerMmd.getAbsoluteFieldNumber(), iter.next());
      }
    }

    if (ownerOP != null && elements != null && !elements.isEmpty()) {
      // Cascade delete
      if (SCOUtils.useQueuedUpdate(ownerOP)) {
        // Queue the cascade delete
        Iterator iter = elements.iterator();
        while (iter.hasNext()) {
          ownerOP
              .getExecutionContext()
              .addOperationToQueue(
                  new CollectionRemoveOperation(
                      ownerOP, ownerMmd.getAbsoluteFieldNumber(), iter.next(), true));
        }
      } else if (SCOUtils.hasDependentElement(ownerMmd)) {
        // Perform the cascade delete
        Iterator iter = elements.iterator();
        while (iter.hasNext()) {
          ownerOP.getExecutionContext().deleteObjectInternal(iter.next());
        }
      }
    }

    if (success) {
      makeDirty();
      if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
      }
    }

    return success;
  }
Beispiel #14
0
  /**
   * Method to add an element to a position in the ArrayList.
   *
   * @param index The position
   * @param element The new element
   */
  public void add(int index, Object element) {
    // Reject inappropriate elements
    if (!allowNulls && element == null) {
      throw new NullPointerException(
          "Nulls not allowed for collection at field "
              + ownerMmd.getName()
              + " but element is null");
    }

    if (useCache) {
      loadFromStore();
    }

    if (backingStore != null) {
      if (SCOUtils.useQueuedUpdate(queued, ownerOP)) {
        ownerOP
            .getExecutionContext()
            .addOperationToQueue(new ListAddAtOperation(ownerOP, backingStore, index, element));
      } else {
        try {
          backingStore.add(ownerOP, element, 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();

    delegate.add(index, element);

    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
      ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
  }
  /**
   * Method to retain a Collection of elements (and remove all others).
   *
   * @param c The collection to retain
   * @return Whether they were retained successfully.
   */
  public boolean retainAll(java.util.Collection c) {
    if (c == null) {
      throw new NullPointerException("Input collection was null");
    }
    Collection collToRemove = new java.util.LinkedHashSet();
    for (Object o : delegate) {
      if (!c.contains(o)) {
        collToRemove.add(o);
      }
    }

    boolean success = delegate.retainAll(c);
    if (success) {
      makeDirty();
      if (SCOUtils.useQueuedUpdate(ownerOP)) {
        // Queue any cascade delete
        Iterator iter = collToRemove.iterator();
        while (iter.hasNext()) {
          ownerOP
              .getExecutionContext()
              .addOperationToQueue(
                  new CollectionRemoveOperation(
                      ownerOP, ownerMmd.getAbsoluteFieldNumber(), iter.next(), true));
        }
      } else if (SCOUtils.hasDependentElement(ownerMmd)) {
        // Perform the cascade delete
        Iterator iter = collToRemove.iterator();
        while (iter.hasNext()) {
          ownerOP.getExecutionContext().deleteObjectInternal(iter.next());
        }
      }

      if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
      }
    }
    return success;
  }
 /**
  * Method to add an element to the LinkedHashSet.
  *
  * @param element The new element
  * @return Whether it was added ok.
  */
 public boolean add(E element) {
   boolean success = delegate.add(element);
   if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) {
     // Relationship management
     ownerOP
         .getExecutionContext()
         .getRelationshipManager(ownerOP)
         .relationAdd(ownerMmd.getAbsoluteFieldNumber(), element);
   }
   if (success) {
     if (SCOUtils.useQueuedUpdate(ownerOP)) {
       ownerOP
           .getExecutionContext()
           .addOperationToQueue(
               new CollectionAddOperation(ownerOP, ownerMmd.getAbsoluteFieldNumber(), element));
     }
     makeDirty();
     if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
       ownerOP.getExecutionContext().processNontransactionalUpdate();
     }
   }
   return success;
 }
Beispiel #17
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);
  }
Beispiel #18
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);
      }
    }
  }
Beispiel #19
0
  /**
   * Method to initialise the SCO from an existing value.
   *
   * @param o The object to set from
   * @param forInsert Whether the object needs inserting in the datastore with this value
   * @param forUpdate Whether to update the datastore with this value
   */
  public void initialise(Object o, boolean forInsert, boolean forUpdate) {
    Collection c = (Collection) o;
    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 objSM = ec.findObjectProvider(pc);
          if (objSM == null) {
            objSM =
                ec.newObjectProviderForEmbedded(
                    pc, false, ownerOP, ownerMmd.getAbsoluteFieldNumber());
          }
        }
      }

      if (backingStore != null && useCache && !isCacheLoaded) {
        // Mark as loaded
        isCacheLoaded = true;
      }

      if (forInsert) {
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
          NucleusLogger.PERSISTENCE.debug(
              LOCALISER.msg(
                  "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size()));
        }

        if (useCache) {
          loadFromStore();
        }
        if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) {
          // Relationship management
          Iterator iter = c.iterator();
          RelationshipManager relMgr =
              ownerOP.getExecutionContext().getRelationshipManager(ownerOP);
          while (iter.hasNext()) {
            relMgr.relationAdd(ownerMmd.getAbsoluteFieldNumber(), iter.next());
          }
        }
        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 (forUpdate) {
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
          NucleusLogger.PERSISTENCE.debug(
              LOCALISER.msg(
                  "023008", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size()));
        }

        // Detect which objects are added and which are deleted
        if (useCache) {
          isCacheLoaded = false; // Mark as false since need to load the old collection
          loadFromStore();

          for (Object elem : c) {
            if (!delegate.contains(elem)) {
              add(elem);
            }
          }
          java.util.TreeSet delegateCopy = new java.util.TreeSet(delegate);
          for (Object elem : delegateCopy) {
            if (!c.contains(elem)) {
              remove(elem);
            }
          }
        } else {
          for (Object elem : c) {
            if (!contains(elem)) {
              add(elem);
            }
          }
          Iterator iter = iterator();
          while (iter.hasNext()) {
            Object elem = iter.next();
            if (!c.contains(elem)) {
              remove(elem);
            }
          }
        }
      } else {
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
          NucleusLogger.PERSISTENCE.debug(
              LOCALISER.msg(
                  "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size()));
        }
        delegate.clear();
        delegate.addAll(c);
      }
    }
  }
Beispiel #20
0
  /**
   * Method to initialise the SCO from an existing value.
   *
   * @param m The object to set from
   * @param forInsert Whether the object needs inserting in the datastore with this value
   * @param forUpdate Whether to update the datastore with this value
   */
  public synchronized void initialise(java.util.Map m, boolean forInsert, boolean forUpdate) {
    if (m != null) {
      // Check for the case of serialised maps, and assign ObjectProviders to any PC keys/values
      // without
      if (SCOUtils.mapHasSerialisedKeysAndValues(ownerMmd)
          && (ownerMmd.getMap().keyIsPersistent() || ownerMmd.getMap().valueIsPersistent())) {
        ExecutionContext ec = ownerOP.getExecutionContext();
        Iterator iter = m.entrySet().iterator();
        while (iter.hasNext()) {
          Map.Entry entry = (Map.Entry) iter.next();
          Object key = entry.getKey();
          Object value = entry.getValue();
          if (ownerMmd.getMap().keyIsPersistent()) {
            ObjectProvider objSM = ec.findObjectProvider(key);
            if (objSM == null) {
              objSM =
                  ec.getNucleusContext()
                      .getObjectProviderFactory()
                      .newForEmbedded(ec, key, false, ownerOP, ownerMmd.getAbsoluteFieldNumber());
            }
          }
          if (ownerMmd.getMap().valueIsPersistent()) {
            ObjectProvider objSM = ec.findObjectProvider(value);
            if (objSM == null) {
              objSM =
                  ec.getNucleusContext()
                      .getObjectProviderFactory()
                      .newForEmbedded(ec, value, false, ownerOP, ownerMmd.getAbsoluteFieldNumber());
            }
          }
        }
      }

      if (backingStore != null && useCache && !isCacheLoaded) {
        // Mark as loaded
        isCacheLoaded = true;
      }

      if (forInsert) {
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
          NucleusLogger.PERSISTENCE.debug(
              Localiser.msg(
                  "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + m.size()));
        }

        makeDirty();
        if (useCache) {
          // Make sure we have all values loaded (e.g if in optimistic tx and we put new entry)
          loadFromStore();
        }
        if (backingStore != null) {
          if (SCOUtils.useQueuedUpdate(ownerOP)) {
            Iterator iter = m.entrySet().iterator();
            while (iter.hasNext()) {
              Map.Entry entry = (Map.Entry) iter.next();
              ownerOP
                  .getExecutionContext()
                  .addOperationToQueue(
                      new MapPutOperation(ownerOP, backingStore, entry.getKey(), entry.getValue()));
            }
          } else {
            backingStore.putAll(ownerOP, m);
          }
        }
        delegate.putAll(m);
      } else if (forUpdate) {
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
          NucleusLogger.PERSISTENCE.debug(
              Localiser.msg(
                  "023008", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + m.size()));
        }

        // TODO This is clear+putAll. Improve it to work out what is changed
        delegate.clear();
        if (backingStore != null) {
          if (SCOUtils.useQueuedUpdate(ownerOP)) {
            // If not yet flushed to store then no need to add to queue (since will be handled via
            // insert)
            if (ownerOP.isFlushedToDatastore() || !ownerOP.getLifecycleState().isNew()) {
              ownerOP
                  .getExecutionContext()
                  .addOperationToQueue(new MapClearOperation(ownerOP, backingStore));
            }
          } else {
            backingStore.clear(ownerOP);
          }
        }
        if (useCache) {
          // Make sure we have all values loaded (e.g if in optimistic tx and we put new entry)
          loadFromStore();
        }

        if (backingStore != null) {
          if (SCOUtils.useQueuedUpdate(ownerOP)) {
            // If not yet flushed to store then no need to add to queue (since will be handled via
            // insert)
            if (ownerOP.isFlushedToDatastore() || !ownerOP.getLifecycleState().isNew()) {
              Iterator iter = m.entrySet().iterator();
              while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                ownerOP
                    .getExecutionContext()
                    .addOperationToQueue(
                        new MapPutOperation(
                            ownerOP, backingStore, entry.getKey(), entry.getValue()));
              }
            }
          } else {
            backingStore.putAll(ownerOP, m);
          }
        }
        delegate.putAll(m);
        makeDirty();
      } else {
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
          NucleusLogger.PERSISTENCE.debug(
              Localiser.msg(
                  "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + m.size()));
        }
        delegate.clear();
        delegate.putAll(m);
      }
    }
  }