br.com.objectos.blog.PostImpl.java Source code

Java tutorial

Introduction

Here is the source code for br.com.objectos.blog.PostImpl.java

Source

/*
 * Copyright 2011 Objectos, Fbrica de Software LTDA.
 *
 * 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 br.com.objectos.blog;

import java.io.File;
import java.io.IOException;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import br.com.objectos.blog.syntax.Document;
import br.com.objectos.blog.syntax.MetaTypes;
import br.com.objectos.blog.syntax.SyntaxException;
import br.com.objectos.comuns.base.Strings;

import com.google.common.base.Charsets;
import com.google.common.io.Files;

/**
 * @author marcio.endo@objectos.com.br (Marcio Endo)
 */
class PostImpl implements Post {

    private final String id;

    private final Document document;

    public PostImpl(Builder builder) throws SyntaxException {
        this.id = builder.getId();
        this.document = builder.getDocument();
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public String getPath() {
        DateTime date = getDate();
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy/MM/dd");
        String directoryName = formatter.print(date);

        String title = getTitle();
        String filename;
        filename = Strings.accentsToAscii(title).alphanum().whitespaceTo("_").toString().toLowerCase();

        return directoryName + "/" + filename + ".html";
    }

    @Override
    public String getTitle() {
        return document.getMetaValue(MetaTypes.TITLE);
    }

    @Override
    public String getAuthor() {
        return document.getMetaValue(MetaTypes.AUTHOR);
    }

    @Override
    public DateTime getDate() {
        return document.getMetaValue(MetaTypes.DATE);
    }

    @Override
    public String getContents() {
        return document.getContents();
    }

    @Override
    public String render(PostTemplate template) {
        String author = getAuthor();
        String title = getTitle();
        DateTime date = getDate();
        String contents = getContents();

        template.setPostId(id);
        template.setAuthor(author);
        template.setTitle(title);
        template.setDate(date);
        template.setContents(contents);

        return template.toString();
    }

    @Override
    public void writeTo(PostTemplate template, File destination) {
        try {
            writeTo0(template, destination);
        } catch (IOException e) {
            throw new BlogIOException(e);
        }
    }

    private void writeTo0(PostTemplate template, File destination) throws IOException {

        String html = render(template);

        DateTime date = getDate();
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy/MM/dd");
        String directoryName = formatter.print(date);

        File directory = new File(destination, directoryName);
        directory.mkdirs();

        String title = getTitle();
        String filename = Strings.accentsToAscii(title).alphanum().whitespaceTo("_").toString().toLowerCase();

        File file = new File(directory, filename + ".html");
        Files.write(html, file, Charsets.UTF_8);

    }

}