01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package info.jtrac.wicket;
18:
19: import info.jtrac.Jtrac;
20: import info.jtrac.domain.ItemSearch;
21: import info.jtrac.domain.Space;
22: import info.jtrac.domain.User;
23: import org.apache.wicket.Component;
24: import org.apache.wicket.model.StringResourceModel;
25:
26: /**
27: * this common class helps to make things easier for the sub-classes
28: * of BasePage and BasePanel to perform common routines and keeps
29: * code in one place, reducing duplication
30: */
31: public class ComponentUtils {
32:
33: public static Jtrac getJtrac(Component c) {
34: return ((JtracApplication) c.getApplication()).getJtrac();
35: }
36:
37: public static User getPrincipal(Component c) {
38: return ((JtracSession) c.getSession()).getUser();
39: }
40:
41: public static void setCurrentSpace(Component c, Space space) {
42: ((JtracSession) c.getSession()).setCurrentSpace(space);
43: }
44:
45: public static Space getCurrentSpace(Component c) {
46: return ((JtracSession) c.getSession()).getCurrentSpace();
47: }
48:
49: public static void setCurrentItemSearch(Component c,
50: ItemSearch itemSearch) {
51: ((JtracSession) c.getSession()).setItemSearch(itemSearch);
52: }
53:
54: public static ItemSearch getCurrentItemSearch(Component c) {
55: return ((JtracSession) c.getSession()).getItemSearch();
56: }
57:
58: /**
59: * conditional flip of session if same user id
60: */
61: public static void refreshPrincipal(Component c, User user) {
62: if (user.getId() == getPrincipal(c).getId()) {
63: refreshPrincipal(c);
64: }
65: }
66:
67: public static void refreshPrincipal(Component c) {
68: User temp = getJtrac(c).loadUser(getPrincipal(c).getId());
69: // loadUserByUsername forces hibernate eager load
70: ((JtracSession) c.getSession()).setUser((User) getJtrac(c)
71: .loadUserByUsername(temp.getLoginName()));
72: }
73:
74: /**
75: * localization helper
76: */
77: public static String localize(Component c, String key) {
78: return c.getLocalizer().getString(key, null);
79: }
80:
81: public static String localize(Component c, String key,
82: Object... params) {
83: // integer params cause problems, go with String only
84: StringResourceModel m = new StringResourceModel(key, null,
85: null, params);
86: m.setLocalizer(c.getLocalizer());
87: return m.getString();
88: }
89:
90: }
|