Kawai Wiki

Rev

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