Search in sources :

Example 1 with ParseUserNameException

use of cz.metacentrum.perun.core.api.exceptions.ParseUserNameException in project perun by CESNET.

the class Utils method parseCommonName.

/**
 * Try to parse rawName to keys: "titleBefore" "firstName" "lastName" "titleAfter"
 *
 * If rawName is null or empty, return map with empty values of all keys.
 *
 * Parsing procedure:
 * 1] prepare list of parts by replacing all characters "," and "_" by spaces
 * 2] change all sequence of invisible characters (space, tabulator etc.) to one space
 * 3] one by one try to parsing parts from the list
 *  - A] try to find all titleBefore parts
 *  - B] try to find one firstName part
 *  - C] try to find all lastName parts
 *  - D] if the rest is not lastName so save it to the title after
 *
 * Example of parsing rawName:
 * 1] rawName = "Mgr. et Mgr.    Petr_Jiri R. Sojka, Ph.D., CSc."
 * 2] convert all ',' and '_' to spaces: rawName = "Mgr. et Mgr.    Petr Jiri R. Sojka  Ph.D.  CSc."
 * 3] convert more than 1 invisible char to 1 space: rawName = "Mgr. et Mgr. Petr Jiri R. Sojka Ph.D. CSc."
 * 4] parse string to list of parts by space: ListOfParts= ["Mgr.","et","Mgr.","Petr","Jiri","R.","Sojka","Ph.D.","CSc."]
 * 5] first fill everything what can be in title before: titleBefore="Mgr. et Mgr."
 * 6] then fill everything what can be in first name (maximum 1 part): firstName="Petr"
 * 7] then fill everything what can be in last name: lastName="Jiri R. Sojka"
 * 8] everything else put to the title after: titleAfter="Ph.D. CSc."
 * 9] put these variables to map like key=value, for ex.: Map[titleBefore="Mgr. et Mgr.",firstName="Petr", ... ] and return this map
 *
 * @param rawName name to parse
 * @param fullNameRequired if true, throw exception if firstName or lastName is missing, do not throw exception otherwise
 * @return map string to string where are 4 keys (titleBefore,titleAfter,firstName and lastName) with their values (value can be null)
 * @throws ParseUserNameException when method was unable to parse both first name and last name from the rawName
 */
public static Map<String, String> parseCommonName(String rawName, boolean fullNameRequired) {
    // prepare variables and result map
    Map<String, String> parsedName = new HashMap<>();
    String titleBefore = "";
    String firstName = "";
    String lastName = "";
    String titleAfter = "";
    if (rawName != null && !rawName.isEmpty()) {
        // replace all ',' and '_' characters for ' ' for rawName
        rawName = rawName.replaceAll("[,_]", " ");
        // replace all invisible chars in row for ' '
        rawName = rawName.replaceAll("\\s+", " ").trim();
        // split parts by space
        List<String> nameParts = new ArrayList<>(Arrays.asList(rawName.split(" ")));
        // if length of nameParts is 1, save it to the lastName
        if (nameParts.size() == 1) {
            lastName = nameParts.get(0);
        // if length of nameParts is more than 1, try to choose which part belong to which value
        } else {
            // join title before name to single string with ' ' as delimiter
            titleBefore = parsePartOfName(nameParts, new StringJoiner(" "), titleBeforePattern);
            // get first name as a next name part if pattern matches and nameParts are not empty
            if (!nameParts.isEmpty())
                firstName = parsePartOfName(nameParts, new StringJoiner(" "), firstNamePattern);
            // join last names to single string with ' ' as delimiter
            if (!nameParts.isEmpty())
                lastName = parsePartOfName(nameParts, new StringJoiner(" "), lastNamePattern);
            // if any nameParts are left join them to one string with ' ' as delimiter and assume they are titles after name
            if (!nameParts.isEmpty()) {
                StringJoiner titleAfterBuilder = new StringJoiner(" ");
                for (String namePart : nameParts) {
                    titleAfterBuilder.add(namePart);
                }
                titleAfter = titleAfterBuilder.toString();
            }
        }
    }
    // add variables to map, empty string means null
    if (titleBefore.isEmpty())
        titleBefore = null;
    parsedName.put(TITLE_BEFORE, titleBefore);
    if (firstName.isEmpty())
        firstName = null;
    parsedName.put(FIRST_NAME, firstName);
    if (lastName.isEmpty())
        lastName = null;
    parsedName.put(LAST_NAME, lastName);
    if (titleAfter.isEmpty())
        titleAfter = null;
    parsedName.put(TITLE_AFTER, titleAfter);
    if (fullNameRequired) {
        if (parsedName.get(FIRST_NAME) == null)
            throw new ParseUserNameException("Unable to parse first name from text.", rawName);
        if (parsedName.get(LAST_NAME) == null)
            throw new ParseUserNameException("Unable to parse last name from text.", rawName);
    }
    return parsedName;
}
Also used : ParseUserNameException(cz.metacentrum.perun.core.api.exceptions.ParseUserNameException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StringJoiner(java.util.StringJoiner)

Aggregations

ParseUserNameException (cz.metacentrum.perun.core.api.exceptions.ParseUserNameException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 StringJoiner (java.util.StringJoiner)1