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.ArrayList;
024import java.util.List;
025import java.util.Optional;
026import static java.util.Objects.requireNonNull;
027
028/** Intentional typed provisioning view; diagnostics are redacted.
029 * @since 1.0.0
030 */
031@Immutable public final class ScimGroup {
032    private final @NonNull ScimResource resource;
033    private ScimGroup(@NonNull ScimResource resource) { this.resource = resource; }
034    /** Creates a typed view of a complete standard Group schema.
035     * Equivalent rebuilt schemas are accepted; a foreign or altered schema is a programming error.
036     * @param resource the resource
037     * @return the view
038     * @since 1.0.0
039     */
040    @CheckReturnValue public static @NonNull ScimGroup fromResource(@NonNull ScimResource resource) {
041        requireNonNull(resource);
042        if (!resource.getResourceType().hasStandardGroupSchema())
043            throw new IllegalArgumentException("The resource does not have the required core schema.");
044        return new ScimGroup(resource);
045    }
046    /** Returns the underlying resource.
047     * @return the resource
048     * @since 1.0.0
049     */
050    public @NonNull ScimResource getResource() { return resource; }
051    /** Returns displayName.
052     * @return the provisioning value
053     * @since 1.0.0
054     */
055    public @NonNull String getDisplayName() { return resource.getDocument().find("displayName").filter(JsonString.class::isInstance).map(JsonString.class::cast).map(JsonString::getValue).orElseThrow(); }
056    /** Returns members.
057     * @return the provisioning value
058     * @since 1.0.0
059     */
060    public @NonNull List<@NonNull JsonObject> getMembers() { return objects("members"); }
061    private @NonNull List<@NonNull JsonObject> objects(@NonNull String name) {
062        JsonValue raw = resource.getDocument().getMembers().get(name);
063        if (!(raw instanceof JsonArray array)) return List.of();
064        List<JsonObject> objects = new ArrayList<>();
065        for (JsonValue value : array.getElements()) if (value instanceof JsonObject object) objects.add(object);
066        return List.copyOf(objects);
067    }
068    /** Returns a fixed redacted description.
069     * @return the description
070     * @since 1.0.0
071     */
072    @Override public @NonNull String toString() { return "ScimGroup{resource=<redacted>}"; }
073}