package com.vci.client.utils;
|
|
import java.io.BufferedInputStream;
|
import java.io.BufferedOutputStream;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.ArrayList;
|
import java.util.Enumeration;
|
import java.util.HashMap;
|
import java.util.HashSet;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipFile;
|
|
import com.vci.client.bof.ClientBusinessObject;
|
import com.vci.client.bof.ClientBusinessObjectOperation;
|
import com.vci.client.bof.ClientLinkObject;
|
import com.vci.client.bof.ClientLinkObjectOperation;
|
import com.vci.client.common.oq.OQTool;
|
import com.vci.client.fm.ClientFileObjectOperation;
|
import com.vci.client.fm.FileObject;
|
import com.vci.client.fm.FileTypeConstants;
|
import com.vci.client.omd.provider.ApProvider;
|
import com.vci.client.omd.provider.EnumProvider;
|
import com.vci.client.oq.QTClient;
|
import com.vci.client.utils.excel.ExcelDocumentUtils;
|
import com.vci.client.utils.excel.SheetDataSet;
|
import com.vci.common.qt.object.Condition;
|
import com.vci.common.qt.object.Connector;
|
import com.vci.common.qt.object.QTConstants;
|
import com.vci.common.qt.object.QueryTemplate;
|
import com.vci.corba.common.VCIError;
|
import com.vci.corba.omd.atm.AttribItem;
|
import com.vci.corba.omd.data.BusinessObject;
|
import com.vci.corba.omd.data.LinkObject;
|
import com.vci.corba.omd.etm.EnumChild;
|
import com.vci.corba.omd.etm.EnumItem;
|
import com.vci.omd.constants.AttributeConstants;
|
|
public class ImportDataTool {
|
public static final String ERROR = "error";
|
public static final String RESULT = "result";
|
public static final String TRUE = "true";
|
public static final String FALSE = "false";
|
private final int PARTIONSIZE = 500;
|
|
private static ImportDataTool instance = null;
|
|
private static ImportDataTool getInstance(){
|
if (instance == null) {
|
instance = new ImportDataTool();
|
}
|
return instance;
|
}
|
|
private ClientLinkObjectOperation instanceLo;
|
|
private ClientLinkObjectOperation getInstanceLo() {
|
if (instanceLo == null) {
|
instanceLo = new ClientLinkObjectOperation();
|
}
|
return instanceLo;
|
}
|
|
private ClientBusinessObjectOperation instanceBo;
|
|
private ClientBusinessObjectOperation getInstanceBo() {
|
if (instanceBo == null) {
|
instanceBo = new ClientBusinessObjectOperation();
|
}
|
return instanceBo;
|
}
|
|
public static Map<String, String> importLOData(File file, String fileName, String type,
|
String linkType, String f_type, String f_oid, String f_revisionOid, String f_nameoid, int updateFlag) throws VCIError, IOException{
|
Map<String, String> infoMap = new HashMap<String, String>();
|
//压缩文件中的excel数据文件
|
InputStream dataInputStream = null;
|
Map<String, File> files = new HashMap<String, File>();
|
boolean zipFlag = false;
|
if(fileName.endsWith(".zip")){
|
zipFlag = true;
|
ZipFile zipFile = new ZipFile(file);
|
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
String tempFolder = file.getParent();
|
while(entries.hasMoreElements()){
|
ZipEntry zipEntry = entries.nextElement();
|
if(!zipEntry.isDirectory()){
|
String fileName_ = zipEntry.getName();
|
InputStream inputStream_ = zipFile.getInputStream(zipEntry);
|
if(fileName_.endsWith(".xlsx") || fileName_.endsWith(".xls")){
|
fileName = fileName_;
|
dataInputStream = inputStream_;
|
}else{
|
String filePath = tempFolder + "\\" + fileName_;
|
File tempFile = new File(filePath);
|
File folder = tempFile.getParentFile();
|
if(!folder.exists()){
|
folder.mkdirs();
|
}
|
if(!tempFile.exists()){
|
tempFile.createNewFile();
|
}
|
BufferedInputStream inBuff = new BufferedInputStream(inputStream_);
|
BufferedOutputStream outBuff = new BufferedOutputStream(new FileOutputStream(tempFile));
|
|
byte[] b = new byte[1024 * 512];
|
int len;
|
while((len = inBuff.read(b)) != -1){
|
outBuff.write(b, 0, len);
|
}
|
|
files.put(fileName_, tempFile);
|
inBuff.close();
|
outBuff.close();
|
}
|
}
|
}
|
}
|
BufferedInputStream bs;
|
if(zipFlag){
|
bs = new BufferedInputStream(dataInputStream);
|
}else{
|
bs = new BufferedInputStream(new FileInputStream(file));
|
}
|
List<SheetDataSet> sheetDataSets = ExcelDocumentUtils
|
.readExcelDocument(fileName, bs);
|
List<String[]> dataSets = sheetDataSets.get(0).getDataSet();
|
String[] dataSet = dataSets.get(0);
|
Set<String> typeSet = new HashSet<String>();
|
Map<Integer, String> typeAttMap = new HashMap<Integer, String>();
|
|
//判重列
|
List<String> uniqueCols = new ArrayList<String>();
|
//文件列
|
List<String> fileCols = new ArrayList<String>();
|
for(int i = 0; i < dataSet.length; i++){
|
String data = dataSet[i];
|
String[] data_ = data.split("\\.");
|
typeSet.add(data_[0]);
|
|
//判重列标识
|
if(data_[1].endsWith("*")){
|
uniqueCols.add(data_[1].substring(0, data_[1].indexOf("*")).toUpperCase());
|
data = data.substring(0, data.indexOf("*"));
|
//文件列标识
|
}else if(data_[1].endsWith("+")){
|
fileCols.add(data_[1].substring(0, data_[1].indexOf("+")).toUpperCase());
|
data = data.substring(0, data.indexOf("+"));
|
}
|
typeAttMap.put(i, data);
|
}
|
if(!typeSet.contains(type)){
|
infoMap.put(ERROR, "文件中无该类型数据");
|
return infoMap;
|
}
|
|
if(typeSet.size() > 2){
|
infoMap.put(ERROR, "文件中含有其他类型数据");
|
return infoMap;
|
}
|
|
boolean flag = false;
|
if(zipFlag){
|
flag = getInstance().saveBOAndLOsWithFile(dataSets, typeAttMap, type, linkType, f_type, f_oid, f_revisionOid, f_nameoid,
|
uniqueCols, updateFlag, fileCols, files);
|
}else{
|
flag = getInstance().saveBOAndLOs(dataSets, typeAttMap, type, linkType, f_type, f_oid, f_revisionOid, f_nameoid, uniqueCols, updateFlag);
|
}
|
//删除临时文件
|
for(Iterator<String> ite = files.keySet().iterator(); ite.hasNext();){
|
String fileName_ = ite.next();
|
File file_ = files.get(fileName_);
|
file_.delete();
|
}
|
file.delete();
|
|
if(flag){
|
infoMap.put(RESULT, TRUE);
|
}else{
|
infoMap.put(RESULT, FALSE);
|
}
|
return infoMap;
|
}
|
|
/**
|
* 保存业务对象 和 链接对象
|
* 当对象不存在时, 创建对象;
|
* 存在时(unique), 更新对象.
|
* @param dataSets
|
* @param typeAttMap
|
* @param type
|
* @param linkType
|
* @param f_nameoid
|
* @param f_revisionOid
|
* @return
|
* @throws PLMError
|
*/
|
private boolean saveBOAndLOs(List<String[]> dataSets,
|
Map<Integer, String> typeAttMap, String type, String linkType, String f_type, String f_oid, String f_revisionOid, String f_nameoid, List<String> uniqueCols, int updateFlag) throws VCIError {
|
List<ClientBusinessObject> createListBo = new ArrayList<ClientBusinessObject>();
|
List<ClientBusinessObject> updateListBo = new ArrayList<ClientBusinessObject>();
|
List<ClientLinkObject> createListLo = new ArrayList<ClientLinkObject>();
|
List<ClientLinkObject> updateListLo = new ArrayList<ClientLinkObject>();
|
List<ClientBusinessObject> allBoList = new ArrayList<ClientBusinessObject>();
|
Map<String, Map<String, String>> map_ = new HashMap<String, Map<String,String>>();
|
List<ClientLinkObject> allLoList = new ArrayList<ClientLinkObject>();
|
Map<String, ClientLinkObject> loMap = new HashMap<String, ClientLinkObject>();
|
ClientBusinessObject cboSource = getInstanceBo().createBusinessObject(type);
|
ClientLinkObject cloSource = getInstanceLo().createLinkObject(linkType);
|
for(int i = 1; i < dataSets.size(); i++){
|
ClientBusinessObject cbo = BOTool.cloneClientBusinessObject(cboSource, true);
|
ClientLinkObject clo = LOTool.cloneClientLinkObject(cloSource, true);
|
String[] data = dataSets.get(i);
|
Map<String, String> map = new HashMap<String, String>();
|
for(int k = 0; k < data.length; k++){
|
String[] data_ = typeAttMap.get(k).split("\\.");
|
String type_ = data_[0];
|
String attName = data_[1];
|
String value = data[k];
|
value = checkEnum(attName, value);
|
//业务类型属性
|
if(type_.equalsIgnoreCase(type)){
|
if(attName.equalsIgnoreCase("ID") || attName.equalsIgnoreCase("NAME") || attName.equalsIgnoreCase("DESCRIPTION")){
|
map.put(attName.toLowerCase(), value);
|
}
|
cbo.setAttributeValue(attName.toLowerCase(), value, true);
|
//链接类型属性
|
}else{
|
clo.setAttributeValue(attName.toLowerCase(), value);
|
}
|
}
|
|
clo.setFromBTMName(f_type);
|
clo.setFromOid(f_oid);
|
clo.setFromRevisionOid(f_revisionOid);
|
clo.setFromNameOid(f_nameoid);
|
clo.setToBTMName(cbo.getBtmName());
|
clo.setToOid(cbo.getOid());
|
clo.setToRevisionOid(cbo.getRevisionid());
|
clo.setToNameOid(cbo.getNameoid());
|
|
map_.put(cbo.getOid(), map);
|
allBoList.add(cbo);
|
allLoList.add(clo);
|
loMap.put(cbo.getOid(), clo);
|
}
|
|
if (uniqueCols.size() > 0 && allBoList.size() > 0) {
|
List<BusinessObject> queryBO = queryBO(allBoList, uniqueCols);
|
for(ClientBusinessObject cbo : allBoList){
|
BusinessObject bo = isExistBo(cbo, uniqueCols, queryBO);
|
ClientLinkObject clo = loMap.get(cbo.getOid());
|
if (bo != null) {
|
if(updateFlag == 1){
|
Map<String, String> map = map_.get(cbo.getOid());
|
if (map.get("id") != null) {
|
cbo.setAttributeValue("id", map.get("id"));
|
}
|
if (map.get("name") != null) {
|
cbo.setAttributeValue("name", map.get("name"));
|
}
|
if (map.get("description") != null) {
|
cbo.setAttributeValue("description", map.get("description"));
|
}
|
cbo.setOid(bo.oid);
|
cbo.setTs(bo.ts);
|
updateListBo.add(cbo);
|
}
|
// 当bo存在, lo不存在时, 不论bo是覆盖还是跳过 , 都需要创建lo
|
|
clo.setToOid(bo.oid);
|
clo.setToRevisionOid(bo.revisionid);
|
clo.setToNameOid(bo.nameoid);
|
|
} else {
|
createListBo.add(cbo);
|
}
|
}
|
if(allLoList.size() > 0){
|
List<LinkObject> queryLO = queryLO(allLoList);
|
for(ClientLinkObject clo : allLoList){
|
LinkObject lo = isExistLo(clo, queryLO);
|
if(lo != null){
|
if (updateFlag == 1) {
|
clo.setOid(lo.oid);
|
clo.setTs(lo.ts);
|
updateListLo.add(clo);
|
}
|
}else{
|
createListLo.add(clo);
|
}
|
}
|
}
|
}else {
|
createListBo.addAll(allBoList);
|
createListLo.addAll(allLoList);
|
}
|
|
boolean flag = true;
|
if(createListBo.size() > 0 || createListLo.size() > 0){
|
flag &= getInstanceBo().batchSaveCreateBuinessObject(
|
createListBo.toArray(new ClientBusinessObject[0]), createListLo.toArray(new ClientLinkObject[0]));
|
}else{
|
flag &= true;
|
}
|
if(updateListBo.size() > 0){
|
flag &= getInstanceBo().batchUpdateBuinessObject(updateListBo.toArray(new ClientBusinessObject[0]));
|
}else{
|
flag &= true;
|
}
|
|
if(updateListLo.size() > 0){
|
flag &= getInstanceLo().batchUpdateLinkObject(updateListLo.toArray(new ClientLinkObject[0]));
|
}else{
|
flag &= true;
|
}
|
|
return flag;
|
}
|
|
/**
|
* 保存业务对象 和 链接对象
|
* 当对象不存在时, 创建对象;
|
* 存在时(unique), 更新对象.
|
* @param dataSets
|
* @param typeAttMap
|
* @param type
|
* @param linkType
|
* @param f_nameoid
|
* @param f_revisionOid
|
* @param fileCols : 文件路径列
|
* @param files : Map<fileName, tempFile>
|
* @return
|
* @throws PLMError
|
*/
|
private boolean saveBOAndLOsWithFile(List<String[]> dataSets,Map<Integer, String> typeAttMap,
|
String type, String linkType, String f_type, String f_oid, String f_revisionOid, String f_nameoid,
|
List<String> uniqueCols, int updateFlag, List<String> fileCols, Map<String, File> files) throws VCIError {
|
List<ClientBusinessObject> createListBo = new ArrayList<ClientBusinessObject>();
|
List<ClientBusinessObject> updateListBo = new ArrayList<ClientBusinessObject>();
|
List<ClientLinkObject> createListLo = new ArrayList<ClientLinkObject>();
|
List<ClientLinkObject> updateListLo = new ArrayList<ClientLinkObject>();
|
List<ClientBusinessObject> allBoList = new ArrayList<ClientBusinessObject>();
|
Map<String, Map<String, String>> map_ = new HashMap<String, Map<String,String>>();
|
Map<String, Map<String, String>> fileMap_ = new HashMap<String, Map<String,String>>();
|
List<ClientLinkObject> allLoList = new ArrayList<ClientLinkObject>();
|
Map<String, ClientLinkObject> loMap = new HashMap<String, ClientLinkObject>();
|
ClientFileObjectOperation operation = new ClientFileObjectOperation();
|
ClientBusinessObject cboSource = getInstanceBo().createBusinessObject(type);
|
ClientLinkObject cloSource = getInstanceLo().createLinkObject(linkType);
|
for(int i = 1; i < dataSets.size(); i++){
|
ClientBusinessObject cbo = BOTool.cloneClientBusinessObject(cboSource, true);
|
ClientLinkObject clo = LOTool.cloneClientLinkObject(cloSource, true);
|
String[] data = dataSets.get(i);
|
Map<String, String> map = new HashMap<String, String>();
|
Map<String, String> fileMap = new HashMap<String, String>();
|
for(int k = 0; k < data.length; k++){
|
String[] data_ = typeAttMap.get(k).split("\\.");
|
String type_ = data_[0];
|
String attName = data_[1];
|
String value = data[k];
|
value = checkEnum(attName, value);
|
//业务类型属性
|
if(type_.equalsIgnoreCase(type)){
|
if(attName.equalsIgnoreCase("ID") || attName.equalsIgnoreCase("NAME") || attName.equalsIgnoreCase("DESCRIPTION")){
|
map.put(attName.toLowerCase(), value);
|
//上传文件
|
}else if(fileCols.contains(attName.toUpperCase())){
|
if(uniqueCols.size() > 0){
|
//文件信息在判重后再补充到对象
|
fileMap.put(attName, value);
|
continue;
|
}else{
|
//不需判重,文件信息直接在此处理
|
//文件以附件形式将对象作为外键(如Document)
|
if(attName.equalsIgnoreCase("_attach")){
|
//文件以附件形式将对象作为外键(如Document)时 , 文件的documentoid
|
String docId = cbo.getOid();
|
|
String[] paths = value.split(";");
|
for (String path : paths) {
|
File file = files.get(path);
|
String fileName = file.getName();
|
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject.setAttributeValue("documentoid", docId);
|
fileObject = operation.createNewFile(file.getAbsolutePath(), fileObject);
|
}
|
continue;
|
//文件是对象属性(如personallog)
|
}else{
|
File file = files.get(value);
|
String fileName = file.getName();
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject = operation.createNewFile(file.getAbsolutePath(),
|
fileObject);
|
value = fileObject.getOid();
|
}
|
}
|
}
|
cbo.setAttributeValue(attName.toLowerCase(), value, true);
|
//链接类型属性
|
}else{
|
clo.setAttributeValue(attName.toLowerCase(), value);
|
}
|
}
|
|
clo.setFromBTMName(f_type);
|
clo.setFromOid(f_oid);
|
clo.setFromRevisionOid(f_revisionOid);
|
clo.setFromNameOid(f_nameoid);
|
clo.setToBTMName(cbo.getBtmName());
|
clo.setToOid(cbo.getOid());
|
clo.setToRevisionOid(cbo.getRevisionid());
|
clo.setToNameOid(cbo.getNameoid());
|
|
map_.put(cbo.getOid(), map);
|
fileMap_.put(cbo.getOid(), fileMap);
|
allBoList.add(cbo);
|
allLoList.add(clo);
|
loMap.put(cbo.getOid(), clo);
|
}
|
|
if (uniqueCols.size() > 0 && allBoList.size() > 0) {
|
List<BusinessObject> queryBO = queryBO(allBoList, uniqueCols);
|
for(ClientBusinessObject cbo : allBoList){
|
BusinessObject bo = isExistBo(cbo, uniqueCols, queryBO);
|
ClientLinkObject clo = loMap.get(cbo.getOid());
|
if (bo != null) {
|
if(updateFlag == 1){
|
Map<String, String> map = map_.get(cbo.getOid());
|
if (map.get("id") != null) {
|
cbo.setAttributeValue("id", map.get("id"));
|
}
|
if (map.get("name") != null) {
|
cbo.setAttributeValue("name", map.get("name"));
|
}
|
if (map.get("description") != null) {
|
cbo.setAttributeValue("description", map.get("description"));
|
}
|
|
//处理cbo的文件信息
|
Map<String, String> fileMap = fileMap_.get(cbo.getOid());
|
for(String key : fileMap.keySet()){
|
String value = fileMap.get(key);
|
//文件以附件形式将对象作为外键(如Document)
|
if(key.equalsIgnoreCase("_attach")){
|
//文件以附件形式将对象作为外键(如Document)时 , 文件的documentoid
|
String docId = bo.oid;
|
|
// 删除对象关联的文档
|
ClientBusinessObject[] fObjs = getClientFileObjectByDocId(docId);
|
if (fObjs != null && fObjs.length > 0) {
|
getInstanceBo().batchDeleteBuinessObject(fObjs);
|
}
|
|
// 创建对象关联的文档
|
String[] paths = value.split(";");
|
for (String path : paths) {
|
File file = files.get(path);
|
String fileName = file.getName();
|
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject.setAttributeValue("documentoid", docId);
|
fileObject = operation.createNewFile(file.getAbsolutePath(), fileObject);
|
}
|
//文件是对象属性(如personallog)
|
}else{
|
File file = files.get(value);
|
String fileName = file.getName();
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject = operation.createNewFile(file.getAbsolutePath(),
|
fileObject);
|
value = fileObject.getOid();
|
cbo.setAttributeValue(key.toLowerCase(), value, true);
|
}
|
}
|
|
cbo.setOid(bo.oid);
|
cbo.setTs(bo.ts);
|
updateListBo.add(cbo);
|
}
|
// 当bo存在, lo不存在时, 不论bo是覆盖还是跳过 , 都需要创建lo
|
|
clo.setToOid(bo.oid);
|
clo.setToRevisionOid(bo.revisionid);
|
clo.setToNameOid(bo.nameoid);
|
|
} else {
|
//处理cbo的文件信息
|
Map<String, String> fileMap = fileMap_.get(cbo.getOid());
|
for(String key : fileMap.keySet()){
|
String value = fileMap.get(key);
|
//文件以附件形式将对象作为外键(如Document)
|
if(key.equalsIgnoreCase("_attach")){
|
//文件以附件形式将对象作为外键(如Document)时 , 文件的documentoid
|
String docId = cbo.getOid();
|
|
String[] paths = value.split(";");
|
for (String path : paths) {
|
File file = files.get(path);
|
String fileName = file.getName();
|
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject.setAttributeValue("documentoid", docId);
|
fileObject = operation.createNewFile(file.getAbsolutePath(), fileObject);
|
}
|
//文件是对象属性(如personallog)
|
}else{
|
File file = files.get(value);
|
String fileName = file.getName();
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject = operation.createNewFile(file.getAbsolutePath(),
|
fileObject);
|
value = fileObject.getOid();
|
cbo.setAttributeValue(key.toLowerCase(), value, true);
|
}
|
}
|
createListBo.add(cbo);
|
}
|
}
|
|
if(allLoList.size() > 0){
|
List<LinkObject> queryLO = queryLO(allLoList);
|
for(ClientLinkObject clo : allLoList){
|
LinkObject lo = isExistLo(clo, queryLO);
|
if(lo != null){
|
if (updateFlag == 1) {
|
clo.setOid(lo.oid);
|
clo.setTs(lo.ts);
|
updateListLo.add(clo);
|
}
|
}else{
|
createListLo.add(clo);
|
}
|
}
|
}
|
}else {
|
createListBo.addAll(allBoList);
|
createListLo.addAll(allLoList);
|
}
|
|
boolean flag = true;
|
if(createListBo.size() > 0 || createListLo.size() > 0){
|
flag &= getInstanceBo().batchSaveCreateBuinessObject(
|
createListBo.toArray(new ClientBusinessObject[0]), createListLo.toArray(new ClientLinkObject[0]));
|
}else{
|
flag &= true;
|
}
|
if(updateListBo.size() > 0){
|
flag &= getInstanceBo().batchUpdateBuinessObject(updateListBo.toArray(new ClientBusinessObject[0]));
|
}else{
|
flag &= true;
|
}
|
|
if(updateListLo.size() > 0){
|
flag &= getInstanceLo().batchUpdateLinkObject(updateListLo.toArray(new ClientLinkObject[0]));
|
}else{
|
flag &= true;
|
}
|
|
return flag;
|
}
|
|
/**
|
* 导入业务对象
|
* @param file
|
* @param fileName
|
* @param type
|
* @param updateFlag: 当有重复记录时: 1:覆盖; 0:跳过
|
* @param folderId : 文件夹分类ID(PLCLASSIFY)
|
* @return
|
* @throws VCIError
|
* @throws IOException
|
*/
|
public static Map<String, String> importBOWithClassifyData(File file, String fileName, String type, int updateFlag, String folderId) throws VCIError, IOException{
|
Map<String, String> infoMap = new HashMap<String, String>();
|
//压缩文件中的excel数据文件
|
InputStream dataInputStream = null;
|
Map<String, File> files = new HashMap<String, File>();
|
boolean zipFlag = false;
|
if(fileName.endsWith(".zip")){
|
zipFlag = true;
|
ZipFile zipFile = new ZipFile(file);
|
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
String tempFolder = file.getParent();
|
while(entries.hasMoreElements()){
|
ZipEntry zipEntry = entries.nextElement();
|
if(!zipEntry.isDirectory()){
|
String fileName_ = zipEntry.getName();
|
InputStream inputStream_ = zipFile.getInputStream(zipEntry);
|
if(fileName_.endsWith(".xlsx") || fileName_.endsWith(".xls")){
|
fileName = fileName_;
|
dataInputStream = inputStream_;
|
}else{
|
String filePath = tempFolder + "\\" + fileName_;
|
File tempFile = new File(filePath);
|
File folder = tempFile.getParentFile();
|
if(!folder.exists()){
|
folder.mkdirs();
|
}
|
if(!tempFile.exists()){
|
tempFile.createNewFile();
|
}
|
BufferedInputStream inBuff = new BufferedInputStream(inputStream_);
|
BufferedOutputStream outBuff = new BufferedOutputStream(new FileOutputStream(tempFile));
|
|
byte[] b = new byte[1024 * 512];
|
int len;
|
while((len = inBuff.read(b)) != -1){
|
outBuff.write(b, 0, len);
|
}
|
|
files.put(fileName_, tempFile);
|
inBuff.close();
|
outBuff.close();
|
}
|
}
|
}
|
}
|
BufferedInputStream bs;
|
if(zipFlag){
|
bs = new BufferedInputStream(dataInputStream);
|
}else{
|
bs = new BufferedInputStream(new FileInputStream(file));
|
}
|
List<SheetDataSet> sheetDataSets = ExcelDocumentUtils
|
.readExcelDocument(fileName, bs);
|
List<String[]> dataSets = sheetDataSets.get(0).getDataSet();
|
String[] dataSet = dataSets.get(0);
|
Set<String> typeSet = new HashSet<String>();
|
Map<Integer, String> typeAttMap = new HashMap<Integer, String>();
|
|
//判重列
|
List<String> uniqueCols = new ArrayList<String>();
|
//文件列
|
List<String> fileCols = new ArrayList<String>();
|
for(int i = 0; i < dataSet.length; i++){
|
String data = dataSet[i];
|
String[] data_ = data.split("\\.");
|
typeSet.add(data_[0]);
|
|
//判重列标识
|
if(data_[1].endsWith("*")){
|
uniqueCols.add(data_[1].substring(0, data_[1].indexOf("*")).toUpperCase());
|
data = data.substring(0, data.indexOf("*"));
|
//文件列标识
|
}else if(data_[1].endsWith("+")){
|
fileCols.add(data_[1].substring(0, data_[1].indexOf("+")).toUpperCase());
|
data = data.substring(0, data.indexOf("+"));
|
}
|
typeAttMap.put(i, data);
|
}
|
|
if(!typeSet.contains(type)){
|
infoMap.put(ERROR, "文件中无该类型数据");
|
return infoMap;
|
}
|
if(typeSet.size() > 1){
|
infoMap.put(ERROR, "文件中含有其他类型数据");
|
return infoMap;
|
}
|
|
boolean flag = false;
|
if(zipFlag){
|
flag = getInstance().saveBOsWithClassifyAndFile(dataSets, typeAttMap, type, uniqueCols, updateFlag, folderId, fileCols, files);
|
}else{
|
flag = getInstance().saveBOsWithClassify(dataSets, typeAttMap, type, uniqueCols, updateFlag, folderId);
|
}
|
|
//删除临时文件
|
for(Iterator<String> ite = files.keySet().iterator(); ite.hasNext();){
|
String fileName_ = ite.next();
|
File file_ = files.get(fileName_);
|
file_.delete();
|
}
|
file.delete();
|
|
if(flag){
|
infoMap.put(RESULT, TRUE);
|
}else{
|
infoMap.put(RESULT, FALSE);
|
}
|
return infoMap;
|
}
|
|
/**
|
* 保存业务对象
|
* 当对象不存在时, 创建对象;
|
* 存在时(unique), 更新对象.
|
* @param dataSet
|
* @param typeAttMap
|
* @param type
|
* @param uniqueCols : 判重列
|
* @param folderId : 文件夹分类ID(PLCLASSIFY)
|
* @return
|
* @throws PLMError
|
* @throws VciException
|
*/
|
private boolean saveBOsWithClassify(List<String[]> dataSets, Map<Integer, String> typeAttMap, String type, List<String> uniqueCols, int updateFlag, String folderId) throws VCIError {
|
List<ClientBusinessObject> createList = new ArrayList<ClientBusinessObject>();
|
List<ClientBusinessObject> updateList = new ArrayList<ClientBusinessObject>();
|
List<ClientBusinessObject> allBoList = new ArrayList<ClientBusinessObject>();
|
Map<String, Map<String, String>> map_ = new HashMap<String, Map<String,String>>();
|
ClientBusinessObject source = getInstanceBo().createBusinessObject(type);
|
for(int i = 1; i < dataSets.size(); i++){
|
ClientBusinessObject cbo = BOTool.cloneClientBusinessObject(source, true);
|
cbo.setBtmName(type);
|
String[] data = dataSets.get(i);
|
Map<String, String> map = new HashMap<String, String>();
|
for(int k = 0; k < data.length; k++){
|
String key = typeAttMap.get(k).split("\\.")[1];
|
String value = data[k];
|
|
value = checkEnum(key, value);
|
if(key.equalsIgnoreCase("ID") || key.equalsIgnoreCase("NAME") || key.equalsIgnoreCase("DESCRIPTION")){
|
map.put(key.toLowerCase(), value);
|
}
|
cbo.setAttributeValue(key.toLowerCase(), value, true);
|
}
|
cbo.setAttributeValue("PLCLASSIFY".toLowerCase(), folderId, true);
|
map_.put(cbo.getOid(), map);
|
allBoList.add(cbo);
|
}
|
|
if(uniqueCols.size() > 0 && allBoList.size() > 0){
|
List<BusinessObject> queryBO = queryBO(allBoList, uniqueCols);
|
for(ClientBusinessObject cbo : allBoList){
|
BusinessObject bo = isExistBo(cbo, uniqueCols, queryBO);
|
if (bo != null) {
|
if(updateFlag == 1){
|
Map<String, String> map = map_.get(cbo.getOid());
|
if (map.get("id") != null) {
|
cbo.setAttributeValue("id", map.get("id"));
|
}
|
if (map.get("name") != null) {
|
cbo.setAttributeValue("name", map.get("name"));
|
}
|
if (map.get("description") != null) {
|
cbo.setAttributeValue("description", map.get("description"));
|
}
|
cbo.setOid(bo.oid);
|
cbo.setTs(bo.ts);
|
updateList.add(cbo);
|
}
|
} else {
|
createList.add(cbo);
|
}
|
}
|
}else{
|
createList.addAll(allBoList);
|
}
|
|
boolean flag = false;
|
if(createList.size() > 0){
|
ClientBusinessObject[] bos = getInstanceBo().batchSaveCreateBuinessObject(createList.toArray(new ClientBusinessObject[0]));
|
if(bos.length == createList.size()){
|
flag = true;
|
}
|
}else{
|
flag = true;
|
}
|
if(updateList.size() > 0){
|
flag &= getInstanceBo().batchUpdateBuinessObject(updateList.toArray(new ClientBusinessObject[0]));
|
}else{
|
flag &= true;
|
}
|
return flag;
|
}
|
|
/**
|
* 保存业务对象
|
* 当对象不存在时, 创建对象;
|
* 存在时(unique), 更新对象.
|
* @param dataSet
|
* @param typeAttMap
|
* @param type
|
* @param uniqueCols : 判重列
|
* @param folderId : 文件夹分类ID(PLCLASSIFY)
|
* @param fileCols : 文件路径列
|
* @param files : Map<fileName, tempFile>
|
* @return
|
* @throws PLMError
|
* @throws VciException
|
*/
|
private boolean saveBOsWithClassifyAndFile(List<String[]> dataSets, Map<Integer, String> typeAttMap, String type, List<String> uniqueCols, int updateFlag, String folderId, List<String> fileCols, Map<String, File> files) throws VCIError {
|
List<ClientBusinessObject> createList = new ArrayList<ClientBusinessObject>();
|
List<ClientBusinessObject> updateList = new ArrayList<ClientBusinessObject>();
|
List<ClientBusinessObject> allBoList = new ArrayList<ClientBusinessObject>();
|
Map<String, Map<String, String>> map_ = new HashMap<String, Map<String,String>>();
|
Map<String, Map<String, String>> fileMap_ = new HashMap<String, Map<String,String>>();
|
ClientFileObjectOperation operation = new ClientFileObjectOperation();
|
for(int i = 1; i < dataSets.size(); i++){
|
ClientBusinessObject cbo = getInstanceBo().createBusinessObject(type);
|
cbo.setBtmName(type);
|
String[] data = dataSets.get(i);
|
Map<String, String> map = new HashMap<String, String>();
|
Map<String, String> fileMap = new HashMap<String, String>();
|
for(int k = 0; k < data.length; k++){
|
String key = typeAttMap.get(k).split("\\.")[1];
|
String value = data[k];
|
|
value = checkEnum(key, value);
|
if(key.equalsIgnoreCase("ID") || key.equalsIgnoreCase("NAME") || key.equalsIgnoreCase("DESCRIPTION")){
|
map.put(key.toLowerCase(), value);
|
//上传文件
|
}else if(fileCols.contains(key.toUpperCase())){
|
if(uniqueCols.size() > 0){
|
//文件信息在判重后再补充到对象
|
fileMap.put(key, value);
|
continue;
|
}else{
|
//不需判重,文件信息直接在此处理
|
//文件以附件形式将对象作为外键(如Document)
|
if(key.equalsIgnoreCase("_attach")){
|
//文件以附件形式将对象作为外键(如Document)时 , 文件的documentoid
|
String docId = cbo.getOid();
|
|
String[] paths = value.split(";");
|
for (String path : paths) {
|
File file = files.get(path);
|
String fileName = file.getName();
|
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject.setAttributeValue("documentoid", docId);
|
fileObject = operation.createNewFile(file.getAbsolutePath(), fileObject);
|
}
|
continue;
|
//文件是对象属性(如personallog)
|
}else{
|
File file = files.get(value);
|
String fileName = file.getName();
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject = operation.createNewFile(file.getAbsolutePath(),
|
fileObject);
|
value = fileObject.getOid();
|
}
|
}
|
}
|
cbo.setAttributeValue(key.toLowerCase(), value, true);
|
}
|
cbo.setAttributeValue("PLCLASSIFY".toLowerCase(), folderId, true);
|
|
map_.put(cbo.getOid(), map);
|
fileMap_.put(cbo.getOid(), fileMap);
|
allBoList.add(cbo);
|
}
|
if(uniqueCols.size() > 0 && allBoList.size() > 0){
|
List<BusinessObject> queryBO = queryBO(allBoList, uniqueCols);
|
for(ClientBusinessObject cbo : allBoList){
|
BusinessObject bo = isExistBo(cbo, uniqueCols, queryBO);
|
if (bo != null) {
|
if(updateFlag == 1){
|
Map<String, String> map = map_.get(cbo.getOid());
|
if (map.get("id") != null) {
|
cbo.setAttributeValue("id", map.get("id"));
|
}
|
if (map.get("name") != null) {
|
cbo.setAttributeValue("name", map.get("name"));
|
}
|
if (map.get("description") != null) {
|
cbo.setAttributeValue("description", map.get("description"));
|
}
|
//处理cbo的文件信息
|
Map<String, String> fileMap = fileMap_.get(cbo.getOid());
|
for(String key : fileMap.keySet()){
|
String value = fileMap.get(key);
|
//文件以附件形式将对象作为外键(如Document)
|
if(key.equalsIgnoreCase("_attach")){
|
//文件以附件形式将对象作为外键(如Document)时 , 文件的documentoid
|
String docId = bo.oid;
|
|
// 删除对象关联的文档
|
ClientBusinessObject[] fObjs = getClientFileObjectByDocId(docId);
|
if (fObjs != null && fObjs.length > 0) {
|
getInstanceBo().batchDeleteBuinessObject(fObjs);
|
}
|
|
// 创建对象关联的文档
|
String[] paths = value.split(";");
|
for (String path : paths) {
|
File file = files.get(path);
|
String fileName = file.getName();
|
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject.setAttributeValue("documentoid", docId);
|
fileObject = operation.createNewFile(file.getAbsolutePath(), fileObject);
|
}
|
//文件是对象属性(如personallog)
|
}else{
|
File file = files.get(value);
|
String fileName = file.getName();
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject = operation.createNewFile(file.getAbsolutePath(),
|
fileObject);
|
value = fileObject.getOid();
|
cbo.setAttributeValue(key.toLowerCase(), value, true);
|
}
|
}
|
cbo.setOid(bo.oid);
|
cbo.setTs(bo.ts);
|
updateList.add(cbo);
|
}
|
} else {
|
//处理cbo的文件信息
|
Map<String, String> fileMap = fileMap_.get(cbo.getOid());
|
for(String key : fileMap.keySet()){
|
String value = fileMap.get(key);
|
//文件以附件形式将对象作为外键(如Document)
|
if(key.equalsIgnoreCase("_attach")){
|
//文件以附件形式将对象作为外键(如Document)时 , 文件的documentoid
|
String docId = cbo.getOid();
|
|
String[] paths = value.split(";");
|
for (String path : paths) {
|
File file = files.get(path);
|
String fileName = file.getName();
|
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject.setAttributeValue("documentoid", docId);
|
fileObject = operation.createNewFile(file.getAbsolutePath(), fileObject);
|
}
|
//文件是对象属性(如personallog)
|
}else{
|
File file = files.get(value);
|
String fileName = file.getName();
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject = operation.createNewFile(file.getAbsolutePath(),
|
fileObject);
|
value = fileObject.getOid();
|
cbo.setAttributeValue(key.toLowerCase(), value, true);
|
}
|
}
|
createList.add(cbo);
|
}
|
}
|
}else{
|
createList.addAll(allBoList);
|
}
|
|
boolean flag = false;
|
if(createList.size() > 0){
|
ClientBusinessObject[] bos = getInstanceBo().batchSaveCreateBuinessObject(createList.toArray(new ClientBusinessObject[0]));
|
if(bos.length == createList.size()){
|
flag = true;
|
}
|
}else{
|
flag = true;
|
}
|
if(updateList.size() > 0){
|
flag &= getInstanceBo().batchUpdateBuinessObject(updateList.toArray(new ClientBusinessObject[0]));
|
}else{
|
flag &= true;
|
}
|
return flag;
|
}
|
|
/**
|
* 导入业务对象
|
* @param file
|
* @param fileName
|
* @param type
|
* @param updateFlag: 当有重复记录时: 1:覆盖; 0:跳过
|
* @return
|
* @throws VCIError
|
* @throws IOException
|
*/
|
public static Map<String, String> importBOData(File file, String fileName, String type, int updateFlag) throws VCIError, IOException{
|
Map<String, String> infoMap = new HashMap<String, String>();
|
//压缩文件中的excel数据文件
|
InputStream dataInputStream = null;
|
Map<String, File> files = new HashMap<String, File>();
|
boolean zipFlag = false;
|
if(fileName.endsWith(".zip")){
|
zipFlag = true;
|
ZipFile zipFile = new ZipFile(file);
|
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
String tempFolder = file.getParent();
|
while(entries.hasMoreElements()){
|
ZipEntry zipEntry = entries.nextElement();
|
if(!zipEntry.isDirectory()){
|
String fileName_ = zipEntry.getName();
|
InputStream inputStream_ = zipFile.getInputStream(zipEntry);
|
if(fileName_.endsWith(".xlsx") || fileName_.endsWith(".xls")){
|
fileName = fileName_;
|
dataInputStream = inputStream_;
|
}else{
|
String filePath = tempFolder + "\\" + fileName_;
|
File tempFile = new File(filePath);
|
File folder = tempFile.getParentFile();
|
if(!folder.exists()){
|
folder.mkdirs();
|
}
|
if(!tempFile.exists()){
|
tempFile.createNewFile();
|
}
|
BufferedInputStream inBuff = new BufferedInputStream(inputStream_);
|
BufferedOutputStream outBuff = new BufferedOutputStream(new FileOutputStream(tempFile));
|
|
byte[] b = new byte[1024 * 512];
|
int len;
|
while((len = inBuff.read(b)) != -1){
|
outBuff.write(b, 0, len);
|
}
|
|
files.put(fileName_, tempFile);
|
inBuff.close();
|
outBuff.close();
|
}
|
}
|
}
|
}
|
BufferedInputStream bs;
|
if(zipFlag){
|
bs = new BufferedInputStream(dataInputStream);
|
}else{
|
bs = new BufferedInputStream(new FileInputStream(file));
|
}
|
List<SheetDataSet> sheetDataSets = ExcelDocumentUtils
|
.readExcelDocument(fileName, bs);
|
List<String[]> dataSets = sheetDataSets.get(0).getDataSet();
|
String[] dataSet = dataSets.get(0);
|
Set<String> typeSet = new HashSet<String>();
|
Map<Integer, String> typeAttMap = new HashMap<Integer, String>();
|
|
//判重列
|
List<String> uniqueCols = new ArrayList<String>();
|
//文件列
|
List<String> fileCols = new ArrayList<String>();
|
for(int i = 0; i < dataSet.length; i++){
|
String data = dataSet[i];
|
String[] data_ = data.split("\\.");
|
typeSet.add(data_[0]);
|
|
//判重列标识
|
if(data_[1].endsWith("*")){
|
uniqueCols.add(data_[1].substring(0, data_[1].indexOf("*")).toUpperCase());
|
data = data.substring(0, data.indexOf("*"));
|
//文件列标识
|
}else if(data_[1].endsWith("+")){
|
fileCols.add(data_[1].substring(0, data_[1].indexOf("+")).toUpperCase());
|
data = data.substring(0, data.indexOf("+"));
|
}
|
typeAttMap.put(i, data);
|
}
|
|
if(!typeSet.contains(type)){
|
infoMap.put(ERROR, "文件中无该类型数据");
|
return infoMap;
|
}
|
if(typeSet.size() > 1){
|
infoMap.put(ERROR, "文件中含有其他类型数据");
|
return infoMap;
|
}
|
|
boolean flag = false;
|
if(zipFlag){
|
flag = getInstance().saveBOsWithFile(dataSets, typeAttMap, type, uniqueCols, updateFlag, fileCols, files);
|
}else{
|
flag = getInstance().saveBOs(dataSets, typeAttMap, type, uniqueCols, updateFlag);
|
}
|
|
//删除临时文件
|
for(Iterator<String> ite = files.keySet().iterator(); ite.hasNext();){
|
String fileName_ = ite.next();
|
File file_ = files.get(fileName_);
|
file_.delete();
|
}
|
file.delete();
|
|
if(flag){
|
infoMap.put(RESULT, TRUE);
|
}else{
|
infoMap.put(RESULT, FALSE);
|
}
|
return infoMap;
|
}
|
|
/**
|
* 保存业务对象
|
* 当对象不存在时, 创建对象;
|
* 存在时(unique), 更新对象.
|
* @param dataSet
|
* @param typeAttMap
|
* @param type
|
* @param uniqueCols : 判重列
|
* @return
|
* @throws PLMError
|
* @throws VciException
|
*/
|
private boolean saveBOs(List<String[]> dataSets, Map<Integer, String> typeAttMap, String type, List<String> uniqueCols, int updateFlag) throws VCIError {
|
List<ClientBusinessObject> createList = new ArrayList<ClientBusinessObject>();
|
List<ClientBusinessObject> updateList = new ArrayList<ClientBusinessObject>();
|
List<ClientBusinessObject> allBoList = new ArrayList<ClientBusinessObject>();
|
Map<String, Map<String, String>> map_ = new HashMap<String, Map<String,String>>();
|
ClientBusinessObject source = getInstanceBo().createBusinessObject(type);
|
for(int i = 1; i < dataSets.size(); i++){
|
ClientBusinessObject cbo = BOTool.cloneClientBusinessObject(source, true);
|
String[] data = dataSets.get(i);
|
Map<String, String> map = new HashMap<String, String>();
|
for(int k = 0; k < data.length; k++){
|
String key = typeAttMap.get(k).split("\\.")[1];
|
String value = data[k];
|
|
value = checkEnum(key, value);
|
if(key.equalsIgnoreCase("ID") || key.equalsIgnoreCase("NAME") || key.equalsIgnoreCase("DESCRIPTION")){
|
map.put(key.toLowerCase(), value);
|
}
|
cbo.setAttributeValue(key.toLowerCase(), value, true);
|
}
|
map_.put(cbo.getOid(), map);
|
allBoList.add(cbo);
|
}
|
|
if(uniqueCols.size() > 0 && allBoList.size() > 0){
|
List<BusinessObject> queryBO = queryBO(allBoList, uniqueCols);
|
for(ClientBusinessObject cbo : allBoList){
|
BusinessObject bo = isExistBo(cbo, uniqueCols, queryBO);
|
if (bo != null) {
|
if(updateFlag == 1){
|
Map<String, String> map = map_.get(cbo.getOid());
|
if (map.get("id") != null) {
|
cbo.setAttributeValue("id", map.get("id"));
|
}
|
if (map.get("name") != null) {
|
cbo.setAttributeValue("name", map.get("name"));
|
}
|
if (map.get("description") != null) {
|
cbo.setAttributeValue("description", map.get("description"));
|
}
|
cbo.setOid(bo.oid);
|
cbo.setTs(bo.ts);
|
updateList.add(cbo);
|
}
|
} else {
|
createList.add(cbo);
|
}
|
}
|
}else{
|
createList.addAll(allBoList);
|
}
|
boolean flag = false;
|
if(createList.size() > 0){
|
ClientBusinessObject[] bos = getInstanceBo().batchSaveCreateBuinessObject(createList.toArray(new ClientBusinessObject[0]));
|
if(bos.length == createList.size()){
|
flag = true;
|
}
|
}else{
|
flag = true;
|
}
|
if(updateList.size() > 0){
|
flag &= getInstanceBo().batchUpdateBuinessObject(updateList.toArray(new ClientBusinessObject[0]));
|
}else{
|
flag &= true;
|
}
|
return flag;
|
}
|
|
/**
|
* 保存业务对象
|
* 当对象不存在时, 创建对象;
|
* 存在时(unique), 更新对象.
|
* @param dataSet
|
* @param typeAttMap
|
* @param type
|
* @param uniqueCols : 判重列
|
* @param updateFlag
|
* @param fileCols : 文件路径列
|
* @param files : Map<fileName, tempFile>
|
* @return
|
* @throws PLMError
|
* @throws VciException
|
*/
|
private boolean saveBOsWithFile(List<String[]> dataSets, Map<Integer, String> typeAttMap, String type, List<String> uniqueCols, int updateFlag, List<String> fileCols, Map<String, File> files) throws VCIError {
|
List<ClientBusinessObject> createList = new ArrayList<ClientBusinessObject>();
|
List<ClientBusinessObject> updateList = new ArrayList<ClientBusinessObject>();
|
List<ClientBusinessObject> allBoList = new ArrayList<ClientBusinessObject>();
|
Map<String, Map<String, String>> map_ = new HashMap<String, Map<String,String>>();
|
Map<String, Map<String, String>> fileMap_ = new HashMap<String, Map<String,String>>();
|
ClientBusinessObject source = getInstanceBo().createBusinessObject(type);
|
ClientFileObjectOperation operation = new ClientFileObjectOperation();
|
for(int i = 1; i < dataSets.size(); i++){
|
ClientBusinessObject cbo = BOTool.cloneClientBusinessObject(source, true);
|
String[] data = dataSets.get(i);
|
Map<String, String> map = new HashMap<String, String>();
|
Map<String, String> fileMap = new HashMap<String, String>();
|
for(int k = 0; k < data.length; k++){
|
String key = typeAttMap.get(k).split("\\.")[1];
|
String value = data[k];
|
|
value = checkEnum(key, value);
|
if(key.equalsIgnoreCase("ID") || key.equalsIgnoreCase("NAME") || key.equalsIgnoreCase("DESCRIPTION")){
|
map.put(key.toLowerCase(), value);
|
//上传文件
|
}else if(fileCols.contains(key.toUpperCase())){
|
if(uniqueCols.size() > 0){
|
//文件信息在判重后再补充到对象
|
fileMap.put(key, value);
|
continue;
|
}else{
|
//不需判重,文件信息直接在此处理
|
//文件以附件形式将对象作为外键(如Document)
|
if(key.equalsIgnoreCase("_attach")){
|
//文件以附件形式将对象作为外键(如Document)时 , 文件的documentoid
|
String docId = cbo.getOid();
|
|
String[] paths = value.split(";");
|
for (String path : paths) {
|
File file = files.get(path);
|
String fileName = file.getName();
|
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject.setAttributeValue("documentoid", docId);
|
fileObject = operation.createNewFile(file.getAbsolutePath(), fileObject);
|
}
|
continue;
|
//文件是对象属性(如personallog)
|
}else{
|
File file = files.get(value);
|
String fileName = file.getName();
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject = operation.createNewFile(file.getAbsolutePath(),
|
fileObject);
|
value = fileObject.getOid();
|
}
|
}
|
}
|
cbo.setAttributeValue(key.toLowerCase(), value, true);
|
}
|
map_.put(cbo.getOid(), map);
|
fileMap_.put(cbo.getOid(), fileMap);
|
allBoList.add(cbo);
|
}
|
if(uniqueCols.size() > 0 && allBoList.size() > 0){
|
List<BusinessObject> queryBO = queryBO(allBoList, uniqueCols);
|
for(ClientBusinessObject cbo : allBoList){
|
BusinessObject bo = isExistBo(cbo, uniqueCols, queryBO);
|
if (bo != null) {
|
if(updateFlag == 1){
|
Map<String, String> map = map_.get(cbo.getOid());
|
if (map.get("id") != null) {
|
cbo.setAttributeValue("id", map.get("id"));
|
}
|
if (map.get("name") != null) {
|
cbo.setAttributeValue("name", map.get("name"));
|
}
|
if (map.get("description") != null) {
|
cbo.setAttributeValue("description", map.get("description"));
|
}
|
//处理cbo的文件信息
|
Map<String, String> fileMap = fileMap_.get(cbo.getOid());
|
for(String key : fileMap.keySet()){
|
String value = fileMap.get(key);
|
//文件以附件形式将对象作为外键(如Document)
|
if(key.equalsIgnoreCase("_attach")){
|
//文件以附件形式将对象作为外键(如Document)时 , 文件的documentoid
|
String docId = bo.oid;
|
|
// 删除对象关联的文档
|
ClientBusinessObject[] fObjs = getClientFileObjectByDocId(docId);
|
if (fObjs != null && fObjs.length > 0) {
|
getInstanceBo().batchDeleteBuinessObject(fObjs);
|
}
|
|
// 创建对象关联的文档
|
String[] paths = value.split(";");
|
for (String path : paths) {
|
File file = files.get(path);
|
String fileName = file.getName();
|
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject.setAttributeValue("documentoid", docId);
|
fileObject = operation.createNewFile(file.getAbsolutePath(), fileObject);
|
}
|
//文件是对象属性(如personallog)
|
}else{
|
File file = files.get(value);
|
String fileName = file.getName();
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject = operation.createNewFile(file.getAbsolutePath(),
|
fileObject);
|
value = fileObject.getOid();
|
cbo.setAttributeValue(key.toLowerCase(), value, true);
|
}
|
}
|
cbo.setOid(bo.oid);
|
cbo.setTs(bo.ts);
|
updateList.add(cbo);
|
}
|
} else {
|
//处理cbo的文件信息
|
Map<String, String> fileMap = fileMap_.get(cbo.getOid());
|
for(String key : fileMap.keySet()){
|
String value = fileMap.get(key);
|
//文件以附件形式将对象作为外键(如Document)
|
if(key.equalsIgnoreCase("_attach")){
|
//文件以附件形式将对象作为外键(如Document)时 , 文件的documentoid
|
String docId = cbo.getOid();
|
|
String[] paths = value.split(";");
|
for (String path : paths) {
|
File file = files.get(path);
|
String fileName = file.getName();
|
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject.setAttributeValue("documentoid", docId);
|
fileObject = operation.createNewFile(file.getAbsolutePath(), fileObject);
|
}
|
//文件是对象属性(如personallog)
|
}else{
|
File file = files.get(value);
|
String fileName = file.getName();
|
FileObject fileObject = operation.createNewFile();
|
fileObject.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
|
fileObject.setRevisionLimit(7);
|
fileObject.setName(fileName);
|
fileObject = operation.createNewFile(file.getAbsolutePath(),
|
fileObject);
|
value = fileObject.getOid();
|
cbo.setAttributeValue(key.toLowerCase(), value, true);
|
}
|
}
|
createList.add(cbo);
|
}
|
}
|
}else{
|
createList.addAll(allBoList);
|
}
|
boolean flag = false;
|
if(createList.size() > 0){
|
ClientBusinessObject[] bos = getInstanceBo().batchSaveCreateBuinessObject(createList.toArray(new ClientBusinessObject[0]));
|
if(bos.length == createList.size()){
|
flag = true;
|
}
|
}else{
|
flag = true;
|
}
|
if(updateList.size() > 0){
|
flag &= getInstanceBo().batchUpdateBuinessObject(updateList.toArray(new ClientBusinessObject[0]));
|
}else{
|
flag &= true;
|
}
|
return flag;
|
}
|
|
/**
|
* 分段查询出满足判重条件(F_OID, T_OID)的LO
|
* @param allLoList
|
* @return
|
*/
|
private List<LinkObject> queryLO(List<ClientLinkObject> allLoList) {
|
List<LinkObject> queryLoList = new ArrayList<LinkObject>();
|
QueryTemplate qt = new QueryTemplate();
|
qt.setId("id");
|
qt.setType(QTConstants.TYPE_LINK);
|
List<String> clauseList = new ArrayList<String>();
|
clauseList.add("*");
|
qt.setClauseList(clauseList);
|
ClientLinkObject clo = allLoList.get(0);
|
qt.setLinkType(clo.getLoName());
|
|
int size = allLoList.size();
|
|
for(int k = 0; k < size/PARTIONSIZE + 1; k++){
|
Condition cond = new Condition();
|
for(int m = k * PARTIONSIZE; m < (k + 1) * PARTIONSIZE && m < size; m++){
|
ClientLinkObject clo_ = allLoList.get(m);
|
Map<String, String> map_ = new HashMap<String, String>();
|
map_.put("f_oid", clo_.getFromOid());
|
map_.put("t_oid", clo_.getToOid());
|
Condition cond_ = OQTool.getCondition(map_);
|
|
cond = OQTool.mergeCondition(cond, cond_, Connector.OR);
|
}
|
qt.setCondition(cond);
|
|
try {
|
LinkObject[] los = QTClient.getService().findLTObjects(qt.getId(), OQTool.getQTTextByQT(qt));
|
queryLoList.addAll(LOTool.ArrayTOList(los));
|
} catch (VCIError e) {
|
e.printStackTrace();
|
}
|
}
|
return queryLoList;
|
}
|
|
/**
|
* 根据f_oid , t_oid在queryLO中判断对象是否已经存在
|
* @param clo
|
* @param queryLO
|
* @return
|
*/
|
private LinkObject isExistLo(ClientLinkObject clo, List<LinkObject> queryLO) {
|
for(LinkObject lo : queryLO){
|
if(lo.fromOid.equals(clo.getFromOid())
|
&& lo.toOid.equals(clo.getToOid())){
|
return lo;
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 分段查询出满足判重条件的BO
|
* @param allBoList
|
* @param uniqueCols
|
* @return
|
*/
|
private List<BusinessObject> queryBO(
|
List<ClientBusinessObject> allBoList, List<String> uniqueCols) {
|
List<BusinessObject> queryBoList = new ArrayList<BusinessObject>();
|
int size = allBoList.size();
|
String type = allBoList.get(0).getBtmName();
|
QueryTemplate qt = new QueryTemplate();
|
qt.setId("qt");
|
qt.setBtmType(type);
|
List<String> clauseList = new ArrayList<String>();
|
clauseList.add("*");
|
qt.setClauseList(clauseList);
|
qt.setType(QTConstants.TYPE_BTM);
|
|
for(int k = 0; k < size/PARTIONSIZE + 1; k++){
|
Condition cond = new Condition();
|
for(int m = k * PARTIONSIZE; m < (k + 1) * PARTIONSIZE && m < size; m++){
|
ClientBusinessObject cbo = allBoList.get(m);
|
Map<String, String> map = new HashMap<String, String>();
|
for(String uniqueCol : uniqueCols){
|
if(uniqueCol.equalsIgnoreCase("ID")){
|
map.put("id", cbo.getId());
|
continue;
|
}
|
if(uniqueCol.equalsIgnoreCase("NAME")){
|
map.put("name", cbo.getName());
|
continue;
|
}
|
if(uniqueCol.equalsIgnoreCase("DESCRIPTION")){
|
map.put("description", cbo.getDescription());
|
continue;
|
}
|
String attributeValue = cbo.getAttributeValue(uniqueCol);
|
if(attributeValue != null && !attributeValue.equals("")){
|
map.put(uniqueCol, attributeValue);
|
}
|
}
|
Condition cond_ = OQTool.getCondition(map);
|
cond = OQTool.mergeCondition(cond, cond_, Connector.OR);
|
}
|
qt.setCondition(cond);
|
BusinessObject[] bos = null;
|
try {
|
bos = QTClient.getService().findBTMObjects(qt.getId(), OQTool.getQTTextByQT(qt));
|
queryBoList.addAll(BOTool.ArrayTOList(bos));
|
} catch (VCIError e) {
|
e.printStackTrace();
|
}
|
}
|
return queryBoList;
|
}
|
|
/**
|
* 根据uniqueCols在boList中判断cbo对象是否已经存在
|
* 处理系统属性:id/name/description (因仅此三个系统属性可用户设置)
|
* 处理自定义属性:cbo.getAttributeValue(attrName)
|
* @param uniqueCols
|
* @param id
|
* @param type
|
* @return
|
* @throws VciException
|
* @throws PLMError
|
*/
|
private BusinessObject isExistBo(ClientBusinessObject cbo, List<String> uniqueCols, List<BusinessObject> boList){
|
for(BusinessObject bo : boList){
|
boolean flag = true;
|
for(String uniqueCol : uniqueCols){
|
if(uniqueCol.equalsIgnoreCase("ID")){
|
if(!cbo.getId().equals(bo.id)){
|
flag = false;
|
break;
|
}
|
}else if(uniqueCol.equalsIgnoreCase("NAME")){
|
if(!cbo.getName().equals(bo.name)){
|
flag = false;
|
break;
|
}
|
}else if(uniqueCol.equalsIgnoreCase("DESCRIPTION")){
|
if(!cbo.getDescription().equals(bo.description)){
|
flag = false;
|
break;
|
}
|
}else{
|
String attributeValue = cbo.getAttributeValue(uniqueCol);
|
if (attributeValue != null && !attributeValue.equals("")) {
|
String boAttVal = BOTool.getBOAttVal(bo, uniqueCol);
|
if (!attributeValue.equals(boAttVal)) {
|
flag = false;
|
break;
|
}
|
}
|
}
|
}
|
if(flag){
|
return bo;
|
}
|
}
|
return null;
|
}
|
/**
|
* 若属性使用枚举, 将得到的枚举名转换为对应的枚举值
|
* @param key
|
* @param value
|
* @return
|
*/
|
private String checkEnum(String key, String value) {
|
AttribItem att = ApProvider.getInstance().getAbItemByName(key.toLowerCase());
|
String enumName = OQTool.getOtherValueByType(att.other, AttributeConstants.ENUMNAME);
|
if(enumName == null || enumName.equals("")){
|
return value;
|
}
|
EnumItem enumItem = EnumProvider.getInstance().getEnumByName(enumName);
|
EnumChild[] children = enumItem.children;
|
for(int i = 0; i < children.length; i++){
|
EnumChild child = children[i];
|
if(child.name.equalsIgnoreCase(value)){
|
return child.value;
|
}
|
}
|
return value;
|
}
|
|
/**
|
* 查询对象关联的文档
|
* @param docId
|
* @return
|
*/
|
private BusinessObject[] getFileObjectByDocId(String docId){
|
QueryTemplate qt = new QueryTemplate();
|
qt.setId("id");
|
qt.setType(QTConstants.TYPE_BTM);
|
qt.setBtmType(FileTypeConstants.FILE_DATA_TABLE);
|
List<String> clauseList = new ArrayList<String>();
|
clauseList.add("*");
|
qt.setClauseList(clauseList);
|
Map<String, String> arg0 = new HashMap<String, String>();
|
arg0.put("documentoid", docId);
|
Condition condition = OQTool.getCondition(arg0 );
|
qt.setCondition(condition);
|
try {
|
return QTClient.getService().findBTMObjects(qt.getId(), OQTool.qtTOXMl(qt).asXML());
|
} catch (VCIError e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 查询对象关联的文档
|
* @param docId
|
* @return
|
*/
|
private ClientBusinessObject[] getClientFileObjectByDocId(String docId){
|
List<ClientBusinessObject> cbos = new ArrayList<ClientBusinessObject>();
|
BusinessObject[] bos = getFileObjectByDocId(docId);
|
if(bos != null && bos.length > 0){
|
for(BusinessObject bo : bos){
|
ClientBusinessObject cbo = new ClientBusinessObject();
|
cbo.setBusinessObject(bo);
|
cbos.add(cbo);
|
}
|
}
|
return cbos.toArray(new ClientBusinessObject[0]);
|
}
|
}
|