private ExportInfosMapping getExportInfosMapping(ChromatticSession session) {
    ExportInfosMapping exportInfosMapping =
        session.findByPath(ExportInfosMapping.class, ExportInfosMapping.NODE_NAME);
    if (exportInfosMapping == null) {
      exportInfosMapping = session.insert(ExportInfosMapping.class, ExportInfosMapping.NODE_NAME);
      exportInfosCount = 0;
    }

    return exportInfosMapping;
  }
  public void testOneToManyReference() {
    ChromatticSession session = login();
    WebSite site = session.insert(WebSite.class, "site");
    Content content1 = session.create(Content.class, "1");
    Content content2 = session.create(Content.class, "2");
    site.getContents().add(content1);
    Page root = session.create(Page.class);
    site.setRootPage(root);

    //
    root.setContent(content1);
  }
  public ExportInfo getExportInfo(long exportTime) {
    ChromatticSession session = persister.getSession();

    ExportInfoMapping eim = session.findByPath(ExportInfoMapping.class, getPathFor(exportTime));

    try {
      if (eim != null) {
        return eim.toModel(null, null);
      } else {
        return null;
      }
    } finally {
      persister.closeSession(false);
    }
  }
  @Override
  protected ConsumerSPI internalSaveChangesTo(Consumer consumer) throws RegistrationException {
    ConsumerSPI consumerSPI = super.internalSaveChangesTo(consumer);

    ChromatticSession session = persister.getSession();
    try {
      ConsumerMapping cm = session.findById(ConsumerMapping.class, consumer.getPersistentKey());
      cm.initFrom(consumer);
      persister.closeSession(true);
    } catch (Exception e) {
      persister.closeSession(false);
      throw new RegistrationException(e);
    }

    return consumerSPI;
  }
  protected RegistrationSPI internalSaveChangesTo(Registration registration)
      throws RegistrationException {
    RegistrationSPI registrationSPI = super.internalSaveChangesTo(registration);

    ChromatticSession session = persister.getSession();
    try {
      RegistrationMapping cm =
          session.findById(RegistrationMapping.class, registration.getPersistentKey());
      cm.initFrom(registration);
      persister.closeSession(true);
    } catch (Exception e) {
      persister.closeSession(false);
      throw new RegistrationException(e);
    }

    return registrationSPI;
  }
  @Override
  protected RegistrationSPI internalCreateRegistration(
      ConsumerSPI consumer, Map registrationProperties) throws RegistrationException {
    ChromatticSession session = persister.getSession();
    RegistrationSPI registration = null;
    try {
      ConsumerMapping cm = session.findById(ConsumerMapping.class, consumer.getPersistentKey());
      RegistrationMapping rm = cm.createAndAddRegistrationMappingFrom(null);
      registration = newRegistrationSPI(consumer, registrationProperties, rm.getPersistentKey());
      rm.initFrom(registration);
      persister.closeSession(true);
    } catch (Exception e) {
      persister.closeSession(false);
      throw new RegistrationException(e);
    }

    return registration;
  }
  public JCRRegistrationPersistenceManager(ExoContainer container) throws Exception {
    persister = new JCRPersister(container, JCRPersister.WSRP_WORKSPACE_NAME);

    List<Class> mappingClasses = new ArrayList<Class>(6);
    Collections.addAll(
        mappingClasses,
        ConsumersAndGroupsMapping.class,
        ConsumerMapping.class,
        ConsumerGroupMapping.class,
        RegistrationMapping.class,
        ConsumerCapabilitiesMapping.class,
        RegistrationPropertiesMapping.class);

    persister.initializeBuilderFor(mappingClasses);

    //      persister = NewJCRPersister.getInstance(container);

    ChromatticSession session = persister.getSession();
    mappings =
        session.findByPath(ConsumersAndGroupsMapping.class, ConsumersAndGroupsMapping.NODE_NAME);
    if (mappings == null) {
      mappings =
          session.insert(ConsumersAndGroupsMapping.class, ConsumersAndGroupsMapping.NODE_NAME);
    }
    persister.save(); // needed right now as the session must still be open to iterate over nodes

    for (ConsumerGroupMapping cgm : mappings.getConsumerGroups()) {
      internalAddConsumerGroup(cgm.toConsumerGroup(this));
    }

    for (ConsumerMapping cm : mappings.getConsumers()) {
      ConsumerSPI consumer = cm.toConsumer(this);
      internalAddConsumer(consumer);

      // get the registrations and add them to local map.
      for (Registration registration : consumer.getRegistrations()) {
        internalAddRegistration((RegistrationSPI) registration);
      }
    }

    persister.closeSession(false);
  }
  @Override
  protected ConsumerGroupSPI internalCreateConsumerGroup(String name) throws RegistrationException {
    ConsumerGroupSPI group = super.internalCreateConsumerGroup(name);

    ChromatticSession session = persister.getSession();
    mappings =
        session.findByPath(ConsumersAndGroupsMapping.class, ConsumersAndGroupsMapping.NODE_NAME);
    try {
      ConsumerGroupMapping cgm = mappings.createConsumerGroup(name);
      mappings.getConsumerGroups().add(cgm);
      group.setPersistentKey(cgm.getPersistentKey());
      cgm.initFrom(group);
      persister.closeSession(true);
    } catch (Exception e) {
      persister.closeSession(false);
      throw new RegistrationException(e);
    }

    return group;
  }
  public void add(ExportInfo info) {
    ChromatticSession session = persister.getSession();

    ExportInfoMapping eim = session.findByPath(ExportInfoMapping.class, getChildPath(info));
    long exportTime = info.getExportTime();
    if (eim != null) {
      persister.closeSession(false);
      throw new IllegalArgumentException(
          "An ExportInfo with export time " + exportTime + " already exists!");
    } else {
      ExportInfosMapping exportInfosMapping = getExportInfosMapping(session);
      String exportTimeAsString = "" + exportTime;
      ExportInfoMapping exportInfo = exportInfosMapping.createExportInfo(exportTimeAsString);
      session.persist(exportInfosMapping, exportInfo, exportTimeAsString);
      exportInfo.initFrom(info);

      persister.closeSession(true);
      exportInfosCount++;
    }
  }
  @Override
  protected ConsumerSPI internalCreateConsumer(String consumerId, String consumerName)
      throws RegistrationException {
    ConsumerSPI consumer = super.internalCreateConsumer(consumerId, consumerName);

    ChromatticSession session = persister.getSession();
    mappings =
        session.findByPath(ConsumersAndGroupsMapping.class, ConsumersAndGroupsMapping.NODE_NAME);
    try {
      ConsumerMapping cm = mappings.createConsumer(consumerId);
      mappings.getConsumers().add(cm);
      cm.initFrom(consumer);
      consumer.setPersistentKey(cm.getPersistentKey());
      persister.closeSession(true);
    } catch (Exception e) {
      persister.closeSession(false);
      throw new RegistrationException(e);
    }

    return consumer;
  }
 private void remove(String id, Class clazz) {
   ChromatticSession session = persister.getSession();
   session.remove(session.findById(clazz, id));
   persister.closeSession(true);
 }