Java根據word模板生成word文檔之後臺解析和實現及部分代碼(三)B

緊接上節,繼續貼相關的代碼:

/**
	    * 通過配置將要展示的類型 來輸出不同的格式
	    * @param sqlStr
	    * @param docType
	    * @param paramMap
	    * @param dataSetRepCnName
	    * @param fieldList
	    * @param conn
	    * @param type
	    * @return
	    */
	   public String getPhotoUrl(String sqlStr,String docType,Map<String,String> paramMap,String dataSetRepCnName,List<FieldBean> fieldList,Connection conn,String type){
		   //System.out.println("sqlStr-------------:" + sqlStr);
		   String photoName = docType + "-";
		   String projectId ="";
		   String workgroupId ="";
		   String evtcaseInstId ="";
		   String userId ="";
		   String photoPath ="";
		   Set<Entry<String, String>> sets = paramMap.entrySet();
		   Iterator<Entry<String, String>> it = sets.iterator();
	        while (it.hasNext()) {
	            Map.Entry<String,String> entry = (Map.Entry<String,String>) it.next();
	            String key = entry.getKey();
	            if(key.equals("project_id")){
	            	projectId =  entry.getValue();
	            }else if(key.equals("workgroupId")){
	            	workgroupId =  entry.getValue();
	            }else if(key.equals("evtcase_inst_id")){
	            	evtcaseInstId =  entry.getValue();
	            }else if(key.equals("userId")){
	            	userId =  entry.getValue();
	            }
	    }
	        
	        Set<Entry<String, String>> sets1 = paramMap.entrySet();
			 Iterator<Entry<String, String>> it1 = sets1.iterator();
			 //判斷是否有參數存在
				if(CommonUtils.isNotNull(sqlStr) && sqlStr.indexOf("$P{") != -1){//如果有參數
			      while (it1.hasNext()) {
					Map.Entry<String,String> mapEntry = (Map.Entry<String,String>)it1.next();
					String key = String.valueOf(mapEntry.getKey());
					String value = String.valueOf(mapEntry.getValue());
					if(CommonUtils.isNotNull(key) && sqlStr.contains("$P{"+ key +"}")){
						sqlStr = sqlStr.replace("$P{"+ key +"}", "'" + value + "'");
					}
				}
			 }
			
			//System.out.println("\n 創建圖片時---最終要執行的SQL爲:\n" + sqlStr);
			if(CommonUtils.isNotNull(userId)){
				photoName  += userId + "-";
			}
			
			if(CommonUtils.isNotNull(workgroupId)){
				photoName  += workgroupId + "-";
			}
			
			if(CommonUtils.isNotNull(workgroupId)){
				photoName  += workgroupId + "-";
			}
			
			if(CommonUtils.isNotNull(evtcaseInstId)){
				photoName  += evtcaseInstId + "-";
			}
			
			if(CommonUtils.isNotNull(photoName)){
				photoName = photoName  + dataSetRepCnName + XmlPathDef.PHOTO_TYPE;
			}else{
				photoName = dataSetRepCnName + XmlPathDef.PHOTO_TYPE;//實際情況是不能有else的,否則的話 會出問題(多個人操作時圖片會被替換)
			}
			
			//找到原來或者未生成的圖片地址
			photoPath = xmlPathDef.getBaseWordResFilePath(docType,projectId,workgroupId) + photoName;
			
			String[] pplotArr = null;
			if(docType.equals(XmlPathDef.ISO27001_DOC)){
				pplotArr = new String[2];
				pplotArr[0] = "合規性要求指標";
				pplotArr[1] = "信息系統差距分析指標";
			}
			//判斷圖片是否存在,如果存在,則先刪除
			if(xmlPathDef.deleteFile(photoPath)){
				creatPieForReport.createChartPic(sqlStr,dataSetRepCnName,fieldList,photoPath,conn,type,0, 0,pplotArr);
			}
		   //System.out.println("image photoPath url:"  + photoPath);
		   return photoPath;
	   }

/**
	    * 執行SQL語句,取得每個標籤的值
	    * @param map
	    * @param sqlStr
	    * @param paramMap
	    * @param fieldList
	    * @param type
	    * @param conn
	    * @param sort
	    * @param docType
	    * @return
	    */
	public HashMap<String,Object> queryValue(HashMap<String,Object> map,String sqlStr,Map<String,String> paramMap,List<FieldBean> fieldList,String type,Connection conn,String sort,String docType){
		   //首先是將要查詢的SQL語句 字段不能爲空
		   if(CommonUtils.isNotNull(sqlStr) && fieldList != null && fieldList.size() > 0){
				 int num = 0;
				 boolean bo = false;
				 if(CommonUtils.isNotNull(sort)){
					 while(num < XmlPathDef.cellNameAndSort.length){
						 if(sort.equals(XmlPathDef.cellNameAndSort[num][1])){
							 bo = true;
							 break;
						 }
						 num++;
					}
				 }
				  if(docType.equals(XmlPathDef.SOC_DOC) && bo){//如果爲等保報告
						 //System.out.println("將要執行的SQL爲:" + sqlStr);
					     Set<Entry<String, String>> sets = paramMap.entrySet();
						 Iterator<Entry<String, String>> it = sets.iterator();
						 //判斷是否有參數存在
							if(sqlStr.indexOf("$P{") != -1){//如果有參數
						      while (it.hasNext()) {
								Map.Entry<String,String> mapEntry = (Map.Entry<String,String>)it.next();
								String key = String.valueOf(mapEntry.getKey());
								String value = String.valueOf(mapEntry.getValue());
								if(CommonUtils.isNotNull(key) && sqlStr.contains("$P{"+ key +"}")){
									sqlStr = sqlStr.replace("$P{"+ key +"}", "'" + value + "'");
								}
							}
						 }
						List<String[]> listValue = new ArrayList<String[]>();
						String[] headContent = new String[]{};
						listValue.add(headContent);
						String[] content = null;
						//System.out.println("最終要執行的SQL爲:" + sqlStr);
						try {
						     //通過Query接口查詢
						     stmt = conn.prepareStatement(sqlStr);
							 rs = stmt.executeQuery(sqlStr);
							 while (rs.next()) {
								 String layerId = rs.getString("layer_id");//取得子類id
								 String evtcaseInstId = rs.getString("evtcase_inst_id");//取得實例id
								 //String strutsName = rs.getString("name");//取得實例id
								 if(CommonUtils.isNotNull(layerId) && CommonUtils.isNotNull(evtcaseInstId)){
									 List<CellEvtContent> bean = evtcaseInstAssetContentService.count(layerId, evtcaseInstId);
									 List<CompontBean> compontbean = evtcaseInstService.getCompanetByInstId(layerId,evtcaseInstId);	
									 if(compontbean != null && compontbean.size() > 0){
										 List<CellEvtContent> countbean = unitConformitService.getCountBean(compontbean, bean);
										 //Integer singleNum = evtcaseInstService.getSingleNum(layerId, evtcaseInstId);
										 int stnum = 1;
										 int sumaryvalue = 0;
										 int convalue = 0;
										 int inconvalue = 0;
										 if(countbean != null && countbean.size() > 0){
											 for (int i = 0; i < countbean.size(); i++) {
												 CellEvtContent cellEvtContent = (CellEvtContent)countbean.get(i);
												 if(cellEvtContent != null){
													 String assetName = cellEvtContent.getAssetName();
													 List<CountBean> beanList = cellEvtContent.getCountList();
													 content = new String[2+ compontbean.size()];//創建數組的長度
													 content[0] = String.valueOf(stnum); 
													 content[1] = assetName; 
													 cellEvtContent.getConform();
													 for(int j = 0; j < beanList.size(); j++) {
														 CountBean countBean =(CountBean)beanList.get(j);
														 if(countBean != null){
															int summary = countBean.getSummary();
															int inconformity = countBean.getInconformity();
															int nacount = countBean.getNacount();
															int conformity = countBean.getConformity();
															if(summary == 0){
																content[2+j] = "—";
															}else if(inconformity == nacount){
																content[2+j] = "不適用";
															}else if(inconformity == 0 && summary != 0){
																content[2+j] = inconformity + "/" + summary + " 符合";
															}else if(inconformity == summary && summary != 0){
																content[2+j] =inconformity + "/" +  summary +  " 不符合";
															}else{
																content[2+j] =inconformity+ "/" + summary + " 部分符合";
															}
															
															//如果是最後一行的統計數據
															sumaryvalue += summary;
															convalue += conformity;
															inconvalue += inconformity;
														 }
													 }
													 
													 stnum++;
													 //System.out.println("放入list中的數組爲:  "+ content);
													 //將表格對象放入list對象中
													 listValue.add(content);
											 	 }
											  }
									       }
									    }else{//如果沒有數據
									    	//將表格對象放入list對象中
											listValue.add(content);
											//System.out.println("content 無數據: " + content);
									    }
										
//									 String  strutsName_tag = "從上表的統計結果來看," + strutsName +"單項測評共有測評指標 " + singleNum + " 個,檢查對象 " + (countbean.size()-1) + " 個,實施測評項 " + sumaryvalue + " 個, 其中 " + convalue +" 個符合項, 部分符合和不符合項 "+inconvalue + " 個。";
//									 map.put("$" + strutsName + "$", strutsName_tag);//放入map中對用的標籤值
									//取得是那種報告類型,判斷表格開始的序列
								    String docNum = xmlPathDef.getTypeNum(docType);//取得是那種報告序列
								    sort = Integer.parseInt(sort) + Integer.parseInt(docNum)+ "";//取得各種word模版對應的表格序列
								    map.put(docType + "*" + XmlPathDef.WORD_T + "@" + sort, listValue);//按照索引放入map對象中
								    //System.out.println("表格" + sort  + "................." + docType + "_" + sort);
								    }
							    }
							 }catch (SQLException e) {
								e.printStackTrace();
						  } catch (Exception e) {
							e.printStackTrace();
						 }
				     }else{//其他報告
						 //System.out.println("將要執行的SQL爲:" + sqlStr);
					     Set<Entry<String, String>> sets = paramMap.entrySet();
						 Iterator<Entry<String, String>> it = sets.iterator();
						 //判斷是否有參數存在
							if(sqlStr.indexOf("$P{") != -1){//如果有參數
						      while (it.hasNext()) {
								Map.Entry<String,String> mapEntry = (Map.Entry<String,String>)it.next();
								String key = String.valueOf(mapEntry.getKey());
								String value = String.valueOf(mapEntry.getValue());
								if(CommonUtils.isNotNull(key) && sqlStr.contains("$P{"+ key +"}")){
									sqlStr = sqlStr.replace("$P{"+ key +"}", "'" + value + "'");
								}
							}
						 }
				
						List<String[]> listValue = new ArrayList<String[]>();
						String[] headContent = new String[]{};
						listValue.add(headContent);
						String[] content = null;
						//System.out.println("最終要執行的SQL爲:" + sqlStr);
						try {
						     //通過Query接口查詢
						     stmt = conn.prepareStatement(sqlStr);
							 rs = stmt.executeQuery(sqlStr);
							 rs.last(); //結果集指針知道最後一行數據
							 int count = rs.getRow();//最大行數
							 //System.out.println("最大行數爲:" + count);
							 int rownum = 0;
							 if(count > 0){//如果最大行數大於 0 表示有結果集 進行循環操作
								 rs.beforeFirst();//將結果集指針指回到開始位置,這樣才能通過while獲取rs中的數據
								 while (rs.next()) {
									++rownum;
									content = new String[fieldList.size()];
									for (int i = 0; i < fieldList.size(); i++) {
										String filedKey = fieldList.get(i).getKey();
										String filedColumn = fieldList.get(i).getColumn();
										String filedValue =  fieldList.get(i).getValue();
										String ctype =  fieldList.get(i).getCtype();
										if(filedKey.equals("$rownum$")){//如果是帶有序號的表格
											filedValue = String.valueOf(rownum);
										}else{
											if(CommonUtils.isNotNull(filedColumn)){
												filedValue = rs.getString(filedColumn);
											}else{
												continue;
											}
										}
										//System.out.println(filedKey);
										//System.out.println(filedValue);
										 if(type.equals("F")){//如果是單個字段
										    map.put(filedKey, filedValue==null?"":XmlPathDef.getWordFValue(ctype, filedValue));
										    //將創建的無用對象置爲空
										    if(listValue != null && listValue.size() > 0){
										    	listValue = null;
										    }
										    if(content != null && content.length > 0){
										    	content = null;
										    }
									    }else if(type.equals(WORD_TYPE)) {//如果是表格
									    	content[i]= (filedValue==null?" ":XmlPathDef.getWordFValue(ctype, filedValue));
									    }
									  }
									if(type.equals(WORD_TYPE)){
										//將表格對象放入list對象中
										listValue.add(content);
									}
								 	}
								   if(type.equals(WORD_TYPE)){
									   //取得是那種報告類型,判斷表格開始的序列
									   String docNum = xmlPathDef.getTypeNum(docType);//取得是那種報告序列
									   sort = Integer.parseInt(sort) + Integer.parseInt(docNum)+ "";//取得各種word模版對應的表格序列
									   map.put(docType + "*" + XmlPathDef.WORD_T + "@" + sort, listValue);//按照索引放入map對象中
									   //System.out.println("表格" + sort  + "................." + docType + "_" + sort);
								   }
							 }else{ 
								    ++rownum;
									content = new String[fieldList.size()];
									for (int i = 0; i < fieldList.size(); i++) {
										String filedKey = fieldList.get(i).getKey();
										 if(type.equals("F")){//如果是單個字段
										    map.put(filedKey, "");
										    //將創建的無用對象置爲空
										    if(listValue != null && listValue.size() > 0){
										    	listValue = null;
										    }
										    if(content != null && content.length > 0){
										    	content = null;
										    }
									    }else if(type.equals(WORD_TYPE)) {//如果是表格
									    	content[i]= " ";
									    }
									  }
									if(type.equals(WORD_TYPE)){
										//將表格對象放入list對象中
										listValue.add(content);
									}
								 if(type.equals(WORD_TYPE)){
								   //取得是那種報告類型,判斷表格開始的序列
								   String docNum = xmlPathDef.getTypeNum(docType);//取得是那種報告序列
								   sort = Integer.parseInt(sort) + Integer.parseInt(docNum)+ "";//取得各種word模版對應的表格序列
								   map.put(docType + "*" + XmlPathDef.WORD_T + "@" + sort, listValue);//按照索引放入map對象中
								   //System.out.println("表格" + sort  + "................." + docType + "_" + sort);
							   }
							 }
							 
						 } catch (SQLException e) {
								e.printStackTrace();
					 }
				  }
		       }
		   return map;
	   }

未完待續

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章