<commit_before>/*
 *
 *  * Copyright (C) 2016 Singular Studios (a.k.a Atom Tecnologia) - www.opensingular.com
 *  *
 *  * Licensed 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.opensingular.server.core.service;

import org.opensingular.flow.persistence.entity.ModuleEntity;
import org.opensingular.lib.commons.util.Loggable;
import org.opensingular.server.commons.config.IServerContext;
import org.opensingular.server.commons.service.dto.BoxConfigurationData;
import org.opensingular.server.commons.wicket.SingularSession;
import org.opensingular.server.commons.wicket.view.template.MenuService;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.*;

@Named
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class ServerMenuService implements MenuService, Loggable {

    @Inject
    private SingularServerSessionConfiguration singularServerSessionConfiguration;

    private Map<IServerContext, Map<ModuleEntity, List<BoxConfigurationData>>> contextMap = new HashMap<>();
    private Map<IServerContext, Map<String, BoxConfigurationData>> mapMenu = new HashMap<>();

    private void loadEntries() {
        singularServerSessionConfiguration.reload();
        for (Map.Entry<ModuleEntity, List<BoxConfigurationData>> entry : singularServerSessionConfiguration.getModuleBoxConfigurationMap().entrySet()) {
            addMenu(entry.getKey(), entry.getValue());
        }
    }

    @Override
    public BoxConfigurationData getDefaultSelectedMenu(ModuleEntity moduleEntity) {
        final List<BoxConfigurationData> menusPorCategoria = getMenusByCategory(moduleEntity);
        if (menusPorCategoria != null && !menusPorCategoria.isEmpty()) {
            return menusPorCategoria.get(0);
        }
        return null;
    }

    @Override
    public List<BoxConfigurationData> getMenusByCategory(ModuleEntity categoria) {
        return getMap().get(categoria);
    }

    @Override
    public BoxConfigurationData getMenuByLabel(String label) {
        return getMapMenu().get(label);
    }

    @Override
    public List<ModuleEntity> getCategories() {
        return new ArrayList<>(getModifiableMap().keySet());
    }

    @Override
    public void reset() {

    }

    private Map<String, BoxConfigurationData> getMapMenu() {
        Map<String, BoxConfigurationData> modifiableMapMenu = getModifiableMapMenu();
        for (Map.Entry<ModuleEntity, List<BoxConfigurationData>> moduleEntityListEntry : getModifiableMap().entrySet()) {
            for (BoxConfigurationData boxConfigurationMetadataDTO : moduleEntityListEntry.getValue()) {
                modifiableMapMenu.put(boxConfigurationMetadataDTO.getLabel(), boxConfigurationMetadataDTO);<commit_msg>correção do reset<commit_after>/*
 *
 *  * Copyright (C) 2016 Singular Studios (a.k.a Atom Tecnologia) - www.opensingular.com
 *  *
 *  * Licensed 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.opensingular.server.core.service;

import org.opensingular.flow.persistence.entity.ModuleEntity;
import org.opensingular.lib.commons.util.Loggable;
import org.opensingular.server.commons.config.IServerContext;
import org.opensingular.server.commons.service.dto.BoxConfigurationData;
import org.opensingular.server.commons.wicket.SingularSession;
import org.opensingular.server.commons.wicket.view.template.MenuService;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.*;

@Named
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class ServerMenuService implements MenuService, Loggable {

    @Inject
    private SingularServerSessionConfiguration singularServerSessionConfiguration;

    private Map<IServerContext, Map<ModuleEntity, List<BoxConfigurationData>>> contextMap = new HashMap<>();
    private Map<IServerContext, Map<String, BoxConfigurationData>> mapMenu = new HashMap<>();

    private void loadEntries() {
        singularServerSessionConfiguration.reload();
        for (Map.Entry<ModuleEntity, List<BoxConfigurationData>> entry : singularServerSessionConfiguration.getModuleBoxConfigurationMap().entrySet()) {
            addMenu(entry.getKey(), entry.getValue());
        }
    }

    @Override
    public BoxConfigurationData getDefaultSelectedMenu(ModuleEntity moduleEntity) {
        final List<BoxConfigurationData> menusPorCategoria = getMenusByCategory(moduleEntity);
        if (menusPorCategoria != null && !menusPorCategoria.isEmpty()) {
            return menusPorCategoria.get(0);
        }
        return null;
    }

    @Override
    public List<BoxConfigurationData> getMenusByCategory(ModuleEntity categoria) {
        return getMap().get(categoria);
    }

    @Override
    public BoxConfigurationData getMenuByLabel(String label) {
        return getMapMenu().get(label);
    }

    @Override
    public List<ModuleEntity> getCategories() {
        return new ArrayList<>(getModifiableMap().keySet());
    }

    @Override
    public void reset() {
        contextMap = new HashMap<>();
        mapMenu = null;
        singularServerSessionConfiguration.reload();
        for (Map.Entry<ModuleEntity, List<BoxConfigurationData>> entry : singularServerSessionConfiguration.getModuleBoxConfigurationMap().entrySet()) {
            addMenu(entry.getKey(), entry.getValue());
        }
    }

    private Map<String, BoxConfigurationData> getMapMenu() {
        Map<String, BoxConfigurationData> modifiableMapMenu = getModifiableMapMenu();
        for (Map.Entry<ModuleEntity, List<BoxConfigurationData>> moduleEntityListEntry : getModifiableMap().entrySet()) {
            for (BoxConfigurationData boxConfigurationMetadataDTO : moduleEntityListEntry.getValue()) {
                modifiableMapMenu.put(boxConfigurationMetadataDTO.getLabel(), boxConfigurationMetadataDTO);
{'fraction_non_alphanumeric': 0.09700649534029936, 'fraction_numerical': 0.0025416548997458347, 'mean_word_length': 4.215758468335788, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '2567207', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1859, 'n_words': 440}