1 package com.lavans.luz2.commons;
\r
3 import java.math.BigDecimal;
\r
4 import java.util.Date;
\r
5 import java.util.HashMap;
\r
6 import java.util.Map;
\r
7 import java.util.Set;
\r
9 import org.apache.commons.logging.Log;
\r
10 import org.apache.commons.logging.LogFactory;
\r
12 import com.lavans.luz2.util.CondTypeEnum;
\r
13 import com.lavans.luz2.util.Config;
\r
15 public class DaoUtils {
\r
17 private static Log logger = LogFactory.getLog(DaoUtils.class);
\r
18 //private static final String CLASSNAME=DaoUtils.class.getName();
\r
25 * @param params パラメータ用Map。ここに格納される。
\r
28 public static String makeInCond(String[] objs, String prefix, Map<String, String[]> params){
\r
29 if(objs.length==0) throw new IllegalArgumentException("target's length==0.");
\r
30 StringBuilder str = new StringBuilder();
\r
31 for(int i=0; i<objs.length; i++){
\r
32 str.append(",:"+prefix+i);
\r
33 params.put(prefix+i, new String[]{objs[i]});
\r
35 return str.substring(1);
\r
38 // どうせWebからStringでしか指定しないんだからジェネリックいらないかも
\r
39 // public static <T> String makeInCond(T[] objs, String prefix, HashMap<String, Object> params){
\r
40 // if(objs.length==0) throw new IllegalArgumentException("target's length==0.");
\r
41 // StringBuilder str = new StringBuilder();
\r
42 // for(int i=0; i<objs.length; i++){
\r
43 // str.append(",:"+prefix+i);
\r
44 // params.put(prefix+i, objs[i]);
\r
46 // return " IN ("+ str.substring(1)+ ")";
\r
49 public static String getSql(Class<?> clazz, String key){
\r
50 return getSql(clazz.getName(), key);
\r
52 public static String getSql(String className, String key){
\r
53 Config config = Config.getInstance(className.replace(".","/")+".xml");
\r
56 sql = config.getNodeValue("/sql/"+key).trim();
\r
57 } catch (Exception e) {
\r
58 logger.error("SQLが見つかりません。["+ key +"]", e);
\r
65 * 検索条件の設定。ここでは特定のprefixがついたkey=valueを取得するだけ
\r
67 public static Map<String, String[]> getSearchCond(Map<String, String[]> requestParameters, String prefix){
\r
68 Map<String, String[]> map = new HashMap<String, String[]>();
\r
69 // just only get "search condition paraemters" here. it use later.
\r
70 for(Map.Entry<String, String[]> entry: requestParameters.entrySet()){
\r
71 if(entry.getKey().startsWith(prefix)){
\r
72 map.put(entry.getKey().substring(prefix.length()), entry.getValue());
\r
79 * make SQL where phrase from search condtions.
\r
82 public static String getWherePhrase(Set<String> attributeNames, Map<String, String[]> cond){
\r
88 // SQL construction start.
\r
89 StringBuilder builder = new StringBuilder();
\r
90 for(String attributeName: attributeNames){
\r
91 String key = attributeName +"."+ StringUtils.toCamelCase(CondTypeEnum.EQUAL.toString());
\r
92 if((cond.get(key)!=null) && !StringUtils.isEmpty(cond.get(key)[0])){
\r
93 builder.append(" AND "+ StringUtils.toUnderscore(attributeName).toUpperCase() +"= :"+ key);
\r
95 key = attributeName +"."+ StringUtils.toCamelCase(CondTypeEnum.GREATER_EQUAL.toString());
\r
96 if((cond.get(key)!=null) && !StringUtils.isEmpty(cond.get(key)[0])){
\r
97 builder.append(" AND "+ StringUtils.toUnderscore(attributeName).toUpperCase() +">= :"+ key);
\r
99 key = attributeName +"."+ StringUtils.toCamelCase(CondTypeEnum.LESS_OR_EQUAL.toString());
\r
100 if((cond.get(key)!=null) && !StringUtils.isEmpty(cond.get(key)[0])){
\r
101 builder.append(" AND "+ StringUtils.toUnderscore(attributeName).toUpperCase() +"<= :"+ key);
\r
105 key = attributeName +"."+ StringUtils.toCamelCase(CondTypeEnum.FUZZY_SEARCH.toString());
\r
106 if((cond.get(key)!=null) && !StringUtils.isEmpty(cond.get(key)[0])){
\r
107 builder.append(" AND "+ StringUtils.toUnderscore(attributeName).toUpperCase() +" LIKE :"+ key);
\r
109 cond.get(key)[0]="%"+cond.get(key)[0]+"%";
\r
113 key = attributeName +"."+ StringUtils.toCamelCase(CondTypeEnum.PREFIX_SEARCH.toString());
\r
114 if((cond.get(key)!=null) && !StringUtils.isEmpty(cond.get(key)[0])){
\r
115 builder.append(" AND "+ StringUtils.toUnderscore(attributeName).toUpperCase() +" LIKE :"+ key);
\r
117 cond.get(key)[0]=cond.get(key)[0]+"%";
\r
121 key = attributeName +"."+ StringUtils.toCamelCase(CondTypeEnum.SUFFIX_SEARCH.toString());
\r
122 if((cond.get(key)!=null) && !StringUtils.isEmpty(cond.get(key)[0])){
\r
123 builder.append(" AND "+ StringUtils.toUnderscore(attributeName).toUpperCase() +" LIKE :"+ key);
\r
125 cond.get(key)[0]="%"+cond.get(key)[0];
\r
129 key = attributeName +"."+ StringUtils.toCamelCase(CondTypeEnum.MULTIPLE.toString());
\r
130 if((cond.get(key)!=null) && !StringUtils.isEmpty(cond.get(key)[0])){
\r
131 builder.append(" AND "+ StringUtils.toUnderscore(attributeName).toUpperCase() +" IN ( "+ makeInCond(cond.get(key),attributeName,cond) +")");
\r
134 // comma separated values
\r
135 key = attributeName +"."+ StringUtils.toCamelCase(CondTypeEnum.LIST.toString());
\r
136 if((cond.get(key)!=null) && !StringUtils.isEmpty(cond.get(key)[0])){
\r
137 // separate all keys
\r
138 String conds[] = StringUtils.splitTrim(cond.get(key)[0], ",");
\r
139 builder.append(" AND "+ StringUtils.toUnderscore(attributeName).toUpperCase() +" IN ( "+ makeInCond(conds,attributeName,cond) +")");
\r
143 return builder.toString();
\r
147 * 検索条件をMap<String,String[]>からMap<String,Object>に変換。
\r
148 * InteterとかLongとか型渡して変換した方がよさそう。
\r
151 public static Map<String, Object> convertSearchCond(Map<String, String[]> cond, Map<String,Class<?>> attributeInfo){
\r
152 Map<String, Object> result = new HashMap<String, Object>();
\r
153 for(Map.Entry<String, String[]> entry: cond.entrySet()){
\r
154 // 値が指定されていなければ評価しない
\r
155 if(StringUtils.isEmpty(entry.getValue()[0])){
\r
159 // 属性名は.より前の部分(ex memberId.equal
\r
160 if(!entry.getKey().contains(".")){
\r
161 logger.debug("No condtion ["+ entry.getKey() +"]");
\r
164 String attributeName = entry.getKey().split("\\.")[0];
\r
166 Class<?> clazz = attributeInfo.get(attributeName);
\r
169 logger.debug("No attribute Info,["+ attributeName +"]");
\r
173 if(clazz.equals(Integer.class)){
\r
174 result.put(entry.getKey(), Integer.valueOf(entry.getValue()[0]));
\r
175 }else if(clazz.equals(Long.class)){
\r
176 result.put(entry.getKey(), Long.valueOf(entry.getValue()[0]));
\r
177 }else if(clazz.equals(Double.class)){
\r
178 result.put(entry.getKey(), Double.valueOf(entry.getValue()[0]));
\r
179 }else if(clazz.equals(BigDecimal.class)){
\r
180 result.put(entry.getKey(), new BigDecimal(entry.getValue()[0]));
\r
181 // }else if(clazz.equals(byte[].class)){
\r
183 // result.put(entry.getKey(), byte.valueOf(entry.getValue()[0]));
\r
184 }else if(clazz.equals(Date.class)){
\r
185 result.put(entry.getKey(), DateUtils.getDate(entry.getValue()[0]));
\r
188 result.put(entry.getKey(), entry.getValue()[0]);
\r