123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package com.primeton.dgs.kernel.core.dao.hibernate;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.regex.Matcher;
- public class HibernateStatementTextField extends HibernateStatementField
- {
- private static final long serialVersionUID = 1L;
- private String value = "";
- public String getValue() {
- return this.value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- public HibernateStatementTextField()
- {
- }
- public HibernateStatementTextField(String value) {
- this.value = value;
- }
- @Override
- public StringBuffer getText(Object vo, AtomicInteger index)
- {
- StringBuffer sb = getRawText(vo);
- StringBuffer sbb = new StringBuffer();
- Matcher m = PLACEHOLDER.matcher(sb);
- while (m.find()) {
- // add zhaopx, Hibernate5 需要在 ? 后加一个索引值
- String rep = (index.get() < 0 ? "?" : "?"+index);
- m.appendReplacement(sbb, rep);
- index.incrementAndGet();
- }
- m.appendTail(sbb);
- return sbb;
- }
- public StringBuffer getRawText(Object vo)
- {
- StringBuffer sb = new StringBuffer();
- Matcher m = REPLACEMENT.matcher(value);
- while (m.find()) {
- String group = m.group();
- String property = group.substring(1, group.length() - 1);
- StringBuffer replace = getReplacementText(getBeanProperty(vo, property));
- if ((replace != null) && (replace.length() > 0)) {
- m.appendReplacement(sb, replace.toString());
- }
- }
- m.appendTail(sb);
- return sb;
- }
- private StringBuffer getReplacementText(Object value)
- {
- if (value == null) {
- return null;
- }
- if ((value instanceof Object[])) {
- return getReplacementTextFromArray((Object[])value);
- }
- if ((value instanceof Collection)) {
- return getReplacementTextFromCollection((Collection)value);
- }
- return new StringBuffer().append(value);
- }
- private StringBuffer getReplacementTextFromArray(Object[] array)
- {
- if ((array == null) || (array.length == 0)) {
- return null;
- }
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < array.length; i++) {
- if (i > 0) sb.append(",");
- sb.append(array[i]);
- }
- return sb;
- }
- private StringBuffer getReplacementTextFromCollection(Collection<?> collection)
- {
- if ((collection == null) || (collection.isEmpty())) {
- return null;
- }
- StringBuffer sb = new StringBuffer();
- int i = 0;
- for (Iterator it = collection.iterator(); it.hasNext(); i++) {
- if (i > 0) sb.append(",");
- sb.append(it.next());
- }
- return sb;
- }
- }
|