|
18 | 18 |
|
19 | 19 | package org.springframework.data.mybatis.repository.support;
|
20 | 20 |
|
| 21 | +import java.io.Serializable; |
| 22 | +import java.lang.annotation.Annotation; |
| 23 | +import java.util.Date; |
21 | 24 | import org.springframework.data.annotation.CreatedBy;
|
22 | 25 | import org.springframework.data.annotation.CreatedDate;
|
23 | 26 | import org.springframework.data.annotation.LastModifiedBy;
|
24 | 27 | import org.springframework.data.annotation.LastModifiedDate;
|
25 | 28 | import org.springframework.data.domain.AuditorAware;
|
| 29 | +import org.springframework.data.mapping.IdentifierAccessor; |
26 | 30 | import org.springframework.data.mapping.PersistentEntity;
|
27 | 31 | import org.springframework.data.mapping.PersistentProperty;
|
28 | 32 | import org.springframework.data.mapping.PersistentPropertyAccessor;
|
| 33 | +import org.springframework.data.mybatis.annotations.Id; |
| 34 | +import org.springframework.data.mybatis.annotations.Id.GenerationType; |
29 | 35 | import org.springframework.data.mybatis.domains.AuditDateAware;
|
30 | 36 | import org.springframework.data.repository.core.support.AbstractEntityInformation;
|
31 | 37 |
|
32 |
| -import java.io.Serializable; |
33 |
| -import java.lang.annotation.Annotation; |
34 |
| -import java.util.Date; |
35 |
| - |
36 | 38 | /**
|
37 | 39 | * mybatis meta.
|
38 | 40 | *
|
39 | 41 | * @author Jarvis Song
|
40 | 42 | */
|
41 |
| -public class MybatisMetamodelEntityInformation<T, ID extends Serializable> extends MybatisEntityInformationSupport<T, ID> { |
| 43 | +public class MybatisMetamodelEntityInformation<T, ID extends Serializable> extends |
| 44 | + MybatisEntityInformationSupport<T, ID> { |
| 45 | + |
| 46 | + private PersistentProperty<?> createdDateProperty; |
| 47 | + private PersistentProperty<?> lastModifiedDateProperty; |
| 48 | + private PersistentProperty<?> createdByProperty; |
| 49 | + private PersistentProperty<?> lastModifiedByProperty; |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * Creates a new {@link AbstractEntityInformation} from the given domain class. |
| 54 | + * |
| 55 | + * @param domainClass must not be {@literal null}. |
| 56 | + */ |
| 57 | + protected MybatisMetamodelEntityInformation(PersistentEntity<T, ?> persistentEntity, |
| 58 | + AuditorAware<?> auditorAware, AuditDateAware<?> auditDateAware, Class<T> domainClass) { |
| 59 | + super(persistentEntity, auditorAware, auditDateAware, domainClass); |
| 60 | + |
| 61 | + createdDateProperty = persistentEntity.getPersistentProperty(CreatedDate.class); |
| 62 | + lastModifiedDateProperty = persistentEntity.getPersistentProperty(LastModifiedDate.class); |
| 63 | + createdByProperty = persistentEntity.getPersistentProperty(CreatedBy.class); |
| 64 | + lastModifiedByProperty = persistentEntity.getPersistentProperty(LastModifiedBy.class); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public ID getId(T entity) { |
| 69 | + if (null == persistentEntity) { |
| 70 | + return null; |
| 71 | + } |
42 | 72 |
|
43 |
| - private PersistentProperty<?> createdDateProperty; |
44 |
| - private PersistentProperty<?> lastModifiedDateProperty; |
45 |
| - private PersistentProperty<?> createdByProperty; |
46 |
| - private PersistentProperty<?> lastModifiedByProperty; |
| 73 | + return (ID) persistentEntity.getIdentifierAccessor(entity).getIdentifier(); |
47 | 74 |
|
| 75 | + } |
48 | 76 |
|
49 |
| - /** |
50 |
| - * Creates a new {@link AbstractEntityInformation} from the given domain class. |
51 |
| - * |
52 |
| - * @param persistentEntity |
53 |
| - * @param domainClass must not be {@literal null}. |
54 |
| - */ |
55 |
| - protected MybatisMetamodelEntityInformation(PersistentEntity<T, ?> persistentEntity, AuditorAware<?> auditorAware, AuditDateAware<?> auditDateAware, Class<T> domainClass) { |
56 |
| - super(persistentEntity, auditorAware, auditDateAware, domainClass); |
| 77 | + @Override |
| 78 | + public Class<ID> getIdType() { |
| 79 | + if (null == persistentEntity) { |
| 80 | + return null; |
| 81 | + } |
57 | 82 |
|
58 |
| - createdDateProperty = persistentEntity.getPersistentProperty(CreatedDate.class); |
59 |
| - lastModifiedDateProperty = persistentEntity.getPersistentProperty(LastModifiedDate.class); |
60 |
| - createdByProperty = persistentEntity.getPersistentProperty(CreatedBy.class); |
61 |
| - lastModifiedByProperty = persistentEntity.getPersistentProperty(LastModifiedBy.class); |
| 83 | + PersistentProperty idProperty = persistentEntity.getIdProperty(); |
| 84 | + if (null == idProperty) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + return (Class<ID>) idProperty.getType(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean hasVersion() { |
| 92 | + return persistentEntity.hasVersionProperty(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public GenerationType getIdGenerationType() { |
| 97 | + PersistentProperty<?> idProperty = persistentEntity.getIdProperty(); |
| 98 | + if (null == idProperty) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + Id id = idProperty.findAnnotation(Id.class); |
| 102 | + if (null == id) { |
| 103 | + return null; |
62 | 104 | }
|
63 | 105 |
|
64 |
| - @Override |
65 |
| - public ID getId(T entity) { |
66 |
| - if (null == persistentEntity) { |
67 |
| - return null; |
68 |
| - } |
| 106 | + return id.strategy(); |
69 | 107 |
|
70 |
| - return (ID) persistentEntity.getIdentifierAccessor(entity).getIdentifier(); |
| 108 | + } |
71 | 109 |
|
| 110 | + @Override |
| 111 | + public void setIdValue(T entity, ID id) { |
| 112 | + IdentifierAccessor identifierAccessor = persistentEntity.getIdentifierAccessor(entity); |
| 113 | + if (null == identifierAccessor) { |
| 114 | + return; |
72 | 115 | }
|
73 |
| - |
74 |
| - @Override |
75 |
| - public Class<ID> getIdType() { |
76 |
| - if (null == persistentEntity) { |
77 |
| - return null; |
78 |
| - } |
79 |
| - |
80 |
| - PersistentProperty idProperty = persistentEntity.getIdProperty(); |
81 |
| - if (null == idProperty) { |
82 |
| - return null; |
83 |
| - } |
84 |
| - return (Class<ID>) idProperty.getType(); |
| 116 | + Object identifier = identifierAccessor.getIdentifier(); |
| 117 | + if (null != identifier) { |
| 118 | + return; |
85 | 119 | }
|
86 |
| - |
87 |
| - @Override |
88 |
| - public boolean hasVersion() { |
89 |
| - return persistentEntity.hasVersionProperty(); |
| 120 | + PersistentProperty<?> idProperty = persistentEntity.getIdProperty(); |
| 121 | + if (null == idProperty) { |
| 122 | + return; |
90 | 123 | }
|
| 124 | + persistentEntity.getPropertyAccessor(entity).setProperty(idProperty, id); |
| 125 | + } |
91 | 126 |
|
| 127 | + private void setAuditDate(PersistentProperty<?> property, T entity, |
| 128 | + Class<? extends Annotation> annotationType) { |
92 | 129 |
|
93 |
| - private void setAuditDate(PersistentProperty<?> property, T entity, Class<? extends Annotation> annotationType) { |
94 |
| - |
95 |
| - Object val = persistentEntity.getPropertyAccessor(entity).getProperty(property); |
96 |
| - if (null != val) { |
97 |
| - return; |
98 |
| - } |
| 130 | + Object val = persistentEntity.getPropertyAccessor(entity).getProperty(property); |
| 131 | + if (null != val) { |
| 132 | + return; |
| 133 | + } |
99 | 134 |
|
| 135 | + if (null != auditDateAware) { |
| 136 | + persistentEntity.getPropertyAccessor(entity) |
| 137 | + .setProperty(property, auditDateAware.getCurrentDate()); |
| 138 | + return; |
| 139 | + } |
100 | 140 |
|
101 |
| - if (null != auditDateAware) { |
102 |
| - persistentEntity.getPropertyAccessor(entity).setProperty(property, auditDateAware.getCurrentDate()); |
103 |
| - return; |
104 |
| - } |
| 141 | + Class<?> type = property.getRawType(); |
| 142 | + if (Date.class.isAssignableFrom(type)) { |
| 143 | + persistentEntity.getPropertyAccessor(entity).setProperty(property, new Date()); |
| 144 | + } else if (Long.class == type || long.class == type) { |
| 145 | + persistentEntity.getPropertyAccessor(entity) |
| 146 | + .setProperty(property, System.currentTimeMillis()); |
| 147 | + } else { |
| 148 | + throw new IllegalArgumentException( |
| 149 | + "now we can not support " + type.getName() + " for " + annotationType.getName() |
| 150 | + + ", you can implement org.springframework.data.mybatis.domains.AuditDateAware interface as a spring bean."); |
| 151 | + } |
| 152 | + } |
105 | 153 |
|
106 |
| - Class<?> type = property.getRawType(); |
107 |
| - if (Date.class.isAssignableFrom(type)) { |
108 |
| - persistentEntity.getPropertyAccessor(entity).setProperty(property, new Date()); |
109 |
| - } else if (Long.class == type || long.class == type) { |
110 |
| - persistentEntity.getPropertyAccessor(entity).setProperty(property, System.currentTimeMillis()); |
111 |
| - } else { |
112 |
| - throw new IllegalArgumentException("now we can not support " + type.getName() + " for " + annotationType.getName() |
113 |
| - + ", you can implement org.springframework.data.mybatis.domains.AuditDateAware interface as a spring bean."); |
114 |
| - } |
| 154 | + @Override |
| 155 | + public void setCreatedDate(T entity) { |
| 156 | + if (null == createdDateProperty) { |
| 157 | + return; |
115 | 158 | }
|
| 159 | + setAuditDate(createdDateProperty, entity, CreatedDate.class); |
| 160 | + } |
116 | 161 |
|
117 |
| - @Override |
118 |
| - public void setCreatedDate(T entity) { |
119 |
| - if (null == createdDateProperty) { |
120 |
| - return; |
121 |
| - } |
122 |
| - setAuditDate(createdDateProperty, entity, CreatedDate.class); |
| 162 | + @Override |
| 163 | + public void setLastModifiedDate(T entity) { |
| 164 | + if (null == lastModifiedDateProperty) { |
| 165 | + return; |
123 | 166 | }
|
| 167 | + setAuditDate(lastModifiedDateProperty, entity, LastModifiedDate.class); |
124 | 168 |
|
125 |
| - @Override |
126 |
| - public void setLastModifiedDate(T entity) { |
127 |
| - if (null == lastModifiedDateProperty) { |
128 |
| - return; |
129 |
| - } |
130 |
| - setAuditDate(lastModifiedDateProperty, entity, LastModifiedDate.class); |
| 169 | + } |
131 | 170 |
|
| 171 | + private void setCurrentAuditor(PersistentProperty<?> property, T entity) { |
| 172 | + Object val = persistentEntity.getPropertyAccessor(entity).getProperty(property); |
| 173 | + if (null != val) { |
| 174 | + return; |
132 | 175 | }
|
133 |
| - |
134 |
| - private void setCurrentAuditor(PersistentProperty<?> property, T entity) { |
135 |
| - Object val = persistentEntity.getPropertyAccessor(entity).getProperty(property); |
136 |
| - if (null != val) { |
137 |
| - return; |
138 |
| - } |
139 |
| - persistentEntity.getPropertyAccessor(entity).setProperty(property, auditorAware.getCurrentAuditor()); |
| 176 | + persistentEntity.getPropertyAccessor(entity) |
| 177 | + .setProperty(property, auditorAware.getCurrentAuditor()); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void setCreatedBy(T entity) { |
| 182 | + if (null == createdByProperty || null == auditorAware) { |
| 183 | + return; |
140 | 184 | }
|
| 185 | + setCurrentAuditor(createdByProperty, entity); |
| 186 | + } |
141 | 187 |
|
142 |
| - @Override |
143 |
| - public void setCreatedBy(T entity) { |
144 |
| - if (null == createdByProperty || null == auditorAware) { |
145 |
| - return; |
146 |
| - } |
147 |
| - setCurrentAuditor(createdByProperty, entity); |
| 188 | + @Override |
| 189 | + public void setLastModifiedBy(T entity) { |
| 190 | + if (null == lastModifiedByProperty || null == auditorAware) { |
| 191 | + return; |
148 | 192 | }
|
149 |
| - |
150 |
| - @Override |
151 |
| - public void setLastModifiedBy(T entity) { |
152 |
| - if (null == lastModifiedByProperty || null == auditorAware) { |
153 |
| - return; |
154 |
| - } |
155 |
| - setCurrentAuditor(lastModifiedByProperty, entity); |
| 193 | + setCurrentAuditor(lastModifiedByProperty, entity); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public int increaseVersion(T entity) { |
| 198 | + PersistentProperty<?> versionProperty = persistentEntity.getVersionProperty(); |
| 199 | + if (null == versionProperty) { |
| 200 | + return 0; |
156 | 201 | }
|
157 | 202 |
|
158 |
| - @Override |
159 |
| - public int increaseVersion(T entity) { |
160 |
| - PersistentProperty<?> versionProperty = persistentEntity.getVersionProperty(); |
161 |
| - if (null == versionProperty) { |
162 |
| - return 0; |
163 |
| - } |
| 203 | + PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(entity); |
| 204 | + int newVer = (Integer) accessor.getProperty(versionProperty) + 1; |
| 205 | + accessor.setProperty(versionProperty, newVer); |
164 | 206 |
|
165 |
| - PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(entity); |
166 |
| - int newVer = (Integer) accessor.getProperty(versionProperty) + 1; |
167 |
| - accessor.setProperty(versionProperty, newVer); |
| 207 | + return newVer; |
| 208 | + } |
168 | 209 |
|
169 |
| - return newVer; |
| 210 | + @Override |
| 211 | + public void setVersion(T entity, int version) { |
| 212 | + PersistentProperty<?> versionProperty = persistentEntity.getVersionProperty(); |
| 213 | + if (null == versionProperty) { |
| 214 | + return; |
170 | 215 | }
|
| 216 | + persistentEntity.getPropertyAccessor(entity).setProperty(versionProperty, version); |
171 | 217 |
|
172 |
| - @Override |
173 |
| - public void setVersion(T entity, int version) { |
174 |
| - PersistentProperty<?> versionProperty = persistentEntity.getVersionProperty(); |
175 |
| - if (null == versionProperty) { |
176 |
| - return; |
177 |
| - } |
178 |
| - persistentEntity.getPropertyAccessor(entity).setProperty(versionProperty, version); |
179 |
| - |
180 |
| - } |
| 218 | + } |
181 | 219 | }
|
0 commit comments