001: package org.claros.mini.controllers;
002:
003: import java.util.HashMap;
004: import java.util.List;
005:
006: import org.claros.commons.models.AuthProfile;
007: import org.claros.mini.models.UserSetting;
008: import org.claros.mini.utility.Constants;
009: import org.claros.mini.utility.Utility;
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 USERSETTINGS where user=?";
039: List prefs = dao.readList(UserSetting.class, sql,
040: new Object[] { username });
041:
042: if (prefs != null) {
043: UserSetting tmp = null;
044: for (int i = 0; i < prefs.size(); i++) {
045: tmp = (UserSetting) prefs.get(i);
046: prefMap.put(tmp.getKeyword(), tmp.getValue());
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: IGenericDao dao = null;
079: try {
080: dao = Utility.getDbConnection();
081:
082: String username = auth.getUsername();
083: String sql = "select * from USERSETTINGS where user=? and keyword = ?";
084: UserSetting pref = (UserSetting) dao.read(
085: UserSetting.class, sql, new Object[] { username,
086: key });
087: if (pref == null) {
088: // it is an insert
089: pref = new UserSetting();
090: pref.setKeyword((key == null) ? "" : key);
091: pref.setUser((username == null) ? "" : username);
092: pref.setValue((value == null) ? "" : value);
093:
094: IObjectMappingKey myObj = Constants.persistMan
095: .getObjectMappingFactory().createInstance(
096: UserSetting.class,
097: new AutoGeneratedColumnsMapper(true));
098:
099: dao.insert(myObj, pref);
100: } else {
101: // it is an update
102: pref.setKeyword(key);
103: pref.setUser(username);
104: pref.setValue(value);
105: dao.update(pref);
106: }
107: } finally {
108: JdbcUtil.close(dao);
109: dao = null;
110: }
111: }
112: }
|