java 代码
- public class CryptoInterceptor extends EmptyInterceptor {
- public boolean onLoad(Object entity, Serializable id, Object[] state,
- String[] propertyNames, Type[] types) {
- if (entity instanceof Worker) {
- for (int i = 0; i < propertyNames.length; i++) {
- if ("wkrPhone".equals(propertyNames[i])) {
- String phone = (String) state[i];
- if (phone != null && !"".equals(phone)) {
- state[i] = CryptoUtils.decrypt(phone);
- }
- }
- }
- }
- return true;
- }
- public boolean onFlushDirty(Object entity, Serializable id, Object[] state,
- String[] propertyNames, Type[] types) {
- if (entity instanceof Worker) {
- for (int i = 0; i < propertyNames.length; i++) {
- if ("wkrPhone".equals(propertyNames[i])) {
- String phone = (String) state[i];
- if (phone != null && !"".equals(phone)) {
- state[i] = CryptoUtils.encrypt(phone);
- }
- }
- }
- }
- return true;
- }
- public boolean onSave(Object entity, Serializable id, Object[] state,
- String[] propertyNames, Type[] types) {
- if (entity instanceof Worker) {
- for (int i = 0; i < propertyNames.length; i++) {
- if ("wkrPhone".equals(propertyNames[i])) {
- String phone = (String) state[i];
- if (phone != null && !"".equals(phone)) {
- state[i] = CryptoUtils.encrypt(phone);
- }
- }
- }
- }
- return true;
- }
- public int[] findDirty(Object entity, Serializable id,
- Object[] currentState, Object[] previousState,
- String[] propertyNames, Type[] types) {
- boolean forceUpdate = false;
- int[] results = null;
- int count = 0;
- if (entity instanceof Worker) {
- for (int i = 0; i < types.length; i++) {
- if (propertyNames[i].equalsIgnoreCase("wkrPhone")) {
- String currentPhone = (String) currentState[i];
- String previusPhone = (String) previousState[i];
- if (currentPhone != null && !"".equals(currentPhone)) {
- if (!currentPhone.equals(previusPhone)) {
- forceUpdate = true;
- currentState[i] = CryptoUtils.encrypt(currentPhone);
- }
- }
- }
- if (results == null) {
- results = new int[types.length];
- }
- results[count++] = i;
- }
- }
- if (count == 0) {
- return null;
- } else {
- int[] trimmed = new int[count];
- System.arraycopy(results, 0, trimmed, 0, count);
- if (forceUpdate) {
- return trimmed;
- } else {
- return null;
- }
- }
- }
- }