All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.kth.iss.ug2.Ug2Schema Maven / Gradle / Ivy

/*
 * MIT License
 *
 * Copyright (c) 2017 Kungliga Tekniska högskolan
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package se.kth.iss.ug2;

import java.util.HashMap;

import static se.kth.iss.ug2.Ug2Protocol.*;

public class Ug2Schema {

    public Ug2Schema(Ug2ClassInformation... classArray) {
        this.classes = new HashMap<>();
        for (Ug2ClassInformation classInfo : classArray) {
            this.classes.put(classInfo.name(), classInfo);
        }
    }

    public Ug2Schema() {
        this.classes = new HashMap<>();
    }

    public Ug2ClassInformation[] classes() {
        Ug2ClassInformation[] classArray = new Ug2ClassInformation[classes.size()];
        classes.values().toArray(classArray);
        return classArray;
    }

    /**
     * Checks if a class contains an attribute.
     *
     * @param className     the class.
     * @param attributeName the attribute.
     * @return true if the class contains the attribute, false otherwise.
     */
    public boolean hasAttribute(String className, String attributeName) {
        Ug2ClassInformation classInfo = classes.get(className);
        return classInfo != null && classInfo.hasAttribute(attributeName);
    }

    /**
     * Gets attribute information for an attribute belonging to a given class.
     *
     * @param className     the class that contains the attribute.
     * @param attributeName the name of the attribute.
     * @return the attribute information.
     * @throws Ug2Exception if the class doesn't have the given attribute or if
     *                      the schema doesn't contain the class.
     */
    public Ug2AttributeInformation getAttribute(String className, String attributeName)
            throws Ug2Exception {
        Ug2ClassInformation ci = getClassInformation(className);
        return ci.getAttributeInformation(attributeName);
    }

    /**
     * Gets class information for a given class.
     *
     * @param className the class.
     * @return the class information.
     * @throws Ug2Exception if the schema doesn't contain the class.
     */
    public Ug2ClassInformation getClassInformation(String className)
            throws Ug2Exception {
        Ug2ClassInformation classInformation = classes.get(className);
        if (classInformation == null) {
            throw new Ug2Exception("No such class in schema: " + className, STATUS_NOTFOUND);
        }
        return classInformation;
    }

    /**
     * Checks if the schema contains a class.
     *
     * @param className the class to check.
     * @return true if the schema contains the class, false otherwise.
     */
    public boolean hasClass(String className) {
        return classes.containsKey(className);
    }

    public String[] getClassNames() {
        String[] result = new String[classes.size()];
        classes.keySet().toArray(result);
        return result;
    }

    /**
     * Gets the attribute names of a given class.
     *
     * @param ug2class the class that contains the attributes.
     * @return the names of the attributes.
     * @throws Ug2Exception if the schema doesn't contain the given class.
     */
    public String[] getAttributeNames(String ug2class)
            throws Ug2Exception {
        Ug2ClassInformation ci = getClassInformation(ug2class);
        return ci.getAttributeNames();
    }

    public String label_en(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).label_en();
    }

    public String label_sv(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).label_sv();
    }

    public String description(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).description();
    }

    public boolean uniq(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).uniq();
    }

    public boolean multival(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).multival();
    }

    public boolean notnull(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).notnull();
    }

    public String domain(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).domain();
    }

    public String domainChars(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).domainChars();
    }

    public String domainClass(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).domainClass();
    }

    public Long domainMin(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).domainMin();
    }

    public Long domainMax(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).domainMax();
    }

    public boolean autoCreate(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).autoCreate();
    }

    public boolean autoDelete(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).autoDelete();
    }

    public boolean special(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).special();
    }

    public boolean pseudo(String ug2class, String attribute)
            throws Ug2Exception {
        return getAttribute(ug2class, attribute).pseudo();
    }

    /**
     * Checks if an attribute belonging to a given class is case insensitive.
     *
     * @param ug2class  the class.
     * @param attribute the attribute.
     * @return true if the attribute is case insensitive, false otherwise.
     * @throws Ug2Exception if the schema doesn't contain the given class or the
     *                      class doesn't have the given attribute.
     */
    public boolean caseInsensitive(String ug2class, String attribute) throws Ug2Exception {
        return getAttribute(ug2class, attribute).caseInsensitive();
    }

    /**
     * Returns the reference schema for this version of UG. This schema is used to
     * verify the database at server startup. It is also used to create a new database
     * and for (other) testing purposes.
     * 

* When a client connects to a server the reference schema and the schema returned * by the server may be different even if the client and the server is running the * same version. * * @return the reference schema for this version of UG */ public static Ug2Schema referenceSchema() { Ug2Schema schema = new Ug2Schema(); schema.addAttribute("handle", "kthid", "KTH-ID", "KTH-ID").makeUniq().makeMandatory().setDomain("handle").makeSpecial(); schema.addAttribute("system", "kthid", "KTH-ID", "KTH-ID").makeUniq().makeMandatory().setDomain("regexp").setDomainChars("^[a-z0-9]+$").setMin(2L).makeSpecial(); schema.addAttribute("system", "description", "Description", "Beskrivning"); schema.addAttribute("system", "ssha_hash", "Password hash", "Hash av l\u00f6senord").makeMandatory().makeSpecial(); schema.addAttribute("system", "otp_ssha_hash", "One time password hash", "Hash av eng\u00e5ngsl\u00f6senord").makeMultival().makeSpecial(); schema.addAttribute("system", "cas_proxy_url", "CAS proxy ticket URL", "CAS proxy ticket URL"); schema.addAttribute("user", "kthid", "KTH-ID", "KTH-ID").makeUniq().makeMandatory().setDomain("uname").makeSpecial(); schema.addAttribute("user", "ssha_hash", "Password hash", "Hash av l\u00f6senord").makeMandatory().makeSpecial(); schema.addAttribute("user", "operator_ssha_hash", "Operator password hash", "Hash av operat\u00f6rsl\u00f6senord").makeSpecial(); schema.addAttribute("user", "password", "Password", "L\u00f6senord").makeMandatory().makeSpecial(); schema.addAttribute("user", "birthdate", "Date of birth", "F\u00f6delsedatum").setDomain("birthdate"); schema.addAttribute("user", "phone_hr", "Phone number (HR+)", "Telefonnummer (HR+)").setDomain("regexp").setDomainChars("^0\\d+$").setMin(8L); schema.addAttribute("user", "fax", "Fax number (HR+)", "Faxnummer (HR+)"); schema.addAttribute("user", "address_ladok", "Temporary address (Ladok)", "Tillf\u00e4llig adress (Ladok)"); schema.addAttribute("user", "address_spar", "Official address (Spar/Ladok)", "Folkbokf\u00f6ringsadress (Spar/Ladok)"); schema.addAttribute("user", "phone_ladok", "Phone number (Ladok)", "Telefonnummer Ladok)"); schema.addAttribute("user", "sms_ladok", "SMS (Ladok)", "SMS (Ladok)"); schema.addAttribute("user", "visiting_address", "Visiting address (HR+)", "Bes\u00f6ksadress (HR+)"); schema.addAttribute("user", "anm_telekatalog_hr", "Note for phone directory (HR+)", "Anm\u00e4rkning i telekatalog (HR+)"); schema.addAttribute("user", "lang", "Language", "Spr\u00e5k"); schema.addAttribute("user", "primary_email", "Primary email address", "Prim\u00e4r e-postadress").makeUniq().makeSpecial(); schema.addAttribute("user", "email_address", "Email address (HR+)", "E-postadress (HR+)"); schema.addAttribute("user", "email_forward", "Email forwarding address", "Efters\u00e4ndingsadress (e-post)"); schema.addAttribute("user", "email_quota", "Email storage quota [MB]", "Lagringsutrymme (e-post) [MB]").setDomain("integer"); schema.addAttribute("user", "email_verifier", "Email verifier", "Verifikationkod (e-post)"); schema.addAttribute("user", "family_name", "Family name", "Efternamn").makeMandatory(); schema.addAttribute("user", "given_name", "Given name", "F\u00f6rnamn").makeMandatory(); schema.addAttribute("user", "phone", "Phone number (HVD)", "Telefonnummer (HVD)").makeMultival().setDomain("regexp").setDomainChars("^0\\d+$").setMin(8L); schema.addAttribute("user", "email_address_old", " Old email addresses", "Gamla e-postadresser").makeMultival(); schema.addAttribute("user", "handle_old", "Old usernames", "Gamla anv\u00e4ndarnamn").makeMultival(); schema.addAttribute("user", "publish", "Publishing consent", "Publicering medgiven").makeMultival(); schema.addAttribute("user", "title_en", "Title in english (HR+)", "Titel p\u00e5 engelska (HR+)").makeMultival(); schema.addAttribute("user", "title_sv", "Title in swedish (HR+)", "Titel p\u00e5 svenska (HR+)").makeMultival(); schema.addAttribute("user", "handle_forbidden", "Forbidden usernames", "F\u00f6rbjudna anv\u00e4ndarnamn").makeMultival().setDomainClass("handle").makeAuto().makeSpecial(); schema.addAttribute("user", "handle_pre", "Preallocated usernames", "F\u00f6rallokerade anv\u00e4ndarnamn").makeMultival().setDomainClass("handle").makeAuto().makeSpecial(); schema.addAttribute("user", "handle_want", "Wanted usernames", "\u00d6nskat anv\u00e4ndarnamn").makeMultival().setDomainClass("handle").makeAuto().makeSpecial(); schema.addAttribute("user", "uid", "Unix UID", "Unix UID").makeUniq().setDomain("integer"); schema.addAttribute("user", ATTR_PERSONNUMMER, "Personnummer", "Personnummer").makeUniq().setDomain("personnummer").setMin(25L).makeSpecial(); schema.addAttribute("user", "personnummer_search", "Personnummer aliases", "Alternativa personnummer").makeUniq().makeMultival().setDomain("personnummer").setMin(5L).makeSpecial(); schema.addAttribute("user", ATTR_KTHID_SEARCH, "KTH-ID aliases", "Alternativa KTH-ID").makeUniq().makeMultival().makeMandatory().setDomain("uname").setDomainClass("user").makeSpecial(); schema.addAttribute("user", "autoname", "Original username", "Ursprungligt anv\u00e4ndarnamn").makeUniq().makeMandatory().setDomain("username").makeSpecial(); schema.addAttribute("user", "username", "Username", "Anv\u00e4ndarnamn").makeUniq().makeMandatory().setDomainClass("handle").makeAuto().makeSpecial(); schema.addAttribute("user", "alias", "Aliases", "Alias").makeUniq().makeMultival().setDomainClass("handle").makeAuto().makeSpecial(); schema.addAttribute("user", "passport", "Passport number", "Passnummer").makeUniq().makeMultival(); schema.addAttribute("user", "otp_ssha_hash", "One time password hash", "Hash av eng\u00e5ngsl\u00f6senord").makeMultival().makeSpecial(); schema.addAttribute("user", "affiliation", "Affiliations", "Anknytning (till KTH)").makeMultival(); schema.addAttribute("user", "primary_affiliation", "Primary affiliation", "Huvudanknytning (till KTH)"); schema.addAttribute("user", "tags", "Tags", "Tags").makeMultival().setDomain("regexp").setDomainChars("^[a-z][a-z0-9.]*$"); schema.addAttribute("user", "services", "Services", "Tj\u00e4nster").makeMultival().setDomain("selection"); schema.addAttribute("user", "disk_quota", "Disk storage quota [MB]", "Lagringsutrymme (disk) [MB]").setDomain("integer"); schema.addAttribute("user", "funka_rights", "Funka student rights", "Funka studentr\u00e4ttigheter").makeMultival().setDomain("regexp").setDomainChars("^[a-z][a-z0-9]*$"); schema.addAttribute("user", "ug_version", "Version", "Version").makeSpecial().makePseudo(); schema.addAttribute("user", "ug_attribute_version", "Attribute versions", "Attribut-versioner").makeMultival().makeSpecial().makePseudo(); schema.addAttribute("user", "ug_last_changed", "Last changed", "Senast \u00e4ndrad").makeMultival().makeSpecial().makePseudo(); schema.addAttribute("group", "kthid", "KTH-ID", "KTH-ID").makeUniq().makeMandatory().setDomain("uname").makeSpecial(); schema.addAttribute("group", "ug1name", "Name", "Namn").makeUniq().makeMandatory().setDomain("ug1name").makeSpecial().makeCaseInsensitive(); schema.addAttribute("group", "parent", "Parent", "F\u00f6r\u00e4lder").setDomainClass("group").makeSpecial(); schema.addAttribute("group", "member", "Member (user)", "Medlem (anv\u00e4ndare)").makeMultival().setDomainClass("user"); schema.addAttribute("group", "system_member", "Member (system)", "Medlem (system)").makeMultival().setDomainClass("system"); schema.addAttribute("group", "group_member", "Member (group)", "Medlem (grupp)").makeMultival().setDomainClass("group"); schema.addAttribute("group", "security_level", "Security level", "S\u00e4kerhetsniv\u00e5").setDomain("integer").setMin(0L); schema.addAttribute("group", "creator", "Owner", "\u00c4gare").setDomainClass("user"); schema.addAttribute("group", "name_sv", "Description in swedish", "Beskrivning p\u00e5 svenska"); schema.addAttribute("group", "name_en", "Description in english", "Beskrivning p\u00e5 engelska"); schema.addAttribute("group", "type", "Group type", "Grupptyp").setDomain("selection"); schema.addAttribute("group", "enhet_uf", "UF unit (HR+)", "Enhet inom UF (HR+)"); schema.addAttribute("group", "fax", "Fax number", "Faxnummer"); schema.addAttribute("group", "acronym", "Acronym", "F\u00f6rkortning"); schema.addAttribute("group", "address", "Address", "Adress").makeMultival(); schema.addAttribute("group", "email_member", "Member (email)", "Medlem (e-post)").makeMultival().setDomain("regexp").setDomainChars("^[\\x21-\\x7E]+@[\\x21-\\x7E]+$").makeCaseInsensitive(); schema.addAttribute("group", "phone", "Phone number", "Telefonnummer").makeMultival(); schema.addAttribute("group", "website", "Web site", "Webbsajt").makeMultival(); schema.addAttribute("group", "handle_reserved", "Reserved username", "Reserverat anv\u00e4ndarnamn").makeMultival().setDomainClass("handle").makeAuto().makeSpecial(); schema.addAttribute("group", ATTR_EMAIL_NAME, "Email canonical name", "Unikt e-postnamn").makeUniq().setDomain("uname").setDomainClass("group").makeSpecial(); schema.addAttribute("group", "email_alias", "Email alias", "E-postalias").makeUniq().makeMultival().setDomainClass("handle").makeAuto().makeSpecial(); schema.addAttribute("group", "editor", "Editor", "Redakt\u00f6r").makeMultival().setDomain("object"); schema.addAttribute("group", "watcher", "Watcher", "Watcher").makeMultival().setDomain("object"); schema.addAttribute("group", "admin", "Administrator", "Administrat\u00f6r").makeMultival().setDomain("object"); schema.addAttribute("group", ATTR_ROLE, "Role system", "Rollsystem").makeMultival().setDomainClass("system"); schema.addAttribute("group", "auth_level", "Authentication level", "Authenticeringsniv\u00e5"); schema.addAttribute("group", ATTR_AUTO_ROLE, "Automatically enabled role", "Automatiskt p\u00e5slagen roll").makeMultival().setDomainClass("system"); schema.addAttribute("group", "setpwd", "Password setter", "L\u00f6senordstilldelare").makeMultival().setDomain("object"); schema.addAttribute("group", "access", "Access rights", "R\u00e4ttigheter").makeMultival().setDomain("access").makeSpecial(); schema.addAttribute("group", "ug_version", "Version", "Version").makeSpecial().makePseudo(); schema.addAttribute("group", "ug_attribute_version", "Attribute versions", "Attribut-versioner").makeMultival().makeSpecial().makePseudo(); schema.addAttribute("group", "ug_last_changed", "Last changed", "Senast \u00e4ndrad").makeMultival().makeSpecial().makePseudo(); return schema; } public Ug2AttributeInformation addAttribute(String className, String attributeName, String label_en, String label_sv) { Ug2ClassInformation classInfo = getOrCreateClass(className); return classInfo.addAttribute(attributeName, label_en, label_sv); } public void addAttribute(Ug2AttributeInformation attribute) { Ug2ClassInformation classInfo = getOrCreateClass(attribute.ug2class()); classInfo.addAttribute(attribute); } private Ug2ClassInformation getOrCreateClass(String className) { Ug2ClassInformation classInfo = classes.get(className); if (classInfo == null) { classInfo = new Ug2ClassInformation(className); classes.put(className, classInfo); } return classInfo; } @Override public String toString() { StringBuilder buf = new StringBuilder(); for (Ug2ClassInformation classInfo : classes.values()) { buf.append(classInfo.toString()); } return buf.toString(); } private HashMap classes; }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy