添加基本表结构数据导入支持;编写并测试Excel表格导入导出器;添加WEB-INF文件夹;

This commit is contained in:
Saturneric 2020-02-19 02:22:25 +08:00
parent eef59c2eb5
commit f4e156185d
21 changed files with 551 additions and 51 deletions

View File

@ -0,0 +1,84 @@
package com.codesdream.ase.component.datamanager;
import com.codesdream.ase.exception.DataFileNotFoundException;
import com.codesdream.ase.exception.DataIOException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
/**
* 利用数据生成Excel文件
*/
public class DataExcelGenerator implements DataGenerator {
Workbook workbook = new XSSFWorkbook();
Sheet sheet;
Integer colNumber;
String path;
public DataExcelGenerator(String path) {
sheet = workbook.createSheet("Data");
this.path = path;
}
public void setTableTitle(Collection<String> titles){
Row sheetRow = sheet.createRow(0);
int idx = 0;
for(String title : titles){
sheetRow.createCell(idx).setCellValue(title);
idx++;
}
colNumber = titles.size();
}
public void insertRow(int rowIndex, Collection<String> dataCollection){
Row row = sheet.createRow(rowIndex);
int idx = 0;
for(String data : dataCollection){
// 限制表头与表体的数据数目
if(idx >= colNumber) break;
row.createCell(idx).setCellValue(data);
idx++;
}
}
public void insertRow(Collection<String> dataCollection){
insertRow(sheet.getLastRowNum() + 1, dataCollection);
}
public void insertRowDataALL(Collection<String> dataCollections){
int cellIdx = 0;
Collection<String> dataCollection = new ArrayList<>();
for(String dataCollectionItem :dataCollections){
dataCollection.add(dataCollectionItem);
cellIdx++;
if(cellIdx % colNumber == 0) {
insertRow(dataCollection);
dataCollection.clear();
cellIdx = 0;
}
}
}
public void save() {
try {
FileOutputStream stream = new FileOutputStream(path, false);
workbook.write(stream);
stream.close();
} catch (FileNotFoundException e) {
throw new DataFileNotFoundException(path);
} catch (IOException e) {
throw new DataIOException();
}
}
}

View File

@ -0,0 +1,119 @@
package com.codesdream.ase.component.datamanager;
import com.codesdream.ase.exception.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
/**
* 读取简单表格式的Excel文件
*/
public class DataExcelReader implements DataReader {
Workbook workbook = null;
Sheet sheet = null;
short colNumber = 0;
int rowNumberNow = 0;
String path;
boolean hasReadFile = false;
public DataExcelReader(String path){
this.path = path;
readFile();
}
public void readFile(){
if(hasReadFile) return;
try{
FileInputStream fileInputStream = new FileInputStream(path);
workbook = WorkbookFactory.create(fileInputStream);
sheet = workbook.getSheetAt(0);
fileInputStream.close();
} catch (FileNotFoundException e) {
throw new DataFileNotFoundException(path);
} catch (InvalidFormatException e) {
throw new DataInvalidFormatException(e);
} catch (IOException e) {
throw new DataIOException();
}
}
public Collection<String> readColsTitle(){
Row titleRow = sheet.getRow(0);
colNumber = titleRow.getLastCellNum();
// 表头项目个数不可为0
if(colNumber == 0) throw new DataIllegalTableFormatException();
Collection<String> title = new ArrayList<>();
for(int cellIdx = 0; cellIdx < colNumber; cellIdx++){
title.add(readCell(titleRow.getCell(cellIdx)));
}
return title;
}
public Collection<String> readRow(int idx){
// 检查是否获取表头数据
if(this.colNumber == 0) readColsTitle();
if(idx > getRowsSize()) throw new DataReaderRowIndexOutOfRangeException();
this.rowNumberNow = idx;
Collection<String> data = new ArrayList<>();
Row dataRow = sheet.getRow(rowNumberNow);
// 检查列数是否合适
if(dataRow.getLastCellNum() > colNumber) throw new DataIllegalTableFormatException();
for(int cellIdx = 0; cellIdx < colNumber; cellIdx++){
data.add(readCell(dataRow.getCell(cellIdx)));
}
return data;
}
@Override
public int getRowsSize() {
return lastDataRowIndex() - firstDataRowIndex();
}
@Override
public int firstDataRowIndex() {
return 1;
}
@Override
public int lastDataRowIndex() {
return sheet.getLastRowNum();
}
public Collection<String> readRow(){
if(rowNumberNow >= getRowsSize()) return null;
return readRow(rowNumberNow + 1);
}
private String readCell(Cell cell){
String cellValue = "";
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
cellValue = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = cell.getDateCellValue().toString();
} else {
cellValue = String.valueOf(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
cellValue = String.valueOf(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK:
break;
default:
}
return cellValue;
}
}

View File

@ -0,0 +1,17 @@
package com.codesdream.ase.component.datamanager;
import java.util.Collection;
/**
* 表给狗数据文件生成器接口
*/
public interface DataGenerator {
// 读取表头信息
void setTableTitle(Collection<String> titles);
// 向表中写入一行数据
void insertRow(int rowIndex, Collection<String> dataCollection);
// 将修改保存表到文件中
void save();
}

View File

@ -8,7 +8,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
/** /**
* DataModel对应Repository层查询器 * Model对应Repository层查询器
*/ */
@Data @Data
@Component @Component
@ -43,7 +43,6 @@ public class DataModelRepositorySearcher {
return null; return null;
} }
public static String doCheckName(String string) { public static String doCheckName(String string) {
char[] charArray = string.toCharArray(); char[] charArray = string.toCharArray();
if(Character.isLowerCase(charArray[0])) charArray[0] -= 32; if(Character.isLowerCase(charArray[0])) charArray[0] -= 32;

View File

@ -10,7 +10,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
/** /**
* 根据子系统及名称查找特定的Data Model * 根据子系统及名称查找特定的MModel
*/ */
@Data @Data
@Component @Component

View File

@ -0,0 +1,27 @@
package com.codesdream.ase.component.datamanager;
import java.util.Collection;
/**
* 表结构信息读取器接口
*/
public interface DataReader {
// 从文件中读取数据(在使用上要求这个调用可有可无)
void readFile();
// 获得表头列的数目
Collection<String> readColsTitle();
// 读取特定序号的行的数据
Collection<String> readRow(int row);
// 得到数据的总行数
int getRowsSize();
// 得到第一数据行的序号
int firstDataRowIndex();
// 得到最后一行数据行的序号
int lastDataRowIndex();
}

View File

@ -0,0 +1,61 @@
package com.codesdream.ase.component.datamanager;
import com.codesdream.ase.exception.DataIllegalTableFormatException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.*;
// 描述一张数据表
@Component
@Scope("prototype")
public class DataTable {
Collection<String> titleCollection = new ArrayList<>();
Vector<DataTableRow> dataRows = new Vector<>();
// 为表添加列及列名
public DataTable addColTitle(String title){
titleCollection.add(title);
return this;
}
// 获得特定行的数据
public Collection<String> getRow(int index){
return dataRows.elementAt(index).getRow();
}
// 从DataReader导入特定表
public void ImportTable(DataReader reader){
// 从文件中读取数据
reader.readFile();
// 读取列信息
titleCollection = reader.readColsTitle();
int rowsSize = reader.getRowsSize();
int index = 0;
for(int i = reader.firstDataRowIndex(); i < reader.lastDataRowIndex(); i++){
dataRows.add(new DataTableRow(index++, reader.readRow(i)));
// 检查是否列数一致
if(dataRows.lastElement().getColsSize() != this.getColsSize()) {
// 清空表数据
this.dataRows.clear();
throw new DataIllegalTableFormatException();
}
}
}
// 为表添加行
public void addRow(Collection<String> row){
dataRows.add(new DataTableRow(dataRows.size() + 1, row));
}
// 获得表的列数
public int getColsSize(){
return titleCollection.size();
}
// 获得表的行数
public int getRowsSize(){
return dataRows.size();
}
}

View File

@ -0,0 +1,21 @@
package com.codesdream.ase.component.datamanager;
import lombok.Data;
import java.util.Collection;
// 描述数据表的行
@Data
public class DataTableRow {
int index;
Collection<String> row;
public DataTableRow(int index, Collection<String> row){
this.index = index;
this.row = row;
}
public int getColsSize(){
return row.size();
}
}

View File

@ -0,0 +1,15 @@
package com.codesdream.ase.exception;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class DataFileNotFoundException extends RuntimeException {
private String path;
public DataFileNotFoundException(String filePath){
super();
this.path = filePath;
}
}

View File

@ -0,0 +1,9 @@
package com.codesdream.ase.exception;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class DataIOException extends RuntimeException {
}

View File

@ -0,0 +1,9 @@
package com.codesdream.ase.exception;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class DataIllegalTableFormatException extends RuntimeException {
}

View File

@ -0,0 +1,15 @@
package com.codesdream.ase.exception;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class DataInvalidFormatException extends RuntimeException {
String information;
public DataInvalidFormatException(Exception e){
super();
information = e.getMessage();
}
}

View File

@ -0,0 +1,9 @@
package com.codesdream.ase.exception;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class DataReaderRowIndexOutOfRangeException extends RuntimeException {
}

View File

@ -31,6 +31,9 @@ public interface IUserService {
// 根据学号生成随机用户名 // 根据学号生成随机用户名
void generateRandomUsernameByStudentID(User user, String id); void generateRandomUsernameByStudentID(User user, String id);
// 随机生成一个用户名
void generateRandomUsername(User user);
// 注册用户 // 注册用户
User save(User user); User save(User user);

View File

@ -17,6 +17,7 @@ import javax.annotation.Resource;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
@Service @Service
public class UserService implements IUserService { public class UserService implements IUserService {
@ -72,6 +73,11 @@ public class UserService implements IUserService {
user.setUsername(usernameEncoder.encode(id)); user.setUsername(usernameEncoder.encode(id));
} }
@Override
public void generateRandomUsername(User user) {
user.setUsername(usernameEncoder.encode(UUID.randomUUID().toString()));
}
@Override @Override
public User save(User user) { public User save(User user) {
// 查找用户名是否已经被注册 // 查找用户名是否已经被注册

View File

@ -1,47 +0,0 @@
package com.codesdream.ase.test;
import com.codesdream.ase.component.ASESpringUtil;
import com.codesdream.ase.component.datamanager.DataModelRepositorySearcher;
import com.codesdream.ase.component.datamanager.DataModelSearcher;
import com.codesdream.ase.repository.permission.UserRepository;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
/**
* 测试DataModel相关查找器
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class DataManagerTest {
@Resource
ASESpringUtil springUtil;
@Test
public void dataModelSearcherTest() {
DataModelSearcher dataModelSearcher = springUtil.getBean(DataModelSearcher.class);
dataModelSearcher.getDataModelClass("permission", "Tag");
Assert.assertTrue(dataModelSearcher.isPresent());
for(String param : dataModelSearcher.getDataModelParamArgs()){
System.out.println(param);
}
}
@Test
public void dataModelRepositorySearcherTest(){
DataModelRepositorySearcher dataModelRepositorySearcher =
springUtil.getBean(DataModelRepositorySearcher.class);
dataModelRepositorySearcher.getDataModelRepositoryClass("permission", "User");
Assert.assertTrue(dataModelRepositorySearcher.isPresent());
UserRepository userRepository = dataModelRepositorySearcher.getDataModelRepositoryInstance();
}
}

View File

@ -0,0 +1,14 @@
INSERT INTO `base_political_status`(`id`, `name`) VALUES (0, '----');
INSERT INTO `base_political_status`(`id`, `name`) VALUES (1, '中共党员');
INSERT INTO `base_political_status`(`id`, `name`) VALUES (2, '中共预备党员');
INSERT INTO `base_political_status`(`id`, `name`) VALUES (3, '共青团员');
INSERT INTO `base_political_status`(`id`, `name`) VALUES (4, '民革会员');
INSERT INTO `base_political_status`(`id`, `name`) VALUES (5, '民盟盟员');
INSERT INTO `base_political_status`(`id`, `name`) VALUES (6, '民建会员');
INSERT INTO `base_political_status`(`id`, `name`) VALUES (7, '民进会员');
INSERT INTO `base_political_status`(`id`, `name`) VALUES (8, '农工党党员');
INSERT INTO `base_political_status`(`id`, `name`) VALUES (9, '致公党党员');
INSERT INTO `base_political_status`(`id`, `name`) VALUES (10, '九三学社社员');
INSERT INTO `base_political_status`(`id`, `name`) VALUES (11, '台盟盟员');
INSERT INTO `base_political_status`(`id`, `name`) VALUES (12, '无党派民主人士');
INSERT INTO `base_political_status`(`id`, `name`) VALUES (13, '群众');

View File

@ -0,0 +1,25 @@
-- ----------------------------
-- Records of base_college
-- ----------------------------
INSERT INTO `base_college` VALUES ('01', '航空学院', '1');
INSERT INTO `base_college` VALUES ('02', '航天学院', '2');
INSERT INTO `base_college` VALUES ('03', '航海学院', '3');
INSERT INTO `base_college` VALUES ('04', '材料学院', '4');
INSERT INTO `base_college` VALUES ('05', '机电学院', '5');
INSERT INTO `base_college` VALUES ('06', '力学与土木建筑学院', '6');
INSERT INTO `base_college` VALUES ('07', '动力与能源学院', '7');
INSERT INTO `base_college` VALUES ('08', '电子信息学院', '8');
INSERT INTO `base_college` VALUES ('09', '自动化学院', '9');
INSERT INTO `base_college` VALUES ('10', '计算机学院', '10');
INSERT INTO `base_college` VALUES ('11', '理学院', '11');
INSERT INTO `base_college` VALUES ('12', '管理学院', '12');
INSERT INTO `base_college` VALUES ('13', '人文与经法学院', '13');
INSERT INTO `base_college` VALUES ('14', '软件学院学院', '14');
INSERT INTO `base_college` VALUES ('15', '生命学院', '15');
INSERT INTO `base_college` VALUES ('16', '外国语学院', '16');
INSERT INTO `base_college` VALUES ('17', '教育实验学院', '17');
INSERT INTO `base_college` VALUES ('18', '西北工业大学伦敦玛丽女王大学工程学院', '18');
INSERT INTO `base_college` VALUES ('19', '马克思主义学院', '19');
INSERT INTO `base_college` VALUES ('20', '微电子学院', '20');
INSERT INTO `base_college` VALUES ('21', '网络空间安全学院', '21');
INSERT INTO `base_college` VALUES ('22', '民航学院', '22');

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>

View File

@ -0,0 +1,108 @@
package com.codesdream.ase;
import com.codesdream.ase.component.ASESpringUtil;
import com.codesdream.ase.component.datamanager.DataExcelGenerator;
import com.codesdream.ase.component.datamanager.DataExcelReader;
import com.codesdream.ase.component.datamanager.DataModelRepositorySearcher;
import com.codesdream.ase.component.datamanager.DataModelSearcher;
import com.codesdream.ase.repository.permission.UserRepository;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* 测试DataModel相关查找器
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class DataManagerTest {
@Resource
ASESpringUtil springUtil;
// Model 查找器测试
@Test
public void dataModelSearcherTest() {
DataModelSearcher dataModelSearcher = springUtil.getBean(DataModelSearcher.class);
dataModelSearcher.getDataModelClass("permission", "Tag");
Assert.assertTrue(dataModelSearcher.isPresent());
for(String param : dataModelSearcher.getDataModelParamArgs()){
System.out.println(param);
}
}
// Repository 查找器测试
@Test
public void dataModelRepositorySearcherTest(){
DataModelRepositorySearcher dataModelRepositorySearcher =
springUtil.getBean(DataModelRepositorySearcher.class);
dataModelRepositorySearcher.getDataModelRepositoryClass("permission", "User");
Assert.assertTrue(dataModelRepositorySearcher.isPresent());
UserRepository userRepository = dataModelRepositorySearcher.getDataModelRepositoryInstance();
}
// 测试Excel导出功能
@Test
public void dataExcelGeneratorTest(){
DataExcelGenerator dataExcelGenerator = new DataExcelGenerator("DataExcelGeneratorTest.xlsx");
// 设置表头信息
Collection<String> titles = new ArrayList<>();
titles.add("Name");
titles.add("Sex");
titles.add("Age");
dataExcelGenerator.setTableTitle(titles);
Collection<String> dataCollection = new ArrayList<>();
dataCollection.add("Tom");
dataCollection.add("M");
dataCollection.add("18");
dataExcelGenerator.insertRow(1, dataCollection);
dataExcelGenerator.insertRow(dataCollection);
dataCollection.add("Tom");
dataCollection.add("M");
dataCollection.add("18");
dataExcelGenerator.insertRowDataALL(dataCollection);
// 保存数据
dataExcelGenerator.save();
}
// 测试Excel导入功能
@Test
public void dataExcelReaderTest(){
DataExcelReader dataExcelReader = new DataExcelReader("DataExcelGeneratorTest.xlsx");
// 从文件中读取数据
dataExcelReader.readFile();
// 读取表头信息
Collection<String> title = dataExcelReader.readColsTitle();
// 读一行取数据
Collection<String> data = dataExcelReader.readRow();
Iterator<String> iterator = title.iterator();
Assert.assertEquals(iterator.next(), "Name");
Assert.assertEquals(iterator.next(), "Sex");
Assert.assertEquals(iterator.next(), "Age");
iterator = data.iterator();
Assert.assertEquals(iterator.next(), "Tom");
Assert.assertEquals(iterator.next(), "M");
Assert.assertEquals(iterator.next(), "18");
}
}

View File

@ -1,4 +1,4 @@
package com.codesdream.ase.test; package com.codesdream.ase;
import com.codesdream.ase.model.permission.User; import com.codesdream.ase.model.permission.User;