項目筆記:導出XML和導出全部XML功能
阿新 • • 發佈:2017-11-16
beanutils xml文件 close exc esc cnblogs word else swa
前臺代碼:
//導出一條Xml
function btn_createXml(){
var title =$("#editButton").attr("title") ;
var keyValue = $("#gridTable").jqGridRowValue("id");
if (checkedArray(keyValue)) {
var newAction =‘/log/windowsSafetyBaseLineLogAction_createXml.do?ids=‘+keyValue+‘&jsonStr=‘+jsonStr;
createFun( newAction );
}
}
//導出全部xml
function btn_createXmlAll(){
var newAction = ‘/log/windowsSafetyBaseLineLogAction_createXmlAll.do?jsonStr=‘+jsonStr;
createFun( newAction );
}
function createFun( newAction ){
var ids=$("#gridTable").getDataIDs();
if(true){
var $form = $("#queryListUI_form") ;
var action = "";
try {
action = $form.attr("action");
$form.attr("action","${basePath}"+newAction).submit();
}finally{
$form.attr("action",action);
}
}
}
後臺代碼:
//導出全部XML
private String downAll;
public void createXmlAll() {
downAll = "1";
createXml();
}
//導出xml功能
public void createXml(){
String ESswitch = getSession().getAttribute("ES").toString();
if("1".equals(ESswitch)){
//targetEScreateXml();
}else{
targetcreateXml();
}
}
public void targetcreateXml(){
log.info("導出Xml開始-BEGIN");
Page<WindowsSafetyBaseLineLog> page = null;
List<WindowsSafetyBaseLineLog> dataList = null;
// 構建路徑
String downLoadPath = "/WEB-INF/download/log/";
String rootPath = getServletContext().getRealPath(downLoadPath);
try {
filterQuery();
@SuppressWarnings("unchecked")
Map<String, List<String>> mgrOrgMap = (Map<String, List<String>>) getSession().getAttribute(OrganizationUtil.ORG_MGR);
//導出全部XML
if (downAll != null && downAll.equals("1")) {
if(OrganizationUtil.isNullMgrOrg(mgrOrgMap)){
dataList = new ArrayList<WindowsSafetyBaseLineLog>();
}else{
queryBean.setSession(getSession());
page = windowsSafetyBaseLineLogService.query(queryBean, 1, getPageSize());
dataList = page.getDataList();
dataList = windowsSafetyBaseLineLogService.queryAll();
dataList= getAllOrgDataList(dataList);
}
} else { //導出單個XML
if (null != getRequest().getParameter("ids")) {
dataList = new ArrayList<WindowsSafetyBaseLineLog>();
String ids[] = getRequest().getParameter("ids").split(",");
for (int j = 0; j < ids.length; j++) {
WindowsSafetyBaseLineLog windowsSafetyBaseLineLog = windowsSafetyBaseLineLogService.queryById(ids[j]);
if (null != windowsSafetyBaseLineLog) {
dataList.add(windowsSafetyBaseLineLog);
dataList= getAllOrgDataList(dataList); //獲取組織機構的方法
}
}
}
}
JsonConfigEx jsonConfigEx = new JsonConfigEx();
String json = JsonUtils.collection2Json(dataList,jsonConfigEx);
JSONArray jsonArr=JSONArray.fromObject(json);
addWordByJson(jsonArr);
Document document = createDocumentByJson(jsonArr);
String targetfile = rootPath + System.getProperty("file.separator") + "windowsSafetyBaseLineLog" + ".xml";
FileOutputStream fw = new FileOutputStream(targetfile);
//設置文件編碼
OutputFormat xmlFormat = new OutputFormat();
xmlFormat.setEncoding("UTF-8");
// 設置換行
xmlFormat.setNewlines(true);
// 生成縮進
xmlFormat.setIndent(true);
// 使用4個空格進行縮進, 可以兼容文本編輯器
xmlFormat.setIndent(" ");
XMLWriter xmlWriter = new XMLWriter(fw,xmlFormat);
//寫入文件
xmlWriter.write(document);
//關閉
xmlWriter.close();
String fileName="windowsSafetyBaseLineLog";
BaseActionUtill u=new BaseActionUtill();
u.downLoadFile(fileName, downLoadPath);
/* ServletActionContext.getRequest().setAttribute("downLoadPath",
downLoadPath);
ServletActionContext.getRequest().setAttribute("fileName",
"deviceOpenLog" + ".xml");*/
this.msg = RESULT_SUCCESS;
log.error("導出XML"+this.msg);
} catch (Exception e) {
e.printStackTrace();
log.error("導出XML失敗");
}finally{
managerOperaterLogService.save("cems_createXML" ,new String[]{ "windowsSafetyBaseLineLog.xml"} );
}
log.info("導出Xml開始-END");
}
其他需要用到的方法:公共配置項,創建xml文件,並寫xml文件
public JSONArray addWordByJson(JSONArray json){
JSONArray jsonArrN=new JSONArray();
LogConfig logConfig =logConfigService.queryByTableName(tableName);//用於其他的一些公共的配置項寫入數據
if(json.size()>0){
for (int i = 0; i < json.size(); i++) {
JSONObject jsonObj=json.getJSONObject(i);
if(logConfig!=null){
jsonObj.put("eventType", logConfig.getType().toString());
jsonObj.put("productType", logConfig.getProductType());
int type=logConfig.getRiskLevel();
RiskLevel riskLevel=riskLevelService.queryRiskLevelByType(type);
if(riskLevel!=null&& StringUtils.isNotBlank(riskLevel.getName())){
log.info("寫入riskLevel一列-BEGIN");
String riskLevelName=MessageUtils.getMessage(riskLevel.getName());
jsonObj.put("riskLevel", riskLevelName);
log.info("寫入riskLevel一列-END");
}
if(logConfig.getBehaviourType()!=null){
log.info("寫入behaviourType一列-BEGIN");
int Type =logConfig.getBehaviourType();
String behaviourType= MessageUtils.getMessage(BehaviourType.getByState(Type).getResourceKey());
jsonObj.put("behaviourType", behaviourType);
log.info("寫入behaviourType一列-END");
}
User user= userService.queryByAccount(jsonObj.getString("userAccount")) ;
if(user!=null){
String name= user.getName();
jsonObj.put("userName", name!=null?name:null);
}
jsonArrN.add(jsonObj);
}else{
jsonObj.put("behaviourType", "");
jsonObj.put("riskLevel", "");
jsonObj.put("eventType", "");
jsonObj.put("productType", "");
User user= userService.queryByAccount(jsonObj.getString("userAccount")) ;
if(user!=null){
String name= user.getName();
jsonObj.put("userName", name!=null?name:null);
}
jsonArrN.add(jsonObj);
}
}
}
return jsonArrN;
}
//寫入XML
public Document createDocumentByJson(JSONArray json){
//創建Document
Document document = DocumentHelper.createDocument();
try {
//獲取列名
String jsonStr = new String(getRequest().getParameter("jsonStr").getBytes("ISO-8859-1"),"UTF-8");
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
RiskLevel rLevel = null;
log.info("構建XML根節點-BEGIN");
Element logRoot = document.addElement("Log");
logRoot.addAttribute("tablename", tableName);
logRoot.addAttribute("Remark", MessageUtils.getMessage("sys.table.name.windowsSafetyBaseLineLog"));
log.info("構建XML根節點-END");
for (int i = 0; i < json.size(); i++) {
JSONObject jsonObj=json.getJSONObject(i);
Element logIDElement = logRoot.addElement("LogID");
logIDElement.addAttribute("Remark", "ID");
logIDElement.setText(jsonObj.get("id")!=null ? jsonObj.get("id").toString() : "");
//事件主體
log.info("構建事件主體-BEGIN");
Element subjectElement = logRoot.addElement("Subject");
subjectElement.addAttribute("Remark", MessageUtils.getMessage("com.vrv.cems.log.eventSubject"));
log.info("構建事件主體-END");
log.info("寫入主體數據-BEGIN");
Element ip = subjectElement.addElement("IP");
ip.addAttribute("Remark", jsonObject.getString("ip"));
ip.setText(jsonObj.get("ip")!=null ? jsonObj.get("ip").toString() : "");
/*Element mac = subjectElement.addElement("MAC");
mac.addAttribute("Remark", jsonObject.getString("mac"));
mac.setText(jsonObj.get("mac")!=null ? jsonObj.get("mac").toString() : "");*/
Element name = subjectElement.addElement("Name");
name.addAttribute("Remark", jsonObject.getString("name"));
name.setText(jsonObj.get("name")!=null ? jsonObj.get("name").toString() : "");
Element orgName = subjectElement.addElement("OrgName");
orgName.addAttribute("Remark", jsonObject.getString("orgName"));
orgName.setText(jsonObj.get("orgName")!=null ? jsonObj.get("orgName").toString() : "");
/*Element regUserAccount = subjectElement.addElement("RegUserAccount");
regUserAccount.addAttribute("Remark", jsonObject.getString("regUserAccount"));
regUserAccount.setText(jsonObj.get("regUserAccount")!=null ? jsonObj.get("regUserAccount").toString() : "");*/
Element userAccount = subjectElement.addElement("UserAccount");
userAccount.addAttribute("Remark", jsonObject.getString("userAccount"));
userAccount.setText(jsonObj.get("userAccount")!=null ? jsonObj.get("userAccount").toString() : "");
Element policyName = subjectElement.addElement("PolicyName");
policyName.addAttribute("Remark", jsonObject.getString("policyName"));
policyName.setText(jsonObj.get("policyName")!=null ? jsonObj.get("policyName").toString() : "");
Element templateName = subjectElement.addElement("TemplateName");
templateName.addAttribute("Remark", jsonObject.getString("templateName"));
templateName.setText(jsonObj.get("templateName")!=null ? jsonObj.get("templateName").toString() : "");
Element state = subjectElement.addElement("State");
state.addAttribute("Remark", jsonObject.getString("state"));
state.setText(jsonObj.get("state")!=null ? jsonObj.get("state").toString() : "");
Element ruleExcuteCount = subjectElement.addElement("RuleExcuteCount");
ruleExcuteCount.addAttribute("Remark", jsonObject.getString("ruleExcuteCount"));
ruleExcuteCount.setText(jsonObj.get("ruleExcuteCount")!=null ? jsonObj.get("ruleExcuteCount").toString() : "");
Element ruleFailCount = subjectElement.addElement("RuleFailCount");
ruleFailCount.addAttribute("Remark", jsonObject.getString("ruleFailCount"));
ruleFailCount.setText(jsonObj.get("ruleFailCount")!=null ? jsonObj.get("ruleFailCount").toString() : "");
log.info("寫入主體數據-END");
//事件客體
log.info("構建事件客體-BEGIN");
Element objectElement = logRoot.addElement("Object");
objectElement.addAttribute("Remark", MessageUtils.getMessage("com.vrv.cems.log.eventObject"));
log.info("構建事件客體-END");
log.info("寫入客體數據-BEGIN");
//判斷是否有其他公共的配置項
LogConfig logConfig =logConfigService.queryByTableName(tableName);
if (logConfig!=null) {
rLevel=riskLevelService.queryRiskLevelByType(logConfig.getRiskLevel());
Element riskLevel = objectElement.addElement("RiskLevel");
riskLevel.addAttribute("Remark", jsonObject.getString("riskLevel"));
riskLevel.setText(MessageUtils.getMessage(rLevel.getName()));
Element behaviourType = objectElement.addElement("BehaviourType");
behaviourType.addAttribute("Remark", jsonObject.getString("behaviourType"));
behaviourType.setText(MessageUtils.getMessage(BehaviourType.getByState(logConfig.getBehaviourType()).getResourceKey()));
Element eventType = objectElement.addElement("EventType");
eventType.addAttribute("Remark", jsonObject.getString("eventType"));
eventType.setText(jsonObj.get("eventType")!=null ? MessageUtils.getMessage(Type.getByState(jsonObj.getInt("eventType")).getResourceKey()) : "");
/*MessageUtils.getMessage(Type.getByState(s)) )(jsonObj.getInt("eventType").toString()) )*/
Element productType = objectElement.addElement("ProductType");
productType.addAttribute("Remark", jsonObject.getString("productType"));
productType.setText(jsonObj.get("productType")!=null ? MessageUtils.getMessage(ProductType.getByState(jsonObj.getString("productType")).getResourceKey()) : "");
}
/*Element hardwareName = objectElement.addElement("auditDesc");
hardwareName.addAttribute("Remark", jsonObject.getString("auditDesc"));
hardwareName.setText(jsonObj.get("auditDesc")!=null ? jsonObj.get("auditDesc").toString() : "");
Element clientTime = objectElement.addElement("ClientTime");
clientTime.addAttribute("Remark", jsonObject.getString("clientTime"));
clientTime.setText((jsonObj.get("clientTime")!=null ? jsonObj.get("clientTime").toString() : ""));
Element reportTime = objectElement.addElement("ReportTime");
reportTime.addAttribute("Remark", jsonObject.getString("reportTime"));
reportTime.setText((jsonObj.get("reportTime")!=null ? jsonObj.get("reportTime").toString() : ""));
Element type= objectElement.addElement("Type");
type.addAttribute("Remark", jsonObject.getString("type"));
type.setText(jsonObj.get("type")!=null ? jsonObj.get("type").toString() : "");
Element state = objectElement.addElement("State");
state.addAttribute("Remark",jsonObject.getString("state"));
state.setText((jsonObj.get("state")!=null ? jsonObj.get("state") : "").toString());*/
log.info("寫入客體數據-END");
}
} catch (Exception e) {
e.printStackTrace();
log.error("生成XML失敗");
}
return document;
}
遞歸獲取組織機構全路徑方法
/*獲取全路徑組織機構*/
public List<WindowsSafetyBaseLineLog> getAllOrgDataList(List<WindowsSafetyBaseLineLog> dataList){
log.info("獲取全路徑組織機構-ST");
List<WindowsSafetyBaseLineLog> orgList = new ArrayList<WindowsSafetyBaseLineLog>();
if(dataList != null && dataList.size() > 0){
for(WindowsSafetyBaseLineLog dspbpl : dataList){
WindowsSafetyBaseLineLog o = new WindowsSafetyBaseLineLog();
String orgName = dspbpl.getOrgName();
String name = getAname(dspbpl.getOrganization(), orgName);
BeanUtils.copyProperties(dspbpl, o);
o.setOrgName(name);
orgList.add(o);
}
}
return orgList;
}
/*遞歸獲取全路徑組織機構方法*/
private String getAname(Organization organization, String orgName) {
log.info("遞歸獲取全路徑組織機構方法-ST");
if( organization != null && organization.getParent()!=null){
String tName="";
tName=organization.getParent().getName()+"/"+orgName;
log.info("遞歸獲取全路徑組織機構方法-END");
return getAname(organization.getParent(), tName);
}else{
log.info("遞歸獲取全路徑組織機構方法-END");
return orgName;
}
}
// 公共查詢過濾功能
private void filterQuery() {
// 拼排序條件。
String orderStr = getRequest().getParameter("order");
String sortStr = getRequest().getParameter("sort");
if (queryBean == null) {
queryBean = new WindowsSafetyBaseLineLogQueryBean();
}
if (sortStr != null && orderStr != null) {
String[] orderArr = orderStr.split(",");
String[] sortArr = sortStr.split(",");
for (int i = 0; i < sortArr.length; i++) {
queryBean.addOrderProperty("this." + sortArr[i],
"asc".equals(orderArr[i]) ? Order.ASC : Order.DESC);
}
}
}
導出XML效果:
項目筆記:導出XML和導出全部XML功能