<commit_before>/*
 * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.gluu.oxtrust.ldap.service;

import static org.gluu.oxtrust.ldap.service.AppInitializer.LDAP_ENTRY_MANAGER_NAME;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;

import org.gluu.oxtrust.model.GluuCustomAttribute;
import org.gluu.oxtrust.model.GluuCustomPerson;
import org.gluu.oxtrust.model.GluuGroup;
import org.gluu.oxtrust.model.User;
import org.gluu.oxtrust.util.OxTrustConstants;
import org.gluu.site.ldap.exception.DuplicateEntryException;
import org.gluu.site.ldap.persistence.AttributeData;
import org.gluu.site.ldap.persistence.LdapEntryManager;
import org.slf4j.Logger;
import org.xdi.config.oxtrust.AppConfiguration;
import org.xdi.util.ArrayHelper;
import org.xdi.util.INumGenerator;
import org.xdi.util.StringHelper;

import com.unboundid.ldap.sdk.Filter;

/**
 * Provides operations with persons
 *
 * @author <NAME> Date: 10.13.2010
 */
@Stateless
@Named
public class PersonService implements Serializable, IPersonService {

    private static final long serialVersionUID = 6685720517520443399L;

    @Inject
    private Logger log;

    @Inject
    private LdapEntryManager ldapEntryManager;

    @Inject
    private AttributeService attributeService;

    @Inject
    private AppConfiguration appConfiguration;

    @Inject
    private OrganizationService organizationService;

    private List<GluuCustomAttribute> mandatoryAttributes;

    /* (non-Javadoc)
     * @see org.gluu.oxtrust.ldap.service.IPersonService#addCustomObjectClass(org.gluu.oxtrust.model.GluuCustomPerson)
     */
    @Override
    public void addCustomObjectClass(GluuCustomPerson person) {
        String customObjectClass = attributeService.getCustomOrigin();
        String[] customObjectClassesArray = person.getCustomObjectClasses();
        if (ArrayHelper.isNotEmpty(customObjectClassesArray)) {
            List<String> customObjectClassesList = Arrays.asList(customObjectClassesArray);
            if (!customObjectClassesList.contains(customObjectClass)) {
                List<String> customObjectClassesListUpdated = new ArrayList<String>();
                customObjectClassesListUpdated.addAll(customObjectClassesList);
                customObjectClassesListUpdated.add(customObjectClass);
                customObjectClassesList = customObjectClassesListUpdated;
            }

            person.setCustomObjectClasses(customObjectClassesList.toArray(new String[0]));

        } else {
            person.setCustomObjectClasses(new String[] { customObjectClass });
        }
    }

    /* (non-Javadoc)
     * @see org.gluu.oxtrust.ldap.service.IPersonService#addPerson(org.gluu.oxtrust.model.GluuCustomPerson)
     */
    // TODO: Review this methods. We need to check if uid is unique in outside
    // method
    @Override
    public void addPerson(GluuCustomPerson person) throws Exception {
        GluuCustomPerson uidPerson = new GluuCustomPerson();
        uidPerson.setUid(person.getUid());
        List<GluuCustomPerson> persons = findPersons(uidPerson, 1);
        if (persons == null || persons.size() == 0) {
            person.setCreationDate(new Date());
            ldapEntryManager.persist(person);
        } else {
            throw new DuplicateEntryException("Duplicate UID value: " + person.getUid());
        }
    }

    /* (non-Javadoc)
     * @see org.gluu.oxtrust.ldap.service.IPersonService#updatePerson(org.gluu.oxtrust.model.GluuCustomPerson)
     */
    @Override
    public void updatePerson(GluuCustomPerson person) {
        person.setUpdatedAt(new Date());
        ldapEntryManager.merge(person);
    }

    /* (non-Javadoc)
     * @see org.gluu.oxtrust.ldap.service.IPersonService#removePerson(org.gluu.oxtrust.model.GluuCustomPerson)
     */
    @Override
    public void removePerson(GluuCustomPerson person) {
        // Remove person
        ldapEntryManager.removeWithSubtree(person.getDn());
    }

    /* (non-Javadoc)
     * @see org.gluu.oxtrust.ldap.service.IPersonService#searchPersons(java.lang.String, int)
     */
    @Override
    public List<GluuCustomPerson> searchPersons(String pattern, int sizeLimit) {
        String[] targetArray = new String[] { pattern };
        Filter uidFilter = Filter.createSubstringFilter(OxTrustConstants.uid, null, targetArray, null);
        Filter mailFilter = Filter.createSubstringFilter(OxTrustConstants.mail, null, targetArray, null);
        Filter nameFilter = Filter.createSubstringFilter(OxTrustConstants.displayName, null, targetArray, null);
        Filter inameFilter = Filter.createSubstringFilter(OxTrustConstants.iname, null, targetArray, null);
        Filter searchFilter = Filter.createORFilter(uidFilter, mailFilter, nameFilter, inameFilter);

        List<GluuCustomPerson> result = ldapEntryManager.findEntries(getDnForPerson(null), GluuCustomPerson.class, searchFilter, 0, sizeLimit);

        return result;
    }

    /* (non-Javadoc)
     * @see org.gluu.oxtrust.ldap.service.IPersonService#searchPersons(java.lang.String)
     */
    @Override
    public List<GluuCustomPerson> searchPersons(String pattern) {
        String[] targetArray = new String[] { pattern };
        Filter uidFilter = Filter.createSubstringFilter(OxTrustConstants.uid, null, targetArray, null);
        Filter mailFilter = Filter.createSubstringFilter(OxTrustConstants.mail, null, targetArray, null);
        Filter nameFilter = Filter.createSubstringFilter(OxTrustConstants.displayName, null, targetArray, null);
        Filter inameFilter = Filter.createSubstringFilter(OxTrustConstants.iname, null, targetArray, null);
        Filter ppidFilter = Filter.createSubstringFilter(OxTrustConstants.iname, null, targetArray, null);
        Filter inumFilter = Filter.createSubstringFilter(OxTrustConstants.inum, null, targetArray, null);
        Filter searchFilter = Filter.createORFilter(uidFilter, mailFilter, nameFilter, inameFilter, ppidFilter, inumFilter);

        List<GluuCustomPerson> result = ldapEntryManager.findEntries(getDnForPerson(null), GluuCustomPerson.class, searchFilter, 0);

        return result;
    }<commit_msg>oxTrust/issues/#824<commit_after>/*
 * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.gluu.oxtrust.ldap.service;

import static org.gluu.oxtrust.ldap.service.AppInitializer.LDAP_ENTRY_MANAGER_NAME;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;

import org.gluu.oxtrust.model.GluuCustomAttribute;
import org.gluu.oxtrust.model.GluuCustomPerson;
import org.gluu.oxtrust.model.GluuGroup;
import org.gluu.oxtrust.model.User;
import org.gluu.oxtrust.util.OxTrustConstants;
import org.gluu.site.ldap.exception.DuplicateEntryException;
import org.gluu.site.ldap.persistence.AttributeData;
import org.gluu.site.ldap.persistence.LdapEntryManager;
import org.slf4j.Logger;
import org.xdi.config.oxtrust.AppConfiguration;
import org.xdi.util.ArrayHelper;
import org.xdi.util.INumGenerator;
import org.xdi.util.StringHelper;

import com.unboundid.ldap.sdk.Filter;

/**
 * Provides operations with persons
 *
 * @author <NAME> Date: 10.13.2010
 */
@Stateless
@Named
public class PersonService implements Serializable, IPersonService {

    private static final long serialVersionUID = 6685720517520443399L;

    @Inject
    private Logger log;

    @Inject
    private LdapEntryManager ldapEntryManager;

    @Inject
    private AttributeService attributeService;

    @Inject
    private AppConfiguration appConfiguration;

    @Inject
    private OrganizationService organizationService;

    private List<GluuCustomAttribute> mandatoryAttributes;

    /* (non-Javadoc)
     * @see org.gluu.oxtrust.ldap.service.IPersonService#addCustomObjectClass(org.gluu.oxtrust.model.GluuCustomPerson)
     */
    @Override
    public void addCustomObjectClass(GluuCustomPerson person) {
        String customObjectClass = attributeService.getCustomOrigin();
        String[] customObjectClassesArray = person.getCustomObjectClasses();
        if (ArrayHelper.isNotEmpty(customObjectClassesArray)) {
            List<String> customObjectClassesList = Arrays.asList(customObjectClassesArray);
            if (!customObjectClassesList.contains(customObjectClass)) {
                List<String> customObjectClassesListUpdated = new ArrayList<String>();
                customObjectClassesListUpdated.addAll(customObjectClassesList);
                customObjectClassesListUpdated.add(customObjectClass);
                customObjectClassesList = customObjectClassesListUpdated;
            }

            person.setCustomObjectClasses(customObjectClassesList.toArray(new String[0]));

        } else {
            person.setCustomObjectClasses(new String[] { customObjectClass });
        }
    }

    /* (non-Javadoc)
     * @see org.gluu.oxtrust.ldap.service.IPersonService#addPerson(org.gluu.oxtrust.model.GluuCustomPerson)
     */
    // TODO: Review this methods. We need to check if uid is unique in outside
    // method
    @Override
    public void addPerson(GluuCustomPerson person) throws Exception {
        GluuCustomPerson uidPerson = new GluuCustomPerson();
        uidPerson.setUid(person.getUid());
        List<GluuCustomPerson> persons = findPersons(uidPerson, 1);
        if (persons == null || persons.size() == 0) {
            person.setCreationDate(new Date());
            ldapEntryManager.persist(person);
        } else {
            throw new DuplicateEntryException("Duplicate UID value: " + person.getUid());
        }
    }

    /* (non-Javadoc)
     * @see org.gluu.oxtrust.ldap.service.IPersonService#updatePerson(org.gluu.oxtrust.model.GluuCustomPerson)
     */
    @Override
    public void updatePerson(GluuCustomPerson person) {
        person.setUpdatedAt(new Date());
        ldapEntryManager.merge(person);
    }

    /* (non-Javadoc)
     * @see org.gluu.oxtrust.ldap.service.IPersonService#removePerson(org.gluu.oxtrust.model.GluuCustomPerson)
     */
    @Override
    public void removePerson(GluuCustomPerson person) {
        // Remove person
        ldapEntryManager.removeWithSubtree(person.getDn());
    }

    /* (non-Javadoc)
     * @see org.gluu.oxtrust.ldap.service.IPersonService#searchPersons(java.lang.String, int)
     */
    @Override
    public List<GluuCustomPerson> searchPersons(String pattern, int sizeLimit) {
        String[] targetArray = new String[] { pattern };
        Filter uidFilter = Filter.createSubstringFilter(OxTrustConstants.uid, null, targetArray, null);
        Filter mailFilter = Filter.createSubstringFilter(OxTrustConstants.mail, null, targetArray, null);
        Filter nameFilter = Filter.createSubstringFilter(OxTrustConstants.displayName, null, targetArray, null);
        Filter inameFilter = Filter.createSubstringFilter(OxTrustConstants.iname, null, targetArray, null);
        Filter searchFilter = Filter.createORFilter(uidFilter, mailFilter, nameFilter, inameFilter);

        List<GluuCustomPerson> result = ldapEntryManager.findEntries(getDnForPerson(null), GluuCustomPerson.class, searchFilter, 0, sizeLimit);

        return result;
    }

    /* (non-Javadoc)
     * @see org.gluu.oxtrust.ldap.service.IPersonService#searchPersons(java.lang.String)
     */
    @Override
    public List<GluuCustomPerson> searchPersons(String pattern) {
        String[] targetArray = new String[] { pattern };
        Filter uidFilter = Filter.createSubstringFilter(OxTrustConstants.uid, null, targetArray, null);
        Filter mailFilter = Filter.createSubstringFilter(OxTrustConstants.mail, null, targetArray, null);
        Filter nameFilter = Filter.createSubstringFilter(OxTrustConstants.displayName, null, targetArray, null);
        Filter inameFilter = Filter.createSubstringFilter(OxTrustConstants.iname, null, targetArray, null);
        Filter ppidFilter = Filter.createSubstringFilter(OxTrustConstants.ppid, null, targetArray, null);
        Filter inumFilter = Filter.createSubstringFilter(OxTrustConstants.inum, null, targetArray, null);
        Filter searchFilter = Filter.createORFilter(uidFilter, mailFilter, nameFilter, inameFilter, ppidFilter, inumFilter);

        List<GluuCustomPerson> result = ldapEntryManager.findEntries(getDnForPerson(null), GluuCustomPerson.class, searchFilter, 0);

        return result;
    }
{'fraction_non_alphanumeric': 0.09778984102365258, 'fraction_numerical': 0.006591702210158977, 'mean_word_length': 3.8756143667296787, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, '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': '6744709', 'n_tokens_mistral': 3863, 'n_tokens_neox': 3719, 'n_words': 688}