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: public static void savePreference(Preference pref) throws Exception {
43: IGenericDao dao = null;
44: try {
45: dao = Utility.getDbConnection();
46:
47: Preference tmp = getPreferenceByUserKey(pref.getUsername(),
48: pref.getPrefKey());
49: if (tmp != null) {
50: pref.setId(tmp.getId());
51: }
52: if (pref.getId() == null) {
53: IObjectMappingKey myObj = Utility.persistMan
54: .getObjectMappingFactory().createInstance(
55: Preference.class,
56: new AutoGeneratedColumnsMapper(true));
57: dao.insert(myObj, pref);
58: } else {
59: dao.update(pref);
60: }
61: } finally {
62: JdbcUtil.close(dao);
63: dao = null;
64: }
65: }
66:
67: public static Preference getPreferenceByUserKey(String user,
68: String key) throws Exception {
69: IGenericDao dao = null;
70: Preference tmp = null;
71: try {
72: dao = Utility.getDbConnection();
73: String sql = "SELECT * FROM PREFERENCES WHERE USERNAME = ? AND PREF_KEY = ?";
74: tmp = (Preference) dao.read(Preference.class, sql,
75: new Object[] { user, key });
76: } finally {
77: JdbcUtil.close(dao);
78: dao = null;
79: }
80: return tmp;
81: }
82:
83: }
|