public void removeSiteNodeReferences(
      Map<SiteNodeVO, RegistryVO> siteNodeRegistryPair,
      InfoGluePrincipal infoGluePrincipal,
      Database db)
      throws SystemException, Exception {
    SiteNodeVO siteNodeVO;
    RegistryVO registryVO;
    for (Map.Entry<SiteNodeVO, RegistryVO> pair : siteNodeRegistryPair.entrySet()) {
      siteNodeVO = pair.getKey();
      registryVO = pair.getValue();
      if (logger.isDebugEnabled()) {
        logger.debug(
            "About to clean SiteNode "
                + siteNodeVO.getContentVersionId()
                + " for references to: "
                + registryVO.getEntityName()
                + "<"
                + registryVO.getEntityId()
                + ">");
      }
      try {
        Integer metaInfoContentId = siteNodeVO.getMetaInfoContentId();
        LanguageVO masterLanguageVO =
            LanguageController.getController().getMasterLanguage(siteNodeVO.getRepositoryId(), db);
        String pageStructure =
            ContentController.getContentController()
                .getContentAttribute(
                    db, metaInfoContentId, masterLanguageVO.getId(), "ComponentStructure");

        if (registryVO.getReferenceType().equals(RegistryVO.PAGE_COMPONENT))
          pageStructure =
              deleteComponentFromXML(pageStructure, new Integer(registryVO.getEntityId()));
        if (registryVO.getReferenceType().equals(RegistryVO.PAGE_COMPONENT_BINDING))
          pageStructure =
              deleteComponentBindingFromXML(
                  pageStructure, new Integer(registryVO.getEntityId()), registryVO.getEntityName());

        ContentVersionVO contentVersionVO =
            ContentVersionController.getContentVersionController()
                .getLatestActiveContentVersionVO(metaInfoContentId, masterLanguageVO.getId(), db);
        ContentVersionController.getContentVersionController()
            .updateAttributeValue(
                contentVersionVO.getContentVersionId(),
                "ComponentStructure",
                pageStructure,
                infoGluePrincipal,
                db);
      } catch (Exception e) {
        logger.warn(
            "Could not remove reference on "
                + (siteNodeVO == null ? "null" : siteNodeVO.getName())
                + ": "
                + e.getMessage(),
            e);
      }
    }
  }
  private void addSiteNodeInconsistency(List inconsistencies, RegistryVO registryVO, Database db)
      throws Exception {
    try {
      String referencingEntityName = registryVO.getReferencingEntityName();
      String referencingEntityCompletingName = registryVO.getReferencingEntityCompletingName();
      Integer referencingEntityId = new Integer(registryVO.getReferencingEntityId());
      Integer referencingEntityCompletingId =
          new Integer(registryVO.getReferencingEntityCompletingId());

      if (referencingEntityCompletingName.equals(SiteNode.class.getName())) {
        SiteNodeVO siteNodeVO =
            SiteNodeController.getController()
                .getSiteNodeVOWithId(
                    new Integer(registryVO.getReferencingEntityCompletingId()), db);
        if (siteNodeVO != null) {
          LanguageVO masterLanguageVO =
              LanguageController.getController().getMasterLanguage(siteNodeVO.getRepositoryId());
          SiteNodeVersionVO siteNodeVersionVO =
              SiteNodeVersionController.getController()
                  .getLatestActiveSiteNodeVersionVO(db, siteNodeVO.getId());

          if (siteNodeVersionVO != null
              && siteNodeVersionVO.getId().intValue() == referencingEntityId.intValue())
            inconsistencies.add(registryVO);
        }
      } else if (referencingEntityCompletingName.equals(Content.class.getName())) {
        ContentVO contentVO =
            ContentController.getContentController()
                .getContentVOWithId(new Integer(registryVO.getReferencingEntityCompletingId()), db);
        if (contentVO != null) {
          LanguageVO masterLanguageVO =
              LanguageController.getController().getMasterLanguage(contentVO.getRepositoryId());
          ContentVersionVO contentVersionVO =
              ContentVersionController.getContentVersionController()
                  .getLatestActiveContentVersionVO(contentVO.getId(), masterLanguageVO.getId(), db);

          if (contentVersionVO != null
              && contentVersionVO.getId().intValue() == referencingEntityId.intValue())
            inconsistencies.add(registryVO);
        }
      } else {
        logger.error(
            "The registry contained a not supported referencingEntityCompletingName:"
                + referencingEntityCompletingName);
      }
    } catch (Exception e) {
      logger.error(
          "There seems to be a problem with finding the inconsistency for registryVO "
              + registryVO.getRegistryId()
              + ":"
              + e.getMessage());
    }
  }
  public List getAllInconsistencies() throws Exception {
    List inconsistencies = new ArrayList();

    Database db = CastorDatabaseService.getDatabase();

    try {
      beginTransaction(db);

      List registryVOList = RegistryController.getController().getRegistryVOList(db);
      if (registryVOList != null && registryVOList.size() > 0) {
        Iterator registryVOListIterator = registryVOList.iterator();
        while (registryVOListIterator.hasNext()) {
          RegistryVO registryVO = (RegistryVO) registryVOListIterator.next();
          if (registryVO.getEntityName().equals(Content.class.getName())) {
            try {
              ContentVO contentVO =
                  ContentController.getContentController()
                      .getContentVOWithId(new Integer(registryVO.getEntityId()), db);
              if (contentVO == null) addContentInconsistency(inconsistencies, registryVO, db);
              // inconsistencies.add(registryVO);
            } catch (Exception e) {
              addContentInconsistency(inconsistencies, registryVO, db);
              // inconsistencies.add(registryVO);
            }
          } else if (registryVO.getEntityName().equals(SiteNode.class.getName())) {
            try {
              SiteNodeVO siteNodeVO =
                  SiteNodeController.getController()
                      .getSiteNodeVOWithId(new Integer(registryVO.getEntityId()), db);
              if (siteNodeVO == null) addSiteNodeInconsistency(inconsistencies, registryVO, db);
              // inconsistencies.add(registryVO);
            } catch (Exception e) {
              addSiteNodeInconsistency(inconsistencies, registryVO, db);
              // inconsistencies.add(registryVO);
            }
          } else {
            logger.error(
                "The registry contained not supported entities:" + registryVO.getEntityName());
          }
        }
      }

      commitTransaction(db);
    } catch (Exception e) {
      rollbackTransaction(db);
      throw new SystemException(
          "An error occurred when we tried to fetch which sitenode uses a metainfo. Reason:"
              + e.getMessage(),
          e);
    }

    return inconsistencies;
  }
  /**
   * @deprecated This method handles clean up of ContentVersions poorly. Please refer to {@link
   *     #removeContentReferences(Map, InfoGluePrincipal, Database)} and {@link
   *     #removeSiteNodeReferences(Map, InfoGluePrincipal, Database)} instead.
   */
  public void removeReferences(Integer registryId, InfoGluePrincipal infoGluePrincipal, Database db)
      throws SystemException, Exception {
    RegistryVO registryVO = RegistryController.getController().getRegistryVOWithId(registryId, db);
    String referencingEntityName = registryVO.getReferencingEntityName();
    String referencingEntityCompletingName = registryVO.getReferencingEntityCompletingName();
    Integer referencingEntityId = new Integer(registryVO.getReferencingEntityId());

    if (referencingEntityCompletingName.equals(SiteNode.class.getName())) {
      SiteNodeVO siteNodeVO =
          SiteNodeController.getController()
              .getSiteNodeVOWithId(new Integer(registryVO.getReferencingEntityCompletingId()), db);
      if (siteNodeVO != null) {
        Integer metaInfoContentId = siteNodeVO.getMetaInfoContentId();
        LanguageVO masterLanguageVO =
            LanguageController.getController().getMasterLanguage(siteNodeVO.getRepositoryId(), db);
        String pageStructure =
            ContentController.getContentController()
                .getContentAttribute(
                    db, metaInfoContentId, masterLanguageVO.getId(), "ComponentStructure");

        if (registryVO.getReferenceType().equals(RegistryVO.PAGE_COMPONENT))
          pageStructure =
              deleteComponentFromXML(pageStructure, new Integer(registryVO.getEntityId()));
        if (registryVO.getReferenceType().equals(RegistryVO.PAGE_COMPONENT_BINDING))
          pageStructure =
              deleteComponentBindingFromXML(
                  pageStructure, new Integer(registryVO.getEntityId()), registryVO.getEntityName());

        ContentVersionVO contentVersionVO =
            ContentVersionController.getContentVersionController()
                .getLatestActiveContentVersionVO(metaInfoContentId, masterLanguageVO.getId(), db);
        ContentVersionController.getContentVersionController()
            .updateAttributeValue(
                contentVersionVO.getContentVersionId(),
                "ComponentStructure",
                pageStructure,
                infoGluePrincipal,
                db);
      }
    } else if (referencingEntityCompletingName.equals(Content.class.getName())) {
      if (referencingEntityName.equals(ContentVersion.class.getName())) {
        ContentVersionVO contentVersionVO =
            ContentVersionController.getContentVersionController()
                .getContentVersionVOWithId(referencingEntityId, db);
        if (contentVersionVO != null) {
          String versionValue = contentVersionVO.getVersionValue();

          if (registryVO.getReferenceType().equals(RegistryVO.INLINE_LINK))
            versionValue =
                deleteInlineLinks(versionValue, new Integer(registryVO.getEntityId()), db);
          if (registryVO.getReferenceType().equals(RegistryVO.INLINE_ASSET))
            versionValue = deleteInlineAssets(versionValue, new Integer(registryVO.getEntityId()));
          if (registryVO.getReferenceType().equals(RegistryVO.INLINE_SITE_NODE_RELATION))
            versionValue =
                deleteInlineSiteNodeRelations(versionValue, new Integer(registryVO.getEntityId()));
          if (registryVO.getReferenceType().equals(RegistryVO.INLINE_CONTENT_RELATION))
            versionValue =
                deleteInlineContentRelations(versionValue, new Integer(registryVO.getEntityId()));

          contentVersionVO.setVersionModifier(infoGluePrincipal.getName());
          contentVersionVO.setModifiedDateTime(DateHelper.getSecondPreciseDate());
          contentVersionVO.setVersionValue(versionValue);

          ContentVersionController.getContentVersionController()
              .update(
                  contentVersionVO.getContentVersionId(), contentVersionVO, infoGluePrincipal, db);
        }
      }
    } else {
      logger.error(
          "The registry contained a not supported referencingEntityCompletingName:"
              + referencingEntityCompletingName);
    }
  }
  public void removeContentReferences(
      Map<ContentVersionVO, RegistryVO> contentVersionRegistryPair,
      InfoGluePrincipal infoGluePrincipal,
      Database db)
      throws SystemException, Exception {
    ContentVersionVO contentVersionVO;
    RegistryVO registryVO;
    for (Map.Entry<ContentVersionVO, RegistryVO> pair : contentVersionRegistryPair.entrySet()) {
      contentVersionVO = pair.getKey();
      registryVO = pair.getValue();
      if (logger.isDebugEnabled()) {
        logger.debug(
            "About to clean ContentVersion "
                + contentVersionVO.getContentVersionId()
                + " for references to: "
                + registryVO.getEntityName()
                + "<"
                + registryVO.getEntityId()
                + ">");
      }
      String versionValue = contentVersionVO.getVersionValue();

      if (registryVO.getReferenceType().equals(RegistryVO.INLINE_LINK))
        versionValue = deleteInlineLinks(versionValue, new Integer(registryVO.getEntityId()), db);
      if (registryVO.getReferenceType().equals(RegistryVO.INLINE_ASSET))
        versionValue = deleteInlineAssets(versionValue, new Integer(registryVO.getEntityId()));
      if (registryVO.getReferenceType().equals(RegistryVO.INLINE_SITE_NODE_RELATION))
        versionValue =
            deleteInlineSiteNodeRelations(versionValue, new Integer(registryVO.getEntityId()));
      if (registryVO.getReferenceType().equals(RegistryVO.INLINE_CONTENT_RELATION))
        versionValue =
            deleteInlineContentRelations(versionValue, new Integer(registryVO.getEntityId()));

      contentVersionVO.setVersionModifier(infoGluePrincipal.getName());
      contentVersionVO.setModifiedDateTime(DateHelper.getSecondPreciseDate());
      contentVersionVO.setVersionValue(versionValue);

      // ContentVersionController.getContentVersionController().update(contentVersionVO.getContentId(), contentVersionVO.getLanguageId(), contentVersionVO, db);
      ContentVersionController.getContentVersionController()
          .update(
              contentVersionVO.getContentId(),
              contentVersionVO.getLanguageId(),
              contentVersionVO,
              infoGluePrincipal,
              true,
              db,
              true);
    }
  }