01: package org.claros.chat.controllers;
02:
03: import java.util.List;
04:
05: import org.claros.chat.models.Preference;
06: import org.claros.chat.utility.Utility;
07:
08: import com.jenkov.mrpersister.impl.mapping.AutoGeneratedColumnsMapper;
09: import com.jenkov.mrpersister.itf.IGenericDao;
10: import com.jenkov.mrpersister.itf.mapping.IObjectMappingKey;
11: import com.jenkov.mrpersister.util.JdbcUtil;
12:
13: public class PreferencesController {
14:
15: /**
16: *
17: * @param user
18: * @return
19: * @throws Exception
20: */
21: public static List getPreferencesByUser(String user)
22: throws Exception {
23: IGenericDao dao = null;
24: List myList = null;
25: try {
26: dao = Utility.getDbConnection();
27: String sql = "SELECT * FROM PREFERENCES WHERE USERNAME = ?";
28: myList = dao.readList(Preference.class, sql,
29: new Object[] { user });
30: } finally {
31: JdbcUtil.close(dao);
32: dao = null;
33: }
34: return myList;
35: }
36:
37: /**
38: *
39: * @param pref
40: * @throws Exception
41: */
42: @SuppressWarnings("deprecation")
43: public static void savePreference(Preference pref) throws Exception {
44: IGenericDao dao = null;
45: try {
46: dao = Utility.getDbConnection();
47:
48: Preference tmp = getPreferenceByUserKey(pref.getUsername(),
49: pref.getPrefKey());
50: if (tmp != null) {
51: pref.setId(tmp.getId());
52: }
53: if (pref.getId() == null) {
54: IObjectMappingKey myObj = Utility.persistMan
55: .getObjectMappingFactory().createInstance(
56: Preference.class,
57: new AutoGeneratedColumnsMapper(true));
58: dao.insert(myObj, pref);
59: } else {
60: dao.update(pref);
61: }
62: } finally {
63: JdbcUtil.close(dao);
64: dao = null;
65: }
66: }
67:
68: public static Preference getPreferenceByUserKey(String user,
69: String key) throws Exception {
70: IGenericDao dao = null;
71: Preference tmp = null;
72: try {
73: dao = Utility.getDbConnection();
74: String sql = "SELECT * FROM PREFERENCES WHERE USERNAME = ? AND PREF_KEY = ?";
75: tmp = (Preference) dao.read(Preference.class, sql,
76: new Object[] { user, key });
77: } finally {
78: JdbcUtil.close(dao);
79: dao = null;
80: }
81: return tmp;
82: }
83:
84: }
|