public LDAPContext(SettingManager sm) {
    String prefix = "system/ldap";

    use = sm.getValueAsBool(prefix + "/use");
    host = sm.getValue(prefix + "/host");
    port = sm.getValueAsInt(prefix + "/port");
    defProfile = sm.getValue(prefix + "/defaultProfile");
    defGroup = sm.getValue(prefix + "/defaultGroup");
    baseDN = sm.getValue(prefix + "/distinguishedNames/base");
    usersDN = sm.getValue(prefix + "/distinguishedNames/users");
    nameAttr = sm.getValue(prefix + "/userAttribs/name");
    profileAttr = sm.getValue(prefix + "/userAttribs/profile");
    emailAttr = "mail"; // TODO make it configurable
    uidAttr = sm.getValue(prefix + "/uidAttr");
    groupAttr = sm.getValue(prefix + "/userAttribs/group");

    if (profileAttr.trim().length() == 0) profileAttr = null;

    if (groupAttr.trim().length() == 0) groupAttr = null;

    // --- init set of allowed profiles

    profiles.add("Hoofdeditor");
    profiles.add("Editor");
    profiles.add("RegisteredUser");
    profiles.add("UserAdmin");
    profiles.add("Monitor");
    profiles.add("Administrator");
  }
  public GeonetResult align(Set<RecordInfo> records) throws Exception {
    log.info("Start of alignment for : " + params.name);

    // -----------------------------------------------------------------------
    // --- retrieve all local categories and groups
    // --- retrieve harvested uuids for given harvesting node

    localCateg = new CategoryMapper(dbms);
    localGroups = new GroupMapper(dbms);
    localUuids = new UUIDMapper(dbms, params.uuid);
    dbms.commit();

    parseXSLFilter();

    // -----------------------------------------------------------------------
    // --- remove old metadata

    for (String uuid : localUuids.getUUIDs())
      if (!exists(records, uuid)) {
        String id = localUuids.getID(uuid);

        if (log.isDebugEnabled()) log.debug("  - Removing old metadata with id:" + id);
        dataMan.deleteMetadata(context, dbms, id);
        dbms.commit();
        result.locallyRemoved++;
      }

    // -----------------------------------------------------------------------
    // --- insert/update new metadata

    for (RecordInfo ri : records) {
      result.totalMetadata++;

      if (!dataMan.existsSchema(ri.schema)) {
        if (log.isDebugEnabled())
          log.debug(
              "  - Metadata skipped due to unknown schema. uuid:"
                  + ri.uuid
                  + ", schema:"
                  + ri.schema);
        result.unknownSchema++;
      } else {
        String id = dataMan.getMetadataId(dbms, ri.uuid);

        // look up value of localrating/enabled
        GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
        SettingManager settingManager = gc.getSettingManager();
        boolean localRating = settingManager.getValueAsBool("system/localrating/enabled", false);

        if (id == null) {
          addMetadata(ri, localRating);
        } else {
          updateMetadata(ri, id, localRating);
        }
      }
    }

    log.info("End of alignment for : " + params.name);

    return result;
  }
  /**
   * @param xml
   * @param fileIds
   * @param context
   * @param toIndex
   * @return
   * @throws Exception
   */
  private boolean insertTransaction(
      Element xml, List<String> fileIds, ServiceContext context, Set<String> toIndex)
      throws Exception {
    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    DataManager dataMan = gc.getDataManager();

    String schema = dataMan.autodetectSchema(xml);

    //		String category   = Util.getParam(request, Params.CATEGORY);
    String category = null, source = null, createDate = null, changeDate = null;

    String uuid;
    uuid = dataMan.extractUUID(schema, xml);
    if (uuid.length() == 0) uuid = UUID.randomUUID().toString();

    // -----------------------------------------------------------------------
    // --- insert metadata into the system

    UserSession us = context.getUserSession();

    if (us.getUserId() == null) throw new NoApplicableCodeEx("User not authenticated.");

    String profile = us.getProfile();

    // Only editors and above are allowed to insert metadata
    if (!profile.equals(Geonet.Profile.EDITOR)
        && !profile.equals(Geonet.Profile.REVIEWER)
        && !profile.equals(Geonet.Profile.USER_ADMIN)
        && !profile.equals(Geonet.Profile.ADMINISTRATOR))
      throw new NoApplicableCodeEx("User not allowed to insert metadata.");

    int userId = us.getUserIdAsInt();

    AccessManager am = gc.getAccessManager();
    Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB);

    // Set default group: user first group
    Set<String> userGroups = am.getVisibleGroups(dbms, userId);
    String group;
    if (userGroups.isEmpty()) {
      group = null;
    } else {
      group = (String) userGroups.iterator().next();
    }
    //
    // insert metadata
    //
    String docType = null, title = null, isTemplate = null;
    boolean ufo = true, indexImmediate = false;
    String id =
        dataMan.insertMetadata(
            context,
            dbms,
            schema,
            xml,
            context.getSerialFactory().getSerial(dbms, "Metadata"),
            uuid,
            userId,
            group,
            source,
            isTemplate,
            docType,
            title,
            category,
            createDate,
            changeDate,
            ufo,
            indexImmediate);

    if (id == null) return false;

    // Set metadata as public if setting enabled
    SettingManager sm = gc.getSettingManager();
    boolean metadataPublic = sm.getValueAsBool("system/csw/metadataPublic", false);

    if (metadataPublic) {
      dataMan.setOperation(context, dbms, id, "1", AccessManager.OPER_VIEW);
    }

    dataMan.indexMetadata(dbms, id);

    fileIds.add(uuid);

    dbms.commit();
    toIndex.add(id);
    return true;
  }