Java tutorial
/* * Copyright 2012 Eng Kam Hon (kamhon@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.kamhon.ieagle.vo; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import javax.persistence.Column; import net.kamhon.ieagle.util.VoUtil; import org.apache.commons.lang.StringUtils; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl; public class ORWrapper { private Object vo; private String aliasName; private BeanWrapper wrapper; public ORWrapper() { } public ORWrapper(Object vo) { if (vo == null) { try { vo = Object.class.newInstance(); } catch (Exception e) { } } this.vo = vo; wrapper = new BeanWrapperImpl(vo); } public ORWrapper(Object vo, String aliasName) { this.vo = vo; this.aliasName = aliasName; wrapper = new BeanWrapperImpl(vo); } public boolean hasProperty(String propertyName) { return getPropertyDescriptor(propertyName) != null; } /** * * @param propertyName * @return null if not found */ protected PropertyDescriptor getPropertyDescriptor(String propertyName) { PropertyDescriptor descriptor = null; try { descriptor = wrapper.getPropertyDescriptor(propertyName); } catch (Exception e) { } return descriptor; } /** * Base on {@link Column} to get the DbColumnName.<br/> * if aliasName exists,<br/> * For example, <code>user.username</code> will return</br> else <code>username</code> will return * * @param propertyName * @return null if not found */ public String getDbColumnName(String propertyName) { String colName = null; Field field = VoUtil.getField(vo, propertyName); if (field != null) { colName = getDbColumn(field); if (StringUtils.isNotBlank(colName)) { if (StringUtils.isNotBlank(aliasName)) { colName = aliasName + "." + colName; } } } return colName; } private String getDbColumn(Field field) { if (field.isAnnotationPresent(Column.class)) { Column column = field.getAnnotation(Column.class); return column.name(); } else { return null; } } public Object getVo() { return vo; } public void setVo(Object vo) { this.vo = vo; } public String getAliasName() { return aliasName; } public void setAliasName(String aliasName) { this.aliasName = aliasName; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((aliasName == null) ? 0 : aliasName.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ORWrapper other = (ORWrapper) obj; if (aliasName == null) { if (other.aliasName != null) return false; } else if (!aliasName.equals(other.aliasName)) return false; return true; } }