public void addColumnDefinition(Predicate<? super K> predicate, ColumnDefinition<K, ?> definition) { for(Object prop : definition.properties()) { addColumnProperty(predicate, newFactory(prop)); } } public void addColumnDefinition(String name, ColumnDefinition<K, ?> definition) {
private static CrudMeta getCrudMeta(Connection connection, String table, ColumnDefinitionProvider<JdbcColumnKey> columnDefinitionProvider, ResultSet resultSet, DatabaseMeta databaseMeta) throws SQLException { ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); ColumnMeta[] columnMetas = new ColumnMeta[resultSetMetaData.getColumnCount()]; List<String> primaryKeys = getPrimaryKeys(connection, resultSetMetaData, columnDefinitionProvider); int length = columnMetas.length; for(int i = 0; i < length; i++) { String columnName = resultSetMetaData.getColumnName(i + 1); ColumnDefinition<JdbcColumnKey,?> columnDefinition = columnDefinitionProvider.getColumnDefinition(JdbcColumnKey.of(resultSetMetaData, i + 1)); AutoGeneratedProperty autoGeneratedProperty = columnDefinition.lookFor(AutoGeneratedProperty.class); if (autoGeneratedProperty == null && resultSetMetaData.isAutoIncrement(i + 1) && ! columnDefinition.has(IgnoreAutoGeneratedProperty.class)) { autoGeneratedProperty = AutoGeneratedProperty.DEFAULT; } columnMetas[i] = new ColumnMeta( columnName, resultSetMetaData.getColumnType(i + 1), primaryKeys.contains(columnName), autoGeneratedProperty); } return new CrudMeta(databaseMeta, table, columnMetas); }
@Override public <P> ContextualGetter<? super S, ? extends P> getGetterFromSource(K columnKey, Type propertyType, ColumnDefinition<K, ?> columnDefinition, Supplier<ClassMeta<P>> propertyClassMetaSupplier, MappingContextFactoryBuilder<?, K> mappingContextFactoryBuilder) { @SuppressWarnings("unchecked") ContextualGetter<? super S, ? extends P> getter = ContextualGetterAdapter.of((Getter<? super S, ? extends P>) columnDefinition.getCustomGetterFrom(sourceType)); GetterFactory<? super S, K> customGetterFactory = (GetterFactory<? super S, K>) columnDefinition.getCustomGetterFactoryFrom(sourceType); if (customGetterFactory != null) { getter = (ContextualGetter<? super S, ? extends P>) ContextualGetterAdapter.<S, P>of(customGetterFactory.<P>newGetter(propertyType, columnKey, columnDefinition.properties())); ConverterProperty converterProperty = columnDefinition.lookFor(ConverterProperty.class); t = columnKey.getType(t); getter = getterFactory.<P>newGetter(t, columnKey, mappingContextFactoryBuilder, columnDefinition.properties()); if (getter == null) { return null; getter = getterFactory.newGetter(propertyType, columnKey, mappingContextFactoryBuilder, columnDefinition.properties()); DefaultValueProperty defaultValueProperty = columnDefinition.lookFor(DefaultValueProperty.class); if (defaultValueProperty != null) { Object value = defaultValueProperty.getValue();
Getter<?, ?> customGetter = pm.getColumnDefinition().getCustomGetterFrom(pm.getPropertyMeta().getOwnerType()); if (customGetter != null) { getter = (Getter<S, P>) customGetter; if (TypeHelper.isPrimitive(type) && !columnDefinition.has(FormatProperty.class)) { if (getter instanceof BooleanGetter) { return new BooleanConstantTargetFieldMapper<S, Appendable>((BooleanGetter<? super S>) getter, new BooleanAppendableSetter(cellWriter)); if (TypeHelper.isEnum(type) && columnDefinition.has(EnumOrdinalFormatProperty.class)) { setter = (ContextualSetter<Appendable, ? super P>) new EnumOrdinalAppendableSetter(cellWriter); if (columnDefinition.has(FormatProperty.class)) { format = columnDefinition.lookFor(FormatProperty.class).format(); } else if (TypeHelper.areEquals(type, Date.class)) { String df = DEFAULT_DATE_FORMAT; DateFormatProperty dfp = columnDefinition.lookFor(DateFormatProperty.class); if (dfp != null) { df = dfp.get(); CharSequence.class, builder, columnDefinition.properties());
public boolean has(Class<?> clazz) { return lookFor(clazz) != null; }
if (!modifiable) throw new IllegalStateException("Builder not modifiable"); if (columnDefinition.ignore()) { propertyMappingsBuilderProbe.ignore(key, columnDefinition); properties.add(null); Predicate<PropertyMeta<?, ?>> propertyFilter = isValidPropertyMeta.predicate(key, columnDefinition.properties(), accessorNotFounds); .findProperty(propertyNameMatcher, columnDefinition.properties(), toTypeAffinity(key), propertyMappingsBuilderProbe.propertyFinderProbe(propertyNameMatcher), propertyFilter); if (!columnDefinition.has(OptionalProperty.class)) { AccessorNotFound accessorNotFound = selectAccessotNotFound(accessorNotFounds); if (accessorNotFound == null) {
public boolean ignore() { return has(IgnoreProperty.class); }
@Override @SuppressWarnings("unchecked") public final ConstantSourceMapperBuilder<S, T, K> addMapping(K key, final ColumnDefinition<K, ?> columnDefinition) { final ColumnDefinition<K, ?> composedDefinition = columnDefinition.compose(mapperConfig.columnDefinitions().getColumnDefinition(key)); final K mappedColumnKey = composedDefinition.rename(key); FieldMapperProperty prop = columnDefinition.lookFor(FieldMapperProperty.class); if (prop != null) { addMapper((FieldMapper<S, T>) prop.getFieldMapper()); } else { PropertyMapping<T, ?, K> propertyMapping = propertyMappingsBuilder.addProperty(mappedColumnKey, composedDefinition); if (propertyMapping != null) { ColumnDefinition<K, ?> effectiveColumnDefinition = propertyMapping.getColumnDefinition(); Predicate<PropertyMeta<?, ?>> keyAppliesTo = effectiveColumnDefinition.keyAppliesTo(); if (effectiveColumnDefinition.isKey() && keyAppliesTo.test(propertyMapping.getPropertyMeta())) { Predicate<S> predicate = buildKeyPredicate(propertyMapping.getPropertyMeta(), keyAppliesTo); mappingContextFactoryBuilder.addKey(new KeyAndPredicate<S, K>(mappedColumnKey, predicate)); } } } return this; }
public GetterProperty getCustomGetterPropertyFrom(Type fromType) { for(GetterProperty getterProperty : lookForAll(GetterProperty.class)) { if (getterProperty.getSourceType() == null || TypeHelper.isAssignable(getterProperty.getSourceType(), fromType)) { return getterProperty; } } return null; }
private boolean isTargetForMapperFieldMapper(PropertyMapping pm) { return pm.getPropertyMeta().isSubProperty() || (JoinUtils.isArrayElement(pm.getPropertyMeta()) && pm.getColumnDefinition().isKey()); }
@SuppressWarnings("unchecked") private List<KeyAndPredicate<S, K>> getSubKeys(List<PropertyMapping<T, ?, K>> properties) { List<KeyAndPredicate<S, K>> keys = new ArrayList<KeyAndPredicate<S, K>>(); // look for keys property of the object for (PropertyMapping<T, ?, K> pm : properties) { Predicate<PropertyMeta<?, ?>> propertyMetaKeyPredicate = pm.getColumnDefinition().keyAppliesTo(); if (pm.getPropertyMeta().isSubProperty()) { SubPropertyMeta<T, ?, ?> subPropertyMeta = (SubPropertyMeta<T, ?, ?>) pm.getPropertyMeta(); if (!(JoinUtils.isArrayElement(subPropertyMeta.getSubProperty()))) { // ignore ArrayElementPropertyMeta as it's a direct getter and will be managed in the setter if (pm.getColumnDefinition().isKey()) { if (propertyMetaKeyPredicate.test(subPropertyMeta.getSubProperty())) { Predicate<? super S> predicate = buildKeyPredicate(subPropertyMeta.getSubProperty(), propertyMetaKeyPredicate);; keys.add(new KeyAndPredicate<S, K>(pm.getColumnKey(), predicate)); } } } } else { if (pm.getColumnDefinition().isKey()) { if (propertyMetaKeyPredicate.test(pm.getPropertyMeta())) { keys.add(new KeyAndPredicate<S, K>(pm.getColumnKey(), null)); } } } } return keys; }
@SuppressWarnings("unchecked") @Override public <S, P> FieldMapper<S, T> newFieldMapper( PropertyMapping<S, P, K> pm, MappingContextFactoryBuilder contextFactoryBuilder, MapperBuilderErrorHandler mappingErrorHandler ) { Getter<? super S, ? extends P> getter = (Getter<? super S, ? extends P>) pm.getColumnDefinition().getCustomGetterFrom(pm.getPropertyMeta().getOwnerType()); if (getter == null) { getter = pm.getPropertyMeta().getGetter(); } if (getter == null) { mappingErrorHandler.accessorNotFound("Could not find getter for " + pm.getColumnKey() + " type " + pm.getPropertyMeta().getPropertyType() + " path " + pm.getPropertyMeta().getPath() + ", See " + ErrorDoc.CTFM_GETTER_NOT_FOUND.toUrl()); return null; } ContextualSetter<? super T, ? super P> setter = getSetterForTarget(pm, contextFactoryBuilder); if (setter == null) { mappingErrorHandler.accessorNotFound("Could not find setter for " + pm.getColumnKey() + " type " + pm.getPropertyMeta().getPropertyType() + " path " + pm.getPropertyMeta().getPath() + " See " + ErrorDoc.CTFM_SETTER_NOT_FOUND.toUrl()); return null; } Type propertyType = pm.getPropertyMeta().getPropertyType(); return buildFieldMapper(getter, setter, propertyType); }
Getter<?, ?> customGetter = pm.getColumnDefinition().getCustomGetterFrom(pm.getPropertyMeta().getOwnerType()); if (customGetter != null) { getter = (Getter<S, P>) customGetter; if (TypeHelper.isPrimitive(type) && !columnDefinition.has(FormatProperty.class)) { if (getter instanceof BooleanGetter) { return new BooleanConstantTargetFieldMapper<S, Appendable>((BooleanGetter<? super S>) getter, new BooleanAppendableSetter(cellWriter)); if (TypeHelper.isEnum(type) && columnDefinition.has(EnumOrdinalFormatProperty.class)) { setter = (ContextualSetter<Appendable, ? super P>) new EnumOrdinalAppendableSetter(cellWriter); if (columnDefinition.has(FormatProperty.class)) { format = columnDefinition.lookFor(FormatProperty.class).format(); } else if (TypeHelper.areEquals(type, Date.class)) { String df = DEFAULT_DATE_FORMAT; DateFormatProperty dfp = columnDefinition.lookFor(DateFormatProperty.class); if (dfp != null) { df = dfp.get(); CharSequence.class, builder, columnDefinition.properties());
public boolean has(Class<?> clazz) { return lookFor(clazz) != null; }
if (!modifiable) throw new IllegalStateException("Builder not modifiable"); if (columnDefinition.ignore()) { propertyMappingsBuilderProbe.ignore(key, columnDefinition); properties.add(null); Predicate<PropertyMeta<?, ?>> propertyFilter = isValidPropertyMeta.predicate(key, columnDefinition.properties(), accessorNotFounds); .findProperty(propertyNameMatcher, columnDefinition.properties(), toTypeAffinity(key), propertyMappingsBuilderProbe.propertyFinderProbe(propertyNameMatcher), propertyFilter); if (!columnDefinition.has(OptionalProperty.class)) { AccessorNotFound accessorNotFound = selectAccessotNotFound(accessorNotFounds); if (accessorNotFound == null) {
public boolean isKey() { return has(KeyProperty.class); }
@Override @SuppressWarnings("unchecked") public final ConstantSourceMapperBuilder<S, T, K> addMapping(K key, final ColumnDefinition<K, ?> columnDefinition) { final ColumnDefinition<K, ?> composedDefinition = columnDefinition.compose(mapperConfig.columnDefinitions().getColumnDefinition(key)); final K mappedColumnKey = composedDefinition.rename(key); FieldMapperProperty prop = columnDefinition.lookFor(FieldMapperProperty.class); if (prop != null) { addMapper((FieldMapper<S, T>) prop.getFieldMapper()); } else { PropertyMapping<T, ?, K> propertyMapping = propertyMappingsBuilder.addProperty(mappedColumnKey, composedDefinition); if (propertyMapping != null) { ColumnDefinition<K, ?> effectiveColumnDefinition = propertyMapping.getColumnDefinition(); Predicate<PropertyMeta<?, ?>> keyAppliesTo = effectiveColumnDefinition.keyAppliesTo(); if (effectiveColumnDefinition.isKey() && keyAppliesTo.test(propertyMapping.getPropertyMeta())) { Predicate<S> predicate = buildKeyPredicate(propertyMapping.getPropertyMeta(), keyAppliesTo); mappingContextFactoryBuilder.addKey(new KeyAndPredicate<S, K>(mappedColumnKey, predicate)); } } } return this; }
public GetterProperty getCustomGetterPropertyFrom(Type fromType) { for(GetterProperty getterProperty : lookForAll(GetterProperty.class)) { if (getterProperty.getSourceType() == null || TypeHelper.isAssignable(getterProperty.getSourceType(), fromType)) { return getterProperty; } } return null; }
private boolean isTargetForMapperFieldMapper(PropertyMapping pm) { return pm.getPropertyMeta().isSubProperty() || (JoinUtils.isArrayElement(pm.getPropertyMeta()) && pm.getColumnDefinition().isKey()); }
@SuppressWarnings("unchecked") private List<KeyAndPredicate<S, K>> getSubKeys(List<PropertyMapping<T, ?, K>> properties) { List<KeyAndPredicate<S, K>> keys = new ArrayList<KeyAndPredicate<S, K>>(); // look for keys property of the object for (PropertyMapping<T, ?, K> pm : properties) { Predicate<PropertyMeta<?, ?>> propertyMetaKeyPredicate = pm.getColumnDefinition().keyAppliesTo(); if (pm.getPropertyMeta().isSubProperty()) { SubPropertyMeta<T, ?, ?> subPropertyMeta = (SubPropertyMeta<T, ?, ?>) pm.getPropertyMeta(); if (!(JoinUtils.isArrayElement(subPropertyMeta.getSubProperty()))) { // ignore ArrayElementPropertyMeta as it's a direct getter and will be managed in the setter if (pm.getColumnDefinition().isKey()) { if (propertyMetaKeyPredicate.test(subPropertyMeta.getSubProperty())) { Predicate<? super S> predicate = buildKeyPredicate(subPropertyMeta.getSubProperty(), propertyMetaKeyPredicate);; keys.add(new KeyAndPredicate<S, K>(pm.getColumnKey(), predicate)); } } } } else { if (pm.getColumnDefinition().isKey()) { if (propertyMetaKeyPredicate.test(pm.getPropertyMeta())) { keys.add(new KeyAndPredicate<S, K>(pm.getColumnKey(), null)); } } } } return keys; }