org.wallride.support.PostUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.wallride.support.PostUtils.java

Source

/*
 * Copyright 2014 Tagbangers, Inc.
 *
 * 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.wallride.support;

import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.wallride.domain.Article;
import org.wallride.domain.Page;

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

public class PostUtils {

    private PageUtils pageUtils;

    public PostUtils(PageUtils pageUtils) {
        this.pageUtils = pageUtils;
    }

    public String link(Article article) {
        UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
        return path(builder, article, true);
    }

    public String link(Article article, boolean encode) {
        UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
        return path(builder, article, encode);
    }

    public String link(Page page) {
        UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
        return path(builder, page, true);
    }

    public String link(Page page, boolean encode) {
        UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
        return path(builder, page, encode);
    }

    public String path(Article article) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromPath("");
        return path(builder, article, true);
    }

    public String path(Article article, boolean encode) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromPath("");
        return path(builder, article, encode);
    }

    public String path(Page page) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromPath("");
        return path(builder, page, true);
    }

    public String path(Page page, boolean encode) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromPath("");
        return path(builder, page, encode);
    }

    private String path(UriComponentsBuilder builder, Article article, boolean encode) {
        Map<String, Object> params = new HashMap<>();
        builder.path("/{year}/{month}/{day}/{code}");
        params.put("year", String.format("%04d", article.getDate().getYear()));
        params.put("month", String.format("%02d", article.getDate().getMonth().getValue()));
        params.put("day", String.format("%02d", article.getDate().getDayOfMonth()));
        params.put("code", article.getCode());

        UriComponents components = builder.buildAndExpand(params);
        if (encode) {
            components = components.encode();
        }
        return components.toUriString();
    }

    private String path(UriComponentsBuilder builder, Page page, boolean encode) {
        Map<String, Object> params = new HashMap<>();

        List<String> codes = new LinkedList<>();
        Map<Page, String> paths = pageUtils.getPaths(page);
        paths.keySet().stream().map(p -> p.getCode()).forEach(codes::add);

        for (int i = 0; i < codes.size(); i++) {
            String key = "code" + i;
            builder.path("/{" + key + "}");
            params.put(key, codes.get(i));
        }

        UriComponents components = builder.buildAndExpand(params);
        if (encode) {
            components = components.encode();
        }
        return components.toUriString();
    }
}