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