interceptor.EntityInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for interceptor.EntityInterceptor.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package interceptor;

import entity.Author;
import entity.DelegateMessage;
import entity.Order;
import entity.Payment;
import entity.UploadedFile;
import java.io.Serializable;
import java.util.Date;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
import org.springframework.beans.factory.annotation.Autowired;
import service.OrderService;
import support.AuthManager;

/**
 *
 * @author Rice Pavel
 */
public class EntityInterceptor extends EmptyInterceptor {

    @Autowired
    OrderService orderService;

    @Autowired
    private AuthManager authManager;

    @Autowired
    private AuthorInterceptor authorInterceptor;

    @Autowired
    private OrderInterceptor orderInterceptor;

    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
        if (entity instanceof UploadedFile) {
            return onSaveUploadedFile(state, propertyNames);
        } else if (entity instanceof Payment) {
            return onSavePayment(entity);
        } else if (entity instanceof Author) {
            return authorInterceptor.onSave(state, propertyNames);
        } else if (entity instanceof Order) {
            return orderInterceptor.onSave(state, propertyNames);
        } else if (entity instanceof DelegateMessage) {
            return onSaveDelegateMessage(state, propertyNames);
        }
        return false;
    }

    private boolean onSaveDelegateMessage(Object[] state, String[] propertyNames) {
        InterceptorUtils.setValue(state, propertyNames, "messageDate", new Date());
        return true;
    }

    /**
     * ??  ?  
     * @param state
     * @param propertyNames
     * @return 
     */
    private boolean onSaveUploadedFile(Object[] state, String[] propertyNames) {
        InterceptorUtils.setValue(state, propertyNames, "uploadDate", new Date());
        return true;
    }

    /**
     * ??  ?  
     * @param entity
     * @return 
     */
    private boolean onSavePayment(Object entity) {
        Payment payment = (Payment) entity;
        Order order = payment.getOrder();
        orderService.setReadyDateByOrder(order);
        //orderService.setReadyDateByPaymentId(payment.getId());
        return false;
    }

    /**
     * ??  ?  orderLog
     * @param state
     * @param propertyNames
     * @return 
     */
    private boolean onSaveOrderLog(Object[] state, String[] propertyNames) {
        InterceptorUtils.setValue(state, propertyNames, "createDate", new Date());
        InterceptorUtils.setValue(state, propertyNames, "insertUser", authManager.getCurrentUser());
        return true;
    }

    @Override
    public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
            String[] propertyNames, Type[] types) {
        if (entity instanceof Order) {
            Order order = (Order) entity;
            orderService.setReadyDateByOrder(order);
            return orderInterceptor.onUpdate(currentState, previousState, propertyNames);
        } else if (entity instanceof Author) {
            return authorInterceptor.onUpdate(currentState, previousState, propertyNames);
        }
        return false;
    }

}