Kawai Wiki

Rev

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

Rev Author Line No. Line
8 soliveira 1
package org.kawai;
6 soliveira 2
 
113 soliveira 3
import java.util.List;
6 soliveira 4
 
104 soliveira 5
import org.kawai.action.FileAction;
8 soliveira 6
import org.kawai.action.LoginAction;
11 soliveira 7
import org.kawai.action.PageAction;
8 soliveira 8
import org.kawai.action.UserAction;
11 soliveira 9
import org.kawai.dao.jdbc.JdbcPageDAO;
8 soliveira 10
import org.kawai.dao.jdbc.JdbcUserDAO;
230 soliveira 11
import org.mentawai.filter.GeoFilter;
138 soliveira 12
import org.kawai.model.Group;
8 soliveira 13
import org.kawai.model.Language;
11 soliveira 14
import org.kawai.model.Page;
60 soliveira 15
import org.kawai.model.Preview;
78 soliveira 16
import org.kawai.model.Revision;
8 soliveira 17
import org.kawai.model.User;
113 soliveira 18
import org.kawai.tag.kcode.KCodeHandler;
6 soliveira 19
import org.mentabean.DBTypes;
20
import org.mentawai.action.LogoutAction;
11 soliveira 21
import org.mentawai.ajax.renderer.JsonRenderer;
18 soliveira 22
import org.mentawai.ajax.renderer.ResultRenderer;
11 soliveira 23
import org.mentawai.core.ActionConfig;
6 soliveira 24
import org.mentawai.core.ApplicationManager;
25
import org.mentawai.core.Context;
133 soliveira 26
import org.mentawai.core.Filter;
6 soliveira 27
import org.mentawai.core.Props;
182 soliveira 28
import org.mentawai.db.BoneCPConnectionHandler;
6 soliveira 29
import org.mentawai.db.ConnectionHandler;
30
import org.mentawai.filter.AuthenticationFilter;
133 soliveira 31
import org.mentawai.filter.AuthorizationFilter;
6 soliveira 32
import org.mentawai.filter.ExceptionFilter;
101 soliveira 33
import org.mentawai.filter.FileUploadFilter;
149 soliveira 34
import org.mentawai.filter.FlashScopeFilter;
6 soliveira 35
import org.mentawai.filter.MentaContainerFilter;
36
import org.mentawai.filter.TransactionFilter;
49 soliveira 37
import org.mentawai.filter.ValidationFilter;
139 soliveira 38
import org.mentawai.i18n.LocaleManager;
6 soliveira 39
import org.mentawai.transaction.JdbcTransaction;
40
 
194 helio.frota 41
/**
42
 * Mentawai ApplicationManager class AppManager.
43
 *
44
 * @author Sergio Oliveira
45
 *
46
 */
6 soliveira 47
public class AppManager extends ApplicationManager {
48
 
194 helio.frota 49
    /** Attribute props of AppManager. */
50
    private Props props;
51
 
52
    /** Attribute connHandler of AppManager. */
53
    private ConnectionHandler connHandler;
54
 
55
    public ConnectionHandler getConnHandler() {
56
 
57
        return connHandler;
58
    }
59
 
60
    /**
61
     * ${@inheritDoc}.
62
     */
63
    @Override
64
    public void init(Context application) {
65
 
66
        ApplicationManager.DEFAULT_ENVIRONMENT = Environment.PROD;
67
 
68
        this.props = getProps();
69
 
70
        ///////////////////////////////////////////////////
71
        // TURN ON/OFF APP MANAGER AUTO-REDEPLOY FEATURE
6 soliveira 72
        // OBS: Requires http://www.javarebel.com to work
194 helio.frota 73
        ///////////////////////////////////////////////////
74
        setReloadable(props.getBoolean("auto_reload"));
6 soliveira 75
 
194 helio.frota 76
        ////////////////////////////////////////////////
77
        // Setting up supported kCodes tags
78
        ////////////////////////////////////////////////
78 soliveira 79
 
194 helio.frota 80
        List<String> kCodeTags = props.getList("kcodes");
81
        for(String tag : kCodeTags) {
82
            KCodeHandler.add(tag);
83
        }
84
    }
137 soliveira 85
 
194 helio.frota 86
    /**
87
     * ${@inheritDoc}.
88
     */
6 soliveira 89
    @Override
194 helio.frota 90
    public void setupDB() {
91
 
92
        String driver = props.getString("jdbc.driver");
93
        String url = props.getString("jdbc.url");
94
        String user = props.getString("jdbc.user");
95
        String pass = props.getString("jdbc.pass");
96
 
97
        this.connHandler = new BoneCPConnectionHandler(driver, url, user, pass, "select username from Users limit 1");
98
    }
99
 
100
    /**
101
     * ${@inheritDoc}.
102
     */
103
    @Override
104
    public void loadBeans() {
105
 
106
        bean(User.class, "Users")
107
            .pk("id", DBTypes.AUTOINCREMENT)
108
            .field("username", DBTypes.STRING)
109
            .field("password", DBTypes.STRING)
110
            .field("groupId", "group_id", DBTypes.INTEGER);
111
 
112
        bean(Page.class, "Pages")
113
            .pk("id", DBTypes.AUTOINCREMENT)
114
            .field("name", DBTypes.STRING)
115
            .field("title", DBTypes.STRING)
116
            .field("body", DBTypes.STRING)
117
            .field("languageId", "language_id", DBTypes.INTEGER)
118
            .field("systemPage", "system_page", DBTypes.BOOLEANSTRING)
119
            .field("frontPage", "front_page", DBTypes.BOOLEANSTRING)
120
            .field("modifiedById", "modified_by", DBTypes.INTEGER)
121
            .field("modifiedOn", "modified_on", DBTypes.NOW_ON_UPDATE_TIMESTAMP)
122
            .field("createdById", "created_by", DBTypes.INTEGER)
123
            .field("createdOn", "created_on", DBTypes.NOW_ON_INSERT_TIMESTAMP)
124
            .field("deleted", DBTypes.BOOLEANSTRING);
125
 
126
        bean(Preview.class, "Previews")
127
            .pk("id", DBTypes.AUTOINCREMENT)
128
            .field("userId", "user_id", DBTypes.INTEGER)
129
            .field("title", DBTypes.STRING)
130
            .field("body", DBTypes.STRING)
131
            .field("createdOn", "created_on", DBTypes.NOW_ON_INSERT_TIMESTAMP)
132
            .field("name", DBTypes.STRING)
133
            .field("comment", DBTypes.STRING)
134
            .field("languageId", "language_id", DBTypes.INTEGER);
135
 
136
        bean(Revision.class, "Revisions")
137
            .pk("id", DBTypes.AUTOINCREMENT)
138
            .field("userId", "user_id", DBTypes.INTEGER)
139
            .field("newTitle", "new_title", DBTypes.STRING)
140
            .field("newBody", "new_body", DBTypes.STRING)
141
            .field("oldTitle", "old_title", DBTypes.STRING)
142
            .field("oldBody", "old_body", DBTypes.STRING)
143
            .field("createdOn", "created_on", DBTypes.AUTOTIMESTAMP)
144
            .field("name", DBTypes.STRING)
145
            .field("languageId", "language_id", DBTypes.INTEGER)
146
            .field("comment", DBTypes.STRING)
147
            .field("revision", DBTypes.INTEGER);
148
 
149
    }
150
 
151
    /**
152
     * ${@inheritDoc}.
153
     */
154
    @Override
155
    public void loadLocales() {
156
 
157
        Language.clear();
158
 
159
        if (!props.has("languages")) {
160
 
161
            Language l = new Language(1, "English", "en");
162
            Language.add(l);
163
            addLocale(l.getLocale());
164
 
165
        } else {
166
 
167
            List<String[]> languages = props.getArrays("languages");
168
 
169
            for(int i = 0; i < languages.size(); i++) {
170
                String[] array = languages.get(i);
171
 
172
                Language l = new Language(i + 1, array[0], array[1], array.length >= 3 ? array[2] : null);
173
                Language.add(l);
174
                addLocale(l.getLocale());
175
            }
176
        }
177
 
178
        LocaleManager.stopLocaleScan();
179
    }
180
 
181
    /**
182
     * ${@inheritDoc}.
183
     */
184
    @Override
6 soliveira 185
    public void loadLists() {
194 helio.frota 186
        addList(Group.getListData());
187
        addList(Language.getListData());
188
    }
6 soliveira 189
 
194 helio.frota 190
    /**
191
     * ${@inheritDoc}.
192
     */
193
    @Override
194
    public void loadFilters() {
49 soliveira 195
 
194 helio.frota 196
        /////////////////////////////////////////////
197
        // GLOBAL FILTERS
198
        /////////////////////////////////////////////
199
 
200
        filter(new ExceptionFilter());
201
        on(EXCEPTION, fwd("/error.jsp"));
202
 
203
        filter(new MentaContainerFilter());
204
 
205
        filter(new FlashScopeFilter());
206
 
207
        //////////////////////////////////////////////////////////
208
        // AUTHENTICATION: ALL ACTIONS THAT DO NOT IMPLEMENT
209
        // THE AuthenticationFree INTERFACE WILL REQUIRE
210
        // AUTHENTICATION
211
        //////////////////////////////////////////////////////////
212
        filter(new AuthenticationFilter());
213
        on(LOGIN, redir("/"));
214
 
215
        filter(new ValidationFilter());
216
 
217
        filter(new TransactionFilter("transaction"));
219 soliveira 218
 
219
        if (props.has("use_geo_filter") && props.getBoolean("use_geo_filter")) {
220
                filter(new GeoFilter());
221
        }
194 helio.frota 222
    }
223
 
224
    /**
225
     * ${@inheritDoc}.
226
     */
227
    @Override
228
    public void setupIoC() {
229
 
230
        ////////////////////////////////////////////////////////
231
        // INVERSION OF CONTROL: SET UP YOUR REPOSITORIES OR
232
        // ANY OTHER OBJECT IMPLEMENTATION YOU WANT TO MAKE
233
        // AVAILABLE THROUGH IOC (INVERSION OF CONTROL)
234
        ////////////////////////////////////////////////////////
235
 
236
        ioc("conn", connHandler);
237
        ioc("beanManager", getBeanManager()); // always return the same instance...
238
        ioc("userDAO", JdbcUserDAO.class);
239
        ioc("pageDAO", JdbcPageDAO.class);
240
        ioc("beanSession", props.getClass("mentabean.dialect"));
241
        ioc("transaction", JdbcTransaction.class);
242
    }
243
 
244
    /**
245
     * ${@inheritDoc}.
246
     */
247
    @Override
248
    public void loadActions() {
249
 
250
        on(AJAX, ajax(new JsonRenderer()));
251
 
252
        ActionConfig mainAction = action("/Page", PageAction.class)
229 soliveira 253
                .prettyURLParams("name", "lang", "title")
206 soliveira 254
                .on(SHOW, redir()) // for the pretty url
194 helio.frota 255
            .on(SUCCESS, fwd("/show_page.jsp"));
256
 
257
        on(INDEX, redir(mainAction));
206 soliveira 258
        on(UPDATED, redir(mainAction));
194 helio.frota 259
 
260
        Filter adminFilter = new AuthorizationFilter("admin");
261
        Filter editorFilter = new AuthorizationFilter("admin", "editor");
262
        on(ACCESSDENIED, redir(mainAction));
263
 
264
        action("/User", UserAction.class, "add")
265
            .filter(adminFilter)
266
            .on(ERROR, chain(mainAction))
267
            .on(CREATED, redir(mainAction));
268
 
269
        action("/User", UserAction.class, "check")
270
            .filter(adminFilter)
271
            .all(ajax(new ResultRenderer()));
272
 
273
        action("/Login", LoginAction.class)
274
            .on(ERROR, chain(mainAction))
206 soliveira 275
            .on(SUCCESS, redir(mainAction));
194 helio.frota 276
 
277
        action("/Logout", LogoutAction.class)
278
            .on(SUCCESS, redir(mainAction));
279
 
280
        action("/Page", PageAction.class, "get")
206 soliveira 281
                .filter(editorFilter);
194 helio.frota 282
 
283
 
284
        action("/Page", PageAction.class, "getPreview")
206 soliveira 285
                .filter(editorFilter);
194 helio.frota 286
 
287
        action("/Page", PageAction.class, "getRevisions")
206 soliveira 288
                .filter(editorFilter);
194 helio.frota 289
 
290
        action("/Page", PageAction.class, "add")
291
            .filter(editorFilter)
292
            .on(ERROR, chain(mainAction))
206 soliveira 293
            .on(CREATED, redir(mainAction));
194 helio.frota 294
 
295
        action("/Page", PageAction.class, "setFrontPage")
296
            .filter(adminFilter);
297
 
298
        action("/Page", PageAction.class, "delete")
299
            .filter(adminFilter);
300
 
301
        action("/Page", PageAction.class, "edit")
302
            .filter(editorFilter);
303
 
304
        action("/Page", PageAction.class, "list")
305
            .filter(editorFilter);
306
 
307
        action("/Page", PageAction.class, "discardPreview")
308
            .filter(editorFilter)
206 soliveira 309
            .on(REMOVED, redir(mainAction));
194 helio.frota 310
 
311
        action("/Page", PageAction.class, "savePreview")
312
            .filter(editorFilter)
206 soliveira 313
            .on(SUCCESS, redir(mainAction));
194 helio.frota 314
 
315
        action("/Page", PageAction.class, "getCSS")
316
            .on(SUCCESS, fwd("/generate_css.jsp"));
317
 
318
        action("/Page", PageAction.class, "getPrintCSS")
319
            .on(SUCCESS, fwd("/generate_css.jsp"));
320
 
321
        action("/File", FileAction.class, "upload")
322
            .filter(editorFilter)
323
            .filter(new FileUploadFilter())
206 soliveira 324
            .on(SUCCESS, redir(mainAction));
194 helio.frota 325
 
326
        action("/File", FileAction.class, "listFiles")
327
            .filter(editorFilter);
328
 
329
    }
330
}