Java Code Examples for org.apache.commons.beanutils.BeanMap#get()
The following examples show how to use
org.apache.commons.beanutils.BeanMap#get() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CiDataInterceptorService.java From we-cmdb with Apache License 2.0 | 6 votes |
private void validateRequiredFieldForCreation(DynamicEntityHolder entityHolder, BeanMap ciBeanMap) { List<AdmCiTypeAttr> attrs = ciTypeAttrRepository.findWithNullableAndIsAuto(entityHolder.getEntityMeta().getCiTypeId(), 0, 0); for (AdmCiTypeAttr attr : attrs) { if (systemFillFields.contains(attr.getPropertyName())) { continue; } if (CiStatus.Decommissioned.getCode().equals(attr.getStatus()) || CiStatus.NotCreated.getCode().equals(attr.getStatus()) ) { continue; } Object val = ciBeanMap.get(attr.getPropertyName()); if (val == null || ((val instanceof String) && "".equals(val))) { Integer ciTypeId = entityHolder.getEntityMeta().getCiTypeId(); throw new InvalidArgumentException(String.format("Field [%s] is required for creation of CiType [%s(%d)].", attr.getPropertyName(), getCiTypeName(ciTypeId), ciTypeId)); } } }
Example 2
Source File: CiTypeServiceImpl.java From we-cmdb with Apache License 2.0 | 6 votes |
@Override public void updateCiType(int ciTypeId, CiTypeDto ciType) { if (!ciTypeRepository.existsById(ciTypeId)) { throw new InvalidArgumentException("Can not find out CiType with given argument.", "ciTypeId", ciTypeId); } Map<String, Object> updateMap = new HashMap<>(); BeanMap ciTypeMap = new BeanMap(ciType); Field[] feilds = CiTypeDto.class.getDeclaredFields(); for (Field field : feilds) { DtoField dtoField = field.getAnnotation(DtoField.class); if (ciTypeMap.get(field.getName()) != null && (dtoField == null || (dtoField != null && dtoField.updatable()))) { updateMap.put(field.getName(), ciTypeMap.get(field.getName())); } } // AdmCiType existingAdmCiType = ciTypeRepository.getOne(ciTypeId); staticDtoService.update(CiTypeDto.class, ciTypeId, updateMap); // AdmCiType updatedAdmCiType = ciType.updateTo(existingAdmCiType); // ciTypeRepository.saveAndFlush(updatedAdmCiType); logger.info(String.format("Updated CI type sucessfully. (CI type id:%d)", ciTypeId)); }
Example 3
Source File: ClassUtils.java From we-cmdb with Apache License 2.0 | 6 votes |
public static Map<String, Object> convertBeanToMap(Object ciObj, DynamicEntityMeta entityMeta, boolean includeNullVal, List<String> requestFields) { Map<String, Object> ciMap = new HashMap<>(); BeanMap ciObjMap = new BeanMap(ciObj); Collection<FieldNode> nodes = entityMeta.getAllFieldNodes(false); for (FieldNode node : nodes) { String name = node.getName(); if (isRequestField(requestFields, name)) { Object val = ciObjMap.get(name); if (includeNullVal) { ciMap.put(name, val); } else { if (val != null) { ciMap.put(name, val); } } } } return ciMap; }
Example 4
Source File: ServiceResource.java From sailfish-core with Apache License 2.0 | 6 votes |
private Element marshal(IServiceSettings settings) throws Exception { BeanMap beanMap = new BeanMap(settings); DOMElement rootElement = new DOMElement(new QName("settings")); for (Object key : beanMap.keySet()) { Object value = beanMap.get(key); if (value != null) { DOMElement domElement = new DOMElement(new QName(key.toString())); domElement.setText(value.toString()); rootElement.add(domElement); } } return rootElement; }
Example 5
Source File: ConverterBean.java From sailfish-core with Apache License 2.0 | 5 votes |
public void convertMatrix() { logger.debug("convert started"); try { TestToolsAPI testToolsAPI = TestToolsAPI.getInstance(); IMatrixConverter converter = testToolsAPI.getMatrixConverter(converterUri); ConversionMonitor monitor = new ConversionMonitor(); IMatrixConverterSettings settings = testToolsAPI .prepareConverterSettings(matrixId, environment, converterUri, outputMatrixName); BeanMap beanMap = new BeanMap(settings); for (ConverterNode setting : currentSettings.getNodes()){ if(beanMap.get(setting.getName()) instanceof Map){ ConverterFormMapAdapter adapter = (ConverterFormMapAdapter) setting.getValue(); adapter.toMap(); BeanUtils.setProperty(settings, setting.getName(), adapter); } else { BeanUtils.setProperty(settings, setting.getName(), setting.getValue()); } } Future<Boolean> converterTask = BeanUtil.getSfContext().getTaskExecutor().addTask(new ConversionTask(converter, settings, monitor)); task = new MatrixConverterFeature(monitor, converterTask, settings.getOutputFile()); } catch(RuntimeException | IOException | IllegalAccessException | InvocationTargetException e) { BeanUtil.showMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""); logger.error(e.getMessage(), e); } }
Example 6
Source File: CopyUtil.java From springBoot with MIT License | 4 votes |
/** * 类型转换:实体Vo <->实体 例如:UserVo <-> User * 支持一级复杂对象复制 */ public static <T> T copy(Object src, Class<T> targetType) { T target = null; try { //创建一个空目标对象,并获取一个BeanWrapper代理器,用于属性填充,BeanWrapperImpl在内部使用Spring的BeanUtils工具类对Bean进行反射操作,设置属性。 target = targetType.newInstance(); BeanWrapper targetBean = new BeanWrapperImpl(target); //获取源对象的BeanMap,属性和属性值直接转换为Map的key-value 形式 BeanMap srcBean = new BeanMap(src); for (Object key : srcBean.keySet()) { //源对象属性名称 String srcPropertyName = key + ""; //源对象属性值 Object srcPropertyVal = srcBean.get(key); //源对象属性类型 Class srcPropertyType = srcBean.getType(srcPropertyName); //目标对象属性类型 Class targetPropertyType = targetBean.getPropertyType(srcPropertyName); //源对象属性值非空判断、目标对象属性类型非空判断,如果为空跳出,继续操作下一个属性 if ("class".equals(srcPropertyName) || targetPropertyType == null) { continue; } //类型相等,可直接设置值,比如:String与String 或者 User与User if (srcPropertyType == targetPropertyType) { targetBean.setPropertyValue(srcPropertyName, srcPropertyVal); } //类型不相等,比如:User与UserVo else { /* 下面的步骤与上面的步骤基本一致 */ //如果源复杂对象为null,直接跳过,不需要复制 if(srcPropertyVal == null){ continue; } Object targetPropertyVal = targetPropertyType.newInstance(); BeanWrapper targetPropertyBean = new BeanWrapperImpl(targetPropertyVal); BeanMap srcPropertyBean = new BeanMap(srcPropertyVal); for (Object srcPropertyBeanKey : srcPropertyBean.keySet()) { String srcPropertyBeanPropertyName = srcPropertyBeanKey + ""; Object srcPropertyBeanPropertyVal = srcPropertyBean.get(srcPropertyBeanKey); Class srcPropertyBeanPropertyType = srcPropertyBean.getType(srcPropertyBeanPropertyName); Class targetPropertyBeanPropertyType = targetPropertyBean.getPropertyType(srcPropertyBeanPropertyName); if ("class".equals(srcPropertyBeanPropertyName) || targetPropertyBeanPropertyType == null) { continue; } if (srcPropertyBeanPropertyType == targetPropertyBeanPropertyType) { targetPropertyBean.setPropertyValue(srcPropertyBeanPropertyName, srcPropertyBeanPropertyVal); } else { //复杂对象里面的复杂对象不再进行处理 } } //设置目标对象属性值 targetBean.setPropertyValue(srcPropertyName, targetPropertyBean.getWrappedInstance()); } } } catch (Exception e) { e.printStackTrace(); } return target; }
Example 7
Source File: CopyUtil.java From springBoot with MIT License | 4 votes |
/** * 类型转换:实体Vo <->实体 例如:UserVo <-> User * 支持一级复杂对象复制 */ public static <T> T copy(Object src, Class<T> targetType) { T target = null; try { //创建一个空目标对象,并获取一个BeanWrapper代理器,用于属性填充,BeanWrapperImpl在内部使用Spring的BeanUtils工具类对Bean进行反射操作,设置属性。 target = targetType.newInstance(); BeanWrapper targetBean = new BeanWrapperImpl(target); //获取源对象的BeanMap,属性和属性值直接转换为Map的key-value 形式 BeanMap srcBean = new BeanMap(src); for (Object key : srcBean.keySet()) { //源对象属性名称 String srcPropertyName = key + ""; //源对象属性值 Object srcPropertyVal = srcBean.get(key); //源对象属性类型 Class srcPropertyType = srcBean.getType(srcPropertyName); //目标对象属性类型 Class targetPropertyType = targetBean.getPropertyType(srcPropertyName); //源对象属性值非空判断、目标对象属性类型非空判断,如果为空跳出,继续操作下一个属性 if ("class".equals(srcPropertyName) || targetPropertyType == null) { continue; } //类型相等,可直接设置值,比如:String与String 或者 User与User if (srcPropertyType == targetPropertyType) { targetBean.setPropertyValue(srcPropertyName, srcPropertyVal); } //类型不相等,比如:User与UserVo else { /* 下面的步骤与上面的步骤基本一致 */ //如果源复杂对象为null,直接跳过,不需要复制 if(srcPropertyVal == null){ continue; } Object targetPropertyVal = targetPropertyType.newInstance(); BeanWrapper targetPropertyBean = new BeanWrapperImpl(targetPropertyVal); BeanMap srcPropertyBean = new BeanMap(srcPropertyVal); for (Object srcPropertyBeanKey : srcPropertyBean.keySet()) { String srcPropertyBeanPropertyName = srcPropertyBeanKey + ""; Object srcPropertyBeanPropertyVal = srcPropertyBean.get(srcPropertyBeanKey); Class srcPropertyBeanPropertyType = srcPropertyBean.getType(srcPropertyBeanPropertyName); Class targetPropertyBeanPropertyType = targetPropertyBean.getPropertyType(srcPropertyBeanPropertyName); if ("class".equals(srcPropertyBeanPropertyName) || targetPropertyBeanPropertyType == null) { continue; } if (srcPropertyBeanPropertyType == targetPropertyBeanPropertyType) { targetPropertyBean.setPropertyValue(srcPropertyBeanPropertyName, srcPropertyBeanPropertyVal); } else { //复杂对象里面的复杂对象不再进行处理 } } //设置目标对象属性值 targetBean.setPropertyValue(srcPropertyName, targetPropertyBean.getWrappedInstance()); } } } catch (Exception e) { e.printStackTrace(); } return target; }
Example 8
Source File: CopyUtil.java From springBoot with MIT License | 4 votes |
/** * 类型转换:实体Vo <->实体 例如:UserVo <-> User * 支持一级复杂对象复制 */ public static <T> T copy(Object src, Class<T> targetType) { T target = null; try { //创建一个空目标对象,并获取一个BeanWrapper代理器,用于属性填充,BeanWrapperImpl在内部使用Spring的BeanUtils工具类对Bean进行反射操作,设置属性。 target = targetType.newInstance(); BeanWrapper targetBean = new BeanWrapperImpl(target); //获取源对象的BeanMap,属性和属性值直接转换为Map的key-value 形式 BeanMap srcBean = new BeanMap(src); for (Object key : srcBean.keySet()) { //源对象属性名称 String srcPropertyName = key + ""; //源对象属性值 Object srcPropertyVal = srcBean.get(key); //源对象属性类型 Class srcPropertyType = srcBean.getType(srcPropertyName); //目标对象属性类型 Class targetPropertyType = targetBean.getPropertyType(srcPropertyName); //源对象属性值非空判断、目标对象属性类型非空判断,如果为空跳出,继续操作下一个属性 if ("class".equals(srcPropertyName) || targetPropertyType == null) { continue; } //类型相等,可直接设置值,比如:String与String 或者 User与User if (srcPropertyType == targetPropertyType) { targetBean.setPropertyValue(srcPropertyName, srcPropertyVal); } //类型不相等,比如:User与UserVo else { /* 下面的步骤与上面的步骤基本一致 */ //如果源复杂对象为null,直接跳过,不需要复制 if(srcPropertyVal == null){ continue; } Object targetPropertyVal = targetPropertyType.newInstance(); BeanWrapper targetPropertyBean = new BeanWrapperImpl(targetPropertyVal); BeanMap srcPropertyBean = new BeanMap(srcPropertyVal); for (Object srcPropertyBeanKey : srcPropertyBean.keySet()) { String srcPropertyBeanPropertyName = srcPropertyBeanKey + ""; Object srcPropertyBeanPropertyVal = srcPropertyBean.get(srcPropertyBeanKey); Class srcPropertyBeanPropertyType = srcPropertyBean.getType(srcPropertyBeanPropertyName); Class targetPropertyBeanPropertyType = targetPropertyBean.getPropertyType(srcPropertyBeanPropertyName); if ("class".equals(srcPropertyBeanPropertyName) || targetPropertyBeanPropertyType == null) { continue; } if (srcPropertyBeanPropertyType == targetPropertyBeanPropertyType) { targetPropertyBean.setPropertyValue(srcPropertyBeanPropertyName, srcPropertyBeanPropertyVal); } else { //复杂对象里面的复杂对象不再进行处理 } } //设置目标对象属性值 targetBean.setPropertyValue(srcPropertyName, targetPropertyBean.getWrappedInstance()); } } } catch (Exception e) { e.printStackTrace(); } return target; }
Example 9
Source File: CopyUtil.java From base-admin with MIT License | 4 votes |
/** * 类型转换:实体Vo <->实体 例如:UserVo <-> User * 支持一级复杂对象复制 */ public static <T> T copy(Object src, Class<T> targetType) { T target = null; try { //创建一个空目标对象,并获取一个BeanWrapper代理器,用于属性填充,BeanWrapperImpl在内部使用Spring的BeanUtils工具类对Bean进行反射操作,设置属性。 target = targetType.newInstance(); BeanWrapper targetBean = new BeanWrapperImpl(target); //获取源对象的BeanMap,属性和属性值直接转换为Map的key-value 形式 BeanMap srcBean = new BeanMap(src); for (Object key : srcBean.keySet()) { //源对象属性名称 String srcPropertyName = key + ""; //源对象属性值 Object srcPropertyVal = srcBean.get(key); //源对象属性类型 Class srcPropertyType = srcBean.getType(srcPropertyName); //目标对象属性类型 Class targetPropertyType = targetBean.getPropertyType(srcPropertyName); //源对象属性值非空判断、目标对象属性类型非空判断,如果为空跳出,继续操作下一个属性 if ("class".equals(srcPropertyName) || targetPropertyType == null) { continue; } //类型相等,可直接设置值,比如:String与String 或者 User与User if (srcPropertyType == targetPropertyType) { targetBean.setPropertyValue(srcPropertyName, srcPropertyVal); } //类型不相等,比如:User与UserVo else { /* 下面的步骤与上面的步骤基本一致 */ //如果源复杂对象为null,直接跳过,不需要复制 if(srcPropertyVal == null){ continue; } Object targetPropertyVal = targetPropertyType.newInstance(); BeanWrapper targetPropertyBean = new BeanWrapperImpl(targetPropertyVal); BeanMap srcPropertyBean = new BeanMap(srcPropertyVal); for (Object srcPropertyBeanKey : srcPropertyBean.keySet()) { String srcPropertyBeanPropertyName = srcPropertyBeanKey + ""; Object srcPropertyBeanPropertyVal = srcPropertyBean.get(srcPropertyBeanKey); Class srcPropertyBeanPropertyType = srcPropertyBean.getType(srcPropertyBeanPropertyName); Class targetPropertyBeanPropertyType = targetPropertyBean.getPropertyType(srcPropertyBeanPropertyName); if ("class".equals(srcPropertyBeanPropertyName) || targetPropertyBeanPropertyType == null) { continue; } if (srcPropertyBeanPropertyType == targetPropertyBeanPropertyType) { targetPropertyBean.setPropertyValue(srcPropertyBeanPropertyName, srcPropertyBeanPropertyVal); } else { //复杂对象里面的复杂对象不再进行处理 } } //设置目标对象属性值 targetBean.setPropertyValue(srcPropertyName, targetPropertyBean.getWrappedInstance()); } } } catch (Exception e) { //输出到日志文件中 log.error(ErrorUtil.errorInfoToString(e)); } return target; }