Rev 104 |
Rev 113 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package org.kawai;
import java.sql.Connection;
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.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.mentabean.DBTypes;
import org.mentabean.util.SQLUtils;
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.Props;
import org.mentawai.db.C3P0ConnectionHandler;
import org.mentawai.db.ConnectionHandler;
import org.mentawai.filter.AuthenticationFilter;
import org.mentawai.filter.ExceptionFilter;
import org.mentawai.filter.FileUploadFilter;
import org.mentawai.filter.MentaContainerFilter;
import org.mentawai.filter.RedirectAfterLoginFilter;
import org.mentawai.filter.TransactionFilter;
import org.mentawai.filter.ValidationFilter;
import org.mentawai.mail.Email;
import org.mentawai.transaction.JdbcTransaction;
public class AppManager
extends ApplicationManager
{
private Props props
;
private ConnectionHandler connHandler
;
public ConnectionHandler getConnHandler
() {
return connHandler
;
}
@
Override
public void init
(Context application
) {
this.
props = getProps
();
////////////////////////////////////////////
// TURN ON/OFF DEBUG MODE
////////////////////////////////////////////
setDebugMode
(props.
getBoolean("debug_mode"),
true);
///////////////////////////////////////////////////
// TURN ON/OFF APP MANAGER AUTO-REDEPLOY FEATURE
// OBS: Requires http://www.javarebel.com to work
///////////////////////////////////////////////////
setReloadable
(props.
getBoolean("auto_reload"));
//////////////////////////////////////////
// FOR SENDING EMAIL
//////////////////////////////////////////
if (!props.
getBoolean("email.send_email")) {
Email.
setSendEmail(false);
} else {
Email.
setDefaultHostName(props.
getString("email.host"));
Email.
setDefaultSslConnection( props.
getBoolean("email.ssl") );
Email.
setDefaultPort( props.
getInt("email.port") );
if (props.
getBoolean("email.use_authentication")) {
Email.
setDefaultAuthentication(props.
getString("email.user"), props.
getString("email.pass"));
}
Email.
setDefaultFrom(props.
getString("email.from_email"), props.
getString("email.from_name"));
}
}
@
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 C3P0ConnectionHandler
(driver, url, user, pass
);
initDatabaseIfNeeded
(connHandler
);
}
@
Override
public void loadBeans
() {
bean
(User.
class,
"Users")
.
pk("id", DBTypes.
AUTOINCREMENT)
.
field("username", DBTypes.
STRING)
.
field("password", DBTypes.
STRING)
.
field("email", 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);
}
@
Override
public void loadLists
() {
addLocalizedLists
(connHandler,
"groups",
"languages");
}
@
Override
public void loadLocales
() {
addLocale
(Language.
ENGLISH.
getLocale());
addLocale
(Language.
PORTUGUESE.
getLocale());
}
@
Override
public void loadFilters
() {
/////////////////////////////////////////////
// GLOBAL FILTERS
/////////////////////////////////////////////
filter
(new ExceptionFilter
());
on
(EXCEPTION, fwd
("/jsp/error.jsp"));
filter
(new MentaContainerFilter
());
//////////////////////////////////////////////////////////
// AUTHENTICATION: ALL ACTIONS THAT DO NOT IMPLEMENT
// THE AuthenticationFree INTERFACE WILL REQUIRE
// AUTHENTICATION
//////////////////////////////////////////////////////////
filter
(new AuthenticationFilter
());
on
(LOGIN, redir
("/jsp/login.jsp"));
filter
(new RedirectAfterLoginFilter
());
on
(REDIR, redir
());
filter
(new ValidationFilter
());
filter
(new TransactionFilter
("transaction"));
}
@
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);
}
@
Override
public void loadActions
() {
on
(AJAX, ajax
(new JsonRenderer
()));
ActionConfig mainAction = action
("/Page", PageAction.
class)
.
on(SUCCESS, fwd
("/show_page.jsp"));
on
(INDEX, redir
(mainAction
));
on
(UPDATED, redir
(mainAction,
true));
action
("/User", UserAction.
class,
"add")
.
on(ERROR, fwd
("/jsp/user/add.jsp"))
.
on(CREATED, fwd
("/frontpage.jsp"));
action
("/User", UserAction.
class,
"edit")
.
on(ERROR, fwd
("/jsp/user/edit.jsp"))
.
on(SHOW, fwd
("/jsp/user/edit.jsp"))
.
on(UPDATED, fwd
("/frontpage.jsp"));
action
("/User", UserAction.
class,
"check")
.
all(ajax
(new ResultRenderer
()));
action
("/Login", LoginAction.
class)
.
on(ERROR, chain
(mainAction
))
.
on(SUCCESS, redir
(mainAction,
true));
action
("/Logout", LogoutAction.
class)
.
on(SUCCESS, redir
(mainAction
));
action
("/Page", PageAction.
class,
"get");
action
("/Page", PageAction.
class,
"getPreview");
action
("/Page", PageAction.
class,
"getRevisions");
action
("/Page", PageAction.
class,
"add")
.
on(ERROR, chain
(mainAction
))
.
on(CREATED, redir
(mainAction,
true));
action
("/Page", PageAction.
class,
"setFrontPage");
action
("/Page", PageAction.
class,
"delete");
action
("/Page", PageAction.
class,
"edit");
action
("/Page", PageAction.
class,
"list");
action
("/Page", PageAction.
class,
"discardPreview")
.
on(REMOVED, redir
(mainAction,
true));
action
("/Page", PageAction.
class,
"savePreview")
.
on(SUCCESS, redir
(mainAction,
true));
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(new FileUploadFilter
())
.
on(SUCCESS, redir
(mainAction,
true));
action
("/File", FileAction.
class,
"listFiles");
}
////////////////////////////////////////////////////////////////////////////////
// Database initialization so this app does not require any database setup
//
// NOTE: It is not necessary to do this if your database is already initialized
////////////////////////////////////////////////////////////////////////////////
private void initDatabaseIfNeeded
(ConnectionHandler connHandler
) {
connHandler.
exec(new ConnectionHandler.
Exec() {
@
Override
public void exec
(Connection conn
) {
initDatabaseIfNeeded
(conn
);
}
});
}
protected void initDatabaseIfNeeded
(Connection conn
) {
if (SQLUtils.
checkIfTableExists(conn,
"users")) return;
try {
String file = props.
getAbsolutePath("db.script");
SQLUtils.
executeScript(conn, file,
"UTF-8");
} catch(Exception e
) {
e.
printStackTrace();
}
if (!SQLUtils.
checkIfTableExists(conn,
"users")) throw new RuntimeException("Failed to initialize db!");
}
}