package com.rquest.riskmaster.component;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import com.rquest.riskmaster.entity.BusinessDays;
import com.rquest.riskmaster.entity.User;
import com.rquest.riskmaster.entity.UserDefiendCalendar;
import com.rquest.riskmaster.enums.SequenceEnum;
import com.rquest.riskmaster.process.MarketRiskCalendarProcess;
import com.rquest.riskmaster.util.Util;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.server.FileDownloader;
import com.vaadin.server.Page;
import com.vaadin.server.Page.BrowserWindowResizeEvent;
import com.vaadin.server.Page.BrowserWindowResizeListener;
import com.vaadin.server.ThemeResource;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Upload;
import com.vaadin.ui.Upload.FailedEvent;
import com.vaadin.ui.Upload.FailedListener;
import com.vaadin.ui.Upload.Receiver;
import com.vaadin.ui.Upload.SucceededEvent;
import com.vaadin.ui.Upload.SucceededListener;
import com.vaadin.ui.VerticalLayout;
@Service
@Scope("prototype")
public class MarketRiskCalendarComponent extends CommonComponent implements Receiver, SucceededListener, FailedListener {
private static final long serialVersionUID = 9087370805007811092L;
public static final Logger logger = LoggerFactory.getLogger(MarketRiskCalendarComponent.class);
/**
* 日历;
*/
// vaadin component
private Label rptNameLb;
private DateField date;
private VerticalLayout reportLayout;
private Panel mainPanel;
private VerticalLayout thumbnailVertical;
private List
private Label space;
private String preChangeDate;
private Label yearAndMonth;
private ClickListener clickListener;
private File fileUploaded=null;//上传文件
private Upload btnUpLoad;
private TextField importFileName;
private User currentUser;
private List
private Button download;
@Autowired
private MarketRiskCalendarProcess marketRiskCalendarProcess;
@Override
public void initLayout() {
currentUser = ((User)VaadinSession.getCurrent().getAttribute("user"));
initLayoutHeader();
addDateTitle();
addWeeklyCaption();
initLayoutCalendar();
}
public void reSize() {
mainPanel.setHeight((Page.getCurrent().getBrowserWindowHeight() - 150) + "px");
mainPanel.setWidth((Page.getCurrent().getBrowserWindowWidth() ) + "px");
}
/**
* Function Name : initLayoutTitleAndExport Description : 初始化标题、导出PDF、excel;
*
* @author : Administrator
*/
public void initLayoutHeader() {
mainPanel = new Panel();
setCompositionRoot(mainPanel);
mainPanel.setWidth("100%");
// mainPanel.setStyleName("daily-root-layout");
reportLayout = new VerticalLayout();
reportLayout.setHeight("790px");
reportLayout.setWidth("1850px");
mainPanel.setContent(reportLayout);
rptNameLb = new Label("交易日历");
rptNameLb.addStyleName("rptname");
rptNameLb.setWidthUndefined();
HorizontalLayout top = new HorizontalLayout();
top.setHeight("60px");
top.setWidth("20%");
top.addComponent(rptNameLb);
top.setComponentAlignment(rptNameLb, Alignment.MIDDLE_LEFT);
HorizontalLayout topRight = new HorizontalLayout();
topRight.addStyleName("daily-second-line");
topRight.setSizeFull();
topRight.setHeight("60px");
topRight.setWidth("900px");
Label blankLabel0 = new Label();
blankLabel0.setWidth("100%");
topRight.addComponent(blankLabel0);
Label dateLb = new Label("日期: ");
dateLb.setStyleName("marketrisk-datelabel");
dateLb.setWidth("40px");
topRight.addComponent(dateLb);
date = new DateField();
date.addStyleName("marketrisk-datefield");
topRight.addComponent(date);
date.setValue((Date) VaadinSession.getCurrent().getAttribute("vardate"));
date.setDateFormat("yyyy-MM-dd");
Label lblAssetType=new Label("上传日历文件:");
lblAssetType.addStyleName("marketrisk-lblassettype");
topRight.addComponent(lblAssetType);
lblAssetType.setWidth("100px");
importFileName=new TextField();
importFileName.addStyleName("marketrisk-importfilename");
importFileName.setWidth("300px");
importFileName.setEnabled(false);
importFileName.setValue("请点击上传按钮并选择相应的CSV文件上传");
topRight.addComponent(importFileName);
btnUpLoad = new Upload(null,this);
btnUpLoad.setButtonCaption("上传");
btnUpLoad.setData("calendar");
btnUpLoad.setImmediate(true);
btnUpLoad.addSucceededListener(this);
btnUpLoad.addFailedListener(this);
btnUpLoad.setWidth("80px");
btnUpLoad.addStyleName("marketrisk-uploadbutton");
topRight.addComponent(btnUpLoad);
download = new Button("下载模版");
download.addStyleName("marketrisk-download");
topRight.addComponent(download);
Label blankLabel1 = new Label();
blankLabel1.setWidth("100%");
topRight.addComponent(blankLabel1);
// Label blankLabel2 = new Label();
// blankLabel2.setWidth("100%");
// topRight.addComponent(blankLabel2);
// topRight.setComponentAlignment(dateLb, Alignment.MIDDLE_LEFT);
// topRight.setComponentAlignment(date, Alignment.MIDDLE_LEFT);
topRight.setComponentAlignment(btnUpLoad, Alignment.MIDDLE_LEFT);
topRight.setExpandRatio(blankLabel1, 1.0f);
// topRight.setExpandRatio(blankLabel2, 1.0f);
top.addComponent(topRight);
top.setComponentAlignment(topRight, Alignment.MIDDLE_LEFT);
reportLayout.addComponent(top);
}
private void addDateTitle() {
HorizontalLayout dateCaption = new HorizontalLayout();
dateCaption.setHeight("30px");
yearAndMonth = new Label("");
dateCaption.addComponent(yearAndMonth);
yearAndMonth.addStyleName("marketrisk-datetitlelb");
reportLayout.addComponent(dateCaption);
}
private void addWeeklyCaption() {
GridLayout grid = new GridLayout(7,1);
grid.addStyleName("marketrisk-grid");
grid.addComponent(newLabel(SequenceEnum.MONDAY, "150", "white"),0,0);
grid.addComponent(newLabel(SequenceEnum.TUESDAY, "150", "white"),1,0);
grid.addComponent(newLabel(SequenceEnum.WEDNESDAY, "150", "white"),2,0);
grid.addComponent(newLabel(SequenceEnum.THURSDAY, "150", "white"),3,0);
grid.addComponent(newLabel(SequenceEnum.FRIDAY, "150", "white"),4,0);
grid.addComponent(newLabel(SequenceEnum.SATURDAY, "150", "white"),5,0);
grid.addComponent(newLabel(SequenceEnum.SUNDAY, "150", "white"),6,0);
reportLayout.addComponent(grid);
}
public void initLayoutCalendar() {
String format = Util.DATE_FORMATE_yyyy_MM_dd.format(date.getValue());
if (preChangeDate!=null&&format.equals(preChangeDate)) {
return;
}
preChangeDate = format;
String startDate = Util.getFirstDayOfMonth(format);
String endDate = Util.getLastDayOfMonth(format);
yearAndMonth.setValue(startDate.split("-")[0]+"年"+startDate.split("-")[1]+"月");
businessDays = marketRiskCalendarProcess.getMonthDays("Bond_CN", startDate, endDate);
listDate = marketRiskCalendarProcess.getUserDefiendCalendar(currentUser.getUserId(),startDate,endDate);
businessDays= getContinousDays(businessDays);
if (thumbnailVertical != null) {
reportLayout.removeComponent(thumbnailVertical);
reportLayout.removeComponent(space);
}
thumbnailVertical = new VerticalLayout();
space =new Label();
thumbnailVertical.addStyleName("marketrisk-thumbnailVertical");
reportLayout.addComponent(thumbnailVertical);
reportLayout.addComponent(space);
reportLayout.setExpandRatio(space, 1);
reportLayout.setComponentAlignment(thumbnailVertical, Alignment.MIDDLE_CENTER);
if (setNobusinessDaysReportLayout()) {
return;
}
int index = 0;
for (int i = 0; i < businessDays.size() / 7 + 2; i++) {
HorizontalLayout thumbnailHorizontalLayout = new HorizontalLayout();
thumbnailHorizontalLayout.addStyleName("marketrisk-thumbnailHorizontalLayout");
for (; index < businessDays.size();) {
addBlankSpaceOfDays(thumbnailHorizontalLayout,index);
generateThumbnailLayout(businessDays, index, thumbnailHorizontalLayout);
index++;
// if (index % 5 == 0) {
// break;
// }
if (thumbnailHorizontalLayout.getComponentCount()%7 ==0) {
break;
}
}
thumbnailVertical.addComponent(thumbnailHorizontalLayout);
// thumbnailVertical.setComponentAlignment(thumbnailHorizontalLayout, Alignment.MIDDLE_CENTER);
}
}
private void addBlankSpaceOfDays(HorizontalLayout thumbnailHorizontalLayout,int index) {
if (index== 0&&Util.getDayofweek(Util.DATE_FORMATE_yyyy_MM_dd.format(businessDays.get(index).getDtBusinessDay())).equals(SequenceEnum.TUESDAY)) {
generateBlankLayout(thumbnailHorizontalLayout);
}
if (index== 0&&Util.getDayofweek(Util.DATE_FORMATE_yyyy_MM_dd.format(businessDays.get(index).getDtBusinessDay())).equals(SequenceEnum.WEDNESDAY)) {
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
}
if (index== 0&&Util.getDayofweek(Util.DATE_FORMATE_yyyy_MM_dd.format(businessDays.get(index).getDtBusinessDay())).equals(SequenceEnum.THURSDAY)) {
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
}
if (index== 0&&Util.getDayofweek(Util.DATE_FORMATE_yyyy_MM_dd.format(businessDays.get(index).getDtBusinessDay())).equals(SequenceEnum.FRIDAY)) {
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
}
if (index== 0&&Util.getDayofweek(Util.DATE_FORMATE_yyyy_MM_dd.format(businessDays.get(index).getDtBusinessDay())).equals(SequenceEnum.SATURDAY)) {
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
}
if (index== 0&&Util.getDayofweek(Util.DATE_FORMATE_yyyy_MM_dd.format(businessDays.get(index).getDtBusinessDay())).equals(SequenceEnum.SUNDAY)) {
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
generateBlankLayout(thumbnailHorizontalLayout);
}
}
private void generateThumbnailLayout(List
VerticalLayout dateVertical = new VerticalLayout();
dateVertical.addStyleName("marketrisk-datevertical");
Label dateLb = new Label(Util.DATE_FORMATE_dd.format(businessDays.get(index).getDtBusinessDay()));
if (businessDays.get(index).getFlagCount()!=null&&businessDays.get(index).getFlagCount().equals("N")) {
dateLb.addStyleName("marketrisk-dateLbnontrading");
}else{
dateLb.addStyleName("marketrisk-dateLb");
}
if (setUserDefiendCalendarHighLight(businessDays.get(index).getDtBusinessDay())) {
dateLb.removeStyleName("marketrisk-dateLb");
dateLb.addStyleName("marketrisk-dateLbnontrading");
}
dateVertical.addComponent(dateLb);
dateVertical.setComponentAlignment(dateLb, Alignment.TOP_CENTER);
Label description = new Label(businessDays.get(index).getNameChange());
// Label description = new Label("轻微跳变");
description.addStyleName("marketrisk-description");
dateVertical.addComponent(description);
dateVertical.setComponentAlignment(description, Alignment.MIDDLE_CENTER);
Button changeStateBtn = new Button("修改状态");
changeStateBtn.setData(Util.DATE_FORMATE_yyyy_MM_dd.format(businessDays.get(index).getDtBusinessDay()));
changeStateBtn.addStyleName("marketrisk-changestatebtn");
buttonListener();
changeStateBtn.addClickListener(clickListener );
dateVertical.addComponent(changeStateBtn);
dateVertical.setComponentAlignment(changeStateBtn, Alignment.MIDDLE_CENTER);
// ReportingView bean = ComponentUtil.applicationContext.getBean(ReportingView.class);
// bean.getHeader().getMenuBar().addComponentToTab(event.getButton().getData().toString());
// Label blankLb = new Label();
// blankLb.addStyleName("marketdata-blankLb");
thumbnailHorizontalLayout.addComponent(dateVertical);
}
private void generateBlankLayout(HorizontalLayout thumbnailHorizontalLayout) {
VerticalLayout dateVertical = new VerticalLayout();
dateVertical.addStyleName("marketrisk-datevertical");
thumbnailHorizontalLayout.addComponent(dateVertical);
}
private boolean setNobusinessDaysReportLayout() {
if (businessDays == null || businessDays.size() <= 0) {
HorizontalLayout noReportLayout = new HorizontalLayout();
noReportLayout.setWidth("400px");
thumbnailVertical.addComponent(noReportLayout);
thumbnailVertical.setComponentAlignment(noReportLayout, Alignment.MIDDLE_CENTER);
Label l = new Label("当前日期无日历");
l.addStyleName("usermanage-l");
noReportLayout.addComponent(l);
noReportLayout.setComponentAlignment(l, Alignment.MIDDLE_CENTER);
return true;
}
return false;
}
private boolean setUserDefiendCalendarHighLight(Date date) {
if (listDate!=null&&listDate.size()>0) {
for (int i = 0; i < listDate.size(); i++) {
boolean equalFlag = Util.isEqualDate(listDate.get(i).getDtNonworking(), date);
if (equalFlag) {
return true;
}
}
}
return false;
}
/**
* Function Name : initContentTable Description : 加载日报表格数据
* @author : Administrator
*/
@Override
public void initContent() {
initDownLoadTemplate();
}
public void initDownLoadTemplate() {
ThemeResource template=new ThemeResource("template/calendar_template.zip");
FileDownloader fileDownLoader=new FileDownloader(template);
fileDownLoader.extend(this.download);
}
private void buttonListener(){
clickListener = new ClickListener() {
private static final long serialVersionUID = 4178446429638195298L;
@Override
public void buttonClick(ClickEvent event) {
String date = event.getButton().getData().toString();
for (int i = 0; i < businessDays.size(); i++) {
if (Util.DATE_FORMATE_yyyy_MM_dd.format(businessDays.get(i).getDtBusinessDay()).equals(date)) {
if (businessDays.get(i).getFlagCount()!=null&&businessDays.get(i).getFlagCount().equals("N")) {
return;
}else{
boolean state = marketRiskCalendarProcess.selectUserOprateState(currentUser.getUserId(),date);
// true 代表用户未标记为非工作日;
if (state) {
marketRiskCalendarProcess.insertNonWorkingDay(((User)VaadinSession.getCurrent().getAttribute("user")).getUserId(),date);
// 修改状态后变颜色
if (event.getButton().getParent() instanceof VerticalLayout) {
VerticalLayout vertical = (VerticalLayout)event.getButton().getParent();
Iterator
while (o.hasNext()) {
Component next = o.next();
if (next instanceof Label&&Util.validateNumberisNaN(((Label) next).getValue())) {
next.removeStyleName("marketrisk-dateLb");
next.addStyleName("marketrisk-dateLbnontrading");
}
}
}
}else{
boolean flag = marketRiskCalendarProcess.deleteNonWorkingDay(currentUser.getUserId(),date);
if (event.getButton().getParent() instanceof VerticalLayout) {
VerticalLayout vertical = (VerticalLayout)event.getButton().getParent();
Iterator
while (o.hasNext()) {
Component next = o.next();
if (next instanceof Label&&Util.validateNumberisNaN(((Label) next).getValue())) {
next.removeStyleName("marketrisk-dateLbnontrading");
next.addStyleName("marketrisk-dateLb");
}
}
}
}
}
}
}
}
};
}
/**
* Function Name : registerHandler Description : 查询按钮注册事件;
*
* @author : Administrator
*/
@Override
public void registerHandler() {
date.addValueChangeListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
initLayoutCalendar();
}
});
reSizeListener = new BrowserWindowResizeListener() {
private static final long serialVersionUID = 3770420583772426984L;
@Override
public void browserWindowResized(BrowserWindowResizeEvent event) {
reSize();
}
};
}
private Component newLabel(String caption,String lenth,String color){
Label lb=new Label(caption);
lb.setWidth(lenth);
if (color.equals("red")) {
lb.addStyleName("red");
}else if (color.equals("green")) {
lb.addStyleName("green");
}else if (color.equals("yellow")) {
lb.addStyleName("yellow");
}else if (color.equals("white")) {
lb.addStyleName("marketrisk-white");
}
return lb;
}
private List
List
if (days.size()<=0) {
return newlist;
}
String format = Util.DATE_FORMATE_yyyy_MM_dd.format(days.get(0).getDtBusinessDay());
Date startDate = Util.getFirstDayOfMonthAsDate(format);
Date endDate = Util.getLastDayOfMonthAsDate(format);
int daysBetween = Util.differentDaysByMillisecond(startDate, endDate);
for (int i = 0; i < daysBetween+1; i++) {
if (getEqualsDate(days,startDate)!=null) {
newlist.add(getEqualsDate(days,startDate));
}else{
BusinessDays b = new BusinessDays();
b.setDtBusinessDay(startDate);
b.setFlagCount("N");
newlist.add(b);
}
startDate = Util.increaseDate(startDate, 1);
}
return newlist;
}
private BusinessDays getEqualsDate(List
for (BusinessDays businessDay : days) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(businessDay.getDtBusinessDay());
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date);
if (cal1.get(Calendar.DAY_OF_MONTH) == cal2
.get(Calendar.DAY_OF_MONTH)) {
return businessDay;
}
}
return null;
}
@Override
public void uploadFailed(FailedEvent event) {
Notification.show("提示:", "上传文件失败,请重新上传", Notification.Type.WARNING_MESSAGE);
}
@Override
public void uploadSucceeded(SucceededEvent event) {
if ("calendar".equals(event.getUpload().getData().toString())) {
importFileName.setValue(fileUploaded.getName());
try {
BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(fileUploaded)));
String line = buf.readLine();
if (line.contains(",")||line.contains(",")) {
Notification.show("提示:", "你上传的文件第一行有误请修改或选择合适的模板 !", Notification.Type.WARNING_MESSAGE);
return;
}
uploadDateList(buf); //调用方法存入数据库
buf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void uploadDateList(BufferedReader buf) throws IOException {
List
String line = null;
try {
while ((line = buf.readLine()) != null) {
if(!Util.IsDate(line)){
Notification.show("提示:", "你上传的文件不符合模板的格式要求,请选择合适的模板 !", Notification.Type.WARNING_MESSAGE);
return;
}
dateList.add(line);
}
marketRiskCalendarProcess.insertNonWorkingDayByDateList(((User)VaadinSession.getCurrent().getAttribute("user")).getUserId(),dateList);
} catch (IOException e) {
buf.close();
e.printStackTrace();
}
}
@Override
public OutputStream receiveUpload(String filename, String mimeType) {
fileUploaded = new File(filename);
FileOutputStream fileStream = null;
try {
fileStream = new FileOutputStream(fileUploaded);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return fileStream;
}
}