com.gerrydevstory.myxie.page.PageService.java Source code

Java tutorial

Introduction

Here is the source code for com.gerrydevstory.myxie.page.PageService.java

Source

/**
 * Copyright 2014 Gerry Tan.
 *
 * 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 com.gerrydevstory.myxie.page;

import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.gerrydevstory.myxie.layout.Layout;
import com.gerrydevstory.myxie.layout.LayoutRepository;
import com.gerrydevstory.myxie.template.Template;
import com.gerrydevstory.myxie.template.TemplateRepository;
import com.gerrydevstory.myxie.template.TemplateService;

/**
 *
 * @author gerrytan
 *
 */
@Service
public class PageService {

    @Autowired
    private PageRepository pageRepo;
    @Autowired
    private TemplateService templateSvc;
    @Autowired
    private TemplateRepository templateRepo;
    @Autowired
    private LayoutRepository layoutRepo;

    /**
     * Install default page for fresh database
     */
    @PostConstruct
    public void postConstruct() {
        Page home = pageRepo.findByPath("/");
        if (home == null) {
            home = new Page();
            home.setPath("/");
            home.setLayoutName("default");
            home.setContent("this is default page content");
            pageRepo.save(home);
        }
    }

    /**
     * Builds page with given path. Page is an association of site template, layout and
     * several components (widgets). First the template is fetched then components are
     * substituted into it.
     * 
     * @param path
     *          path of the requested page. For example if the URL is
     *          http://mycoolblog.com/contact-us then the path is /contact-us. If /
     *          prefix not given it will be appended.
     * @param out
     *          output writer where the resulting page is written
     */
    public void buildPage(String path, Writer out, Model model) {
        Template siteTemplate = templateRepo.findByName("master");
        Page page = pageRepo.findByPath(path);
        Layout layout = layoutRepo.findByName(page.getLayoutName());

        Map<String, Object> context = new HashMap<String, Object>();
        context.put("layout", layout);
        context.put("page", page);
        context.putAll(model.asMap());

        siteTemplate.render(context, out);
    }

}