001/*
002 * Copyright 2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.revetsec.scim;
018
019import com.google.errorprone.annotations.CheckReturnValue;
020import com.revetsec.json.*;
021import org.jspecify.annotations.NonNull;
022import javax.annotation.concurrent.Immutable;
023import java.util.Optional;
024import static java.util.Objects.requireNonNull;
025
026/** Intentional Enterprise User extension view; diagnostics are redacted.
027 * @since 1.0.0
028 */
029@Immutable public final class ScimEnterpriseUser {
030    private final @NonNull ScimResource resource;
031    private final @NonNull JsonObject extension;
032    private ScimEnterpriseUser(@NonNull ScimResource resource, @NonNull JsonObject extension) { this.resource = resource; this.extension = extension; }
033    /** Creates a view of a complete standard User schema and standard Enterprise User extension.
034     * Equivalent rebuilt schemas are accepted; an absent extension or altered/foreign schema is a programming error.
035     * @param resource the User resource with an Enterprise extension
036     * @return the view
037     * @since 1.0.0
038     */
039    @CheckReturnValue public static @NonNull ScimEnterpriseUser fromResource(@NonNull ScimResource resource) {
040        requireNonNull(resource);
041        if (!resource.getResourceType().hasStandardUserSchema())
042            throw new IllegalArgumentException("The resource does not have the required User schema.");
043        if (!resource.getResourceType().hasStandardEnterpriseUserSchema())
044            throw new IllegalArgumentException("The resource does not have the required Enterprise User schema.");
045        JsonObject extension = resource.getExtension(ScimSchema.enterpriseUserInstance().getId()).orElseThrow(() -> new IllegalArgumentException("The resource has no Enterprise User extension."));
046        return new ScimEnterpriseUser(resource,extension);
047    }
048    /** Returns the underlying resource.
049     * @return the resource
050     * @since 1.0.0
051     */
052    public @NonNull ScimResource getResource() { return resource; }
053    /** Returns employeeNumber.
054     * @return the provisioning value
055     * @since 1.0.0
056     */
057    public @NonNull Optional<@NonNull String> getEmployeeNumber() { return extension.find("employeeNumber").filter(JsonString.class::isInstance).map(JsonString.class::cast).map(JsonString::getValue); }
058    /** Returns department.
059     * @return the provisioning value
060     * @since 1.0.0
061     */
062    public @NonNull Optional<@NonNull String> getDepartment() { return extension.find("department").filter(JsonString.class::isInstance).map(JsonString.class::cast).map(JsonString::getValue); }
063    /** Returns organization.
064     * @return the provisioning value
065     * @since 1.0.0
066     */
067    public @NonNull Optional<@NonNull String> getOrganization() { return extension.find("organization").filter(JsonString.class::isInstance).map(JsonString.class::cast).map(JsonString::getValue); }
068    /** Returns division.
069     * @return the provisioning value
070     * @since 1.0.0
071     */
072    public @NonNull Optional<@NonNull String> getDivision() { return extension.find("division").filter(JsonString.class::isInstance).map(JsonString.class::cast).map(JsonString::getValue); }
073    /** Returns the manager object without fetching its link.
074     * @return the manager
075     * @since 1.0.0
076     */
077    public @NonNull Optional<@NonNull JsonObject> getManager() {
078        JsonValue raw = extension.getMembers().get("manager");
079        return raw instanceof JsonObject object ? Optional.of(object) : Optional.empty();
080    }
081    /** Returns a fixed redacted description.
082     * @return the description
083     * @since 1.0.0
084     */
085    @Override public @NonNull String toString() { return "ScimEnterpriseUser{resource=<redacted>}"; }
086}