You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
13 KiB
Java

package com.engine.ld.optimize;
import com.engine.core.exception.ECException;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import weaver.conn.RecordSet;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
/**
* @author ZhangLei
* @date 2021/6/24
*
*/
public class Khzsj {
private static final BaseBean bb = new BaseBean();
//新建
public String create(String requestid,String tableName) throws ParserConfigurationException, UnsupportedEncodingException {
// String workflowId = properties.getProperty("KhzsjworkflowId");
Map<String, Object> requestdMap = new HashMap<>();
String requestName = "客户主数据创建流程";
bb.writeLog("客户主数据创建流程");
// params数组存储数据库中的表字段名
String[] groupParams = {"KTOKD","KUNNR","TITLE","NAME1","NAME2","NAME3","NAME4","SORT1",
"SORT2","STRAS","HOUSE_NUM1","POST_CODE1","CITY1","COUNTRY","REGION","TIME_ZONE",
"LANGU","TELF1","TELF2","FAX_NUMBER","SMTP_ADDR","LIFNR","BRSCH","BRAN1","STCEG",
"BANKS","BANKN","BANKL","IBAN","KUKLA","ZFLAG","LOEVMJT","BANKA"};
// 注意
// LOEVM LOEVMJT
String[] companyParams = {"KUNNRGSDM","BUKRS","AKONT","ZUAWA","ZTERM","TOGRU","ZWELS",
"LOEVMGSDM","ZFLAGGSDM"};
// 注意
// KUNNR KUNNRGSDM
// LOEVM LOEVMGSDM
// ZFLAG ZFLAGGSDM
String[] saleParams = {"KUNNRXSZZ","VKORG","VTWEG","SPART","BZIRK","VKBUR","VKGRP","KDGRP",
"KONDA","KALKS","PLTYP","VERSG","LPRIO","VSBED","VWERK","KZAZU","ANTLF","WAERS",
"ZTERMXSZZ","INCO1","INCO2","KTGRD","ALAND","TATYP","TAXKD","ZKTONR_BP","ZKTONR_PY",
"ZKTONR_SH","LOEVMXSZZ","ZFLAGXSZZ"};
// 注意
// KUNNRXSZZ KUNNR
// ZTERMXSZZ ZTERM
// LOEVMXSZZ LOEVM
// ZFLAGXSZZ ZFLAG
// 查询
// 查询GROUP子节点所需要的数据
Map<String, Object> selectGroup = select(groupParams, tableName, requestid);
// 查询Company子节点所需要的数据
Map<String, Object> selectCompany = select(companyParams, tableName, requestid);
// 查询sale自己的点所需要的数据
Map<String, Object> selectSale = select(saleParams, tableName, requestid);
// 组装XML并返回
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element root = document.createElement("ROOT");
document.appendChild(root);
// 创建GROUP节点以及子节点并赋值
Element group = document.createElement("GROUP");
for (String groupParam : groupParams) {
if ("LOEVMJT".equals(groupParam)){
appendChild(document,group,"LOEVM", Util.null2String(selectGroup.get(groupParam).toString()));
}else if ("LANGU".equals(groupParam)){
appendChild(document,group,"LANGU","ZH".equals(selectGroup.get(groupParam).toString())?"1":"2");
}else if ("BRAN1".equals(groupParam)){
appendChild(document,group,"BRAN1","");
}else {
appendChild(document,group,groupParam, Util.null2String(selectGroup.get(groupParam).toString()));
}
}
root.appendChild(group);
// 创建COMPANY节点以及子节点并赋值
Element company = document.createElement("COMPANY");
for (String companyParam : companyParams) {
if ("KUNNRGSDM".equals(companyParam)){
appendChild(document,company,"KUNNR", Util.null2String(selectCompany.get(companyParam).toString()));
// appendChild(document,company,"KUNNR","");
}else if ("LOEVMGSDM".equals(companyParam)){
appendChild(document,company,"LOEVM", Util.null2String(selectCompany.get(companyParam).toString()));
}else if ("ZFLAGGSDM".equals(companyParam)){
appendChild(document,company,"ZFLAG", Util.null2String(selectCompany.get(companyParam).toString()));
}else {
appendChild(document,company,companyParam, Util.null2String(selectCompany.get(companyParam).toString()));
}
}
root.appendChild(company);
// 创建SALE节点以及子节点并赋值
Element sale = document.createElement("SALE");
for (String saleParam : saleParams) {
if ("KUNNRXSZZ".equals(saleParam)){
appendChild(document,sale,"KUNNR", Util.null2String(selectSale.get(saleParam).toString()));
// appendChild(document,sale,"KUNNR","");
}else if ("ZTERMXSZZ".equals(saleParam)){
appendChild(document,sale,"ZTERM", Util.null2String(selectSale.get(saleParam).toString()));
}else if ("LOEVMXSZZ".equals(saleParam)){
appendChild(document,sale,"LOEVM", Util.null2String(selectSale.get(saleParam).toString()));
}else if ("ZFLAGXSZZ".equals(saleParam)){
appendChild(document,sale,"ZFLAG", Util.null2String(selectSale.get(saleParam).toString()));
}else {
appendChild(document,sale,saleParam, Util.null2String(selectSale.get(saleParam).toString()));
}
}
root.appendChild(sale);
bb.writeLog("客户主数据创建流程requestid:"+requestid);
bb.writeLog("result: " + createXmlToString(document));
return createXmlToString(document);
}
//修改
public void updateWorkflow(String requestid, String xmlResult,String tableName){
bb.writeLog("请求返回的xmlResult" + xmlResult);
String updateSql = "update "+tableName+" set TYPE = ?, MESSAGE = ? where requestid = ?";
if(xmlResult == null || "".equals(xmlResult) || requestid == null || "".equals(requestid)) {
throw new ECException("参数不全");
}
xmlResult = trimXmlStr(xmlResult);
Document document;
try {
document = strToDocument(xmlResult);
} catch(Exception e) {
bb.writeLog("XML解析失败: " + e.getMessage());
bb.writeLog("xmlResult: " + xmlResult);
throw new ECException("XML解析失败: " + e.getMessage());
}
Element root = document.getDocumentElement();
NodeList typeNodeList = root.getElementsByTagName("TYPE");
NodeList messageNodeList = root.getElementsByTagName("MESSAGE");
if(typeNodeList.getLength() > 0 && messageNodeList.getLength() > 0) {
String type = Util.null2String(typeNodeList.item(0).getTextContent());
String message = Util.null2String(messageNodeList.item(0).getTextContent());
bb.writeLog("type: " + type);
bb.writeLog("message : " + message);
RecordSet rs = new RecordSet();
rs.executeUpdate(updateSql, type, message, requestid);
} else {
bb.writeLog("未获取到正常返回数据");
bb.writeLog("xmlResult: " + xmlResult);
throw new ECException("未获取到正常返回数据");
}
}
/*
*
* 1String[]:
* 2
* 3where(requestId)
* Map<String,Object>:
* xml
* */
private static Map<String,Object> select(String[] params, String tableName, String selectWhere){
Map<String,Object> resultMap = new HashMap<>();
// 组装xml
StringBuilder sql = new StringBuilder("SELECT ");
for (int i = 0; i < params.length; i++) {
if (i == params.length-1){
sql.append(params[i]).append(" from ");
}else if ("BRAN1".equals(params[i])){
sql.append("");
}else {
sql.append(params[i]).append(',');
}
}
sql.append(tableName).append(" WHERE REQUESTID = ").append(selectWhere);
// 查询
RecordSet rs = new RecordSet();
rs.executeQuery(sql.toString());
if (rs.next()){
for (String param : params) {
resultMap.put(param, Util.null2String(rs.getString(param)));
}
}
return resultMap;
}
/**
* xml
*/
private static String xmlHeadStr() {
return "<asx:abap version=\"1.0\" xmlns:asx=\"http://www.sap.com/abapxml\">\n" +
"\t<asx:values>\n" +
" <_--5CTYPE_--3DSTRING>\n";
}
/**
* xml
*/
private static String xmlTailStr() {
return " </_--5CTYPE_--3DSTRING>\n" +
"\t</asx:values>\n" +
"</asx:abap>";
}
/**
* xml
* @param document Document
* @return xml
*/
private static String createXmlToString(Document document){
String xmlString = null;
try {
// 创建TransformerFactory工厂对象
TransformerFactory transFactory = TransformerFactory.newInstance();
// 通过工厂对象, 创建Transformer对象
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "GBK");
//使Xml自动换行, 并自动缩进
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //中间的参数网址固定写法(这里还没搞懂)
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //是否设置缩进indent: yes|no
// 创建DOMSource对象并将Document加载到其中
DOMSource domSource = new DOMSource(document);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// 使用Transformer的transform()方法将DOM树转换成XML
transformer.transform(domSource, new StreamResult(bos));
xmlString = bos.toString();
} catch (TransformerException e) {
e.printStackTrace();
}
return xmlString;
}
/**
* document
* @param document ducument
* @param parent
* @param key
* @param value content
*/
private static void appendChild(Document document, Element parent, String key, String value) {
Element child = document.createElement(key);
if(child != null) {
child.setTextContent(value);
}
parent.appendChild(child);
}
/**
* <_--5CTYPE_--3DSTRING></_--5CTYPE_--3DSTRING>
* @param xmlString xml
* @return xml
*/
private static String trimXmlStr(String xmlString) {
if(!xmlString.contains("<_--5CTYPE_--3DSTRING>")) {
return xmlString;
}
int start = xmlString.indexOf("<_--5CTYPE_--3DSTRING>") + "<_--5CTYPE_--3DSTRING>".length();
int end = xmlString.indexOf("</_--5CTYPE_--3DSTRING>");
return xmlString.substring(start, end).trim();
}
/**
* xmldom
* @param xmlString xml
* @return dom
* @throws ParserConfigurationException document
* @throws IOException IO
* @throws SAXException
*/
private static Document strToDocument(String xmlString) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
return documentBuilder.parse(
new InputSource(new StringReader(xmlString)));
}
}