PreferencesController.java :  » Chat » claros-intouch » org » claros » chat » controllers » Java Open Source

Java Open Source » Chat » claros intouch 
claros intouch » org » claros » chat » controllers » PreferencesController.java
package org.claros.chat.controllers;

import java.util.List;

import org.claros.chat.models.Preference;
import org.claros.chat.utility.Utility;

import com.jenkov.mrpersister.impl.mapping.AutoGeneratedColumnsMapper;
import com.jenkov.mrpersister.itf.IGenericDao;
import com.jenkov.mrpersister.itf.mapping.IObjectMappingKey;
import com.jenkov.mrpersister.util.JdbcUtil;

public class PreferencesController {
  
  /**
   * 
   * @param user
   * @return
   * @throws Exception
   */
  public static List getPreferencesByUser(String user) throws Exception {
    IGenericDao dao = null;
    List myList = null;
    try {
      dao = Utility.getDbConnection();
      String sql = "SELECT * FROM PREFERENCES WHERE USERNAME = ?";
      myList = dao.readList(Preference.class, sql, new Object[] {user});
    } finally {
      JdbcUtil.close(dao);
      dao = null;
    }
    return myList;
  }

  /**
   * 
   * @param pref
   * @throws Exception
   */
  public static void savePreference(Preference pref) throws Exception {
    IGenericDao dao = null;
    try {
      dao = Utility.getDbConnection();
      
      Preference tmp = getPreferenceByUserKey(pref.getUsername(), pref.getPrefKey());
      if (tmp != null) {
        pref.setId(tmp.getId());
      }
      if (pref.getId() == null) {
        IObjectMappingKey myObj = Utility.persistMan.getObjectMappingFactory().createInstance(Preference.class, new AutoGeneratedColumnsMapper(true));
        dao.insert(myObj, pref);
      } else {
        dao.update(pref);
      }
    } finally {
      JdbcUtil.close(dao);
      dao = null;
    }
  }

  public static Preference getPreferenceByUserKey(String user, String key) throws Exception {
    IGenericDao dao = null;
    Preference tmp = null;
    try {
      dao = Utility.getDbConnection();
      String sql = "SELECT * FROM PREFERENCES WHERE USERNAME = ? AND PREF_KEY = ?";
      tmp = (Preference)dao.read(Preference.class, sql, new Object[] {user, key});
    } finally {
      JdbcUtil.close(dao);
      dao = null;
    }
    return tmp;
  }
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.