package com.nanometer.smartlab.controller;
|
|
import com.nanometer.smartlab.entity.BasePage;
|
import com.nanometer.smartlab.entity.SysFile;
|
import com.nanometer.smartlab.service.*;
|
import com.nanometer.smartlab.util.Constants;
|
import com.nanometer.smartlab.util.FacesUtils;
|
import org.apache.log4j.Logger;
|
import org.primefaces.context.RequestContext;
|
import org.primefaces.event.FileUploadEvent;
|
import org.primefaces.model.LazyDataModel;
|
import org.primefaces.model.SortOrder;
|
import org.primefaces.model.UploadedFile;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.context.annotation.Scope;
|
import org.springframework.stereotype.Controller;
|
|
import javax.annotation.Resource;
|
import javax.faces.application.FacesMessage;
|
import javax.faces.context.FacesContext;
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.util.*;
|
|
@Controller
|
@Scope("session")
|
public class SysFileMngController extends BaseController {
|
|
private static Logger logger = Logger.getLogger(SysFileMngController.class);
|
|
@Resource
|
private SysFileService sysFileService;
|
@Resource
|
private BasePageService basePageService;
|
@Value("${filePath}")
|
private String filePath; //配置文件配置的物理保存地址
|
private LazyDataModel<SysFile> dataModel;
|
private SysFile sysFile;
|
private List<SysFile> selectedList;
|
private List<BasePage> basePageList;
|
private int action;
|
private UploadedFile file;
|
|
public UploadedFile getFile() {
|
return file;
|
}
|
|
public void setFile(UploadedFile file) {
|
this.file = file;
|
}
|
|
public List<SysFile> getSelectedList() {
|
return selectedList;
|
}
|
|
public void setSelectedList(List<SysFile> selectedList) {
|
this.selectedList = selectedList;
|
}
|
|
private String name;
|
|
public SysFile getSysFile() {
|
return sysFile;
|
}
|
|
public void setSysFile(SysFile sysFile) {
|
this.sysFile = sysFile;
|
}
|
|
public String getName() {
|
return name;
|
}
|
|
public void setName(String name) {
|
this.name = name;
|
}
|
|
public static Logger getLogger() {
|
return logger;
|
}
|
|
|
public static void setLogger(Logger logger) {
|
SysFileMngController.logger = logger;
|
}
|
|
private void uploadFile(InputStream is){
|
|
}
|
|
public void upload(FileUploadEvent event) throws Exception{
|
UploadedFile uploadedFile = event.getFile();
|
String fileName = uploadedFile.getFileName();
|
String contentType = uploadedFile.getContentType();
|
//InputStream is = file.getInputstream();
|
System.out.println("上传文件结束");
|
String oldName = fileName;
|
Date time=new Date();
|
String newName = oldName.substring(0,oldName.lastIndexOf("."))+ oldName.substring(oldName.lastIndexOf("."));
|
String realPath=filePath+time.getTime()+"/";
|
|
File dest = new File(realPath, newName);
|
if (!dest.getParentFile().exists()) {
|
boolean rel = dest.getParentFile().mkdirs();
|
if (!rel) {
|
throw new Exception("文件夹创建失败");
|
}
|
}
|
InputStream is = uploadedFile.getInputstream();
|
OutputStream os = new FileOutputStream(dest);
|
try {
|
byte[] buffer = new byte[8 * 1024];
|
int bytesRead;
|
while ((bytesRead = is.read(buffer)) != -1) {
|
os.write(buffer, 0, bytesRead);
|
}
|
sysFile.setFileName(oldName);
|
sysFile.setUrl(realPath+oldName);
|
FacesUtils.warn("文件上传成功。");
|
} catch (Exception e) {
|
FacesUtils.warn("文件上传失败。");
|
throw e;
|
} finally {
|
if (is != null) {
|
is.close();
|
}
|
if (os != null) {
|
os.close();
|
}
|
}
|
//return time.getTime()+"/"+newName;
|
|
/* if (file != null) {
|
FacesMessage message = new FacesMessage("Successful", file.getFileName() + " is uploaded.");
|
FacesContext.getCurrentInstance().addMessage(null, message);
|
}*/
|
}
|
public void onNewBtnClick() {
|
this.sysFile = new SysFile();
|
this.action = Constants.ACTION_ADD;
|
//this.basePageList = this.basePageService.getBasePageList();
|
}
|
|
public void onEditBtnClick() {
|
if (this.selectedList == null
|
|| this.selectedList.size() == 0) {
|
FacesUtils.warn("请选择数据。");
|
return;
|
}
|
if (this.selectedList.size() > 1) {
|
FacesUtils.warn("只能选择一个数据进行修改。");
|
return;
|
}
|
this.sysFile = this.sysFileService.getSysFileById(this.selectedList.get(0).getId());
|
this.action = Constants.ACTION_EDIT;
|
//this.basePageList = this.basePageService.getBasePageList();
|
RequestContext.getCurrentInstance().execute("PF('dialog').show()");
|
}
|
|
public void onSaveBtnClick() {
|
try {
|
// 新建
|
if (this.action == Constants.ACTION_ADD) {
|
if (this.sysFile == null) {
|
FacesUtils.warn("新建对象为空。");
|
return;
|
}
|
|
if (this.sysFile.getName() == null || this.sysFile.getName()=="") {
|
FacesUtils.warn("请输入名称。");
|
return;
|
}
|
if (this.sysFile.getVersion() == null) {
|
FacesUtils.warn("请输入版本号。");
|
return;
|
}
|
if (this.sysFile.getNewOrOld() == null) {
|
FacesUtils.warn("请选择新旧RFID。");
|
return;
|
}
|
//upload();
|
if (this.sysFile.getFileName() == null || this.sysFile.getFileName()=="") {
|
FacesUtils.warn("请上传文件。");
|
return;
|
}
|
|
BaseController c =new BaseController();
|
this.sysFileService.insertSysFile(this.sysFile);
|
|
FacesUtils.info("新建成功。");
|
RequestContext.getCurrentInstance().execute("PF('dialog').hide()");
|
// 修改
|
} else if (this.action == Constants.ACTION_EDIT) {
|
if (this.sysFile == null) {
|
FacesUtils.warn("修改对象为空。");
|
return;
|
}
|
|
if (this.sysFile.getName() == null || this.sysFile.getName()=="") {
|
FacesUtils.warn("请输入名称。");
|
return;
|
}
|
if (this.sysFile.getVersion() == null) {
|
FacesUtils.warn("请输入版本号。");
|
return;
|
}
|
if (this.sysFile.getNewOrOld() == null) {
|
FacesUtils.warn("请选择新旧RFID。");
|
return;
|
}
|
if (this.sysFile.getFileName() == null || this.sysFile.getFileName()=="") {
|
FacesUtils.warn("请上传文件。");
|
return;
|
}
|
this.sysFileService.updateSysFile(this.sysFile);
|
|
|
FacesUtils.info("修改成功。");
|
RequestContext.getCurrentInstance().execute("PF('dialog').hide()");
|
}
|
} catch (Exception e) {
|
logger.error("操作失败。", e);
|
FacesUtils.warn("操作失败。");
|
}
|
}
|
|
public void onDeleteBtnClick() {
|
try {
|
if (this.selectedList == null
|
|| this.selectedList.size() == 0) {
|
FacesUtils.warn("请选择数据。");
|
return;
|
}
|
|
this.sysFileService.deleteSysFile(this.selectedList);
|
|
FacesUtils.info("删除成功。");
|
} catch (Exception e) {
|
logger.error("操作失败。", e);
|
FacesUtils.warn("操作失败。");
|
}
|
}
|
|
|
public LazyDataModel<SysFile> getDataModel() {
|
if (this.dataModel == null) {
|
this.dataModel = new LazyDataModel<SysFile>() {
|
@Override
|
public List<SysFile> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
|
List<SysFile> list = null;
|
try {
|
int count = sysFileService.getSysFileTotalCount(name);
|
this.setRowCount(count);
|
if (count > 0) {
|
list = sysFileService.getSysFileList(name,first, pageSize);
|
}
|
selectedList = new ArrayList<>();
|
} catch (Exception e) {
|
logger.error(e);
|
}
|
return list;
|
}
|
|
@Override
|
public SysFile getRowData(String rowKey) {
|
// Iterator<SysFile> iterator = this.iterator();
|
// if (iterator != null) {
|
// SysFile br = null;
|
// while (iterator.hasNext()) {
|
// br = iterator.next();
|
// if (rowKey.equals(br.getId())) {
|
// return br;
|
// }
|
// }
|
// }
|
return sysFileService.getSysFileById(rowKey);
|
}
|
};
|
}
|
|
return dataModel;
|
}
|
|
|
public void setDataModel(LazyDataModel<SysFile> dataModel) {
|
this.dataModel = dataModel;
|
}
|
|
public List<BasePage> getBasePageList() {
|
return basePageList;
|
}
|
|
public void setBasePageList(List<BasePage> basePageList) {
|
this.basePageList = basePageList;
|
}
|
|
public int getAction() {
|
return action;
|
}
|
|
|
}
|