📄 util.java
字号:
/* * Copyright(C) 2002 Azrul Azwar (pikapikane@yahoo.co.jp) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */package com.javanovic.karapansapi.strutsgen;import com.javanovic.karapansapi.xml.*;import org.apache.velocity.util.StringUtils;import java.lang.reflect.Array;import java.util.*;/** * @author roel * @version 1.1.2 * */public class Util extends StringUtils { HashMap sqlTypeMap; HashMap javaTypeMap; HashMap refs; private String dbms; private String dateFormat = "dd/MM/yyyy"; /** * Set date format. * @param dateFormat */ public void setDateFormat(String dateFormat) { this.dateFormat = dateFormat; } /** * Default constructor */ public Util() { javaTypeMap = new HashMap(); javaTypeMap.put("string", "String"); javaTypeMap.put("auto", "long"); javaTypeMap.put("password", "String"); javaTypeMap.put("email", "String"); javaTypeMap.put("creditcard", "String"); javaTypeMap.put("memo", "String"); javaTypeMap.put("date", "Date"); javaTypeMap.put("timestamp", "Timestamp"); } /** * Set GlobalValueReference * @param refsArray */ public void setGlobalValueRef(GlobalValueRef[] refsArray) { refs = new HashMap(); for(int i = 0; i < refsArray.length; i++) { refs.put(refsArray[i].getName(), refsArray[i].getChoice()); } } /** * Return globalValueChoice * @param name * @return */ public Choice[] getGlobalValueChoice(String name) { return (Choice[]) refs.get(name); } /** * set SQL type mapping for certain DBMS. * * @param dbms * @exception Exception */ public void setDbms(String dbms) throws Exception { this.dbms = dbms; PropertyMgr prop = new PropertyMgr("SQLType.properties"); Properties p = prop.getProperties(); sqlTypeMap = new HashMap(); sqlTypeMap.put("int", p.getProperty(dbms + ".type.int")); sqlTypeMap.put("long", p.getProperty(dbms + ".type.long")); sqlTypeMap.put("float", p.getProperty(dbms + ".type.float")); sqlTypeMap.put("double", p.getProperty(dbms + ".type.double")); sqlTypeMap.put("boolean", p.getProperty(dbms + ".type.boolean")); sqlTypeMap.put("char", p.getProperty(dbms + ".type.char")); sqlTypeMap.put("byte", p.getProperty(dbms + ".type.byte")); sqlTypeMap.put("short", p.getProperty(dbms + ".type.short")); sqlTypeMap.put("string", p.getProperty(dbms + ".type.string")); sqlTypeMap.put("auto", p.getProperty(dbms + ".type.auto")); sqlTypeMap.put("date", p.getProperty(dbms + ".type.date")); sqlTypeMap.put("timestamp", p.getProperty(dbms + ".type.timestamp")); sqlTypeMap.put("memo", p.getProperty(dbms + ".type.memo")); sqlTypeMap.put("email", p.getProperty(dbms + ".type.email")); sqlTypeMap.put("creditcard", p.getProperty(dbms + ".type.creditcard")); } /** * Specify if the type is number type group. * @param type * @return */ public boolean isNumberType(String type) { if("int".equals(type) || "long".equals(type) || "float".equals(type) || "double".equals(type) || "short".equals(type) || "auto".equals(type)) { return true; } else { return false; } } /** * Check if type is String type. * @param type * @return */ public boolean isStringType(String type) { if("string".equals(type) || "password".equals(type) || "email".equals(type) || "creditcard".equals(type)) { return true; } else { return false; } } /** * Check if type is date type. * @param type * @return */ public boolean isDateType(String type) { if("date".equals(type) || "timestamp".equals(type)) { return true; } else { return false; } } /** * Get union of PrimaryKey's columns and Attribute's columns. Primary key * columns last, because will be used much by SQL operation * *@param bean Description of Parameter *@return The allColumns value */ public Column[] getAllColumns(Bean bean) { Column[] pks = bean.getPrimaryKey().getColumn(); Attribute attr = bean.getAttribute(); if(attr != null) { Column[] ats = attr.getColumn(); Column[] uni = new Column[pks.length + ats.length]; System.arraycopy(ats, 0, uni, 0, ats.length); System.arraycopy(pks, 0, uni, ats.length, pks.length); return uni; } else { return pks; } } /** * Get union of PrimaryKey's columns and Attribute's columns. * *@param bean Description of Parameter *@return The allColumns value */ public Column[] getAllColumnsPKFirst(Bean bean) { Column[] pks = bean.getPrimaryKey().getColumn(); Attribute attr = bean.getAttribute(); if(attr != null) { Column[] ats = attr.getColumn(); Column[] uni = new Column[pks.length + ats.length]; System.arraycopy(pks, 0, uni, 0, pks.length); System.arraycopy(ats, 0, uni, pks.length, ats.length); return uni; } else { return pks; } } /** * Get union of PrimaryKey's columns and Attribute's columns with column with * type auto excluded. Assumed that primary key that has auto type is the * only primary key column * *@param bean Description of Parameter *@return The allColumnsNoAuto value */ public Column[] getAllColumnsNoAuto(Bean bean) { Column[] pks = bean.getPrimaryKey().getColumn(); Attribute attr = bean.getAttribute(); Column[] ats = null; Column[] temp; if(attr != null) { ats = attr.getColumn(); temp = new Column[pks.length + ats.length]; } else { temp = new Column[pks.length]; } int index = 0; for(int i = 0; i < pks.length; i++) { if(!pks[i].getType().equals("auto")) { temp[index++] = pks[i]; } } if(ats != null) { for(int i = 0; i < ats.length; i++) { temp[index++] = ats[i]; } } Column[] uni = new Column[index]; System.arraycopy(temp, 0, uni, 0, index); return uni; } /** * convert capital in text to _ and lowercase, but the first letter e.g. * firstName become first_name * *@param name Description of Parameter *@return Description of the Returned Value */ public String java2sqlName(String name) { String column = ""; for(int i = 0; i < name.length(); i++) { if(i < name.length() - 1 && (name.charAt(i) >= 'a' && name.charAt(i) <= 'z') && (name.charAt(i + 1) >= 'A' && name.charAt(i + 1) <= 'Z')) { column += name.charAt(i) + "_"; } else { column += name.charAt(i); } } return column.toLowerCase(); } /** * Convert sql field naming (field_name) to java field naming (fieldName). * @param name * @return */ public String sql2javaName(String name) { String column = ""; for(int i = 0; i < name.length(); i++) { if(name.charAt(i) == '_') { column += ++i < name.length() ? String.valueOf(name.charAt(i)).toUpperCase() : ""; } else { column += name.charAt(i); } } return column; } /** * Get all query condition that it's field-value is "?". * @param bean * @param query * @return */ public Column[] getQueryVariableColumnList(Bean bean, Query query) { Column[] cols = getAllColumns(bean); Condition[] cond = query.getCondition(); Column[] temp = new Column[cond.length]; int index = 0; for(int i = 0; i < cond.length; i++) { if(cond[i].getFieldCondition().indexOf("?") > 0) { Column col = findColumnByName(cond[i].getFieldName(), cols); if(col != null) { temp[index++] = col; } } } if(index > 0) { Column[] vars = new Column[index]; System.arraycopy(temp, 0, vars, 0, index); return vars; } else { return null; } } /** * Get list of field used by a process. * @param bean * @param page * @return */ public Column[] getProcessPageFieldList(Bean bean, Page page) { Column[] cols = getAllColumns(bean); Column[] temp = new Column[page.getFieldNameCount()]; String[] fieldNames = page.getFieldName(); for(int i = 0; i < fieldNames.length; i++) { Column col = findColumnByName(fieldNames[i], cols); temp[i] = col; } return temp; } /** * Find a column from a list of column. * @param colName * @param columnListReference * @return */ private Column findColumnByName(String colName, Column[] columnListReference) { for(int i = 0; i < columnListReference.length; i++) { if(columnListReference[i].getName().equals(colName)) { return columnListReference[i]; } } return null; } /** * * @param column * @return */ public String sqlType(Column column) { boolean required; Validation val = column.getValidation(); if(val != null) { required = val.getRequired(); } else { required = false; } return sqlType(column.getType(), required, column.getValidation().getMaxLength()); } /** * Java type mapping. * *@param type Description of Parameter *@return Description of the Returned Value */ public String javaType(String type) { if(javaTypeMap.containsKey(type)) { return (String) javaTypeMap.get(type); } else { return type; } } /** * Full Qualified Java type mapping. Almost similar to javaType(), * but return java.util.Date for date type and java.sql.Timestamp * for * *@param type Description of Parameter *@return Description of the Returned Value */ public String fqJavaType(String type) { if("date".equals(type)) { return "java.util.Date"; } else if("timestamp".equals(type)) { return "java.sql.Timestamp"; } else if(javaTypeMap.containsKey(type)) { return (String) javaTypeMap.get(type); } else { return type; } } /** * Attribute with default initial value. * * @param type * @param attr * @return */ public String initAttr(String type, String attr) { if("int".equals(type) || "long".equals(type) || "short".equals(type)) { return type + " " + attr + " = 0"; } else if("double".equalsIgnoreCase(type)) { return type + " " + attr + " = 0.0"; } else if("float".equalsIgnoreCase(type)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -