<commit_before>/*
 * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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.wso2.carbon.identity.mgt;


import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.mgt.constants.IdentityMgtConstants;
import org.wso2.carbon.identity.mgt.dto.ChallengeQuestionDTO;
import org.wso2.carbon.identity.mgt.dto.ChallengeQuestionIdsDTO;
import org.wso2.carbon.identity.mgt.dto.UserChallengesDTO;
import org.wso2.carbon.identity.mgt.internal.IdentityMgtServiceComponent;
import org.wso2.carbon.identity.mgt.util.Utils;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.RegistryConstants;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.user.core.UserStoreException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * process user challenges and questions
 */
public class ChallengeQuestionProcessor {

    private static final Log log = LogFactory.getLog(ChallengeQuestionProcessor.class);

    /**
     * @return
     * @throws IdentityException
     */
    public List<ChallengeQuestionDTO> getAllChallengeQuestions() throws IdentityException {

        List<ChallengeQuestionDTO> questionDTOs = new ArrayList<ChallengeQuestionDTO>();
        try {
            Registry registry = IdentityMgtServiceComponent.getRegistryService().
                    getConfigSystemRegistry();
            if (registry.resourceExists(IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS)) {
                Collection collection = (Collection) registry.
                        get(IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS);
                String[] children = collection.getChildren();
                for (String child : children) {
                    Resource resource = registry.get(child);
                    String question = resource.getProperty("question");
                    String isPromoteQuestion = resource.getProperty("isPromoteQuestion");
                    String questionSetId = resource.getProperty("questionSetId");
                    if (question != null) {
                        ChallengeQuestionDTO questionDTO = new ChallengeQuestionDTO();
                        questionDTO.setQuestion(question);
                        if (isPromoteQuestion != null) {
                            questionDTO.setPromoteQuestion(Boolean.parseBoolean(isPromoteQuestion));
                        }
                        if (questionSetId != null) {
                            questionDTO.setQuestionSetId(questionSetId);
                        }
                        questionDTO.setPromoteQuestion(false);
                        questionDTOs.add(questionDTO);
                    }
                }

            }
        } catch (RegistryException e) {
            throw new IdentityException(e.getMessage(), e);
        }
        return questionDTOs;
    }

    /**
     * @param questionDTOs
     * @throws IdentityException
     */
    public void setChallengeQuestions(ChallengeQuestionDTO[] questionDTOs) throws IdentityException {
        Registry registry = null;
        try {
            registry = IdentityMgtServiceComponent.getRegistryService().getConfigSystemRegistry();

            Resource identityMgtResource = registry.get(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH);
            if (identityMgtResource != null) {
                String questionCollectionPath = IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS;
                if (registry.resourceExists(questionCollectionPath)) {
                    registry.delete(questionCollectionPath);
                }

                Collection questionCollection = registry.newCollection();
                registry.put(questionCollectionPath, questionCollection);

                for (int i = 0; i < questionDTOs.length; i++) {
                    Resource resource = registry.newResource();
                    resource.addProperty("question", questionDTOs[i].getQuestion());
                    resource.addProperty("isPromoteQuestion", String.valueOf(questionDTOs[i].isPromoteQuestion()));
                    resource.addProperty("questionSetId", questionDTOs[i].getQuestionSetId());
                    registry.put(IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS +
                            RegistryConstants.PATH_SEPARATOR + "question" + i +
                            RegistryConstants.PATH_SEPARATOR, resource);
                }
            }
        } catch (RegistryException e) {<commit_msg>Fixing set and get security questions issue in tenants<commit_after>/*
 * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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.wso2.carbon.identity.mgt;


import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.mgt.constants.IdentityMgtConstants;
import org.wso2.carbon.identity.mgt.dto.ChallengeQuestionDTO;
import org.wso2.carbon.identity.mgt.dto.ChallengeQuestionIdsDTO;
import org.wso2.carbon.identity.mgt.dto.UserChallengesDTO;
import org.wso2.carbon.identity.mgt.internal.IdentityMgtServiceComponent;
import org.wso2.carbon.identity.mgt.util.Utils;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.RegistryConstants;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.user.core.UserStoreException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * process user challenges and questions
 */
public class ChallengeQuestionProcessor {

    private static final Log log = LogFactory.getLog(ChallengeQuestionProcessor.class);

    /**
     * @return
     * @throws IdentityException
     */
    public List<ChallengeQuestionDTO> getAllChallengeQuestions() throws IdentityException {

        List<ChallengeQuestionDTO> questionDTOs = new ArrayList<ChallengeQuestionDTO>();
        try {
            int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
            Registry registry = IdentityMgtServiceComponent.getRegistryService().
                    getConfigSystemRegistry(tenantId);
            if (registry.resourceExists(IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS)) {
                Collection collection = (Collection) registry.
                        get(IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS);
                String[] children = collection.getChildren();
                for (String child : children) {
                    Resource resource = registry.get(child);
                    String question = resource.getProperty("question");
                    String isPromoteQuestion = resource.getProperty("isPromoteQuestion");
                    String questionSetId = resource.getProperty("questionSetId");
                    if (question != null) {
                        ChallengeQuestionDTO questionDTO = new ChallengeQuestionDTO();
                        questionDTO.setQuestion(question);
                        if (isPromoteQuestion != null) {
                            questionDTO.setPromoteQuestion(Boolean.parseBoolean(isPromoteQuestion));
                        }
                        if (questionSetId != null) {
                            questionDTO.setQuestionSetId(questionSetId);
                        }
                        questionDTO.setPromoteQuestion(false);
                        questionDTOs.add(questionDTO);
                    }
                }

            }
        } catch (RegistryException e) {
            throw new IdentityException(e.getMessage(), e);
        }
        return questionDTOs;
    }

    /**
     * @param questionDTOs
     * @throws IdentityException
     */
    public void setChallengeQuestions(ChallengeQuestionDTO[] questionDTOs) throws IdentityException {
        Registry registry = null;
        try {
            int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
            registry = IdentityMgtServiceComponent.getRegistryService().getConfigSystemRegistry(tenantId);

            if (!registry.resourceExists(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH)) {
                Collection securityQuestionResource = registry.newCollection();
                registry.put(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH, securityQuestionResource);
            }
            Resource identityMgtResource = registry.get(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH);
            if (identityMgtResource != null) {
                String questionCollectionPath = IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS;
                if (registry.resourceExists(questionCollectionPath)) {
                    registry.delete(questionCollectionPath);
                }

                Collection questionCollection = registry.newCollection();
                registry.put(questionCollectionPath, questionCollection);

                for (int i = 0; i < questionDTOs.length; i++) {
                    Resource resource = registry.newResource();
                    resource.addProperty("question", questionDTOs[i].getQuestion());
                    resource.addProperty("isPromoteQuestion", String.valueOf(questionDTOs[i].isPromoteQuestion()));
                    resource.addProperty("questionSetId", questionDTOs[i].getQuestionSetId());
                    registry.put(IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS +
                            RegistryConstants.PATH_SEPARATOR + "question" + i +
                            RegistryConstants.PATH_SEPARATOR, resource);
                }
            }
        } catch (RegistryException e) {
{'fraction_non_alphanumeric': 0.08579392039020992, 'fraction_numerical': 0.004616322619980838, 'mean_word_length': 2.5858838226108682, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '1701288', 'n_tokens_mistral': 2942, 'n_tokens_neox': 2841, 'n_words': 583}