private RegistryObjectType getLatestVersionOfRegistryObject(RegistryObjectType ro)
      throws RegistryException {
    RegistryObjectType latestRO = null;

    // Call in case versions have not been initialized yet.
    getAllRegistryObjectVersions(ro);

    if (versions.size() == 0) {
      return null;
    }

    String latestVersion = null;
    for (Iterator it = versions.iterator(); it.hasNext(); ) {
      if (latestRO == null) {
        latestRO = (RegistryObjectType) it.next();
        latestVersion = latestRO.getVersionInfo().getVersionName();
        continue;
      }

      RegistryObjectType next = (RegistryObjectType) it.next();
      String nextVersion = next.getVersionInfo().getVersionName();

      if (compareVersions(nextVersion, latestVersion) > 0) {
        latestRO = next;
        latestVersion = latestRO.getVersionInfo().getVersionName();
      }
    }

    return latestRO;
  }
  public void checkRegistryObjectLidOnNewObject(RegistryObjectType ro) throws RegistryException {
    String id = ro.getId();
    String lid = ro.getLid();

    // Object does not exists already
    if (lid == null) {
      ro.setLid(ro.getId());
    }
  }
  // TODO: Consider replacing versions with idToVersionsMap as a performance optimization in future.
  private boolean isIdInVersions(String id) {
    RegistryObjectType ro = null;
    boolean foundId = false;

    if (versions != null) {
      Iterator iter = versions.iterator();

      while (iter.hasNext()) {
        ro = (RegistryObjectType) iter.next();

        if (id.equals(ro.getId())) {
          foundId = true;
          break;
        }
      }
    }

    return (foundId);
  }
  public void checkRegistryObjectLid(RegistryObjectType ro) throws RegistryException {
    String id = ro.getId();
    String lid = ro.getLid();

    String existingObjectLid = (String) context.getIdToLidMap().get(id);

    // Assign lid if not specified, validate lid if specified
    if (existingObjectLid != null) {
      if (lid == null) {
        ro.setLid(existingObjectLid);
      } else {
        // Validate that lid matches existing objects lid
        if (!lid.equals(existingObjectLid)) {
          throw new RegistryException(
              ServerResourceBundle.getInstance()
                  .getString("message.idDoesNotMatch", new Object[] {lid, existingObjectLid, id}));
        }
      }
    } else {
      checkRegistryObjectLidOnNewObject(ro);
    }
  }
  private boolean isVersionableClass(RegistryObjectType ro) {
    boolean isVersionable = false;

    // System.err.println("isVersionable entered. ro=" + ro.getClass().getName());
    Iterator iter = versionableClassNameSet.iterator();
    while (iter.hasNext()) {
      Class clazz = (Class) iter.next();
      // System.err.println("    isVersionable clazz=" + clazz.getName() + " isAssignableFrom = " +
      // clazz.isAssignableFrom(ro.getClass()));
      if (clazz.isAssignableFrom(ro.getClass())) {
        isVersionable = true;
        break;
      }
    }
    return isVersionable;
  }
  private List getAllRegistryObjectVersions(RegistryObjectType ro) throws RegistryException {
    if (versions == null) {
      ServerRequestContext queryContext = null;

      // Note: ORDER BY versionName DESC is not safe because String(1.10) < String(1.9)
      String query =
          "SELECT ro.* FROM "
              + Utility.getInstance().mapTableName(ro)
              + " ro WHERE ro.lid = '"
              + ro.getLid()
              + "'";

      try {
        AdhocQueryRequest queryRequest = bu.createAdhocQueryRequest(query);
        queryContext =
            new ServerRequestContext("VersionProcessor.getAllRegistryObjectVersions", queryRequest);

        queryContext.setUser(ac.registryOperator);

        AdhocQueryResponseType queryResp = qm.submitAdhocQuery(queryContext);
        versions = queryResp.getRegistryObjectList().getIdentifiable();
        queryContext.commit();
        queryContext = null;
      } catch (JAXBException e) {
        throw (new RegistryException(e));
      } catch (JAXRException e) {
        throw (new RegistryException(e));
      } finally {
        if (queryContext != null) {
          queryContext.rollback();
        }
      }
    }

    return (versions);
  }
  public boolean needToVersionRegistryObject(RegistryObjectType ro) throws RegistryException {
    boolean needToVersion = true;

    BindingUtility bu = BindingUtility.getInstance();
    boolean newObject = false;

    try {
      needToVersion = isVersionableClass(ro);

      if (needToVersion) {
        HashMap slotsMap;
        // Honour dontVersion flag if specified on request
        if (!context.getRegistryRequestStack().empty()) {
          slotsMap = bu.getSlotsFromRequest(context.getCurrentRegistryRequest());
          if (slotsMap.containsKey(bu.CANONICAL_SLOT_LCM_DONT_VERSION)) {
            String val = (String) slotsMap.get(bu.CANONICAL_SLOT_LCM_DONT_VERSION);
            if (val.trim().equalsIgnoreCase("true")) {
              needToVersion = false;
            }
          }
        }

        // Honour dontVersion flag if specified on ro
        slotsMap = bu.getSlotsFromRegistryObject(ro);
        if (slotsMap.containsKey(bu.CANONICAL_SLOT_LCM_DONT_VERSION)) {
          String val = (String) slotsMap.get(bu.CANONICAL_SLOT_LCM_DONT_VERSION);
          if (val.trim().equalsIgnoreCase("true")) {
            needToVersion = false;
          }
        }
      }

      // TODO:
      // Need to investigate case where not versioning and it is a new object.
      // Need unit test for this case.
      if (needToVersion) {
        versions = getAllRegistryObjectVersions(ro);

        if (versions.size()
            == 0) { // If there are any existing versions (ie. ro's with same LID) then we need to
          // version
          // This is a new ro and therefor need not be versioned
          needToVersion = false;
          newObject = true;
        }
      }

      // Must set versionName to match latest versionName if existing object
      // or set to version 1.1 if new object.
      if (!needToVersion) {
        RegistryObjectType lastVersion = getLatestVersionOfRegistryObject(ro);
        String versionName = null;
        if (lastVersion == null) {
          versionName = "1.1";
        } else {
          versionName = lastVersion.getVersionInfo().getVersionName();

          // Must bump up versionName for new objects
          if (newObject) {
            versionName = nextVersion(versionName);
          }
        }

        VersionInfoType versionInfo = ro.getVersionInfo();
        if (versionInfo == null) {
          versionInfo = bu.rimFac.createVersionInfoType();
          ro.setVersionInfo(versionInfo);
        }
        versionInfo.setVersionName(versionName);
        if (!context.getRegistryRequestStack().empty()) {
          setVersionInfoComment(versionInfo);
        }
      }
    } catch (JAXBException e) {
      throw new RegistryException(e);
    }

    return needToVersion;
  }
  public RegistryObjectType createRegistryObjectVersion(RegistryObjectType ro)
      throws RegistryException {
    RegistryObjectType roNew = null;

    try {
      Utility util = Utility.getInstance();

      RegistryObjectType lastVersion = getLatestVersionOfRegistryObject(ro);
      String nextVersion = null;
      if (lastVersion == null) {
        nextVersion = "1.1";
      } else {
        nextVersion = nextVersion(lastVersion.getVersionInfo().getVersionName());
      }

      roNew = bu.cloneRegistryObject(ro);
      VersionInfoType nextVersionInfo = bu.rimFac.createVersionInfoType();
      nextVersionInfo.setVersionName(nextVersion);

      // Set the comment from the request comment (per the spec)
      if (!context.getRegistryRequestStack().empty()) {
        nextVersionInfo.setComment(context.getCurrentRegistryRequest().getComment());
      }

      roNew.setVersionInfo(nextVersionInfo);

      // A new version must have a unique id
      String id = ro.getId();
      String lid = ro.getLid();
      String idNew = id;

      // Only change id if it already exists in versions
      // Need to preserve client supplied id in the case where lid is an
      // existing lid but id is new
      if (isIdInVersions(id)) {
        // Make id of next version be lid with ":nextVersion" as suffix, if this id is already in
        // use in a version
        idNew = lid + ":" + nextVersion; // Utility.getInstance().createId();
        roNew.setId(idNew);

        // Add entry to idMap so old id and refs to it are mapped to idNew
        context.getIdMap().put(id, idNew);
      }

      // Add entry to context.newROVersionMap for later replacement
      context.getNewROVersionMap().put(ro, roNew);

      // Assign new ids to all composed RegistryObjects within roNew
      Set composedObjects = bu.getComposedRegistryObjects(roNew, -1);

      Iterator iter = composedObjects.iterator();
      while (iter.hasNext()) {
        RegistryObjectType composedObject = (RegistryObjectType) iter.next();

        // check for composed object if exist change the id and lid and
        // also update the idMap.
        if (objectExists(composedObject.getId())) {
          String oldId = composedObject.getId();
          String newId = oldId + ":" + nextVersion;
          composedObject.setId(newId);
          composedObject.setLid(newId);
          context.getIdMap().put(oldId, newId);
        }

        String composedId = composedObject.getId();
        String composedLid = composedObject.getLid();
        String composedIdNew = composedId;

        if (!util.isValidRegistryId(composedId)) { // Replace the id if it's not a valid ID already
          composedIdNew = util.createId();

          composedObject.setId(composedIdNew);

          // Add entry to idMap so old composedId and refs to it are mapped to composedIdNew
          context.getIdMap().put(composedId, composedIdNew);
        }

        if (composedLid == null || composedLid.trim().length() == 0) {
          composedObject.setLid(composedIdNew);
        }
        // Set the parent id of this composed object to point to the new parent
        bu.setParentIdForComposedObject(composedObject, idNew);
      }
    } catch (JAXRException e) {
      throw new RegistryException(e);
    } catch (JAXBException e) {
      throw new RegistryException(e);
    }
    return roNew;
  }