<commit_before>var ldap = require('ldapjs');
var path = require("path");
var fs = require('fs');
var _ = require('underscore');


function Employee(object, fake) {
  this.id = object.employeeNumber;
  this.username = object.uid;
  this.name = object.cn;

  // this.first_name = object.sn
  // this.last_name = object.givenName
  // this.name = object.sn + ', ' + object.givenName

  this.title = object.employeeType;

  this.email = object.mail;
  this.mobile = object.mobile || 'unknown';
  this.phone = object.telephoneNumber;

  this.location = object.l
  this.room = object.roomNumber;
  this.link = 'http://floor.suse.de/floor.cgi?login=' + this.uid

  if (fake) {
    this.pic = 'https://randomuser.me/api/portraits/thumb/men/' + this.username + '.jpg'
  } else {
    this.pic = 'http://floor.suse.de/gif.cgi/' + this.username
  }
};

var config = JSON.parse(fs.readFileSync('config.json', 'utf8'))
var ldap_server = config.ldap_server
var searchBase = 'ou=people,dc=suse,dc=de';

var client = ldap.createClient({
  url: ldap_server
});


exports.findAll = function (request, response) {
  console.log("*** findAll =>" + JSON.stringify(request.query))

  var search = request.query.search

  if(search) {
    exports._search(request, response)
  } else {
    exports._all(request, response)
  }
};


exports._all = function (request, response) {
  console.log("*** findAll =>" + JSON.stringify(request.query))

  var search = request.query.search
  var page = parseInt(request.query.page) || 0
  var limit = parseInt(request.query.limit) || 20

  var opts = {
    scope: 'sub',
    sizeLimit: limit * (page + 1)
  };

  if (search) {
    opts['filter'] = '(|(givenName=*' + search + '*)(sn=*' + search + '*)(uid=*' + search + '*)(mail=*' + search + '*))';
  } else {
    opts['filter'] = '(objectClass=person)';
  }

  console.log(opts)

  client.search(searchBase, opts, function (err, res) {
    var employees = [];
    var res_position = 0

    res.on('searchEntry', function (entry) {
      if (res_position >= page * limit) {
        if (employees.length < limit) {
          //console.log(entry.object)
          employees.push(new Employee(entry.object))
        }
      } else {
        res_position++;
      }
    });

    res.on('error', function (err) {
      console.log('Error: ' + err.message)
      response.send(employees);
    });

    res.on('end', function (result) {
      this.removeAllListeners('searchEntry');
      this.removeAllListeners('end');
      this.removeAllListeners('error');

      response.send(employees);
    });

  })
};


exports._search = function (request, response) {
  console.log("*** findAll =>" + JSON.stringify(request.query))

  var search = request.query.search
  var page = parseInt(request.query.page) || 0
  var limit = parseInt(request.query.limit) || 20

  var opts = {
    scope: 'sub',
    sizeLimit: limit * (page + 1)
  };

  if (search) {
    opts['filter'] = '(|(givenName=*' + search + '*)(sn=*' + search + '*)(uid=*' + search + '*)(mail=*' + search + '*))';
  } else {
    opts['filter'] = '(objectClass=person)';
  }

  console.log(opts)

  client.search(searchBase, opts, function (err, res) {
    var employees = [];
    var res_position = 0

    res.on('searchEntry', function (entry) {
      if (res_position >= page * limit) {
        if (employees.length < limit) {
          //console.log(entry.object)
          employees.push(new Employee(entry.object))
        }
      } else {
        res_position++;
      }
    });

    res.on('error', function (err) {
      console.log('Error: ' + err.message)
      response.send(_.sortBy(employees, 'name'));
    });

    res.on('end', function (result) {
      this.removeAllListeners('searchEntry');
      this.removeAllListeners('end');
      this.removeAllListeners('error');

      response.send(_.sortBy(employees, 'name'));
    });

  })
};

exports.findAllFaked = function (request, response) {
  console.log("*** findAllFaked =>" + JSON.stringify(request.query))
  var page = parseInt(request.query.page) || 0
  var limit = parseInt(request.query.limit) || 50

  console.log("page: " + page + ' limit: ' + limit)

  var employees = JSON.parse(fs.readFileSync('fixtures/_employees.json', 'utf8'))

  if (request.query.search) {
    employees = _.select(employees, function (employee) {
      return employee.uid == request.query.search;
    });
  } else {
    var start = page * limit;
    var end = start + limit;

    console.log("range start: " + start + ' range end: ' + end)

    employees = _.sortBy(employees, function (employee) {
      return employee.name<commit_msg>search method<commit_after>var ldap = require('ldapjs');
var path = require("path");
var fs = require('fs');
var _ = require('underscore');


function Employee(object, fake) {
  this.id = object.employeeNumber;
  this.username = object.uid;
  this.name = object.cn;

  // this.first_name = object.sn
  // this.last_name = object.givenName
  // this.name = object.sn + ', ' + object.givenName

  this.title = object.employeeType;

  this.email = object.mail;
  this.mobile = object.mobile || 'unknown';
  this.phone = object.telephoneNumber;

  this.location = object.l
  this.room = object.roomNumber;
  this.link = 'http://floor.suse.de/floor.cgi?login=' + this.uid

  if (fake) {
    this.pic = 'https://randomuser.me/api/portraits/thumb/men/' + this.username + '.jpg'
  } else {
    this.pic = 'http://floor.suse.de/gif.cgi/' + this.username
  }
};

var config = JSON.parse(fs.readFileSync('config.json', 'utf8'))
var ldap_server = config.ldap_server
var searchBase = 'ou=people,dc=suse,dc=de';

var client = ldap.createClient({
  url: ldap_server
});


exports.findAll = function (request, response) {
  console.log("*** findAll =>" + JSON.stringify(request.query))

  var search = request.query.search

  if(search) {
    exports._search(request, response)
  } else {
    exports._all(request, response)
  }
};


exports._all = function (request, response) {
  console.log("*** findAll =>" + JSON.stringify(request.query))

  var search = request.query.search
  var page = parseInt(request.query.page) || 0
  var limit = parseInt(request.query.limit) || 20

  var opts = {
    scope: 'sub',
    sizeLimit: limit * (page + 1)
  };

  if (search) {
    opts['filter'] = '(|(givenName=*' + search + '*)(sn=*' + search + '*)(uid=*' + search + '*)(mail=*' + search + '*))';
  } else {
    opts['filter'] = '(objectClass=person)';
  }

  console.log(opts)

  client.search(searchBase, opts, function (err, res) {
    var employees = [];
    var res_position = 0

    res.on('searchEntry', function (entry) {
      if (res_position >= page * limit) {
        if (employees.length < limit) {
          //console.log(entry.object)
          employees.push(new Employee(entry.object))
        }
      } else {
        res_position++;
      }
    });

    res.on('error', function (err) {
      console.log('Error: ' + err.message)
      response.send(employees);
    });

    res.on('end', function (result) {
      this.removeAllListeners('searchEntry');
      this.removeAllListeners('end');
      this.removeAllListeners('error');

      response.send(employees);
    });

  })
};


exports._search = function (request, response) {
  console.log("*** search =>" + JSON.stringify(request.query))

  var search = request.query.search
  var page = parseInt(request.query.page) || 0
  var limit = parseInt(request.query.limit) || 20

  var opts = {
    scope: 'sub',
    filter: '(|(givenName=*' + search + '*)(sn=*' + search + '*)(uid=*' + search + '*)(mail=*' + search + '*))'
  };

  console.log(opts)

  client.search(searchBase, opts, function (err, res) {
    var staff = [];

    res.on('searchEntry', function (entry) {
      console.log(entry.object)
      staff.push(entry.object)
    });

    res.on('end', function(result) {
      var employees = []
      var start = page * limit;
      var end = start + limit;
      console.log("range start: " + start + ' range end: ' + end)
      staff = _.sortBy(staff, 'cn').slice(start, end)
      for(i in staff) { employees.push(new Employee(staff[i])) }
      response.send(employees);
    });

  })

};

exports.findAllFaked = function (request, response) {
  console.log("*** findAllFaked =>" + JSON.stringify(request.query))
  var page = parseInt(request.query.page) || 0
  var limit = parseInt(request.query.limit) || 50

  console.log("page: " + page + ' limit: ' + limit)

  var employees = JSON.parse(fs.readFileSync('fixtures/_employees.json', 'utf8'))

  if (request.query.search) {
    employees = _.select(employees, function (employee) {
      return employee.uid == request.query.search;
    });
  } else {
    var start = page * limit;
    var end = start + limit;

    console.log("range start: " + start + ' range end: ' + end)

    employees = _.sortBy(employees, function (employee) {
      return employee.name
{'fraction_non_alphanumeric': 0.17445132245357345, 'fraction_numerical': 0.0031513787281935847, 'mean_word_length': 3.0873965041398344, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3667062', 'n_tokens_mistral': 3035, 'n_tokens_neox': 2907, 'n_words': 585}