package com.din.din.webapp.beans.util;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.SoftReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import com.din.din.webapp.beans.LocaleBean;
public final class BeanUtil {
private static final String KEY_FORMAT_DATETIME = "date.format.datetime";
private static final Pattern normalId = Pattern.compile("^(\\:?(?:[^:0-9][^:]+\\:)*(?:[^:0-9][^:]+))(?=\\:|$)");
private static final Pattern nChildOfId = Pattern.compile("^([0-9]+)(?=\\:|$)");
public static Map<String, SoftReference<LocaleBean>> localeBeanMap = new HashMap<String, SoftReference<LocaleBean>>();
public static void addMessage(FacesMessage.Severity severity, String summaryKey, String detail) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, getMessage(summaryKey), detail));
}
public static void addMessageFmt(FacesMessage.Severity severity, String summaryKey, String detailKey, Object ... params) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, getMessage(summaryKey), getMessage(detailKey, params)));
}
public static void addMessage(String summaryKey, String detail) {
addMessage(FacesMessage.SEVERITY_INFO, summaryKey, detail);
}
public static Object getBean(Class<?> beanClass, String string) {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication()
.evaluateExpressionGet(context, string, beanClass);
}
public static String getMessage(String key) {
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession)facesContext.getExternalContext().getSession(true);
String sessionId = session.getId();
SoftReference<LocaleBean> weakReference = localeBeanMap.get(sessionId);
LocaleBean localeBean = weakReference != null ? weakReference.get() : null;
if(localeBean == null) {
localeBean = (LocaleBean)getBean(LocaleBean.class, "#{localeBean}");
if(localeBean != null) {
localeBeanMap.put(sessionId, new SoftReference<LocaleBean>(localeBean));
}
}
String message = "???";
if(localeBean != null) {
message = localeBean.getString(key);
}
return message != null ? message : "???";
}
public static String getMessage(String key, Object ... params) {
return params != null && params.length > 0 ? MessageFormat.format(getMessage(key), params) : getMessage(key);
}
public static Object copy(Object obj) {
Object newInstance = null;
try {
Constructor<? extends Object> constructor = obj.getClass().getConstructor(new Class<?>[0]);
newInstance = constructor.newInstance((Object[])null);
copyProperties(obj, newInstance);
} catch (Exception e) {
newInstance = null;
}
return newInstance;
}
public static void copyProperties(Object from, Object to) throws IntrospectionException, IllegalAccessException, InvocationTargetException {
if(from != null && to != null && from.getClass().equals(to.getClass())) {
BeanInfo beanInfo = Introspector.getBeanInfo(from.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor propertyDescriptor : propertyDescriptors) {
Method readMethod = propertyDescriptor.getReadMethod();
Method writeMethod = propertyDescriptor.getWriteMethod();
if(readMethod != null && writeMethod != null && !propertyDescriptor.isHidden() && !readMethod.isAnnotationPresent(IgnoreMe.class)) {
Object propertyValue = readMethod.invoke(from, (Object[])null);
writeMethod.invoke(to, new Object[] { propertyValue });
}
}
} else {
throw new IllegalArgumentException("Don't call copyProperties without two non-null objects of the same type.");
}
}
/**
* Apply this to any getter method of any property that should not be copied by calling copy or copyProperties of an object
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreMe {
}
public static UIComponent findComponent(String id) {
return findComponent(FacesContext.getCurrentInstance().getViewRoot(), id);
}
private static UIComponent findComponent(UIComponent root, String id) {
UIComponent component = null;
Matcher matcher = normalId.matcher(id);
UIComponent subroot = null;
String idSegment = null;
if(matcher.find()) {
idSegment = matcher.group(0);
subroot = root.findComponent(idSegment);
} else {
matcher = nChildOfId.matcher(id);
if(matcher.find()) {
idSegment = matcher.group(0);
int childNum = Integer.parseInt(idSegment);
subroot = root.getChildren().get(childNum);
}
}
if(subroot != null) {
id = id.substring(Math.min(idSegment.length() + 1, id.length()));
if(id.length() == 0) {
component = subroot;
} else {
component = findComponent(subroot, id);
}
}
return component;
}
public static String getDateString(Date startDate) {
return new SimpleDateFormat(getMessage(KEY_FORMAT_DATETIME)).format(startDate);
}
}