private void copy(final Directory directory, final Map<String, Object> metadata) { final TagDescriptor<?> descriptor = MetadataHelper.getDescriptor(directory); final LazyDynaClass directoryClass = new LazyDynaClass(directory.getName(), null, directory.getTags() .parallelStream() .map(t -> new DynaProperty(t.getTagName().replaceAll(NON_WORD_CHARS, ""), String.class)) .toArray(DynaProperty[]::new)); directoryClass.setReturnNull(true); final DynaBean directoryMetadata = new LazyDynaBean(directoryClass); directory.getTags().stream().forEach(t -> directoryMetadata.set( t.getTagName().replaceAll(NON_WORD_CHARS, ""), descriptor.getDescription(t.getTagType()) )); metadata.put(directory.getName().replaceAll(NON_WORD_CHARS, ""), directoryMetadata); }
/** * <p>Set the value of a simple property with the specified name.</p> * * @param name Name of the property whose value is to be set * @param value Value to which this property is to be set * * @exception ConversionException if the specified value cannot be * converted to the type required for this property * @exception IllegalArgumentException if there is no property * of the specified name * @exception NullPointerException if the type specified for the * property is invalid * @exception NullPointerException if an attempt is made to set a * primitive property to null */ public void set(String name, Object value) { DynaProperty descriptor = getDynaProperty(name); if (descriptor.getType() == null) { throw new NullPointerException ("The type for property " + name + " is invalid"); } if (value == null) { if (descriptor.getType().isPrimitive()) { throw new NullPointerException ("Primitive value for '" + name + "'"); } } else if (!isDynaAssignable(descriptor.getType(), value.getClass())) { throw new ConversionException ("Cannot assign value of type '" + value.getClass().getName() + "' to property '" + name + "' of type '" + descriptor.getType().getName() + "'"); } dynaValues.put(name, value); }
/** * <p>Render a <code>String</code> representation of this object.</p> */ public String toString() { StringBuffer sb = new StringBuffer("DynaActionFormBean[name="); sb.append(name); DynaProperty props[] = getDynaProperties(); if (props == null) { props = new DynaProperty[0]; } for (int i = 0; i < props.length; i++) { sb.append(','); sb.append(props[i].getName()); sb.append('/'); sb.append(props[i].getType()); } sb.append("]"); return (sb.toString()); }
/** * Initializes the primary key and non primary key property arrays. */ protected void initPrimaryKeys() { List pkProps = new ArrayList(); List nonPkProps = new ArrayList(); DynaProperty[] properties = getDynaProperties(); for (int idx = 0; idx < properties.length; idx++) { if (properties[idx] instanceof SqlDynaProperty) { SqlDynaProperty sqlProperty = (SqlDynaProperty)properties[idx]; if (sqlProperty.isPrimaryKey()) { pkProps.add(sqlProperty); } else { nonPkProps.add(sqlProperty); } } } _primaryKeyProperties = (SqlDynaProperty[])pkProps.toArray(new SqlDynaProperty[pkProps.size()]); _nonPrimaryKeyProperties = (SqlDynaProperty[])nonPkProps.toArray(new SqlDynaProperty[nonPkProps.size()]); }
/** * {@inheritDoc} */ public String toString() { StringBuilder result = new StringBuilder(); DynaClass type = getDynaClass(); DynaProperty[] props = type.getDynaProperties(); result.append(type.getName()); result.append(": "); for (int idx = 0; idx < props.length; idx++) { if (idx > 0) { result.append(", "); } result.append(props[idx].getName()); result.append(" = "); result.append(get(props[idx].getName())); } return result.toString(); }
/** * Determines the value of the bean's property that has the given name. Depending on the * case-setting of the current builder, the case of teh name is considered or not. * * @param bean The bean * @param propName The name of the property * @return The value */ protected Object getPropertyValue(DynaBean bean, String propName) { if (getPlatform().isDelimitedIdentifierModeOn()) { return bean.get(propName); } else { DynaProperty[] props = bean.getDynaClass().getDynaProperties(); for (int idx = 0; idx < props.length; idx++) { if (propName.equalsIgnoreCase(props[idx].getName())) { return bean.get(props[idx].getName()); } } throw new IllegalArgumentException("The bean has no property with the name "+propName); } }
/** * <p>Set the value of a simple property with the specified name.</p> * * @param name Name of the property whose value is to be set * @param value Value to which this property is to be set * @throws ConversionException if the specified value cannot be * converted to the type required for * this property * @throws IllegalArgumentException if there is no property of the * specified name * @throws NullPointerException if the type specified for the property * is invalid * @throws NullPointerException if an attempt is made to set a * primitive property to null */ public void set(String name, Object value) { DynaProperty descriptor = getDynaProperty(name); if (descriptor.getType() == null) { throw new NullPointerException("The type for property " + name + " is invalid"); } if (value == null) { if (descriptor.getType().isPrimitive()) { throw new NullPointerException("Primitive value for '" + name + "'"); } } else if (!isDynaAssignable(descriptor.getType(), value.getClass())) { throw new ConversionException("Cannot assign value of type '" + value.getClass().getName() + "' to property '" + name + "' of type '" + descriptor.getType().getName() + "'"); } dynaValues.put(name, value); }
/** * <p>Render a <code>String</code> representation of this object.</p> * * @return The string representation of this instance. */ public String toString() { StringBuffer sb = new StringBuffer("DynaActionFormBean[name="); sb.append(name); DynaProperty[] props = getDynaProperties(); if (props == null) { props = new DynaProperty[0]; } for (int i = 0; i < props.length; i++) { sb.append(','); sb.append(props[i].getName()); sb.append('/'); sb.append(props[i].getType()); } sb.append("]"); return (sb.toString()); }
/** * Positive test for getDynaPropertys(). Each property name listed in * <code>properties</code> should be returned exactly once. */ public void testGetDescriptors() { DynaProperty[] pd = dynaForm.getDynaClass().getDynaProperties(); assertNotNull("Got descriptors", pd); int[] count = new int[properties.length]; for (int i = 0; i < pd.length; i++) { String name = pd[i].getName(); for (int j = 0; j < properties.length; j++) { if (name.equals(properties[j])) { count[j]++; } } } for (int j = 0; j < properties.length; j++) { if (count[j] < 0) { fail("Missing property " + properties[j]); } else if (count[j] > 1) { fail("Duplicate property " + properties[j]); } } }
@Override public DynaProperty getDynaProperty(String aName) { if (name != null) { if (properties.isEmpty()) { if (delegate.hasMember(aName)) { Object oMember = delegate.getMember(aName); if (!(oMember instanceof JSObject) || !((JSObject) oMember).isFunction()) { return new DynaProperty(aName); } return null; } } else { return properties.get(aName); } } throw new IllegalArgumentException("No property name specified"); }
/** * build a map of business object with its specified property names and corresponding values * * @param businessObject the given business object * @param the specified fields that need to be included in the return map * @return the map of business object with its property names and values */ public static Map<String, Object> buildPropertyMap(Object object, List<String> keyFields) { DynaClass dynaClass = WrapDynaClass.createDynaClass(object.getClass()); DynaProperty[] properties = dynaClass.getDynaProperties(); Map<String, Object> propertyMap = new LinkedHashMap<String, Object>(); for (DynaProperty property : properties) { String propertyName = property.getName(); if (PropertyUtils.isReadable(object, propertyName) && keyFields.contains(propertyName)) { try { Object propertyValue = PropertyUtils.getProperty(object, propertyName); if (propertyValue != null && !StringUtils.isEmpty(propertyValue.toString())) { propertyMap.put(propertyName, propertyValue); } } catch (Exception e) { LOG.info(e); } } } return propertyMap; }
/** * determine if the source object has a field with null as its value * * @param sourceObject the source object */ public static boolean hasNullValueField(Object sourceObject) { DynaClass dynaClass = WrapDynaClass.createDynaClass(sourceObject.getClass()); DynaProperty[] properties = dynaClass.getDynaProperties(); for (DynaProperty property : properties) { String propertyName = property.getName(); if (PropertyUtils.isReadable(sourceObject, propertyName)) { try { Object propertyValue = PropertyUtils.getProperty(sourceObject, propertyName); if (propertyValue == null) { return true; } } catch (Exception e) { LOG.info(e); return false; } } } return false; }
/** * This method builds a map of business object with its property names and values * * @param businessObject the given business object * @return the map of business object with its property names and values */ public static LinkedHashMap buildPropertyMap(Object businessObject) { DynaClass dynaClass = WrapDynaClass.createDynaClass(businessObject.getClass()); DynaProperty[] properties = dynaClass.getDynaProperties(); LinkedHashMap propertyMap = new LinkedHashMap(); try { for (int numOfProperty = 0; numOfProperty < properties.length; numOfProperty++) { String propertyName = properties[numOfProperty].getName(); if (PropertyUtils.isWriteable(businessObject, propertyName)) { Object propertyValue = PropertyUtils.getProperty(businessObject, propertyName); propertyMap.put(propertyName, propertyValue); } } } catch (Exception e) { LOG.error("OJBUtility.buildPropertyMap()" + e); } return propertyMap; }
public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof MorphDynaBean)) { return false; } MorphDynaBean other = (MorphDynaBean) obj; EqualsBuilder builder = new EqualsBuilder().append(this.dynaClass, other.dynaClass); DynaProperty[] props = dynaClass.getDynaProperties(); for (int i = 0; i < props.length; i++) { DynaProperty prop = props[i]; builder.append(dynaValues.get(prop.getName()), dynaValues.get(prop.getName())); } return builder.isEquals(); }
public Object get(String name, int index) { DynaProperty dynaProperty = getDynaProperty(name); Class type = dynaProperty.getType(); if (!type.isArray() && !List.class.isAssignableFrom(type)) { throw new MorphException("Non-Indexed property name: " + name + " index: " + index); } Object value = dynaValues.get(name); if (value.getClass() .isArray()) { value = Array.get(value, index); } else if (value instanceof List) { value = ((List) value).get(index); } return value; }
/** * Constructs a DynaBean using the values and properties specified. * @param values property values. * @param properties properties to add to the bean. * @param classes properties types. * @return DynBean with the values and properties specified. */ protected DynaBean objectArrayToBean(Object[] values, String[] properties, Class<?>[] classes) { DynaBean dynaBean = null; try { DynaProperty[] columnsDynaProperties = getColumnsDynaProperties(properties, classes); BasicDynaClass clazz = new BasicDynaClass(this.getClass().getSimpleName(), BasicDynaBean.class, columnsDynaProperties); dynaBean = clazz.newInstance(); for(int i = 0; i < columnsDynaProperties.length; i++) { dynaBean.set(columnsDynaProperties[i].getName(), values[i]); } } catch (Exception e) { throw new RuntimeException("Error creating dynamic bean", e); } return dynaBean; }
public Class getPropType(final String propertyName) { final String propName = propertyName.toString(); final DynaProperty dynaProperty = getDynaClass().getDynaProperty(propName); final Class<?> type; if (dynaProperty != null) type = dynaProperty.getType(); else if (nestedProps.contains(propName)) type = getNestedPropType(propName); else throw new IllegalArgumentException("Wrong propertyId"); if (type.isPrimitive()) { // Vaadin can't handle primitive types in _all_ places, so use // wrappers instead. FieldGroup works, but e.g. Table in _editable_ // mode fails for some reason return ClassUtils.primitiveToWrapper(type); } return type; }
public Collection<String> getPropertyIds() { final ArrayList<String> properties = new ArrayList<String>(); if (getDynaClass() != null) { for (final DynaProperty db : getDynaClass().getDynaProperties()) { if (db.getType() != null) { properties.add(db.getName()); } else { // type may be null in some cases Logger.getLogger(JpaPropertyProvider.class.getName()).log( Level.FINE, "Type not detected for property {0}", db.getName()); } } properties.remove("class"); properties.addAll(nestedProps); } return properties; }
@Override public Object getValueAt(final int rowIndex, final int columnIndex) { final Object value; final DynaProperty property = this.properties[rowIndex]; switch (columnIndex) { case 0: value = property.getName(); break; case 1: value = content.get(property.getName()); break; default: throw new IndexOutOfBoundsException("No such column: " + columnIndex); } return value; }
/** * {@inheritDoc} */ public String toString() { StringBuffer result = new StringBuffer(); DynaClass type = getDynaClass(); DynaProperty[] props = type.getDynaProperties(); result.append(type.getName()); result.append(": "); for (int idx = 0; idx < props.length; idx++) { if (idx > 0) { result.append(", "); } result.append(props[idx].getName()); result.append(" = "); result.append(get(props[idx].getName())); } return result.toString(); }
/** * Calculate the property type. * * @param target The bean * @param name The property name * @param propName The Simple name of target property * @return The property's type * * @throws IllegalAccessException if the caller does not have * access to the property accessor method * @throws InvocationTargetException if the property accessor method * throws an exception */ protected Class<?> definePropertyType(final Object target, final String name, final String propName) throws IllegalAccessException, InvocationTargetException { Class<?> type = null; // Java type of target property if (target instanceof DynaBean) { final DynaClass dynaClass = ((DynaBean) target).getDynaClass(); final DynaProperty dynaProperty = dynaClass.getDynaProperty(propName); if (dynaProperty == null) { return null; // Skip this property setter } type = dynaProperty.getType(); } else { PropertyDescriptor descriptor = null; try { descriptor = getPropertyUtils().getPropertyDescriptor(target, name); if (descriptor == null) { return null; // Skip this property setter } } catch (final NoSuchMethodException e) { return null; // Skip this property setter } if (descriptor instanceof MappedPropertyDescriptor) { type = ((MappedPropertyDescriptor) descriptor). getMappedPropertyType(); } else if (descriptor instanceof IndexedPropertyDescriptor) { type = ((IndexedPropertyDescriptor) descriptor). getIndexedPropertyType(); } else { type = descriptor.getPropertyType(); } } return type; }
/** * <p>Return the property descriptor for the specified property name.</p> * * @param name Name of the property for which to retrieve the descriptor * * @exception IllegalArgumentException if this is not a valid property * name for our DynaClass */ protected DynaProperty getDynaProperty(String name) { DynaProperty descriptor = getDynaClass().getDynaProperty(name); if (descriptor == null) { throw new IllegalArgumentException ("Invalid property name '" + name + "'"); } return (descriptor); }
/** * <p>Return a property descriptor for the specified property, if it exists; * otherwise, return <code>null</code>.</p> * * @param name Name of the dynamic property for which a descriptor * is requested * * @exception IllegalArgumentException if no property name is specified */ public DynaProperty getDynaProperty(String name) { if (name == null) { throw new IllegalArgumentException ("No property name specified"); } return ((DynaProperty) propertiesMap.get(name)); }
/** * <p>Return an array of <code>DynaProperty</code>s for the properties * currently defined in this <code>DynaClass</code>. If no properties are * defined, a zero-length array will be returned.</p> */ public DynaProperty[] getDynaProperties() { return (properties); // :FIXME: Should we really be implementing // getBeanInfo instead, which returns property descriptors // and a bunch of other stuff? }
/** * <p>Introspect our form bean configuration to identify the supported * properties.</p> * * @param config The FormBeanConfig instance describing the properties * of the bean to be created * * @exception IllegalArgumentException if the bean implementation class * specified in the configuration is not DynaActionForm (or a subclass * of DynaActionForm) */ protected void introspect(FormBeanConfig config) { this.config = config; // Validate the ActionFormBean implementation class try { beanClass = RequestUtils.applicationClass(config.getType()); } catch (Throwable t) { throw new IllegalArgumentException ("Cannot instantiate ActionFormBean class '" + config.getType() + "': " + t); } if (!DynaActionForm.class.isAssignableFrom(beanClass)) { throw new IllegalArgumentException ("Class '" + config.getType() + "' is not a subclass of " + "'org.apache.struts.action.DynaActionForm'"); } // Set the name we will know ourselves by from the form bean name this.name = config.getName(); // Look up the property descriptors for this bean class FormPropertyConfig descriptors[] = config.findFormPropertyConfigs(); if (descriptors == null) { descriptors = new FormPropertyConfig[0]; } // Create corresponding dynamic property definitions properties = new DynaProperty[descriptors.length]; for (int i = 0; i < descriptors.length; i++) { properties[i] = new DynaProperty(descriptors[i].getName(), descriptors[i].getTypeClass()); propertiesMap.put(properties[i].getName(), properties[i]); } }
/** * Returns the properties of this dyna class. * * @return The properties */ public SqlDynaProperty[] getSqlDynaProperties() { DynaProperty[] props = getDynaProperties(); SqlDynaProperty[] result = new SqlDynaProperty[props.length]; System.arraycopy(props, 0, result, 0, props.length); return result; }
/** * {@inheritDoc} */ public boolean equals(Object obj) { if (obj instanceof SqlDynaBean) { SqlDynaBean other = (SqlDynaBean)obj; DynaClass dynaClass = getDynaClass(); if (dynaClass.equals(other.getDynaClass())) { DynaProperty[] props = dynaClass.getDynaProperties(); for (int idx = 0; idx < props.length; idx++) { Object value = get(props[idx].getName()); Object otherValue = other.get(props[idx].getName()); if (value == null) { if (otherValue != null) { return false; } } else { return value.equals(otherValue); } } return true; } } return false; }
/** * <p>Return the property descriptor for the specified property name.</p> * * @param name Name of the property for which to retrieve the descriptor * @return The property descriptor for the specified property name. * @throws IllegalArgumentException if this is not a valid property name * for our DynaClass */ protected DynaProperty getDynaProperty(String name) { DynaProperty descriptor = getDynaClass().getDynaProperty(name); if (descriptor == null) { throw new IllegalArgumentException("Invalid property name '" + name + "'"); } return (descriptor); }
/** * <p>Return an array of <code>DynaProperty</code>s for the properties * currently defined in this <code>DynaClass</code>. If no properties are * defined, a zero-length array will be returned.</p> * * @return An array of property instances for this class. */ public DynaProperty[] getDynaProperties() { return (properties); // :FIXME: Should we really be implementing // getBeanInfo instead, which returns property descriptors // and a bunch of other stuff? }
/** * <p>Introspect our form bean configuration to identify the supported * properties.</p> * * @param config The FormBeanConfig instance describing the properties of * the bean to be created * @throws IllegalArgumentException if the bean implementation class * specified in the configuration is not * DynaActionForm (or a subclass of * DynaActionForm) */ protected void introspect(FormBeanConfig config) { this.config = config; // Validate the ActionFormBean implementation class try { beanClass = RequestUtils.applicationClass(config.getType()); } catch (Throwable t) { throw new IllegalArgumentException( "Cannot instantiate ActionFormBean class '" + config.getType() + "': " + t); } if (!DynaActionForm.class.isAssignableFrom(beanClass)) { throw new IllegalArgumentException("Class '" + config.getType() + "' is not a subclass of " + "'org.apache.struts.action.DynaActionForm'"); } // Set the name we will know ourselves by from the form bean name this.name = config.getName(); // Look up the property descriptors for this bean class FormPropertyConfig[] descriptors = config.findFormPropertyConfigs(); if (descriptors == null) { descriptors = new FormPropertyConfig[0]; } // Create corresponding dynamic property definitions properties = new DynaProperty[descriptors.length]; for (int i = 0; i < descriptors.length; i++) { properties[i] = new DynaProperty(descriptors[i].getName(), descriptors[i].getTypeClass()); propertiesMap.put(properties[i].getName(), properties[i]); } }
/** * Corner cases on getDynaProperty invalid arguments. */ public void testGetDescriptorArguments() { DynaProperty descriptor = dynaForm.getDynaClass().getDynaProperty("unknown"); assertNull("Unknown property descriptor should be null", descriptor); try { dynaForm.getDynaClass().getDynaProperty(null); fail("Should throw IllegalArgumentException"); } catch (IllegalArgumentException e) { ; // Expected response } }
/** * Base for testGetDescriptorXxxxx() series of tests. * * @param name Name of the property to be retrieved * @param type Expected class type of this property */ protected void testGetDescriptorBase(String name, Class type) { DynaProperty descriptor = dynaForm.getDynaClass().getDynaProperty(name); assertNotNull("Got descriptor", descriptor); assertEquals("Got correct type", type, descriptor.getType()); }