/** * This follows a path (like object.childobject.value), to its end value, so it can compare the values of two * paths. * This method follows the path recursively until it reaches the end value. * * @param object object to search * @param path path of value * @return result */ private Object deepValue(final Object object, final String path) { final List<String> paths = new LinkedList<>(Arrays.asList(path.split("\\."))); final String currentPath = paths.get(0); final PropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(object); Object field = accessor.getPropertyValue(currentPath); paths.remove(0); if ((field != null) && (!paths.isEmpty())) { field = deepValue(field, String.join(".", paths)); } return field; }
/** * Write the value to the existingEntity field with the name of key * * @param <T> Type of the entity * @param existingEntity The entity we are changing * @param key The key we are changing * @param value The new value */ private <T extends BaseEntity> void writeToEntity(T existingEntity, String key, Object value) { final PropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(existingEntity); if (accessor.getPropertyType(key) != null) { try { if (value.getClass().equals(JSONObject.class) && ((JSONObject) value).has("_isMap") && ((JSONObject) value).get("_isMap").equals(true)) { writeArrayMapToEntity(accessor, key, (JSONObject) value); } else if (value.getClass().equals(JSONObject.class)) { writeObjectToEntity(accessor, key, (JSONObject) value); } else if (value.getClass().equals(JSONArray.class)) { writeArrayToEntity(accessor, key, (JSONArray) value); } else if (isFieldValid(accessor, key, existingEntity.getClass())) { writeValueToEntity(accessor, key, value); } } catch (JSONException e) { logger.info("[FormParse] [writeToEntity] Unable To Process JSON", e); } } }
protected boolean hasRestrictionOnField(Query query, String fieldName) { for (Restriction<PropertyAccessor> restriction : query.getRestrictions()) { if (restriction instanceof FieldCondition) { if (fieldName.equals(((FieldCondition) restriction).getFieldName())) { return true; } } else if (restriction instanceof DisjunctionCondition) { for (Query subQuery : ((DisjunctionCondition) restriction).getQueries()) { if (hasRestrictionOnField(subQuery, fieldName)) { return true; } } } } return false; }
/** * Returns the index of the last nested property separator in the given * property path, ignoring dots in keys (like "map[my.key]"). */ protected int getLastPropertySeparatorIndex(String propertyPath) { boolean inKey = false; for (int i = propertyPath.length() - 1; i >= 0; i--) { switch (propertyPath.charAt(i)) { case PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR: inKey = true; break; case PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR: return i; case PropertyAccessor.NESTED_PROPERTY_SEPARATOR_CHAR: if (!inKey) { return i; } break; } } return -1; }
public boolean isReadableProperty(String propertyPath) { if (PropertyAccessorUtils.isNestedProperty(propertyPath)) { String baseProperty = getBasePropertyName(propertyPath); String childPropertyPath = getChildPropertyPath(propertyPath); if (!super.isReadableProperty(baseProperty)) { return false; } else { return ((PropertyAccessor) childPropertyAccessors.get(baseProperty)) .isReadableProperty(childPropertyPath); } } else { return super.isReadableProperty(propertyPath); } }
public static int getNestingLevel(String propertyName) { propertyName = getPropertyName(propertyName); int nestingLevel = 0; boolean inKey = false; for (int i = 0; i < propertyName.length(); i++) { switch (propertyName.charAt(i)) { case PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR: if (!inKey) { nestingLevel++; } case PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR: inKey = !inKey; } } return nestingLevel; }
public void autobind(Object view) { BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(getModel()); PropertyAccessor viewPropertyAccessor = new DirectFieldAccessor(view); // iterate on model properties for (PropertyDescriptor pd : bw.getPropertyDescriptors()) { String propertyName = pd.getName(); if ( !ignoredProperties.contains(propertyName) && viewPropertyAccessor.isReadableProperty(propertyName)) { Object control = viewPropertyAccessor.getPropertyValue(propertyName); if (control != null) { if (log.isDebugEnabled()) log.debug("Found control: " + control.getClass().getSimpleName() + " for property: " + propertyName); bind(control, propertyName); } } } }
@Override public boolean isValid(final WithId<?> value, final ConstraintValidatorContext context) { if (value == null) { return true; } final PropertyAccessor bean = new BeanWrapperImpl(value); final String propertyValue = String.valueOf(bean.getPropertyValue(property)); @SuppressWarnings({"rawtypes", "unchecked"}) final Class<WithId> modelClass = (Class) value.getKind().modelClass; @SuppressWarnings("unchecked") final Set<String> ids = dataManager.fetchIdsByPropertyValue(modelClass, property, propertyValue); final boolean isUnique = ids.isEmpty() || value.getId().map(id -> ids.contains(id)).orElse(false); if (!isUnique) { if (ids.stream().allMatch(id -> consideredValidByException(modelClass, id))) { return true; } context.disableDefaultConstraintViolation(); context.unwrap(HibernateConstraintValidatorContext.class).addExpressionVariable("nonUnique", propertyValue) .buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()) .addPropertyNode(property).addConstraintViolation(); } return isUnique; }
/** * Write normal value to entity. * * @param accessor The accessor for the existing entity * @param key The fields name we are overwriting * @param value The new value */ private void writeValueToEntity(final PropertyAccessor accessor, final String key, final Object value) { final ResolvableType type = accessor.getPropertyTypeDescriptor(key).getResolvableType(); accessor.setPropertyValue(key, parseValue(value, type)); }
/** * Is the field editable according to the SchemaView annotation * * @param accessor the accessor * @param key the key * @return boolean boolean */ private Boolean isFieldEditable(final PropertyAccessor accessor, final String key) { final SchemaView schemaView = accessor.getPropertyTypeDescriptor(key).getAnnotation(SchemaView.class); if (schemaView != null) { final boolean isLocked = schemaView.locked() && accessor.getPropertyValue(key) != null; final boolean isVisible = schemaView.visible(); return !isLocked && isVisible; } return false; }
/** * Set the path that this tag should apply. * <p>E.g. "customer" to allow bind paths like "address.street" * rather than "customer.address.street". * @see BindTag#setPath */ public void setPath(String path) { if (path == null) { path = ""; } if (path.length() > 0 && !path.endsWith(PropertyAccessor.NESTED_PROPERTY_SEPARATOR)) { path += PropertyAccessor.NESTED_PROPERTY_SEPARATOR; } this.path = path; }
/** * Get the {@link BindStatus} for this tag. */ protected BindStatus getBindStatus() throws JspException { if (this.bindStatus == null) { // HTML escaping in tags is performed by the ValueFormatter class. String nestedPath = getNestedPath(); String pathToUse = (nestedPath != null ? nestedPath + getPath() : getPath()); if (pathToUse.endsWith(PropertyAccessor.NESTED_PROPERTY_SEPARATOR)) { pathToUse = pathToUse.substring(0, pathToUse.length() - 1); } this.bindStatus = new BindStatus(getRequestContext(), pathToUse, false); } return this.bindStatus; }
private Directory getDirectoryService() { HttpTransport httpTransport = new NetHttpTransport(); JacksonFactory jacksonFactory = new JacksonFactory(); GoogleCredential credential = getGoogleCredential(); PropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(credential); accessor.setPropertyValue("serviceAccountUser", config.getAdminUsername()); accessor.setPropertyValue("serviceAccountScopes", SERVICE_ACCOUNT_SCOPES); return new Directory.Builder(httpTransport, jacksonFactory, credential) .setApplicationName("Spinnaker-Fiat") .build(); }
@Test public void overrideEnableFallback() throws Exception { PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor( "spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.enableFallback:true"); assertThat(accessor.getPropertyValue("enableFallback")).isEqualTo(Boolean.TRUE); }
@Test public void overrideNormalPrefix() throws Exception { PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor( "spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.normalPrefix:normal/"); assertThat(accessor.getPropertyValue("normalPrefix")).isEqualTo("normal/"); }
@Test public void overrideMobilePrefix() throws Exception { PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor( "spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.mobilePrefix:mob/"); assertThat(accessor.getPropertyValue("mobilePrefix")).isEqualTo("mob/"); }
@Test public void overrideTabletPrefix() throws Exception { PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor( "spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.tabletPrefix:tab/"); assertThat(accessor.getPropertyValue("tabletPrefix")).isEqualTo("tab/"); }
@Test public void overrideNormalSuffix() throws Exception { PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor( "spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.normalSuffix:.nor"); assertThat(accessor.getPropertyValue("normalSuffix")).isEqualTo(".nor"); }
@Test public void overrideMobileSuffix() throws Exception { PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor( "spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.mobileSuffix:.mob"); assertThat(accessor.getPropertyValue("mobileSuffix")).isEqualTo(".mob"); }
@Test public void overrideTabletSuffix() throws Exception { PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor( "spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.tabletSuffix:.tab"); assertThat(accessor.getPropertyValue("tabletSuffix")).isEqualTo(".tab"); }
private PropertyAccessor getLiteDeviceDelegatingViewResolverAccessor( String... configuration) { load(configuration); LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context .getBean("deviceDelegatingJspViewResolver", LiteDeviceDelegatingViewResolver.class); return new DirectFieldAccessor(liteDeviceDelegatingViewResolver); }
private PropertyAccessor getLiteDeviceDelegatingViewResolverAccessor( String... configuration) { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, configuration); this.context.register(Config.class, WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, DeviceDelegatingViewResolverConfiguration.class); this.context.refresh(); LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context .getBean("deviceDelegatingViewResolver", LiteDeviceDelegatingViewResolver.class); return new DirectFieldAccessor(liteDeviceDelegatingViewResolver); }
/** * Gets index of property name. * * <p> * Returns the index number of the location of the given property name. * </p> * * @param propertyName name of property to find index of. * @return index number representing location of property name. */ private Integer extractIndex(String propertyName) { int firstIndex = propertyName.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR); int lastIndex = propertyName.lastIndexOf(PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR); if (firstIndex != -1 && lastIndex != -1) { String indexValue = propertyName.substring(firstIndex + 1, lastIndex); try { int index = Integer.parseInt(indexValue); return Integer.valueOf(index); } catch (NumberFormatException e) { // if we encounter this then it wasn't really an index, ignore } } return null; }
@Test public void overrideEnableFallback() throws Exception { PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor( "spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.enableFallback:true"); assertEquals(true, accessor.getPropertyValue("enableFallback")); }
@Test public void overrideNormalPrefix() throws Exception { PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor( "spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.normalPrefix:normal/"); assertEquals("normal/", accessor.getPropertyValue("normalPrefix")); }
@Test public void overrideMobilePrefix() throws Exception { PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor( "spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.mobilePrefix:mob/"); assertEquals("mob/", accessor.getPropertyValue("mobilePrefix")); }
@Test public void overrideTabletPrefix() throws Exception { PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor( "spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.tabletPrefix:tab/"); assertEquals("tab/", accessor.getPropertyValue("tabletPrefix")); }
@Test public void overrideNormalSuffix() throws Exception { PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor( "spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.normalSuffix:.nor"); assertEquals(".nor", accessor.getPropertyValue("normalSuffix")); }
@Test public void overrideMobileSuffix() throws Exception { PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor( "spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.mobileSuffix:.mob"); assertEquals(".mob", accessor.getPropertyValue("mobileSuffix")); }