/** * 将对象中的属性值置入到fields中。 * * <p>对于<code>isValidated()</code>为<code>true</code>的group,该方法无效。 */ public void mapTo(Object object) { if (isValidated() || object == null) { return; } if (log.isDebugEnabled()) { log.debug( "Mapping properties to fields: group=\"{}\", object={}", getName(), ObjectUtil.identityToString(object)); } BeanWrapper bean = new BeanWrapperImpl(object); getForm().getFormConfig().getPropertyEditorRegistrar().registerCustomEditors(bean); for (Field field : getFields()) { String propertyName = field.getFieldConfig().getPropertyName(); if (bean.isReadableProperty(propertyName)) { Object propertyValue = bean.getPropertyValue(propertyName); Class<?> propertyType = bean.getPropertyType(propertyName); PropertyEditor editor = bean.findCustomEditor(propertyType, propertyName); if (editor == null) { editor = BeanUtils.findEditorByConvention(propertyType); } if (editor == null) { if (propertyType.isArray() || CollectionFactory.isApproximableCollectionType(propertyType)) { field.setValues((String[]) bean.convertIfNecessary(propertyValue, String[].class)); } else { field.setValue(bean.convertIfNecessary(propertyValue, String.class)); } } else { editor.setValue(propertyValue); field.setValue(editor.getAsText()); } } else { log.debug( "No readable property \"{}\" found in type {}", propertyName, object.getClass().getName()); } } }
@PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Path("{userCriteria}") public Response updateUser( @PathParam("userCriteria") final String userCriteria, final MultivaluedMapImpl params) { OnmsUser user = null; writeLock(); try { try { user = m_userManager.getOnmsUser(userCriteria); } catch (final Throwable t) { throw getException(Status.BAD_REQUEST, t); } if (user == null) throw getException(Status.BAD_REQUEST, "updateUser: User does not exist: " + userCriteria); log().debug("updateUser: updating user " + user); final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(user); for (final String key : params.keySet()) { if (wrapper.isWritableProperty(key)) { final String stringValue = params.getFirst(key); @SuppressWarnings("unchecked") final Object value = wrapper.convertIfNecessary(stringValue, wrapper.getPropertyType(key)); wrapper.setPropertyValue(key, value); } } log().debug("updateUser: user " + user + " updated"); try { m_userManager.save(user); } catch (final Throwable t) { throw getException(Status.INTERNAL_SERVER_ERROR, t); } return Response.seeOther(getRedirectUri(m_uriInfo)).build(); } finally { writeUnlock(); } }
protected void applyQueryFilters( final MultivaluedMap<String, String> p, final CriteriaBuilder builder) { final MultivaluedMap<String, String> params = new MultivaluedMapImpl(); params.putAll(p); builder.distinct(); builder.limit(DEFAULT_LIMIT); // not sure why we remove this, but that's what the old query filter code did, I presume there's // a reason :) params.remove("_dc"); if (params.containsKey("limit")) { builder.limit(Integer.valueOf(params.getFirst("limit"))); params.remove("limit"); } if (params.containsKey("offset")) { builder.offset(Integer.valueOf(params.getFirst("offset"))); params.remove("offset"); } // Is this necessary anymore? setLimitOffset() comments implies it's for Ext-JS. if (params.containsKey("start")) { builder.offset(Integer.valueOf(params.getFirst("start"))); params.remove("start"); } if (params.containsKey("orderBy")) { builder.orderBy(params.getFirst("orderBy")); params.remove("orderBy"); if (params.containsKey("order")) { if ("desc".equalsIgnoreCase(params.getFirst("order"))) { builder.desc(); } else { builder.asc(); } params.remove("order"); } } final String query = removeParameter(params, "query"); if (query != null) builder.sql(query); final String matchType; final String match = removeParameter(params, "match"); if (match == null) { matchType = "all"; } else { matchType = match; } builder.match(matchType); final Class<?> criteriaClass = builder.toCriteria().getCriteriaClass(); final BeanWrapper wrapper = getBeanWrapperForClass(criteriaClass); final String comparatorParam = removeParameter(params, "comparator", "eq").toLowerCase(); final Criteria currentCriteria = builder.toCriteria(); for (final String key : params.keySet()) { for (final String paramValue : params.get(key)) { // NOSONAR // NOSONAR the interface of MultivaluedMap.class declares List<String> as return value, // the actual implementation com.sun.jersey.core.util.MultivaluedMapImpl returns a String, // so this is fine in some way ... if ("null".equalsIgnoreCase(paramValue)) { builder.isNull(key); } else if ("notnull".equalsIgnoreCase(paramValue)) { builder.isNotNull(key); } else { Object value; Class<?> type = Object.class; try { type = currentCriteria.getType(key); } catch (final IntrospectionException e) { LOG.debug("Unable to determine type for key {}", key); } if (type == null) { type = Object.class; } LOG.warn("comparator = {}, key = {}, propertyType = {}", comparatorParam, key, type); if (comparatorParam.equals("contains") || comparatorParam.equals("iplike") || comparatorParam.equals("ilike") || comparatorParam.equals("like")) { value = paramValue; } else { LOG.debug("convertIfNecessary({}, {})", key, paramValue); try { value = wrapper.convertIfNecessary(paramValue, type); } catch (final Throwable t) { LOG.debug("failed to introspect (key = {}, value = {})", key, paramValue, t); value = paramValue; } } try { final Method m = builder.getClass().getMethod(comparatorParam, String.class, Object.class); m.invoke(builder, new Object[] {key, value}); } catch (final Throwable t) { LOG.warn( "Unable to find method for comparator: {}, key: {}, value: {}", comparatorParam, key, value, t); } } } } }