1 package org.contineo.core.config.dao; 2 3 import org.apache.log4j.Level; 4 import org.apache.log4j.Logger; 5 import org.apache.ojb.broker.PBKey; 6 import org.apache.ojb.broker.PersistenceBroker; 7 import org.apache.ojb.broker.PersistenceBrokerFactory; 8 import org.apache.ojb.broker.query.Criteria; 9 import org.apache.ojb.broker.query.Query; 10 import org.apache.ojb.broker.query.QueryByCriteria; 11 import org.contineo.core.LoggingManager; 12 import org.contineo.core.config.SearchSettings; 13 14 18 public class SearchSettingsDAO { 19 20 private PersistenceBroker broker = null; 21 private PBKey pbkey; 22 private Logger logger; 23 24 27 public SearchSettingsDAO() { 28 logger = LoggingManager.getLogger(this.getClass()); 29 pbkey = new PBKey("contineo"); 30 } 31 32 36 public boolean store(SearchSettings settings) { 37 boolean result = true; 38 try { 39 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 40 broker.clearCache(); 41 broker.beginTransaction(); 42 broker.store(settings); 43 broker.commitTransaction(); 44 } 45 catch (Exception e) { 46 broker.abortTransaction(); 47 if (logger.isEnabledFor(Level.ERROR)) 48 logger.error(e.getMessage()); 49 result = false; 50 } finally { 51 if (broker != null) broker.close(); 52 } 53 return result; 54 } 55 56 61 public SearchSettings findByPrimaryKey(String username) { 62 SearchSettings settings = null; 63 try { 64 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 65 Criteria crit = new Criteria(); 66 crit.addEqualTo("co_username", username); 67 Query query = new QueryByCriteria(SearchSettings.class, crit); 68 settings = (SearchSettings)broker.getObjectByQuery(query); 69 if (settings == null) { 70 settings = new SearchSettings(); 71 settings.setDefault(); 72 } 73 } 74 catch (Exception e) { 75 if (logger.isEnabledFor(Level.ERROR)) 76 logger.error(e.getMessage()); 77 settings = new SearchSettings(); 78 settings.setDefault(); 79 } finally { 80 if (broker != null) broker.close(); 81 } 82 return settings; 83 } 84 } 85 | Popular Tags |