List of usage examples for org.apache.commons.beanutils BeanUtils populate
public static void populate(Object bean, Map properties)
From source file:org.latticesoft.util.resource.DatabaseUtil.java
/** * Retrieves the results into a bean// w ww . jav a2 s. c om * @param rs the ResultSet * @param columns the column name * @param beanClass the bean to be instantiated * @param beanProps the attributes of the column * @return a collection of beans of result */ public static Collection retrieveResult(ResultSet rs, String[] columns, Class beanClass, String[] beanProps) throws SQLException { Collection c = new ArrayList(); if (rs == null || beanClass == null || columns == null || beanProps == null) { return c; } if (columns.length != beanProps.length) { return c; } Map map = new HashMap(); while (rs.next()) { try { Object bean = beanClass.newInstance(); map.clear(); for (int i = 0; i < columns.length; i++) { Object column = rs.getObject(columns[i]); map.put(beanProps[i], column); } BeanUtils.populate(bean, map); } catch (InvocationTargetException ite) { if (log.isErrorEnabled()) { log.error(ite); } } catch (IllegalAccessException iae) { if (log.isErrorEnabled()) { log.error(iae); } } catch (InstantiationException ie) { if (log.isErrorEnabled()) { log.error(ie); } } catch (SQLException sqle) { if (log.isErrorEnabled()) { log.error(sqle); } } } return c; }
From source file:org.latticesoft.util.resource.DatabaseUtil.java
/** * Executes the query and populate the results into a collection of the beanClass instance. * The sql statement must include the bean attribute name map to the database column * E.g./*from w w w . j av a 2s. c o m*/ * SELECT dbcol1 AS beanAttribute1, dbcol2 AS beanAttribute2 FROM someTable * In this case the dbcol1 is the name of the table column which maps to the * beanAttribute1 name of the java bean * * @param sql the query to execute * @param beanClass the class of the java bean * @param conn the connection * @param closeConn true to close the connection false will leave it open * @return a collection of the populated bean object */ public static Collection executeQuery(String sql, Class beanClass, Connection conn, boolean closeConn) { ArrayList a = new ArrayList(); if (sql == null || conn == null || beanClass == null) return a; Statement stmt = null; ResultSet rs = null; Map map = new HashMap(); try { boolean isAll = false; String[] dbFields = DatabaseUtil.analyseSelectSQL(sql); String[] classFields = ClassUtil.getAttributeNamesAsArray(beanClass, true); if (dbFields == null) { isAll = true; } if (dbFields == null && classFields == null) { return a; } stmt = conn.createStatement(); rs = stmt.executeQuery(sql); Object o = null; String field = null; while (rs.next()) { if (isAll) { for (int i = 0; i < classFields.length; i++) { field = classFields[i]; o = rs.getObject(field); map.put(field, o); } } else { for (int i = 0; i < dbFields.length; i++) { field = dbFields[i]; o = rs.getObject(field); map.put(field, o); } } Object bean = beanClass.newInstance(); BeanUtils.populate(bean, map); a.add(bean); } } catch (Exception e) { if (log.isErrorEnabled()) { log.error(e); } } finally { try { stmt.close(); } catch (Exception e) { } if (closeConn) { try { conn.close(); } catch (Exception e) { } } stmt = null; conn = null; } return a; }
From source file:org.liveSense.core.wrapper.JcrNodeTransformer.java
public static Object transformNodeToBean(Node node, Object bean) throws RepositoryException, IllegalAccessException, InvocationTargetException { HashMap map = new HashMap(); PropertyIterator names = node.getProperties(); while (names.hasNext()) { Property prop = names.nextProperty(); GenericValue val = new GenericValue(prop); map.put(prop.getName(), val.getGenericObject()); }/*from w w w . j a v a 2 s .co m*/ BeanUtils.populate(bean, map); return bean; }
From source file:org.liveSense.core.wrapper.JcrNodeTransformer.java
public static Node transformMapToNode(Node node, Map values, Class beanClass, String[] skippedFields) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {//from www . j a v a2 s. c om Object bean = beanClass.newInstance(); BeanUtils.populate(bean, values); return transformBeanToNode(node, bean, skippedFields); }
From source file:org.mcplissken.gateway.restful.RESTfulRequest.java
protected void multipartRequestEnded(HttpAdapter httpAdapter, RESTfulRequest request, RESTfulResponse response, MultipartRequestHandler handler, HashMap<String, String> formAttrsMap) throws Exception { Object formInstance = handler.createFormInstance(); BeanUtils.populate(formInstance, formAttrsMap); handler.handle(httpAdapter, request, response, formInstance, uploads); }
From source file:org.mule.providers.service.ConnectorFactory.java
private static UMOConnector getOrCreateConnectorByProtocol(UMOEndpointURI uri, int create) throws ConnectorFactoryException { UMOConnector connector = getConnectorByProtocol(uri.getFullScheme()); if (ConnectorFactory.ALWAYS_CREATE_CONNECTOR == create || (connector == null && create == ConnectorFactory.GET_OR_CREATE_CONNECTOR)) { connector = ConnectorFactory.createConnector(uri); try {/*from w w w. j a v a 2 s. c o m*/ BeanUtils.populate(connector, uri.getParams()); MuleManager.getInstance().registerConnector(connector); } catch (Exception e) { throw new ConnectorFactoryException( new Message(Messages.FAILED_TO_SET_PROPERTIES_ON_X, "Connector"), e); } } else if (create == ConnectorFactory.NEVER_CREATE_CONNECTOR && connector == null) { logger.warn("There is no connector for protocol: " + uri.getScheme() + " and 'createConnector' is set to NEVER. Returning null"); } return connector; }
From source file:org.openengsb.core.util.BeanUtilsExtended.java
/** * Creates a new instance of the beanType and populates it with the property-values from the map * * @throws IllegalArgumentException if the bean cannot be populated because of errors in the definition of the * beantype//from w w w. j a va 2 s. c o m */ public static <BeanType> BeanType createBeanFromAttributeMap(Class<BeanType> beanType, Map<String, ? extends Object> attributeValues) { BeanType instance; try { instance = beanType.newInstance(); BeanUtils.populate(instance, attributeValues); } catch (Exception e) { ReflectionUtils.handleReflectionException(e); throw new IllegalStateException("Should never get here"); } return instance; }
From source file:org.opens.urlmanager.rest.unserializer.generic.GenericGetAndPostParameterUnserializer.java
@Override public T unserialize(HttpServletRequest request) { T bean;//www .j a va 2s .com Map httpParameterMap; httpParameterMap = new HashMap(request.getParameterMap()); try { bean = targetClass.newInstance(); } catch (Exception ex) { Logger.getLogger(GenericGetAndPostParameterUnserializer.class.getName()).log(Level.SEVERE, null, ex); throw new BadClientRequestException("Unable to unserialize requested entity", ex); } prePopulate(bean, httpParameterMap); try { BeanUtils.populate(bean, httpParameterMap); } catch (Exception ex) { Logger.getLogger(GenericGetAndPostParameterUnserializer.class.getName()).log(Level.SEVERE, null, ex); throw new BadClientRequestException("Unable to unserialize requested entity", ex); } return bean; }
From source file:org.openvpms.web.component.im.archetype.ArchetypeHandler.java
/** * Initialises the handler.// www . j ava 2s . c o m * * @param handler the handler * @return the handler * @throws IllegalAccessException if the caller does not have * access to the property accessor method * @throws InvocationTargetException if the property accessor method * throws an exception */ public T initialise(T handler) throws IllegalAccessException, InvocationTargetException { if (properties != null && properties.size() != 0) { BeanUtils.populate(handler, properties); } return handler; }
From source file:org.pentaho.ui.xul.impl.XulParser.java
protected XulComponent getElement(org.dom4j.Element srcEle, XulContainer parent) throws XulException { String handlerName = srcEle.getName().toUpperCase(); Attribute att = srcEle//from w w w .ja va 2 s . c o m .attribute(new QName("customclass", new Namespace("pen", "http://www.pentaho.org/2008/xul"))); // If the custom handler is registered, use it; otherwise, fall back to the original element handler... if (att != null) { String potentialHandlerName = att.getValue().toUpperCase(); if (handlers.get(potentialHandlerName) != null) { handlerName = potentialHandlerName; } } Object handler = handlers.get(handlerName); if (handler == null) { logger.error("handler not found: " + handlerName); return null; // throw new XulException(String.format("No handler available for input: %s", srcEle.getName())); } String tagName = srcEle.getName(); try { Constructor<?> constructor = getContructor((String) handler); // create a generic element representation of the current Dom4J node Element domEle = DocumentFactory.createElement(srcEle.getName().toLowerCase()); List<Attribute> attrs = srcEle.attributes(); for (Attribute attr : attrs) { domEle.setAttribute(attr.getName(), attr.getValue()); } XulComponent ele = (XulComponent) constructor.newInstance(domEle, parent, xulDomContainer, tagName); // preserve atributes in new Generic Dom node for (Attribute attr : attrs) { ele.setAttribute(attr.getName(), attr.getValue()); } Map<String, String> attributesMap = XulUtil.AttributesToMap(srcEle.attributes()); BeanUtils.populate(ele, attributesMap); return ele; } catch (Exception e) { throw new XulException(e); } }