Kawai Wiki

Rev

Rev 229 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.kawai;

import java.util.List;

import org.kawai.action.FileAction;
import org.kawai.action.LoginAction;
import org.kawai.action.PageAction;
import org.kawai.action.UserAction;
import org.kawai.dao.jdbc.JdbcPageDAO;
import org.kawai.dao.jdbc.JdbcUserDAO;
import org.mentawai.filter.GeoFilter;
import org.kawai.model.Group;
import org.kawai.model.Language;
import org.kawai.model.Page;
import org.kawai.model.Preview;
import org.kawai.model.Revision;
import org.kawai.model.User;
import org.kawai.tag.kcode.KCodeHandler;
import org.mentabean.DBTypes;
import org.mentawai.action.LogoutAction;
import org.mentawai.ajax.renderer.JsonRenderer;
import org.mentawai.ajax.renderer.ResultRenderer;
import org.mentawai.core.ActionConfig;
import org.mentawai.core.ApplicationManager;
import org.mentawai.core.Context;
import org.mentawai.core.Filter;
import org.mentawai.core.Props;
import org.mentawai.db.BoneCPConnectionHandler;
import org.mentawai.db.ConnectionHandler;
import org.mentawai.filter.AuthenticationFilter;
import org.mentawai.filter.AuthorizationFilter;
import org.mentawai.filter.ExceptionFilter;
import org.mentawai.filter.FileUploadFilter;
import org.mentawai.filter.FlashScopeFilter;
import org.mentawai.filter.MentaContainerFilter;
import org.mentawai.filter.TransactionFilter;
import org.mentawai.filter.ValidationFilter;
import org.mentawai.i18n.LocaleManager;
import org.mentawai.transaction.JdbcTransaction;

/**
 * Mentawai ApplicationManager class AppManager.
 *
 * @author Sergio Oliveira
 *
 */

public class AppManager extends ApplicationManager {

    /** Attribute props of AppManager. */
    private Props props;

    /** Attribute connHandler of AppManager. */
    private ConnectionHandler connHandler;

    public ConnectionHandler getConnHandler() {

        return connHandler;
    }

    /**
     * ${@inheritDoc}.
     */

    @Override
    public void init(Context application) {

        ApplicationManager.DEFAULT_ENVIRONMENT = Environment.PROD;

        this.props = getProps();

        ///////////////////////////////////////////////////
        // TURN ON/OFF APP MANAGER AUTO-REDEPLOY FEATURE
        // OBS: Requires http://www.javarebel.com to work
        ///////////////////////////////////////////////////
        setReloadable(props.getBoolean("auto_reload"));

        ////////////////////////////////////////////////
        // Setting up supported kCodes tags
        ////////////////////////////////////////////////

        List<String> kCodeTags = props.getList("kcodes");
        for(String tag : kCodeTags) {
            KCodeHandler.add(tag);
        }
    }

    /**
     * ${@inheritDoc}.
     */

    @Override
    public void setupDB() {

        String driver = props.getString("jdbc.driver");
        String url = props.getString("jdbc.url");
        String user = props.getString("jdbc.user");
        String pass = props.getString("jdbc.pass");

        this.connHandler = new BoneCPConnectionHandler(driver, url, user, pass, "select username from Users limit 1");
    }

    /**
     * ${@inheritDoc}.
     */

    @Override
    public void loadBeans() {

        bean(User.class, "Users")
            .pk("id", DBTypes.AUTOINCREMENT)
            .field("username", DBTypes.STRING)
            .field("password", DBTypes.STRING)
            .field("groupId", "group_id", DBTypes.INTEGER);

        bean(Page.class, "Pages")
            .pk("id", DBTypes.AUTOINCREMENT)
            .field("name", DBTypes.STRING)
            .field("title", DBTypes.STRING)
            .field("body", DBTypes.STRING)
            .field("languageId", "language_id", DBTypes.INTEGER)
            .field("systemPage", "system_page", DBTypes.BOOLEANSTRING)
            .field("frontPage", "front_page", DBTypes.BOOLEANSTRING)
            .field("modifiedById", "modified_by", DBTypes.INTEGER)
            .field("modifiedOn", "modified_on", DBTypes.NOW_ON_UPDATE_TIMESTAMP)
            .field("createdById", "created_by", DBTypes.INTEGER)
            .field("createdOn", "created_on", DBTypes.NOW_ON_INSERT_TIMESTAMP)
            .field("deleted", DBTypes.BOOLEANSTRING);

        bean(Preview.class, "Previews")
            .pk("id", DBTypes.AUTOINCREMENT)
            .field("userId", "user_id", DBTypes.INTEGER)
            .field("title", DBTypes.STRING)
            .field("body", DBTypes.STRING)
            .field("createdOn", "created_on", DBTypes.NOW_ON_INSERT_TIMESTAMP)
            .field("name", DBTypes.STRING)
            .field("comment", DBTypes.STRING)
            .field("languageId", "language_id", DBTypes.INTEGER);

        bean(Revision.class, "Revisions")
            .pk("id", DBTypes.AUTOINCREMENT)
            .field("userId", "user_id", DBTypes.INTEGER)
            .field("newTitle", "new_title", DBTypes.STRING)
            .field("newBody", "new_body", DBTypes.STRING)
            .field("oldTitle", "old_title", DBTypes.STRING)
            .field("oldBody", "old_body", DBTypes.STRING)
            .field("createdOn", "created_on", DBTypes.AUTOTIMESTAMP)
            .field("name", DBTypes.STRING)
            .field("languageId", "language_id", DBTypes.INTEGER)
            .field("comment", DBTypes.STRING)
            .field("revision", DBTypes.INTEGER);

    }

    /**
     * ${@inheritDoc}.
     */

    @Override
    public void loadLocales() {

        Language.clear();

        if (!props.has("languages")) {

            Language l = new Language(1, "English", "en");
            Language.add(l);
            addLocale(l.getLocale());

        } else {

            List<String[]> languages = props.getArrays("languages");

            for(int i = 0; i < languages.size(); i++) {
                String[] array = languages.get(i);

                Language l = new Language(i + 1, array[0], array[1], array.length >= 3 ? array[2] : null);
                Language.add(l);
                addLocale(l.getLocale());
            }
        }

        LocaleManager.stopLocaleScan();
    }

    /**
     * ${@inheritDoc}.
     */

    @Override
    public void loadLists() {
        addList(Group.getListData());
        addList(Language.getListData());
    }

    /**
     * ${@inheritDoc}.
     */

    @Override
    public void loadFilters() {

        /////////////////////////////////////////////
        // GLOBAL FILTERS
        /////////////////////////////////////////////

        filter(new ExceptionFilter());
        on(EXCEPTION, fwd("/error.jsp"));

        filter(new MentaContainerFilter());

        filter(new FlashScopeFilter());

        //////////////////////////////////////////////////////////
        // AUTHENTICATION: ALL ACTIONS THAT DO NOT IMPLEMENT
        // THE AuthenticationFree INTERFACE WILL REQUIRE
        // AUTHENTICATION
        //////////////////////////////////////////////////////////
        filter(new AuthenticationFilter());
        on(LOGIN, redir("/"));

        filter(new ValidationFilter());

        filter(new TransactionFilter("transaction"));
       
        if (props.has("use_geo_filter") && props.getBoolean("use_geo_filter")) {
                filter(new GeoFilter());
        }
    }

    /**
     * ${@inheritDoc}.
     */

    @Override
    public void setupIoC() {

        ////////////////////////////////////////////////////////
        // INVERSION OF CONTROL: SET UP YOUR REPOSITORIES OR
        // ANY OTHER OBJECT IMPLEMENTATION YOU WANT TO MAKE
        // AVAILABLE THROUGH IOC (INVERSION OF CONTROL)
        ////////////////////////////////////////////////////////

        ioc("conn", connHandler);
        ioc("beanManager", getBeanManager()); // always return the same instance...
        ioc("userDAO", JdbcUserDAO.class);
        ioc("pageDAO", JdbcPageDAO.class);
        ioc("beanSession", props.getClass("mentabean.dialect"));
        ioc("transaction", JdbcTransaction.class);
    }

    /**
     * ${@inheritDoc}.
     */

    @Override
    public void loadActions() {

        on(AJAX, ajax(new JsonRenderer()));

        ActionConfig mainAction = action("/Page", PageAction.class)
                .prettyURLParams("name", "lang", "title")
                .on(SHOW, redir()) // for the pretty url
            .on(SUCCESS, fwd("/show_page.jsp"));

        on(INDEX, redir(mainAction));
        on(UPDATED, redir(mainAction));

        Filter adminFilter = new AuthorizationFilter("admin");
        Filter editorFilter = new AuthorizationFilter("admin", "editor");
        on(ACCESSDENIED, redir(mainAction));

        action("/User", UserAction.class, "add")
            .filter(adminFilter)
            .on(ERROR, chain(mainAction))
            .on(CREATED, redir(mainAction));

        action("/User", UserAction.class, "check")
            .filter(adminFilter)
            .all(ajax(new ResultRenderer()));

        action("/Login", LoginAction.class)
            .on(ERROR, chain(mainAction))
            .on(SUCCESS, redir(mainAction));

        action("/Logout", LogoutAction.class)
            .on(SUCCESS, redir(mainAction));

        action("/Page", PageAction.class, "get")
                .filter(editorFilter);


        action("/Page", PageAction.class, "getPreview")
                .filter(editorFilter);

        action("/Page", PageAction.class, "getRevisions")
                .filter(editorFilter);

        action("/Page", PageAction.class, "add")
            .filter(editorFilter)
            .on(ERROR, chain(mainAction))
            .on(CREATED, redir(mainAction));

        action("/Page", PageAction.class, "setFrontPage")
            .filter(adminFilter);

        action("/Page", PageAction.class, "delete")
            .filter(adminFilter);

        action("/Page", PageAction.class, "edit")
            .filter(editorFilter);

        action("/Page", PageAction.class, "list")
            .filter(editorFilter);

        action("/Page", PageAction.class, "discardPreview")
            .filter(editorFilter)
            .on(REMOVED, redir(mainAction));

        action("/Page", PageAction.class, "savePreview")
            .filter(editorFilter)
            .on(SUCCESS, redir(mainAction));

        action("/Page", PageAction.class, "getCSS")
            .on(SUCCESS, fwd("/generate_css.jsp"));

        action("/Page", PageAction.class, "getPrintCSS")
            .on(SUCCESS, fwd("/generate_css.jsp"));

        action("/File", FileAction.class, "upload")
            .filter(editorFilter)
            .filter(new FileUploadFilter())
            .on(SUCCESS, redir(mainAction));

        action("/File", FileAction.class, "listFiles")
            .filter(editorFilter);

    }
}