@Before
 public void setUp() throws Exception {
   // Now we have a role that can edit roles, we can edit this role to include more privileges
   final RoleData role = roleAccessSession.findRole(this.getClass().getSimpleName());
   final List<AccessRuleData> accessRules = new ArrayList<AccessRuleData>();
   accessRules.add(
       new AccessRuleData(
           role.getRoleName(), StandardRules.CAADD.resource(), AccessRuleState.RULE_ACCEPT, true));
   accessRules.add(
       new AccessRuleData(
           role.getRoleName(),
           StandardRules.CAEDIT.resource(),
           AccessRuleState.RULE_ACCEPT,
           true));
   accessRules.add(
       new AccessRuleData(
           role.getRoleName(),
           StandardRules.CAREMOVE.resource(),
           AccessRuleState.RULE_ACCEPT,
           true));
   accessRules.add(
       new AccessRuleData(
           role.getRoleName(),
           StandardRules.CAACCESSBASE.resource(),
           AccessRuleState.RULE_ACCEPT,
           true));
   accessRules.add(
       new AccessRuleData(
           role.getRoleName(),
           StandardRules.CREATECRL.resource(),
           AccessRuleState.RULE_ACCEPT,
           true));
   accessRules.add(
       new AccessRuleData(
           role.getRoleName(),
           StandardRules.CREATECERT.resource(),
           AccessRuleState.RULE_ACCEPT,
           true));
   roleManagementSession.addAccessRulesToRole(alwaysAllowToken, role, accessRules);
   // Remove any lingering testca before starting the tests
   caSession.removeCA(alwaysAllowToken, X509CADN.hashCode());
   // Now add the test CA so it is available in the tests
   caSession.addCA(alwaysAllowToken, testx509ca);
 }
  public boolean writeNextChunk(final List<T> entities) {
    boolean addedSomething = false;
    boolean rollBack = false;
    /*//final EntityTransaction transaction = entityManager.getTransaction();
    if (entityManager.getTransaction().isActive()) {
        LOG.error("Transaction was already active.");
    } else {*/
    if (entities.size() > 0) {
      EntityTransaction transaction = entityManager.getTransaction();
      transaction.begin();

      try {
        for (final T entity : entities) {
          // LOG.debug("About to persist a " + entity.getClass().getSimpleName());

          if (RoleData.class.equals(entity.getClass())) {
            final RoleData roleData = ((RoleData) entity);
            final Set<Integer> accessRulesPrimaryKeys = roleData.getAccessRules().keySet();
            final Set<Integer> accessUserAspectPrimaryKeys = roleData.getAccessUsers().keySet();
            final Map<Integer, AccessRuleData> accessRuleMap =
                new HashMap<Integer, AccessRuleData>();
            for (int primaryKey : accessRulesPrimaryKeys) {
              AccessRuleData accessRuleData = entityManager.find(AccessRuleData.class, primaryKey);
              if (accessRuleData == null) {
                accessRuleData = roleData.getAccessRules().get(primaryKey);
                entityManager.persist(accessRuleData);
              }
              accessRuleMap.put(primaryKey, accessRuleData);
            }
            final Map<Integer, AccessUserAspectData> accessUserAspectMap =
                new HashMap<Integer, AccessUserAspectData>();
            for (int primaryKey : accessUserAspectPrimaryKeys) {
              AccessUserAspectData accessUserAspectData =
                  entityManager.find(AccessUserAspectData.class, primaryKey);
              if (accessUserAspectData == null) {
                accessUserAspectData = roleData.getAccessUsers().get(primaryKey);
                entityManager.persist(accessUserAspectData);
              }
              accessUserAspectMap.put(primaryKey, accessUserAspectData);
            }
            // roleData.setAccessRules(new HashMap<Integer,AccessRuleData>());
            // roleData.setAccessUsers(new HashMap<Integer,AccessUserAspectData>());
            roleData.setAccessRules(accessRuleMap);
            roleData.setAccessUsers(accessUserAspectMap);
            entityManager.persist(entity);
            // roleData.setAccessRules(accessRuleMap);
            // roleData.setAccessUsers(accessUserAspectMap);
            // entityManager.merge(entity);
          } else {
            entityManager.persist(entity);
          }
          addedSomething = true;
        }
      } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        rollBack = true;
      } finally {
        if (rollBack) {
          transaction.rollback();
        } else {
          transaction.commit();
          totalRowCount += entities.size();
        }
      }
      // }
      entityManager.clear(); // Detach all entities to free up some memory.
    }
    return addedSomething;
  }