com.mg.common.metadata.service.MetaDataImportServiceImpl Maven / Gradle / Ivy
package com.mg.common.metadata.service;
import com.mg.common.metadata.dao.MImportTemplateDao;
import com.mg.common.metadata.util.ExcelUtil;
import com.mg.framework.entity.metadata.*;
import com.mysema.query.jpa.impl.JPAQuery;
import com.mg.framework.exception.ServiceException;
import com.mg.framework.sys.PropertyConfigurer;
import com.mg.framework.utils.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
/**
* 元数据数据导入
* Created by liukefu on 2015/10/21.
*/
@Service
public class MetaDataImportServiceImpl implements MetaDataImportService {
Logger logger = LoggerFactory.getLogger(getClass());
private static final String STR_IGNORE = "#";
@PersistenceContext
private EntityManager entityManager;
@Autowired
MetaDataQueryService metaDataQueryService;
@Autowired
CustomFormService customFormService;
@Autowired
MImportTemplateDao mImportTemplateDao;
@Autowired
MEnumService mEnumService;
@Autowired
MetaDataService metaDataService;
@Autowired
MetaDataExpressService metaDataExpressService;
/**
* 数据导入
* @param path
*/
@Transactional
public void importData(String path) {
if(!(path.endsWith(".xlsx") || path.endsWith(".xls"))){
throw new ServiceException("不合法的文件类型,请上传Excel类型文件!");
}
//打开excel
Workbook wb = null;
FileInputStream inputStream = null;
try {
inputStream =new FileInputStream(new File(path));
wb = WorkbookFactory.create(inputStream);
int totalSheetNum = wb.getNumberOfSheets();
for (int i = 0; i < totalSheetNum; i++) {
Sheet sheet = wb.getSheetAt(i);
String sheetName = sheet.getSheetName();
if (sheetName.startsWith(STR_IGNORE)) {
logger.debug("ignore this sheet:{}", sheetName);
continue;
}
MObjectEntity mObjectEntity = metaDataQueryService.findMObjectByName(sheetName);
if(mObjectEntity!=null){
importDataFromSheet(wb.getSheet(sheetName), mObjectEntity);
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("open/create Excel failure, filePath={}", path);
throw new ServiceException(e);
}finally {
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void importDataFromSheet(Sheet sheet, MObjectEntity mObjectEntity) {
//总列数、总行数
Row rowHead = sheet.getRow(0);
int totalRows = sheet.getLastRowNum();
int totalColumns = rowHead.getLastCellNum();
//pkProperties 修改功能
List pkProperties = new ArrayList<>();
Map pkPropertiesMap = new HashMap<>();
Map propertiesMap = new HashMap<>();
List properties = new ArrayList<>();
//读取第一行标题
for (int i = 0; i < totalColumns; i++) {
Cell cell = rowHead.getCell(i);
if (cell == null) {
continue;
}
String metadataName = ExcelUtil.getStringValue2007(cell);
if(StringUtils.isNotBlank(metadataName)){
MirrorPropertyEntity property = metaDataQueryService.findMPropertyByName(mObjectEntity, metadataName);
if(property!=null){
properties.add(property);
propertiesMap.put(i, property);
//如果是粗体,表示按字段唯一
Font font = sheet.getWorkbook().getFontAt(cell.getCellStyle().getFontIndex());
if (font.getBoldweight() == Font.BOLDWEIGHT_BOLD) {
pkProperties.add(property);
pkPropertiesMap.put(property.getPropertyPath(),i);
}
}else{
logger.debug("cannot find metadata {}",metadataName);
}
}
}
//对象的所有属性
List mPropertyEntityList = metaDataQueryService.findMPropertyByBelongMObject(mObjectEntity);
//读取数据列,第二行开始
for (int i = 1; i <= totalRows; i++) {
Row row = sheet.getRow(i);
String firstValue = String.valueOf(row.getCell(0));
if (row == null || StringUtils.isBlank(firstValue)) {
continue;
}
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy