1 16 package dlog4j; 17 18 import java.sql.SQLException ; 19 import java.text.DateFormat ; 20 import java.text.ParseException ; 21 import java.text.SimpleDateFormat ; 22 import java.util.Date ; 23 import java.util.List ; 24 25 import dlog4j.formbean.ParamForm; 26 import dlog4j.formbean.SiteForm; 27 28 import net.sf.hibernate.HibernateException; 29 import net.sf.hibernate.Session; 30 import net.sf.hibernate.expression.Expression; 31 32 36 public class ParamManager { 37 38 public final static String LOGS_PER_PAGE = "LOGS_PER_PAGE"; 39 40 47 public static int getLogPerPage(Session ssn, SiteForm site) throws Exception { 48 return getIntParam(ssn,site,LOGS_PER_PAGE,5); 49 } 50 57 public static List listParams(Session ssn, SiteForm site) 58 throws HibernateException { 59 int siteid = (site != null) ? site.getId() : -1; 60 return ssn 61 .createCriteria(ParamForm.class) 62 .add(Expression.eq("site.id", new Integer (siteid))) 63 .list(); 64 } 65 74 public static ParamForm getParam(Session ssn, SiteForm site, String param) 75 throws SQLException , HibernateException { 76 int siteid = (site == null) ? 0 : site.getId(); 77 ParamForm p = null; 78 List params = 79 ssn 80 .createCriteria(ParamForm.class) 81 .add(Expression.eq("name", param)) 82 .add(Expression.eq("site.id", new Integer (siteid))) 83 .list(); 84 if (params.size() > 0) 85 p = (ParamForm) params.get(0); 86 return p; 87 } 88 97 public static int getIntParam( 98 Session ssn, 99 SiteForm site, 100 String param, 101 int defValue) 102 throws SQLException , HibernateException { 103 ParamForm p = getParam(ssn, site, param); 104 if (p == null 105 || p.getValue() == null 106 || p.getValue().trim().length() == 0) 107 return defValue; 108 return Integer.parseInt(p.getValue()); 109 } 110 119 public static String getStringParam( 120 Session ssn, 121 SiteForm site, 122 String param, 123 String defValue) 124 throws SQLException , HibernateException { 125 ParamForm p = getParam(ssn, site, param); 126 if (p == null || p.getValue() == null) 127 return defValue; 128 return p.getValue(); 129 } 130 139 public static Date getDateParam( 140 Session ssn, 141 SiteForm site, 142 String param, 143 Date defValue) 144 throws SQLException , HibernateException, ParseException { 145 ParamForm p = getParam(ssn, site, param); 146 if (p == null || p.getValue() == null) 147 return defValue; 148 return FMT_DATE.parse(p.getValue()); 149 } 150 159 public static Date getTimeParam( 160 Session ssn, 161 SiteForm site, 162 String param, 163 Date defValue) 164 throws SQLException , HibernateException, ParseException { 165 ParamForm p = getParam(ssn, site, param); 166 if (p == null || p.getValue() == null) 167 return defValue; 168 return FMT_TIME.parse(p.getValue()); 169 } 170 179 public static Date getDateTimeParam( 180 Session ssn, 181 SiteForm site, 182 String param, 183 Date defValue) 184 throws SQLException , HibernateException, ParseException { 185 ParamForm p = getParam(ssn, site, param); 186 if (p == null || p.getValue() == null) 187 return defValue; 188 return FMT_DATETIME.parse(p.getValue()); 189 } 190 191 public final static DateFormat FMT_TIME = new SimpleDateFormat ("hh:mm:ss"); 192 public final static DateFormat FMT_DATE = 193 new SimpleDateFormat ("yyyy-MM-dd"); 194 public final static DateFormat FMT_DATETIME = 195 new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss"); 196 197 } 198 | Popular Tags |