/** * @param obj ValueObject related to the current row * @param colIndex TableModel column index * @return Object contained into the TableModel at the specified column and ValueObject */ public final Object getField(ValueObject obj, int colIndex) { try { Method[] m = (Method[]) voGetterMethods.get(getFieldName(colIndex)); if (m == null) Logger.error( this.getClass().getName(), "getField", "No getter method for index " + colIndex + " and attribute name '" + getFieldName(colIndex) + "'.", null); for (int i = 0; i < m.length - 1; i++) { obj = (ValueObject) m[i].invoke(obj, new Object[0]); if (obj == null) { if (grids.getGridControl() == null || !grids.getGridControl().isCreateInnerVO()) return null; else obj = (ValueObject) m[i].getReturnType().newInstance(); } } return m[m.length - 1].invoke(obj, new Object[0]); } catch (Exception ex) { ex.printStackTrace(); return null; } }
/** * @param fieldName attribute name * @return int column index */ public final int getFieldIndex(String fieldName) { Integer index = (Integer) reverseIndexes.get(fieldName); if (index != null) return index.intValue(); Logger.error( this.getClass().getName(), "getFieldIndex", "The column '" + fieldName + "' does not exist", null); return (-1); }
/** * @param colIndex TableModel column index * @return TableModel column name */ public final String getFieldName(int colIndex) { String attributeName = (String) indexes.get(new Integer(colIndex)); if (attributeName == null) Logger.error( this.getClass().getName(), "getFieldName", "No attribute found for index " + colIndex + ".", null); return attributeName; }
/** * @param colIndex TableModel column index * @return column type */ public final Class getFieldClass(int colIndex) { try { Method[] m = (Method[]) voGetterMethods.get(getFieldName(colIndex)); if (m == null) Logger.error( this.getClass().getName(), "getField", "No getter method for index " + colIndex + " and attribute name '" + getFieldName(colIndex) + "'.", null); return m[m.length - 1].getReturnType(); } catch (Exception ex) { ex.printStackTrace(); return String.class; } }
/** * Analyze class fields and fill in "voSetterMethods","voGetterMethods","indexes",reverseIndexes" * attributes. * * @param prefix e.g. "attrx.attry." * @param parentMethods getter methods of parent v.o. * @param classType class to analyze */ private void analyzeClassFields(String prefix, Method[] parentMethods, Class classType) { try { if (prefix.split("\\.").length > ClientSettings.MAX_NR_OF_LOOPS_IN_ANALYZE_VO) return; // retrieve all getter and setter methods defined in the specified value object... String attributeName = null; Method[] methods = classType.getMethods(); String aName = null; for (int i = 0; i < methods.length; i++) { attributeName = methods[i].getName(); if (attributeName.startsWith("get") && methods[i].getParameterTypes().length == 0 && ValueObject.class.isAssignableFrom(methods[i].getReturnType())) { aName = getAttributeName(attributeName, classType); Method[] newparentMethods = new Method[parentMethods.length + 1]; System.arraycopy(parentMethods, 0, newparentMethods, 0, parentMethods.length); newparentMethods[parentMethods.length] = methods[i]; analyzeClassFields(prefix + aName + ".", newparentMethods, methods[i].getReturnType()); } if (attributeName.startsWith("get") && methods[i].getParameterTypes().length == 0 && (methods[i].getReturnType().equals(String.class) || methods[i].getReturnType().equals(Long.class) || methods[i].getReturnType().equals(Long.TYPE) || methods[i].getReturnType().equals(Float.class) || methods[i].getReturnType().equals(Float.TYPE) || methods[i].getReturnType().equals(Short.class) || methods[i].getReturnType().equals(Short.TYPE) || methods[i].getReturnType().equals(Double.class) || methods[i].getReturnType().equals(Double.TYPE) || methods[i].getReturnType().equals(BigDecimal.class) || methods[i].getReturnType().equals(java.util.Date.class) || methods[i].getReturnType().equals(java.sql.Date.class) || methods[i].getReturnType().equals(java.sql.Timestamp.class) || methods[i].getReturnType().equals(Integer.class) || methods[i].getReturnType().equals(Integer.TYPE) || methods[i].getReturnType().equals(Character.class) || methods[i].getReturnType().equals(Boolean.class) || methods[i].getReturnType().equals(boolean.class) || methods[i].getReturnType().equals(ImageIcon.class) || methods[i].getReturnType().equals(Icon.class) || methods[i].getReturnType().equals(byte[].class) || methods[i].getReturnType().equals(Object.class) || ValueObject.class.isAssignableFrom(methods[i].getReturnType()))) { attributeName = getAttributeName(attributeName, classType); // try { // if // (classType.getMethod("set"+attributeName.substring(0,1).toUpperCase()+attributeName.substring(1),new Class[]{methods[i].getReturnType()})!=null) Method[] newparentMethods = new Method[parentMethods.length + 1]; System.arraycopy(parentMethods, 0, newparentMethods, 0, parentMethods.length); newparentMethods[parentMethods.length] = methods[i]; voGetterMethods.put(prefix + attributeName, newparentMethods); // } catch (NoSuchMethodException ex) { // } } else if (attributeName.startsWith("is") && methods[i].getParameterTypes().length == 0 && (methods[i].getReturnType().equals(Boolean.class) || methods[i].getReturnType().equals(boolean.class))) { attributeName = getAttributeName(attributeName, classType); Method[] newparentMethods = new Method[parentMethods.length + 1]; System.arraycopy(parentMethods, 0, newparentMethods, 0, parentMethods.length); newparentMethods[parentMethods.length] = methods[i]; voGetterMethods.put(prefix + attributeName, newparentMethods); } else if (attributeName.startsWith("set") && methods[i].getParameterTypes().length == 1) { attributeName = getAttributeName(attributeName, classType); try { if (classType.getMethod( "get" + attributeName.substring(0, 1).toUpperCase() + attributeName.substring(1), new Class[0]) != null) { Method[] newparentMethods = new Method[parentMethods.length + 1]; System.arraycopy(parentMethods, 0, newparentMethods, 0, parentMethods.length); newparentMethods[parentMethods.length] = methods[i]; voSetterMethods.put(prefix + attributeName, newparentMethods); } } catch (NoSuchMethodException ex) { try { if (classType.getMethod( "is" + attributeName.substring(0, 1).toUpperCase() + attributeName.substring(1), new Class[0]) != null) { Method[] newparentMethods = new Method[parentMethods.length + 1]; System.arraycopy(parentMethods, 0, newparentMethods, 0, parentMethods.length); newparentMethods[parentMethods.length] = methods[i]; voSetterMethods.put(prefix + attributeName, newparentMethods); } } catch (NoSuchMethodException exx) { } } } } // fill in indexes with the colProperties indexes first; after them, it will be added the // other indexes (of attributes not mapped with grid column...) HashSet alreadyAdded = new HashSet(); int i = 0; for (i = 0; i < colProperties.length; i++) { indexes.put(new Integer(i), colProperties[i].getColumnName()); reverseIndexes.put(colProperties[i].getColumnName(), new Integer(i)); alreadyAdded.add(colProperties[i].getColumnName()); } Enumeration en = voGetterMethods.keys(); while (en.hasMoreElements()) { attributeName = en.nextElement().toString(); if (!alreadyAdded.contains(attributeName)) { indexes.put(new Integer(i), attributeName); reverseIndexes.put(attributeName, new Integer(i)); i++; } } } catch (Exception ex) { ex.printStackTrace(); } }
/** * @param obj ValueObject where updating the value for the specified attribute (identified by * colunm index) * @param attributeName attribute name * @param value new Object to set onto ValueObject */ public final void setField(ValueObject obj, String attributeName, Object value) { try { Method[] getter = ((Method[]) voGetterMethods.get(attributeName)); Method[] setter = ((Method[]) voSetterMethods.get(attributeName)); if (getter == null) Logger.error( this.getClass().getName(), "setField", "No getter method for attribute name '" + attributeName + "'.", null); if (setter == null) Logger.error( this.getClass().getName(), "setField", "No setter method for attribute name '" + attributeName + "'.", null); if (value != null && (value instanceof Number || !value.equals("") && value instanceof String)) { if (!getter[getter.length - 1].getReturnType().equals(value.getClass())) { Class attrType = getter[getter.length - 1].getReturnType(); if (attrType.equals(Integer.class) || attrType.equals(Integer.TYPE)) value = new Integer(Double.valueOf(value.toString()).intValue()); else if (attrType.equals(Double.class) || attrType.equals(Double.TYPE)) value = new Double(value.toString()); else if (attrType.equals(BigDecimal.class)) value = new BigDecimal(value.toString()); else if (attrType.equals(Long.class) || attrType.equals(Long.TYPE)) value = new Long(Double.valueOf(value.toString()).longValue()); else if (attrType.equals(Short.class) || attrType.equals(Short.TYPE)) value = new Short(Double.valueOf(value.toString()).shortValue()); else if (attrType.equals(Float.class) || attrType.equals(Float.TYPE)) value = new Float(Double.valueOf(value.toString()).floatValue()); } } else if (value != null && value.equals("")) { if (!getter[getter.length - 1].getReturnType().equals(value.getClass())) value = null; } // test date compatibility... if (value != null && value.getClass().equals(java.util.Date.class)) { if (setter[setter.length - 1].getParameterTypes()[0].equals(java.sql.Date.class)) value = new java.sql.Date(((java.util.Date) value).getTime()); else if (setter[setter.length - 1].getParameterTypes()[0].equals(java.sql.Timestamp.class)) value = new java.sql.Timestamp(((java.util.Date) value).getTime()); } // retrieve inner v.o.: if not present then maybe create it, according to "createInnerVO" // property... Method[] m = (Method[]) voGetterMethods.get(attributeName); if (m == null) Logger.error( this.getClass().getName(), "setField", "No getter method for attribute name '" + attributeName + "'.", null); Object oldObj = obj; String auxAttr; for (int i = 0; i < m.length - 1; i++) { oldObj = obj; obj = (ValueObject) m[i].invoke(oldObj, new Object[0]); if (obj == null) { if (grids.getGridControl() == null || !grids.getGridControl().isCreateInnerVO()) return; else { obj = (ValueObject) m[i].getReturnType().newInstance(); String[] attrs = attributeName.split("\\."); auxAttr = ""; for (int k = 0; k <= i; k++) auxAttr += attrs[k] + "."; auxAttr = auxAttr.substring(0, auxAttr.length() - 1); Method aux = ((Method[]) voSetterMethods.get(auxAttr))[i]; aux.invoke(oldObj, new Object[] {obj}); } } } // avoid to set null for primitive types! if (value == null && setter[setter.length - 1].getParameterTypes()[0].equals(Long.TYPE)) setter[setter.length - 1].invoke(obj, new Object[] {new Long(0)}); if (value == null && setter[setter.length - 1].getParameterTypes()[0].equals(Integer.TYPE)) setter[setter.length - 1].invoke(obj, new Object[] {new Integer(0)}); if (value == null && setter[setter.length - 1].getParameterTypes()[0].equals(Short.TYPE)) setter[setter.length - 1].invoke(obj, new Object[] {new Short((short) 0)}); if (value == null && setter[setter.length - 1].getParameterTypes()[0].equals(Float.TYPE)) setter[setter.length - 1].invoke(obj, new Object[] {new Float(0)}); if (value == null && setter[setter.length - 1].getParameterTypes()[0].equals(Double.TYPE)) setter[setter.length - 1].invoke(obj, new Object[] {new Double(0)}); if (value == null && setter[setter.length - 1].getParameterTypes()[0].equals(Boolean.TYPE)) setter[setter.length - 1].invoke(obj, new Object[] {Boolean.FALSE}); else setter[setter.length - 1].invoke(obj, new Object[] {value}); } catch (Exception ex) { ex.printStackTrace(); } }