-----------
package saas.framework.event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Service;
@Service
public class EventAPI {
@Autowired
private ApplicationEventMulticaster eventMultiCaster;
public void publishEvent(ApplicationEvent event) {
eventMultiCaster.multicastEvent(event);
}
}
------------------------------------------------------------------------------------------------------
private void publishEvent(Object source, OperateStateEnum operateState) {
if(source != null) {
eventAPI.publishEvent(new AprvEvent(source, operateState));
}
}
----------------------------------------------------------------------------------------------------
package saas.crm.aprv.service.event;
import org.springframework.context.ApplicationEvent;
import saas.crm.aprv.model.enums.OperateStateEnum;
/**
* Created by lee on 2016/10/18.
*/
public class AprvEvent extends ApplicationEvent {
private final OperateStateEnum operateState;
/**
* Create a new ApplicationEvent.
*
* @param source the component that published the event (never {@code null})
*/
public AprvEvent(Object source, OperateStateEnum operateState) {
super(source);
this.operateState = operateState;
}
public OperateStateEnum getOperateState() {
return operateState;
}
}
----------------------------------------------------------------------------------------------------
package saas.crm.aprv.service.event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import saas.crm.aprv.model.AprvDocument;
import saas.crm.aprv.model.AprvFlowItem;
import saas.crm.aprv.model.enums.OperateStateEnum;
import saas.crm.aprv.service.AprvDocumentReadService;
import saas.crm.aprv.service.util.AprvUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lee on 2016/10/18.
*/
@Component
public class AprvListener implements ApplicationListener<AprvEvent> {
@Autowired
AprvDocumentReadService aprvDocumentReadService;
@Override
public void onApplicationEvent(AprvEvent event) {
try {
nextProcessUnread(event);
} catch (Exception e) {
e.printStackTrace();
}
}
private void nextProcessUnread(AprvEvent event) throws ParseException {
OperateStateEnum operateState = event.getOperateState();
List<Long> unReadUserIds = new ArrayList<Long>();
long dbId = -1;
switch (operateState) {
case PASS:
case VETO:
case TURN:
case BACK: {
AprvFlowItem aprvFlowItem = (AprvFlowItem) event.getSource();
dbId = aprvFlowItem.getDbId();
String approverIds = aprvFlowItem.getApproverIds();
AprvUtils.addApproverIds(unReadUserIds, approverIds);
break;
}
case HANDLING:
case HANDLED: {
AprvDocument aprvDocument = (AprvDocument) event.getSource();
dbId = aprvDocument.getDbId();
unReadUserIds.add(aprvDocument.getUserId());
break;
}
default:
return ;
}
if(! unReadUserIds.isEmpty()) {
aprvDocumentReadService.updateReadCount(dbId, 0, unReadUserIds.toArray(new Long[unReadUserIds.size()]));
}
}
}