/**
   * Convenience method to load fields from the datastore. Note that if the fieldNumbers is
   * null/empty we still should call the persistence handler since it may mean that the version
   * field needs loading.
   *
   * @param fieldNumbers The field numbers.
   */
  protected void loadFieldsFromDatastore(int[] fieldNumbers) {
    if (myLC.isNew() && myLC.isPersistent() && !isFlushedNew()) {
      // Not yet flushed new persistent object to datastore so no point in "loading"
      return;
    }

    if ((flags & FLAG_NEED_INHERITANCE_VALIDATION)
        != 0) // TODO Merge this into fetch object handler
    {
      String className =
          getStoreManager().getClassNameForObjectID(myID, myEC.getClassLoaderResolver(), myEC);
      if (!getObject().getClass().getName().equals(className)) {
        myEC.removeObjectFromLevel1Cache(myID);
        myEC.removeObjectFromLevel2Cache(myID);
        throw new NucleusObjectNotFoundException(
            "Object with id "
                + myID
                + " was created without validating of type "
                + getObject().getClass().getName()
                + " but is actually of type "
                + className);
      }
      flags &= ~FLAG_NEED_INHERITANCE_VALIDATION;
    }

    // TODO If the field has "loadFetchGroup" defined, then add it to the fetch plan etc
    getStoreManager().getPersistenceHandler().fetchObject(this, fieldNumbers);
  }
  /**
   * Use this in place of valueOf.
   *
   * @param value real value
   * @return LifeCycleState corresponding to the value
   */
  public static LifeCycleState fromValue(String value) {
    if (value == null || "".equals(value)) {
      throw new IllegalArgumentException("Value cannot be null or empty!");
    }

    for (LifeCycleState enumEntry : LifeCycleState.values()) {
      if (enumEntry.toString().equals(value)) {
        return enumEntry;
      }
    }

    throw new IllegalArgumentException("Cannot create enum from " + value + " value!");
  }
 /**
  * Method invoked just before a transaction starts for the ExecutionContext managing us.
  *
  * @param tx The transaction
  */
 public void preBegin(org.datanucleus.Transaction tx) {
   preStateChange();
   try {
     myLC = myLC.transitionBegin(this, tx);
   } finally {
     postStateChange();
   }
 }
 /** Method to change the object state to nontransactional. */
 public void makeNontransactional() {
   preStateChange();
   try {
     myLC = myLC.transitionMakeNontransactional(this);
   } finally {
     postStateChange();
   }
 }
 /**
  * Method to retrieve the object.
  *
  * @param fgOnly Only load the current fetch group fields
  */
 public void retrieve(boolean fgOnly) {
   preStateChange();
   try {
     myLC = myLC.transitionRetrieve(this, fgOnly);
   } finally {
     postStateChange();
   }
 }
 /** Method to refresh the object. */
 public void refresh() {
   preStateChange();
   try {
     myLC = myLC.transitionRefresh(this);
   } finally {
     postStateChange();
   }
 }
  public static PlanStatus fromJsonObject(PlanResultKey planResultKey, JSONObject jsonObject)
      throws JSONException {
    final String state = jsonObject.getString("state");
    final String lifecycle = jsonObject.getString("lifeCycleState");

    final BuildState buildState = BuildState.getInstance(state);
    final LifeCycleState lifeCycleState = LifeCycleState.getInstance(lifecycle);

    return new PlanStatus(planResultKey, buildState, lifeCycleState, true);
  }
 /**
  * This method is invoked just after a commit is performed in a Transaction involving the
  * persistable object managed by this StateManager
  *
  * @param tx The transaction
  */
 public void postCommit(org.datanucleus.Transaction tx) {
   preStateChange();
   try {
     myLC = myLC.transitionCommit(this, tx);
     if (transactionalVersion != myVersion) {
       myVersion = transactionalVersion;
     }
     this.lockMode = LockManager.LOCK_MODE_NONE;
   } finally {
     postStateChange();
   }
 }
 /**
  * This method is invoked just before a rollback is performed in a Transaction involving the
  * persistable object managed by this StateManager.
  *
  * @param tx The transaction
  */
 public void preRollback(org.datanucleus.Transaction tx) {
   preStateChange();
   try {
     myEC.clearDirty(this);
     myLC = myLC.transitionRollback(this, tx);
     if (transactionalVersion != myVersion) {
       transactionalVersion = myVersion;
     }
     this.lockMode = LockManager.LOCK_MODE_NONE;
   } finally {
     postStateChange();
   }
 }
 /** Makes Transactional Transient instances persistent. */
 public void makePersistentTransactionalTransient() {
   preStateChange();
   try {
     if (myLC.isTransactional && !myLC.isPersistent) {
       // make the transient instance persistent in the datastore, if is transactional and
       // !persistent
       makePersistent();
       myLC = myLC.transitionMakePersistent(this);
     }
   } finally {
     postStateChange();
   }
 }
  /**
   * Convenience method to retrieve field values from an L2 cached object if they are loaded in that
   * object. If the object is not in the L2 cache then just returns, and similarly if the required
   * fields aren't available.
   *
   * @param fieldNumbers Numbers of fields to load from the L2 cache
   * @return The fields that couldn't be loaded
   */
  protected int[] loadFieldsFromLevel2Cache(int[] fieldNumbers) {
    // Only continue if there are fields, and not being deleted/flushed etc
    if (fieldNumbers == null
        || fieldNumbers.length == 0
        || myEC.isFlushing()
        || myLC.isDeleted()
        || isDeleting()
        || getExecutionContext().getTransaction().isCommitting()) {
      return fieldNumbers;
    }
    // TODO Drop this check when we're confident that this doesn't affect some use-cases
    if (!myEC.getNucleusContext()
        .getConfiguration()
        .getBooleanProperty(PropertyNames.PROPERTY_CACHE_L2_LOADFIELDS, true)) {
      return fieldNumbers;
    }

    Level2Cache l2cache = myEC.getNucleusContext().getLevel2Cache();
    if (l2cache != null && myEC.getNucleusContext().isClassCacheable(cmd)) {
      CachedPC<T> cachedPC = l2cache.get(myID);
      if (cachedPC != null) {
        int[] cacheFieldsToLoad =
            ClassUtils.getFlagsSetTo(cachedPC.getLoadedFields(), fieldNumbers, true);
        if (cacheFieldsToLoad != null && cacheFieldsToLoad.length > 0) {
          if (NucleusLogger.CACHE.isDebugEnabled()) {
            NucleusLogger.CACHE.debug(
                Localiser.msg(
                    "026034",
                    StringUtils.toJVMIDString(getObject()),
                    myID,
                    StringUtils.intArrayToString(cacheFieldsToLoad)));
          }

          L2CacheRetrieveFieldManager l2RetFM = new L2CacheRetrieveFieldManager(this, cachedPC);
          this.replaceFields(cacheFieldsToLoad, l2RetFM);
          int[] fieldsNotLoaded = l2RetFM.getFieldsNotLoaded();
          if (fieldsNotLoaded != null) {
            for (int i = 0; i < fieldsNotLoaded.length; i++) {
              loadedFields[fieldsNotLoaded[i]] = false;
            }
          }
        }
      }
    }

    return ClassUtils.getFlagsSetTo(loadedFields, fieldNumbers, false);
  }
  /** Method to change the object state to write-field. */
  protected void transitionWriteField() {
    try {
      if (myEC.getMultithreaded()) {
        myEC.getLock().lock();
        lock.lock();
      }

      preStateChange();
      try {
        myLC = myLC.transitionWriteField(this);
      } finally {
        postStateChange();
      }
    } finally {
      if (myEC.getMultithreaded()) {
        lock.unlock();
        myEC.getLock().unlock();
      }
    }
  }
  /** Method to change the object state to evicted. */
  public void evict() {
    if (myLC != myEC.getNucleusContext().getApiAdapter().getLifeCycleState(LifeCycleState.P_CLEAN)
        && myLC
            != myEC.getNucleusContext()
                .getApiAdapter()
                .getLifeCycleState(LifeCycleState.P_NONTRANS)) {
      return;
    }

    preStateChange();
    try {
      try {
        getCallbackHandler().preClear(getObject());

        getCallbackHandler().postClear(getObject());
      } finally {
        myLC = myLC.transitionEvict(this);
      }
    } finally {
      postStateChange();
    }
  }
  /**
   * Method to change the object state to read-field.
   *
   * @param isLoaded if the field was previously loaded
   */
  protected void transitionReadField(boolean isLoaded) {
    try {
      if (myEC.getMultithreaded()) {
        myEC.getLock().lock();
        lock.lock();
      }

      if (myLC == null) {
        return;
      }
      preStateChange();
      try {
        myLC = myLC.transitionReadField(this, isLoaded);
      } finally {
        postStateChange();
      }
    } finally {
      if (myEC.getMultithreaded()) {
        lock.unlock();
        myEC.getLock().unlock();
      }
    }
  }
 /**
  * Accessor for whether the instance is newly persistent yet hasnt yet been flushed to the
  * datastore.
  *
  * @return Whether not yet flushed to the datastore
  */
 public boolean isWaitingToBeFlushedToDatastore() {
   // Return true if object is new and not yet flushed to datastore
   return myLC.stateType() == LifeCycleState.P_NEW && !isFlushedNew();
 }