662b24a5faf3ebc19e7191e319bde26e4488b466
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.common.framework.entity;
2
3 import java.lang.reflect.Field;
4 import java.util.Date;
5
6 import javax.persistence.Column;
7 import javax.persistence.Id;
8
9 import com.supwisdom.institute.backend.common.util.ReflectUtils;
10
11 import lombok.Getter;
12 import lombok.Setter;
13 import lombok.ToString;
14
15 /**
16  * 对 entity 的操作 如:复制、合并、转换等
17  * 
18  * @author loie
19  *
20  */
21 public class EntityUtils {
22
23   /**
24    * 合并 domain 中带有{@link Column}注解的字段值, 将 newEntity 中值为null的字段,使用 oldEntity 中的值
25    * 进行覆盖
26    * 
27    * @param oldEntity
28    *          ,覆盖的实体
29    * @param newEntity
30    *          ,待覆盖的实体
31    * @return 合并后的newEntity
32    */
33   public static <T> T merge(T oldEntity, T newEntity) {
34
35     for (Class<?> clazz = oldEntity.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
36       for (Field field : clazz.getDeclaredFields()) {
37         Column[] annotations = field.getAnnotationsByType(Column.class);
38         if (annotations == null || annotations.length == 0) {
39           Id[] idAnnotations = field.getAnnotationsByType(Id.class);
40           if (idAnnotations == null || idAnnotations.length == 0) {
41             continue;
42           }
43         }
44
45         String fieldName = field.getName();
46         Object newFieldValue = ReflectUtils.getFieldValue(newEntity, fieldName);
47
48         if (newFieldValue == null) {
49           Object oldFieldValue = ReflectUtils.getFieldValue(oldEntity, fieldName);
50           ReflectUtils.setFieldValue(newEntity, fieldName, oldFieldValue,field.getType());
51         }
52       }
53     }
54
55     return newEntity;
56   }
57
58   public static <S, T> T copy(S sourceEntity, T targetEntity) {
59     
60     for (Class<?> clazz = targetEntity.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
61       for (Field field : clazz.getDeclaredFields()) {
62         Column[] annotations = field.getAnnotationsByType(Column.class);
63         if (annotations == null || annotations.length == 0) {
64           Id[] idAnnotations = field.getAnnotationsByType(Id.class);
65           if (idAnnotations == null || idAnnotations.length == 0) {
66             continue;
67           }
68         }
69
70         String fieldName = field.getName();
71         Object sFieldValue = ReflectUtils.getFieldValue(sourceEntity, fieldName);
72
73         if (sFieldValue != null) {
74           ReflectUtils.setFieldValue(targetEntity, fieldName, sFieldValue,field.getType());
75         }
76       }
77     }
78
79     return targetEntity;
80   }
81
82   public static <S, T> T fatherToChild (S father, T child){
83     for (Class<?> clazz = child.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
84       for (Field field : clazz.getDeclaredFields()) {
85         String fieldName = field.getName();
86         if(fieldName.equals("serialVersionUID")){continue;}
87         Object sFieldValue = ReflectUtils.getFieldValue(father, fieldName);
88
89         if (sFieldValue != null) {
90           ReflectUtils.setFieldValue(child, fieldName, sFieldValue,field.getType());
91         }
92       }
93     }
94
95     return child;
96   }
97
98     public static void main(String[] args) {
99
100     Test target0 = new Test();
101     target0.setId("id0");
102     target0.setCode("code");
103     target0.setName("name");
104     target0.setDate(new Date());
105     target0.setEnabled(false);
106     target0.setStatus(1);
107
108     System.out.println("target0 == " + target0.toString());
109     System.out.println();
110
111     Test source1 = new Test();
112     // source1.setId("id1");
113     source1.setCode("code1");
114     // source1.setName("name");
115     // source1.setDate(new Date());
116     source1.setEnabled(true);
117     // source1.setStatus(1);
118     System.out.println("source1 == " + source1.toString());
119
120     Test target1 = EntityUtils.merge(source1, target0);
121     System.out.println("target0 == " + target0.toString());
122     System.out.println("target1 == " + target1.toString());
123     System.out.println();
124
125     Test source2 = new Test();
126     // source2.setId("id2");
127     source2.setCode("code2");
128     source2.setName("name2");
129     // source2.setDate(new Date());
130     // source2.setEnabled(true);
131     source2.setStatus(2);
132     System.out.println("source2 == " + source2.toString());
133
134     Test target2 = EntityUtils.merge(source2, target0);
135     System.out.println("target0 == " + target0.toString());
136     System.out.println("target2 == " + target2.toString());
137     System.out.println();
138
139
140     Test test = new Test();
141     test.setId("id0");
142     test.setCode("code");
143     test.setName("name");
144     test.setDate(new Date());
145     test.setEnabled(false);
146     test.setStatus(1);
147
148     Test2 test2 = new Test2();
149     test2 = EntityUtils.copy(test, test2);
150     System.out.println("test    == " + test.toString());
151     System.out.println("test2   == " + test2.toString());
152     System.out.println();
153
154   }
155   
156   @Getter
157   @Setter
158   @ToString
159   public static class Test extends ABaseEntity {
160     
161     /**
162      * 
163      */
164     private static final long serialVersionUID = -8348781653151879484L;
165     
166     @Column
167     private String code = null;
168     @Column
169     private String name = null;
170     @Column
171     private Date date = null;
172     @Column
173     private Boolean enabled = null;
174     @Column
175     private Integer status = null;
176     
177   }
178   
179   public static class Test2 extends ABaseEntity {
180
181     /**
182      * 
183      */
184     private static final long serialVersionUID = -5565959639168005384L;
185     
186     @Column
187     private String name = null;
188     @Column
189     private String memo = null;
190     @Column
191     private Date date = null;
192     @Column
193     private Boolean enabled = null;
194     @Column
195     private Integer status = null;
196
197   }
198
199 }