/kickup/trunk/src/ak/kickup/util/ModelSecurityException.java |
---|
0,0 → 1,10 |
package ak.kickup.util; |
public class ModelSecurityException |
extends ModelException |
{ |
public ModelSecurityException() |
{ |
super("ak.kickup.core.access.denied"); |
} |
} |
/kickup/trunk/src/ak/kickup/util/UserException.java |
---|
0,0 → 1,28 |
package ak.kickup.util; |
public class UserException |
extends Exception |
{ |
private Object[] values; |
public UserException() |
{ |
this(null, null); |
} |
public UserException(String message) |
{ |
this(message, null); |
} |
public UserException(String message, Object[] values) |
{ |
super(message); |
this.values = values; |
} |
public Object[] getValues() |
{ |
return values; |
} |
} |
/kickup/trunk/src/ak/kickup/util/StringConverter.java |
---|
0,0 → 1,119 |
package ak.kickup.util; |
import java.math.BigDecimal; |
import java.util.Date; |
import java.text.DateFormat; |
import java.text.NumberFormat; |
import java.text.DecimalFormat; |
import java.text.ParseException; |
public abstract class StringConverter |
{ |
public static boolean isEmpty(Object o) |
{ |
if(o == null) |
return true; |
else if((o instanceof String) && ((String)o).equals("")) |
return true; |
else |
return false; |
} |
public static String preparse(Object o) |
{ |
if(o instanceof String) { |
String s = (String)o; |
if(s == null || s.equals("")) |
return null; |
else |
return s; |
} |
else { |
throw new ClassCastException("String is expected, but it is " + o.getClass()); |
} |
} |
public static Long parseLong(Object o) |
throws NumberFormatException |
{ |
String s = preparse(o); |
return (s == null) ? null : new Long(s); |
} |
public static Integer parseInteger(Object o) |
throws NumberFormatException |
{ |
String s = preparse(o); |
return (s == null) ? null : new Integer(s); |
} |
public static BigDecimal parseNumber(Object o) |
throws NumberFormatException, ParseException |
{ |
String s = preparse(o); |
return (s == null) |
? null : new BigDecimal(NumberFormat.getInstance().parse(s).doubleValue()); |
} |
public static String toNumber(Number n) |
{ |
if(n == null) |
return null; |
else |
return (new DecimalFormat("0")).format(n); |
} |
public static BigDecimal parseCurrency(Object o) |
throws NumberFormatException, ParseException |
{ |
String s = preparse(o); |
return (s == null) |
? null : new BigDecimal(NumberFormat.getInstance().parse(s).doubleValue()); |
} |
public static String toCurrency(Number n) |
{ |
if(n == null) |
return null; |
else |
return (new DecimalFormat("0.00")).format(n); |
} |
public static Date parseDate(Object o) |
throws NumberFormatException, ParseException |
{ |
String s = preparse(o); |
return (s == null) ? null : DateFormat.getDateInstance().parse(s); |
} |
public static String toDate(Date d) |
{ |
if(d == null) |
return null; |
else |
return DateFormat.getDateInstance().format(d); |
} |
public static Date parseDateTime(Object o) |
throws NumberFormatException, ParseException |
{ |
String s = preparse(o); |
return (s == null) ? null : DateFormat.getDateTimeInstance().parse(s); |
} |
public static String toDateTime(Date d) |
{ |
if(d == null) |
return null; |
else |
return DateFormat.getDateTimeInstance().format(d); |
} |
public static String toString(Object o) |
{ |
if(o == null) |
return null; |
else |
return o.toString(); |
} |
} |
/kickup/trunk/src/ak/kickup/util/HibernateUtil.java |
---|
0,0 → 1,109 |
package ak.kickup.util; |
import net.sf.hibernate.*; |
import net.sf.hibernate.cfg.*; |
public class HibernateUtil |
{ |
private static Configuration configuration; |
private static SessionFactory sessionFactory; |
private static final ThreadLocal hibernateBean = new ThreadLocal(); |
public static Configuration getConfiguration() |
throws HibernateException |
{ |
if(configuration == null) |
configuration = new Configuration(); |
return configuration; |
} |
public static SessionFactory getSessionFactory() |
throws HibernateException |
{ |
if(sessionFactory == null) |
sessionFactory = getConfiguration().configure().buildSessionFactory(); |
return sessionFactory; |
} |
private static HibernateBean currentBean() |
throws HibernateException |
{ |
HibernateBean hb = (HibernateBean)hibernateBean.get(); |
if(hb == null) { |
hb = new HibernateBean(); |
hb.session = getSessionFactory().openSession(); |
hibernateBean.set(hb); |
} |
return hb; |
} |
public static Session currentSession() |
throws HibernateException |
{ |
return currentBean().session; |
} |
public static void closeSession() |
throws HibernateException, ModelException |
{ |
HibernateBean hb = (HibernateBean)hibernateBean.get(); |
if(hb == null) |
throw new ModelException("No session found for this thread"); |
hibernateBean.set(null); |
hb.session.close(); |
} |
public static void beginTransaction() |
throws HibernateException, ModelException |
{ |
HibernateBean hb = (HibernateBean)hibernateBean.get(); |
if(hb != null && hb.transaction != null) |
throw new ModelException("Transaction is already open"); |
currentBean().transaction = currentSession().beginTransaction(); |
} |
public static boolean isTransactionOpen() |
throws HibernateException, ModelException |
{ |
HibernateBean hb = (HibernateBean)hibernateBean.get(); |
return (hb != null) && (hb.transaction != null); |
} |
public static void commitTransaction() |
throws HibernateException, ModelException |
{ |
HibernateBean hb = (HibernateBean)hibernateBean.get(); |
if(hb == null || hb.transaction == null) |
throw new ModelException("No open transaction"); |
hb.transaction.commit(); |
hb.transaction = null; |
} |
public static void rollbackTransaction() |
throws HibernateException, ModelException |
{ |
HibernateBean hb = (HibernateBean)hibernateBean.get(); |
if(hb == null || hb.transaction == null) |
throw new ModelException("No open transaction"); |
hb.transaction.rollback(); |
hb.transaction = null; |
} |
static class HibernateBean |
{ |
public Session session; |
public Transaction transaction; |
} |
} |
/kickup/trunk/src/ak/kickup/util/ModelException.java |
---|
0,0 → 1,44 |
package ak.kickup.util; |
public class ModelException |
extends Exception |
{ |
private Exception chainedException; |
public ModelException() |
{ |
this(null, null); |
} |
public ModelException(String message) |
{ |
this(message, null); |
} |
public ModelException(Exception chainedException) |
{ |
this(null, chainedException); |
} |
public ModelException(String message, Exception chainedException) |
{ |
super(message); |
this.chainedException = chainedException; |
} |
public Exception getChainedException() |
{ |
return chainedException; |
} |
public void setChainedException(Exception chainedException) |
{ |
this.chainedException = chainedException; |
} |
public String toString() |
{ |
return super.toString() |
+ (chainedException == null ? "" : "\n" + chainedException.toString()); |
} |
} |
/kickup/trunk/src/ak/kickup/util/FormException.java |
---|
0,0 → 1,39 |
package ak.kickup.util; |
public class FormException |
extends UserException |
{ |
private String property; |
public FormException() |
{ |
this(null, null, null); |
} |
public FormException(String message) |
{ |
this(message, null, null); |
} |
public FormException(String message, String property) |
{ |
this(message, property, null); |
} |
public FormException(String message, String property, Object[] values) |
{ |
super(message, values); |
this.property = property; |
} |
public String getProperty() |
{ |
return property; |
} |
public String toString() |
{ |
return super.toString() |
+ (property == null ? "" : " for " + property); |
} |
} |
/kickup/trunk/src/ak/kickup/util/Validator.java |
---|
0,0 → 1,58 |
package ak.kickup.util; |
import java.math.BigDecimal; |
import java.text.NumberFormat; |
import java.text.ParseException; |
import javax.servlet.http.HttpServletRequest; |
import org.apache.commons.validator.Field; |
import org.apache.commons.validator.ValidatorAction; |
import org.apache.commons.validator.ValidatorUtil; |
import org.apache.struts.action.ActionError; |
import org.apache.struts.action.ActionErrors; |
import org.apache.struts.validator.Resources; |
public class Validator |
{ |
public static BigDecimal validateNumber(Object bean, ValidatorAction va, Field field, |
ActionErrors errors, HttpServletRequest request) |
{ |
String value = null; |
if((bean == null) || (bean instanceof String)) |
value = (String)bean; |
else |
value = ValidatorUtil.getValueAsString(bean, field.getProperty()); |
if(value == null || "".equals(value)) return null; |
try { |
return new BigDecimal(NumberFormat.getInstance().parse(value).doubleValue()); |
} |
catch(ParseException ex) { |
errors.add(field.getKey(), Resources.getActionError(request, va, field)); |
return null; |
} |
} |
public static BigDecimal validateCurrency(Object bean, ValidatorAction va, Field field, |
ActionErrors errors, HttpServletRequest request) |
{ |
String value = null; |
if((bean == null) || (bean instanceof String)) |
value = (String)bean; |
else |
value = ValidatorUtil.getValueAsString(bean, field.getProperty()); |
if(value == null || "".equals(value)) return null; |
try { |
return new BigDecimal(NumberFormat.getInstance().parse(value).doubleValue()); |
} |
catch(ParseException ex) { |
errors.add(field.getKey(), Resources.getActionError(request, va, field)); |
return null; |
} |
} |
} |