<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.syncope.client.console.commons.status;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.apache.syncope.client.console.commons.ConnIdSpecialName;
import org.apache.syncope.client.console.commons.Constants;
import org.apache.syncope.client.console.panels.LabelPanel;
import org.apache.syncope.client.console.rest.ResourceRestClient;
import org.apache.syncope.common.lib.patch.PasswordPatch;
import org.apache.syncope.common.lib.patch.StatusPatch;
import org.apache.syncope.common.lib.to.AnyTO;
import org.apache.syncope.common.lib.to.AttrTO;
import org.apache.syncope.common.lib.to.ConnObjectTO;
import org.apache.syncope.common.lib.to.RealmTO;
import org.apache.syncope.common.lib.types.PropagationTaskExecStatus;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StatusUtils implements Serializable {

    private static final long serialVersionUID = 7238009174387184309L;

    private static final Logger LOG = LoggerFactory.getLogger(StatusUtils.class);

    private final ResourceRestClient restClient = new ResourceRestClient();

    public List<ConnObjectWrapper> getConnectorObjects(final AnyTO any) {
        final List<ConnObjectWrapper> objects = new ArrayList<>();
        objects.addAll(getConnectorObjects(any, any.getResources()));
        return objects;
    }

    public List<ConnObjectWrapper> getConnectorObjects(
            final Collection<AnyTO> anys, final Collection<String> resources) {

        final List<ConnObjectWrapper> objects = new ArrayList<>();

        for (AnyTO any : anys) {
            objects.addAll(getConnectorObjects(any, resources));
        }

        return objects;
    }

    public List<ConnObjectWrapper> getConnectorObjects(
            final AnyTO any, final Collection<String> resources) {

        final List<ConnObjectWrapper> objects = new ArrayList<>();

        for (String resourceName : resources) {
            ConnObjectTO objectTO = null;
            try {
                objectTO = restClient.readConnObject(resourceName, any.getType(), any.getKey());
            } catch (Exception e) {
                LOG.warn("ConnObject '{}' not found on resource '{}'", any.getKey(), resourceName);
            }

            objects.add(new ConnObjectWrapper(any, resourceName, objectTO));
        }

        return objects;
    }

    public StatusBean getStatusBean(
            final AnyTO anyTO,
            final String resourceName,
            final ConnObjectTO objectTO,
            final boolean notUser) {

        final StatusBean statusBean = new StatusBean(anyTO, resourceName);

        if (objectTO != null) {
            final Boolean enabled = isEnabled(objectTO);

            final Status status = enabled == null
                    ? (notUser ? Status.ACTIVE : Status.UNDEFINED)
                    : enabled
                            ? Status.ACTIVE
                            : Status.SUSPENDED;

            String connObjectLink = getConnObjectLink(objectTO);

            statusBean.setStatus(status);
            statusBean.setConnObjectLink(connObjectLink);
        }

        return statusBean;
    }

    public StatusBean getStatusBean(
            final RealmTO anyTO,
            final String resourceName,
            final ConnObjectTO objectTO) {

        final StatusBean statusBean = new StatusBean(anyTO, resourceName);

        if (objectTO != null) {
            final Boolean enabled = isEnabled(objectTO);

            final Status status = enabled == null
                    ? Status.ACTIVE
                    : enabled
                            ? Status.ACTIVE
                            : Status.SUSPENDED;

            String connObjectLink = getConnObjectLink(objectTO);

            statusBean.setStatus(status);
            statusBean.setConnObjectLink(connObjectLink);
        }

        return statusBean;
    }

    private Boolean isEnabled(final ConnObjectTO objectTO) {
        Optional<AttrTO> status = objectTO.getAttr(ConnIdSpecialName.ENABLE);
        return status.isPresent() && status.get().getValues() != null && !status.get().getValues().isEmpty()
                ? Boolean.valueOf(status.get().getValues().get(0))
                : Boolean.FALSE;
    }

    private String getConnObjectLink(final ConnObjectTO objectTO) {
        Optional<AttrTO> name = objectTO == null ? null : objectTO.getAttr(ConnIdSpecialName.NAME);
        return name.isPresent() && name.get().getValues() != null && !name.get().getValues().isEmpty()
                ? name.get().getValues().get(0)
                : null;
    }

    public static PasswordPatch buildPasswordPatch(final String password, final Collection<StatusBean> statuses) {
        final PasswordPatch.Builder builder = new PasswordPatch.Builder();
        builder.value(password);

        for (StatusBean status : statuses) {
            if (Constants.SYNCOPE.equalsIgnoreCase(status.getResource())) {
                builder.onSyncope(true);
            } else {
                builder.resource(status.getResource());
            }
        }
        return builder.build();
    }

    public static StatusPatch buildStatusPatch(final Collection<StatusBean> statuses) {
        return buildStatusPatch(statuses, null);
    }

    public static StatusPatch buildStatusPatch(final Collection<StatusBean> statuses, final Boolean enable) {
        StatusPatch.Builder builder = new StatusPatch.Builder();

        for (StatusBean status : statuses) {
            if (enable == null
                    || (enable && !status.getStatus().isActive()) || (!enable && status.getStatus().isActive())) {

                if ("syncope".equalsIgnoreCase(status.getResource())) {
                    builder.onSyncope(true);
                } else {
                    builder.resource(status.getResource());
                }

            }
        }

        return builder.build();
    }

    public static Panel getStatusImagePanel(final String componentId, final Status status) {
        return new LabelPanel(componentId, getStatusImage("label", status));
    }

    public static Label getStatusImage(final String componentId, final Status status) {
        final String alt, title, clazz;

        switch (status) {

            case NOT_YET_SUBMITTED:
                alt = "undefined icon";
                title = "Not yet submitted";
                clazz = Constants.UNDEFINED_ICON;
                break;

            case ACTIVE:
                alt = "active icon";
                title = "Enabled";
                clazz = Constants.ACTIVE_ICON;<commit_msg>removed check on previous status while enabling/disabling user<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.syncope.client.console.commons.status;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.apache.syncope.client.console.commons.ConnIdSpecialName;
import org.apache.syncope.client.console.commons.Constants;
import org.apache.syncope.client.console.panels.LabelPanel;
import org.apache.syncope.client.console.rest.ResourceRestClient;
import org.apache.syncope.common.lib.patch.PasswordPatch;
import org.apache.syncope.common.lib.patch.StatusPatch;
import org.apache.syncope.common.lib.to.AnyTO;
import org.apache.syncope.common.lib.to.AttrTO;
import org.apache.syncope.common.lib.to.ConnObjectTO;
import org.apache.syncope.common.lib.to.RealmTO;
import org.apache.syncope.common.lib.types.PropagationTaskExecStatus;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StatusUtils implements Serializable {

    private static final long serialVersionUID = 7238009174387184309L;

    private static final Logger LOG = LoggerFactory.getLogger(StatusUtils.class);

    private final ResourceRestClient restClient = new ResourceRestClient();

    public List<ConnObjectWrapper> getConnectorObjects(final AnyTO any) {
        final List<ConnObjectWrapper> objects = new ArrayList<>();
        objects.addAll(getConnectorObjects(any, any.getResources()));
        return objects;
    }

    public List<ConnObjectWrapper> getConnectorObjects(
            final Collection<AnyTO> anys, final Collection<String> resources) {

        final List<ConnObjectWrapper> objects = new ArrayList<>();

        for (AnyTO any : anys) {
            objects.addAll(getConnectorObjects(any, resources));
        }

        return objects;
    }

    public List<ConnObjectWrapper> getConnectorObjects(
            final AnyTO any, final Collection<String> resources) {

        final List<ConnObjectWrapper> objects = new ArrayList<>();

        for (String resourceName : resources) {
            ConnObjectTO objectTO = null;
            try {
                objectTO = restClient.readConnObject(resourceName, any.getType(), any.getKey());
            } catch (Exception e) {
                LOG.warn("ConnObject '{}' not found on resource '{}'", any.getKey(), resourceName);
            }

            objects.add(new ConnObjectWrapper(any, resourceName, objectTO));
        }

        return objects;
    }

    public StatusBean getStatusBean(
            final AnyTO anyTO,
            final String resourceName,
            final ConnObjectTO objectTO,
            final boolean notUser) {

        final StatusBean statusBean = new StatusBean(anyTO, resourceName);

        if (objectTO != null) {
            final Boolean enabled = isEnabled(objectTO);

            final Status status = enabled == null
                    ? (notUser ? Status.ACTIVE : Status.UNDEFINED)
                    : enabled
                            ? Status.ACTIVE
                            : Status.SUSPENDED;

            String connObjectLink = getConnObjectLink(objectTO);

            statusBean.setStatus(status);
            statusBean.setConnObjectLink(connObjectLink);
        }

        return statusBean;
    }

    public StatusBean getStatusBean(
            final RealmTO anyTO,
            final String resourceName,
            final ConnObjectTO objectTO) {

        final StatusBean statusBean = new StatusBean(anyTO, resourceName);

        if (objectTO != null) {
            final Boolean enabled = isEnabled(objectTO);

            final Status status = enabled == null
                    ? Status.ACTIVE
                    : enabled
                            ? Status.ACTIVE
                            : Status.SUSPENDED;

            String connObjectLink = getConnObjectLink(objectTO);

            statusBean.setStatus(status);
            statusBean.setConnObjectLink(connObjectLink);
        }

        return statusBean;
    }

    private Boolean isEnabled(final ConnObjectTO objectTO) {
        Optional<AttrTO> status = objectTO.getAttr(ConnIdSpecialName.ENABLE);
        return status.isPresent() && status.get().getValues() != null && !status.get().getValues().isEmpty()
                ? Boolean.valueOf(status.get().getValues().get(0))
                : Boolean.FALSE;
    }

    private String getConnObjectLink(final ConnObjectTO objectTO) {
        Optional<AttrTO> name = objectTO == null ? null : objectTO.getAttr(ConnIdSpecialName.NAME);
        return name.isPresent() && name.get().getValues() != null && !name.get().getValues().isEmpty()
                ? name.get().getValues().get(0)
                : null;
    }

    public static PasswordPatch buildPasswordPatch(final String password, final Collection<StatusBean> statuses) {
        final PasswordPatch.Builder builder = new PasswordPatch.Builder();
        builder.value(password);

        for (StatusBean status : statuses) {
            if (Constants.SYNCOPE.equalsIgnoreCase(status.getResource())) {
                builder.onSyncope(true);
            } else {
                builder.resource(status.getResource());
            }
        }
        return builder.build();
    }

    public static StatusPatch buildStatusPatch(final Collection<StatusBean> statuses) {
        return buildStatusPatch(statuses, null);
    }

    public static StatusPatch buildStatusPatch(final Collection<StatusBean> statuses, final Boolean enable) {
        StatusPatch.Builder builder = new StatusPatch.Builder();

        for (StatusBean status : statuses) {
            if ("syncope".equalsIgnoreCase(status.getResource())) {
                builder.onSyncope(true);
            } else {
                builder.resource(status.getResource());
            }
        }

        return builder.build();
    }

    public static Panel getStatusImagePanel(final String componentId, final Status status) {
        return new LabelPanel(componentId, getStatusImage("label", status));
    }

    public static Label getStatusImage(final String componentId, final Status status) {
        final String alt, title, clazz;

        switch (status) {

            case NOT_YET_SUBMITTED:
                alt = "undefined icon";
                title = "Not yet submitted";
                clazz = Constants.UNDEFINED_ICON;
                break;

            case ACTIVE:
                alt = "active icon";
                title = "Enabled";
                clazz = Constants.ACTIVE_ICON;
{'fraction_non_alphanumeric': 0.09118321861684693, 'fraction_numerical': 0.0036709275647328747, 'mean_word_length': 2.5225121219117987, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6546064', 'n_tokens_mistral': 3975, 'n_tokens_neox': 3818, 'n_words': 993}