<commit_before>/*
 * Copyright 2015-2018 the original author or authors.
 *
 * 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.docksidestage.mylasta.direction.sponsor.planner;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.dbflute.optional.OptionalThing;
import org.dbflute.util.Srl;
import org.lastaflute.core.util.ContainerUtil;
import org.lastaflute.web.Execute;
import org.lastaflute.web.path.UrlMappingOption;
import org.lastaflute.web.path.UrlMappingResource;
import org.lastaflute.web.path.UrlReverseOption;
import org.lastaflute.web.path.UrlReverseResource;
import org.lastaflute.web.ruts.config.analyzer.MethodNameAnalyzer;
import org.lastaflute.web.servlet.request.RequestManager;

/**
 * @author jflute
 */
public class NumericBasedRestfulRouter {

    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    // #thinking jflute already unneeded? waiting for working of LastaFlute RESTful GET pair (2021/05/16)
    protected boolean virtualListHandling;

    // ===================================================================================
    //                                                                              Option
    //                                                                              ======
    public NumericBasedRestfulRouter enableVirtualListHandling() {
        virtualListHandling = true;
        return this;
    }

    // ===================================================================================
    //                                                                         URL Mapping
    //                                                                         ===========
    public OptionalThing<UrlMappingOption> toRestfulMappingPath(UrlMappingResource resource) {
        final List<String> elementList = splitPath(resource.getMakingMappingPath()); // only for determination

        if (!isRestfulPath(elementList)) { // e.g. /1/products/, /products/purchases/
            // you should log as debug here if RESTful only application
            return OptionalThing.empty(); // no filter
        }
        // comment out because of virtual list handling
        //if (elementList.size() <= 2) { // e.g. /products/, /products/1/
        //    return OptionalThing.empty(); // no filter
        //}

        final boolean listGetRequest = isVirtualListGetRequest(elementList); // e.g. GET /products/1/purchases/

        final UrlMappingOption option = new UrlMappingOption();
        option.filterRequestPath(requestPath -> { // is makingMappingPaths
            return convertToMappingPath(requestPath, listGetRequest);
        });
        return OptionalThing.of(option);
    }

    protected String convertToMappingPath(String requestPath, boolean listGetRequest) {
        // e.g.
        //  /products/1/purchases/
        //  /products/1/purchases/2/
        //  /products/1/purchases/2/payments/
        final List<String> stringList = new ArrayList<>();
        final List<String> numberList = new ArrayList<>();
        final List<String> elementList = splitPath(requestPath);
        for (String element : elementList) {
            if (Srl.isNumberHarfAll(element)) {
                numberList.add(element);
            } else {
                stringList.add(element);
            }
        }

        final List<String> arrangedList = new ArrayList<>();
        arrangedList.addAll(stringList);
        if (listGetRequest) {
            arrangedList.add(getListGetMethodKeyword()); // e.g. get$list()
        }
        arrangedList.addAll(numberList);
        return buildPath(arrangedList);
    }

    // -----------------------------------------------------
    //                                 RESTful Determination
    //                                 ---------------------<commit_msg>use tellRestfulMapping()<commit_after>/*
 * Copyright 2015-2018 the original author or authors.
 *
 * 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.docksidestage.mylasta.direction.sponsor.planner;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.dbflute.optional.OptionalThing;
import org.dbflute.util.Srl;
import org.lastaflute.core.util.ContainerUtil;
import org.lastaflute.web.Execute;
import org.lastaflute.web.path.UrlMappingOption;
import org.lastaflute.web.path.UrlMappingResource;
import org.lastaflute.web.path.UrlReverseOption;
import org.lastaflute.web.path.UrlReverseResource;
import org.lastaflute.web.ruts.config.analyzer.MethodNameAnalyzer;
import org.lastaflute.web.servlet.request.RequestManager;

/**
 * @author jflute
 */
public class NumericBasedRestfulRouter {

    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    // #thinking jflute already unneeded? waiting for working of LastaFlute RESTful GET pair (2021/05/16)
    protected boolean virtualListHandling;

    // ===================================================================================
    //                                                                              Option
    //                                                                              ======
    public NumericBasedRestfulRouter enableVirtualListHandling() {
        virtualListHandling = true;
        return this;
    }

    // ===================================================================================
    //                                                                         URL Mapping
    //                                                                         ===========
    public OptionalThing<UrlMappingOption> toRestfulMappingPath(UrlMappingResource resource) {
        final List<String> elementList = splitPath(resource.getMakingMappingPath()); // only for determination

        if (!isRestfulPath(elementList)) { // e.g. /1/products/, /products/purchases/
            // you should log as debug here if RESTful only application
            return OptionalThing.empty(); // no filter
        }
        // comment out because of virtual list handling and RESTful mapping message
        //if (elementList.size() <= 2) { // e.g. /products/, /products/1/
        //    return OptionalThing.empty(); // no filter
        //}
        return OptionalThing.of(createUrlMappingOption(resource, elementList));
    }

    protected UrlMappingOption createUrlMappingOption(UrlMappingResource resource, List<String> elementList) {
        final UrlMappingOption option = new UrlMappingOption();
        final boolean listGetRequest = isVirtualListGetRequest(elementList); // e.g. GET /products/1/purchases/
        option.filterRequestPath(requestPath -> { // is makingMappingPaths
            return convertToMappingPath(requestPath, listGetRequest);
        });
        option.tellRestfulMapping();
        return option;
    }

    // -----------------------------------------------------
    //                                          Convert Path
    //                                          ------------
    protected String convertToMappingPath(String requestPath, boolean listGetRequest) {
        // e.g.
        //  /products/1/purchases/
        //  /products/1/purchases/2/
        //  /products/1/purchases/2/payments/
        final List<String> stringList = new ArrayList<>();
        final List<String> numberList = new ArrayList<>();
        final List<String> elementList = splitPath(requestPath);
        for (String element : elementList) {
            if (Srl.isNumberHarfAll(element)) {
                numberList.add(element);
            } else {
                stringList.add(element);
            }
        }

        final List<String> arrangedList = new ArrayList<>();
        arrangedList.addAll(stringList);
        if (listGetRequest) {
            arrangedList.add(getListGetMethodKeyword()); // e.g. get$list()
        }
        arrangedList.addAll(numberList);
        return buildPath(arrangedList);
    }

    // -----------------------------------------------------
    //                                 RESTful Determination
    //                                 ---------------------
{'fraction_non_alphanumeric': 0.16050772852902037, 'fraction_numerical': 0.005937148121609172, 'mean_word_length': 1.8684674104521433, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '145517', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2219, 'n_words': 553}