|
| 1 | +/* |
| 2 | + * Copyright 2016 http://www.hswebframework.org |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.hswebframework.web.starter; |
| 20 | + |
| 21 | +import org.hswebframework.web.commons.entity.Entity; |
| 22 | +import org.hswebframework.web.commons.entity.factory.MapperEntityFactory; |
| 23 | +import org.hswebframwork.utils.MapUtils; |
| 24 | +import org.hswebframwork.utils.StringUtils; |
| 25 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 26 | +import org.springframework.util.ClassUtils; |
| 27 | + |
| 28 | +import java.lang.instrument.IllegalClassFormatException; |
| 29 | +import java.util.*; |
| 30 | +import java.util.stream.Collectors; |
| 31 | + |
| 32 | +/** |
| 33 | + * TODO 完成注释 |
| 34 | + * <p> |
| 35 | + * <pre> |
| 36 | + * hsweb: |
| 37 | + * entity: |
| 38 | + * mapping: |
| 39 | + * org.hswebframework.web.entity.user.UserEntity:com.company.entity.user.CustomUserEntity |
| 40 | + * </pre> |
| 41 | + * |
| 42 | + * @author zhouhao |
| 43 | + */ |
| 44 | +@ConfigurationProperties(prefix = "hsweb.entity") |
| 45 | +public class EntityProperties { |
| 46 | + List<Mapping> mappings; |
| 47 | + |
| 48 | + public void setMappings(List<Mapping> mappings) { |
| 49 | + this.mappings = mappings; |
| 50 | + } |
| 51 | + |
| 52 | + public List<Mapping> getMappings() { |
| 53 | + return mappings; |
| 54 | + } |
| 55 | + |
| 56 | + public Map<Class<Entity>, MapperEntityFactory.Mapper> createMappers() { |
| 57 | + if (mappings == null || mappings.isEmpty()) return Collections.emptyMap(); |
| 58 | + return mappings.stream() |
| 59 | + .map(Mapping::create) |
| 60 | + .reduce(MapUtils::merge) |
| 61 | + .get(); |
| 62 | + } |
| 63 | + |
| 64 | + public static class Mapping { |
| 65 | + String sourceBasePackage = ""; |
| 66 | + String targetBasePackage = ""; |
| 67 | + Map<String, String> mapping; |
| 68 | + |
| 69 | + Map<Class<Entity>, MapperEntityFactory.Mapper> create() { |
| 70 | + if (mapping == null || mapping.isEmpty()) return Collections.emptyMap(); |
| 71 | + return mapping.entrySet().stream() |
| 72 | + .collect(Collectors.toMap( |
| 73 | + entry -> getSourceClass(entry.getKey()), |
| 74 | + entry -> MapperEntityFactory.defaultMapper(getTargetClass(entry.getValue())))); |
| 75 | + } |
| 76 | + |
| 77 | + protected Class<Entity> getClass(String basePackage, String name) { |
| 78 | + if (!StringUtils.isNullOrEmpty(basePackage)) |
| 79 | + name = basePackage.concat(".").concat(name); |
| 80 | + return classForName(name); |
| 81 | + } |
| 82 | + |
| 83 | + protected Class<Entity> getSourceClass(String name) { |
| 84 | + return getClass(sourceBasePackage, name); |
| 85 | + } |
| 86 | + |
| 87 | + protected Class<Entity> getTargetClass(String name) { |
| 88 | + Class<Entity> entityClass = getClass(targetBasePackage, name); |
| 89 | + if (entityClass.isInterface()) { |
| 90 | + throw new RuntimeException("class " + name + " is interface!"); |
| 91 | + } |
| 92 | + return entityClass; |
| 93 | + } |
| 94 | + |
| 95 | + @SuppressWarnings("unchecked") |
| 96 | + public Class<Entity> classForName(String name) { |
| 97 | + try { |
| 98 | + Class target = ClassUtils.forName(name, this.getClass().getClassLoader()); |
| 99 | + return target; |
| 100 | + } catch (ClassNotFoundException e) { |
| 101 | + throw new RuntimeException(e); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public String getSourceBasePackage() { |
| 106 | + return sourceBasePackage; |
| 107 | + } |
| 108 | + |
| 109 | + public void setSourceBasePackage(String sourceBasePackage) { |
| 110 | + this.sourceBasePackage = sourceBasePackage; |
| 111 | + } |
| 112 | + |
| 113 | + public String getTargetBasePackage() { |
| 114 | + return targetBasePackage; |
| 115 | + } |
| 116 | + |
| 117 | + public void setTargetBasePackage(String targetBasePackage) { |
| 118 | + this.targetBasePackage = targetBasePackage; |
| 119 | + } |
| 120 | + |
| 121 | + public Map<String, String> getMapping() { |
| 122 | + return mapping; |
| 123 | + } |
| 124 | + |
| 125 | + public void setMapping(Map<String, String> mapping) { |
| 126 | + this.mapping = mapping; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments