/** * 使用自定义的属性过滤函数 * * @param srcBean 原Bean * @param destBean 目标bean * @param filter 自定义的过滤函数 */ public static void copyPropertiesPeaceful( Object srcBean, Object destBean, PropertyFilter filter) { add(srcBean); add(destBean); Map<String, BeanStruct> srcMap = simpleProperties(srcBean); Map<String, BeanStruct> dstMap = simpleProperties(destBean); if (valid.valid(srcMap, dstMap)) { Map<String, String> srcMapFilter = new HashMap<>(); Map<String, String> dstMapFilter = new HashMap<>(); for (Map.Entry<String, BeanStruct> entry : srcMap.entrySet()) { srcMapFilter.put(filter.Properties(entry.getKey()), entry.getKey()); } for (Map.Entry<String, BeanStruct> entry : dstMap.entrySet()) { dstMapFilter.put(filter.Properties(entry.getKey()), entry.getKey()); } Map<String, String> intersection = CollectionUtil.intersection(srcMapFilter, dstMapFilter); if (valid.valid(intersection)) { for (Map.Entry<String, String> entry : intersection.entrySet()) { String key = entry.getKey(); String srcKey = srcMapFilter.get(key); String dstKey = dstMapFilter.get(key); try { Object value = readMethod(srcBean, getReadMethod(srcBean, srcKey)); writeMethod(destBean, getWriteMethod(destBean, dstKey), value); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } }
public void write(JSONSerializer serializer, Object object) throws IOException { SerializeWriter out = serializer.getWrier(); DataVO vo = (DataVO) object; if (vo == null) { out.append("null"); return; } try { String[] fields = vo.getFieldNames(); if (fields.length == 0) { out.append("{}"); return; } out.append('{'); boolean commaFlag = false; for (int i = 0; i < fields.length; ++i) { Object propertyValue = vo.getValue(fields[i]); if (propertyValue == null && (!serializer.isEnabled(SerializerFeature.WriteMapNullValue))) { continue; } List<PropertyFilter> propertyFilters = serializer.getPropertyFiltersDirect(); if (propertyFilters != null) { boolean apply = true; for (PropertyFilter propertyFilter : propertyFilters) { if (!propertyFilter.apply(object, fields[i], propertyValue)) { apply = false; break; } } if (!apply) { continue; } } if (commaFlag) { out.append(','); } commaFlag = true; if (propertyValue == null) { out.write(fields[i] + " : null "); continue; } ObjectSerializer objserializer = serializer.getObjectWriter(propertyValue.getClass()); out.write("\"".concat(fields[i]).concat("\":")); objserializer.write(serializer, propertyValue); } out.append('}'); } catch (Exception e) { throw new JSONException("write javaBean error", e); } }
protected String constructSqlString(List<PropertyFilter> in) { // 查询参数 if (in != null && in.size() > 0) { StringBuilder sb = new StringBuilder(); for (PropertyFilter filter : in) { sb.append("and" + filter.getSqlString()); } log.debug("getWhereSqlString :" + sb.toString()); return sb.toString(); } return ""; }
/** * 使用自定义的filter进行属性设值 * * @param bean 操作的Bean * @param pro 类型属性 * @param value 设置属性的值 * @param filter 自定义的函数 * @throws InvocationTargetException * @throws IllegalAccessException */ public static void setPropertyFilter(Object bean, String pro, Object value, PropertyFilter filter) throws InvocationTargetException, IllegalAccessException { add(bean); pro = filter.Properties(pro); Map<String, BeanStruct> map = simpleProperties(bean); if (valid.valid(map)) { Set<String> set = map.keySet(); for (String s : set) { if (pro.equals(filter.Properties(s))) { writeMethod(bean, getWriteMethodIgnore(bean, pro), value); } } } }
/** * 使用自定义的过滤器 * * @param bean 判断的目标bean * @param pro 判断的属性 * @param filter 自定义的属性过滤函数 * @return 是否存在 */ public static boolean hasPropertyFilter(Object bean, String pro, PropertyFilter filter) { add(bean); pro = filter.Properties(pro); Map<String, BeanStruct> map = simpleProperties(bean); if (valid.valid(map)) { Set<String> set = map.keySet(); for (String s : set) { if (pro.equals(filter.Properties(s))) { return true; } } } return false; }
boolean acceptEdgeProperty(String key) { if (edgeFilter != null) { return edgeFilter.acceptProperty(key); } else { return true; } }
boolean acceptNodeProperty(String key) { if (nodeFilter != null) { return nodeFilter.acceptProperty(key); } else { return true; } }
/** * 使用自定义的过滤器获取对象的属性获取对象的属性 * * @param bean 操作的Bean * @param pro 类型属性 * @param filter 自定义的过滤函数 * @return 返回属性的值如果发生异常返回空 * @throws InvocationTargetException * @throws IllegalAccessException */ public static Object getPropertyFilter(Object bean, String pro, PropertyFilter filter) throws InvocationTargetException, IllegalAccessException { add(bean); Object result = null; pro = filter.Properties(pro); Map<String, BeanStruct> map = simpleProperties(bean); if (valid.valid(map)) { Set<String> set = map.keySet(); for (String s : set) { if (pro.equals(filter.Properties(s))) { result = readMethod(bean, getReadMethod(bean, s)); } } } return result; }
/** * 使用自定义的filter进行属性设值 * * @param bean 操作的Bean * @param pro 类型属性 * @param value 设置属性的值 * @param filter 自定义的函数 */ public static void setPropertyFilterPeaceful( Object bean, String pro, Object value, PropertyFilter filter) { add(bean); pro = filter.Properties(pro); Map<String, BeanStruct> map = simpleProperties(bean); if (valid.valid(map)) { Set<String> set = map.keySet(); try { for (String s : set) { if (pro.equals(filter.Properties(s))) { writeMethod(bean, getWriteMethodIgnore(bean, pro), value); } } } catch (InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); } } }
/** * 使用自定义的过滤器获取对象的属性 * * @param bean 操作的Bean * @param pro 类型属性 * @param filter 自定义的过滤函数 * @return 返回属性的值如果发生异常返回空 */ public static Object getPropertyFilterPeaceful(Object bean, String pro, PropertyFilter filter) { add(bean); Object result = null; pro = filter.Properties(pro); Map<String, BeanStruct> map = simpleProperties(bean); if (valid.valid(map)) { Set<String> set = map.keySet(); try { for (String s : set) { if (pro.equals(filter.Properties(s))) { result = readMethod(bean, getReadMethod(bean, s)); } } } catch (InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); } } return result; }
private boolean isTimeUUID(PropertyParsingContext context, Field field) { boolean timeUUID = false; if (filter.hasAnnotation(field, TimeUUID.class)) { Validator.validateBeanMappingTrue( field.getType().equals(UUID.class), "The field '%s' from class '%s' annotated with @TimeUUID should be of java.util.UUID type", field.getName(), context.getCurrentEntityClass().getCanonicalName()); timeUUID = true; } return timeUUID; }
public static Properties getProperties(PropertyFilter filter) { if (null == filter) { return System.getProperties(); } Properties ret = new Properties(); Properties props = System.getProperties(); for (String pName : props.stringPropertyNames()) { String pValue = props.getProperty(pName); Pair<String, String> p = Pair.of(pName, pValue); if (filter.accept(p)) { ret.put(p.getFirst(), p.getSecond()); } } return ret; }
/** 按属性条件列表创建Criterion数组,辅助函数. */ protected Criterion[] buildCriterionByPropertyFilter(final List<PropertyFilter> filters) { List<Criterion> criterionList = new ArrayList<Criterion>(); for (PropertyFilter filter : filters) { if (!filter.hasMultiProperties()) { // 只有一个属性需要比较的情况. Criterion criterion = buildCriterion(filter.getPropertyName(), filter.getMatchValue(), filter.getMatchType()); criterionList.add(criterion); } else { // 包含多个属性需要比较的情况,进行or处理. Disjunction disjunction = Restrictions.disjunction(); for (String param : filter.getPropertyNames()) { Criterion criterion = buildCriterion(param, filter.getMatchValue(), filter.getMatchType()); disjunction.add(criterion); } criterionList.add(disjunction); } } return criterionList.toArray(new Criterion[criterionList.size()]); }
public EntityMeta parseEntity(EntityParsingContext context) { log.debug("Parsing entity class {}", context.getCurrentEntityClass().getCanonicalName()); Class<?> entityClass = context.getCurrentEntityClass(); validateEntityAndGetObjectMapper(context); String columnFamilyName = introspector.inferColumnFamilyName(entityClass, entityClass.getName()); Pair<ConsistencyLevel, ConsistencyLevel> consistencyLevels = introspector.findConsistencyLevels(entityClass, context.getConfigurableCLPolicy()); context.setCurrentConsistencyLevels(consistencyLevels); context.setCurrentColumnFamilyName(columnFamilyName); PropertyMeta idMeta = null; List<Field> inheritedFields = introspector.getInheritedPrivateFields(entityClass); for (Field field : inheritedFields) { PropertyParsingContext propertyContext = context.newPropertyContext(field); if (filter.hasAnnotation(field, Id.class)) { propertyContext.setPrimaryKey(true); idMeta = parser.parse(propertyContext); } if (filter.hasAnnotation(field, EmbeddedId.class)) { context.setClusteredEntity(true); propertyContext.isEmbeddedId(true); idMeta = parser.parse(propertyContext); } else if (filter.hasAnnotation(field, Column.class)) { parser.parse(propertyContext); } else { log.trace( "Un-mapped field {} of entity {} will not be managed by Achilles", field.getName(), context.getCurrentEntityClass().getCanonicalName()); } } // First validate id meta validator.validateHasIdMeta(entityClass, idMeta); // Deferred counter property meta completion completeCounterPropertyMeta(context, idMeta); // Finish validation of property metas // validator.validatePropertyMetas(context, idMeta); validator.validateClusteredEntities(context); EntityMeta entityMeta = entityMetaBuilder(idMeta) .entityClass(entityClass) .className(entityClass.getCanonicalName()) .columnFamilyName(columnFamilyName) .propertyMetas(context.getPropertyMetas()) .consistencyLevels(context.getCurrentConsistencyLevels()) .build(); saveConsistencyLevel(context, columnFamilyName, consistencyLevels); log.trace( "Entity meta built for entity class {} : {}", context.getCurrentEntityClass().getCanonicalName(), entityMeta); return entityMeta; }