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: @SuppressWarnings("deprecation")
077: public static void saveItem(AuthProfile auth, String key,
078: String value) throws Exception {
079: if (key == null || value == null || key.trim().equals("")) {
080: return;
081: }
082: IGenericDao dao = null;
083: try {
084: dao = Utility.getDbConnection();
085:
086: String username = auth.getUsername();
087: String sql = "SELECT * FROM USER_PREFERENCES WHERE USERNAME=? AND KEYWORD = ?";
088: UserPreference pref = (UserPreference) dao.read(
089: UserPreference.class, sql, new Object[] { username,
090: key });
091: if (pref == null) {
092: // it is an insert
093: pref = new UserPreference();
094: pref.setKeyword((key == null) ? "" : key);
095: pref.setUsername((username == null) ? "" : username);
096: pref.setPrefValue((value == null) ? "" : value);
097:
098: IObjectMappingKey myObj = Constants.persistMan
099: .getObjectMappingFactory().createInstance(
100: UserPreference.class,
101: new AutoGeneratedColumnsMapper(true));
102:
103: dao.insert(myObj, pref);
104: } else {
105: // it is an update
106: pref.setKeyword(key);
107: pref.setUsername(username);
108: pref.setPrefValue(value);
109: dao.update(pref);
110: }
111: } finally {
112: JdbcUtil.close(dao);
113: dao = null;
114: }
115: }
116: }
|