001: package org.claros.intouch.preferences.controllers;
002:
003: import java.util.HashMap;
004: import java.util.List;
005:
006: import org.claros.commons.auth.models.AuthProfile;
007: import org.claros.intouch.common.utility.Constants;
008: import org.claros.intouch.common.utility.Utility;
009: import org.claros.intouch.preferences.models.UserPreference;
010:
011: import com.jenkov.mrpersister.impl.mapping.AutoGeneratedColumnsMapper;
012: import com.jenkov.mrpersister.itf.IGenericDao;
013: import com.jenkov.mrpersister.itf.mapping.IObjectMappingKey;
014: import com.jenkov.mrpersister.util.JdbcUtil;
015:
016: /**
017: * @author Umut Gokbayrak
018: */
019: public class UserPrefsController {
020: public UserPrefsController() {
021: super ();
022: }
023:
024: /**
025: * Fetches all user preferences from DB.
026: * @param auth
027: * @return
028: * @throws Exception
029: */
030: public static HashMap getUserPreferences(AuthProfile auth)
031: throws Exception {
032: HashMap prefMap = new HashMap();
033: IGenericDao dao = null;
034: try {
035: dao = Utility.getDbConnection();
036:
037: String username = auth.getUsername();
038: String sql = "SELECT * FROM USER_PREFERENCES WHERE USERNAME=?";
039: List prefs = dao.readList(UserPreference.class, sql,
040: new Object[] { username });
041:
042: if (prefs != null) {
043: UserPreference tmp = null;
044: for (int i = 0; i < prefs.size(); i++) {
045: tmp = (UserPreference) prefs.get(i);
046: prefMap.put(tmp.getKeyword(), tmp.getPrefValue());
047: }
048: }
049: } finally {
050: JdbcUtil.close(dao);
051: dao = null;
052: }
053: return prefMap;
054: }
055:
056: /**
057: * Retrieves a single preference setting from DB.
058: * @param auth
059: * @param key
060: * @return
061: * @throws Exception
062: */
063: public static String getUserSetting(AuthProfile auth, String key)
064: throws Exception {
065: HashMap map = getUserPreferences(auth);
066: return (String) map.get(key);
067: }
068:
069: /**
070: * Saves a preference setting row into DB.
071: * @param auth
072: * @param key
073: * @param value
074: * @throws Exception
075: */
076: public static void saveItem(AuthProfile auth, String key,
077: String value) throws Exception {
078: if (key == null || value == null || key.trim().equals("")) {
079: return;
080: }
081: IGenericDao dao = null;
082: try {
083: dao = Utility.getDbConnection();
084:
085: String username = auth.getUsername();
086: String sql = "SELECT * FROM USER_PREFERENCES WHERE USERNAME=? AND KEYWORD = ?";
087: UserPreference pref = (UserPreference) dao.read(
088: UserPreference.class, sql, new Object[] { username,
089: key });
090: if (pref == null) {
091: // it is an insert
092: pref = new UserPreference();
093: pref.setKeyword((key == null) ? "" : key);
094: pref.setUsername((username == null) ? "" : username);
095: pref.setPrefValue((value == null) ? "" : value);
096:
097: IObjectMappingKey myObj = Constants.persistMan
098: .getObjectMappingFactory().createInstance(
099: UserPreference.class,
100: new AutoGeneratedColumnsMapper(true));
101:
102: dao.insert(myObj, pref);
103: } else {
104: // it is an update
105: pref.setKeyword(key);
106: pref.setUsername(username);
107: pref.setPrefValue(value);
108: dao.update(pref);
109: }
110: } finally {
111: JdbcUtil.close(dao);
112: dao = null;
113: }
114: }
115: }
|