Rev 143 |
Rev 149 |
Go to most recent revision |
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.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.C3P0ConnectionHandler;
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.MentaContainerFilter;
import org.mentawai.filter.TransactionFilter;
import org.mentawai.filter.ValidationFilter;
import org.mentawai.i18n.LocaleManager;
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 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
);
}
}
@
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
);
}
@
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);
}
@
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();
}
@
Override
public void loadLists
() {
addList
(Group.
getListData());
addList
(Language.
getListData());
}
@
Override
public void loadFilters
() {
/////////////////////////////////////////////
// GLOBAL FILTERS
/////////////////////////////////////////////
filter
(new ExceptionFilter
());
on
(EXCEPTION, fwd
("/error.jsp"));
filter
(new MentaContainerFilter
());
//////////////////////////////////////////////////////////
// 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"));
}
@
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));
Filter adminFilter =
new AuthorizationFilter
("admin");
Filter editorFilter =
new AuthorizationFilter
("admin",
"editor");
on
(ACCESSDENIED, redir
(mainAction
));
action
("/User", UserAction.
class,
"add")
.
filter(adminFilter
)
.
on(ERROR, fwd
("/jsp/user/add.jsp"))
.
on(CREATED, fwd
("/frontpage.jsp"));
action
("/User", UserAction.
class,
"check")
.
filter(adminFilter
)
.
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")
.
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,
true));
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,
true));
action
("/Page", PageAction.
class,
"savePreview")
.
filter(editorFilter
)
.
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(editorFilter
)
.
filter(new FileUploadFilter
())
.
on(SUCCESS, redir
(mainAction,
true));
action
("/File", FileAction.
class,
"listFiles")
.
filter(editorFilter
);
}
}