在下面 [p]块中,发现如果Map 不用两个就一个就报java.util.ConcurrentModificationException
记录下来
package com.xyj.server.imp; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; 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 com.xyj.business.CodeBo; import com.xyj.business.DataBo; import com.xyj.business.FileDicBo; import com.xyj.business.FileTypeBo; import com.xyj.business.PrintBo; import com.xyj.server.PrintService; import com.xyj.util.BusinessException; import com.xyj.util.CacheManage; import com.xyj.util.Constants; import com.xyj.util.ServerException; import com.xyj.util.StringUtil; import com.xyj.vo.Code; import com.xyj.vo.Data; import com.xyj.vo.FileDic; import com.xyj.vo.FileType; import com.xyj.vo.Print; import common.Logger; /** * 打印 * @author 易小斌 * */ public class PrintServerImp implements PrintService{ private PrintBo printBo; private FileTypeBo fileTypeBo; private FileDicBo fileDicBo; private DataBo dataBo; private CacheManage cacheManage; private CodeBo codeBo; private Logger logger =Logger.getLogger(getClass()); public List<Print> queryPrintModels(Print model) throws ServerException { try { return printBo.queryPrintModels(model); } catch (BusinessException e) { throw new ServerException(e); } } public PrintBo getPrintBo() { return printBo; } public void setPrintBo(PrintBo printBo) { this.printBo = printBo; } public void uploadPrintModel(Map param) throws ServerException { String path = (String) param.get(Constants.PARAM_PATH); String path1 = (String) param.get(Constants.PARAM_PATH); String filedata = (String) param.get(Constants.PARAM_FILE_DATA); String fileName = (String) param.get(Constants.PARAM_FILE_NAME); String id= (String) param.get(Constants.PARAM_ID); File file = new File(filedata); String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); String ext=fileName.substring(fileName.lastIndexOf(".")); String saveName=System.currentTimeMillis()+""+ext; String fileName1 = fileName.substring(0,fileName.lastIndexOf(".")); if(!new File(path).exists()){ new File(path).mkdir(); } path+=File.separator+date; path1+=File.separator+date; if(!new File(path).exists()){ new File(path).mkdir(); } byte[] by = new byte[1024*8]; int len=0; try { FileOutputStream out = new FileOutputStream(path+File.separator+saveName); FileInputStream in = new FileInputStream(file); while((len=in.read(by))>=0){ out.write(by,0,len); } in.close(); out.flush(); out.close(); } catch (Exception e) { throw new ServerException(e); } file.delete(); param.put("URL",path1); param.put("MODELNAME",fileName1); param.put("SAVENAME",saveName); } public void updatePrint(Print print) throws ServerException { try { printBo.updatePrint(print); } catch (BusinessException e) { throw new ServerException(e); } } public List printData(Map<String, String> param) throws ServerException { //得到参数,filetypeId,tableFlag, String fileTypeId = param.get(Constants.PARAM_FILE_TYPE_ID); String tableFlag = param.get(Constants.PARAM_TABLE_FLAG); String oldTableFlag=param.get(Constants.PARAM_TABLE_FLAG); String DATATABLEFLAG=param.get("DATATABLEFLAG"); //数据TableFlag if("AJ,JN".equals(tableFlag)){ //如果是AJ+JN==JN tableFlag="JN"; } //得到参数 PageInfo String pageInfo = param.get(Constants.PARAM_PAGE_INFO); //查询 Print String select= param.get("id"); Print print = new Print(); print.setId(select); print.setStatus(Constants.STATUS_TRUE); List<Print> prints = this.queryPrintModels(print); print =prints.get(0); Set<String> existsAddColumns = new HashSet<String>(); //获得 where 条件 String where=""; try { if(Constants.TABLE_FLAG_JN.equals(tableFlag)){ //是卷内 param.put("append","a."); param.put(Constants.PARAM_TABLE_FLAG,Constants.TABLE_FLAG_AJ); param.put("CURRTABLEFLAG",DATATABLEFLAG); String ajWHere=fileDicBo.toChSqlByPageInfo(pageInfo, param); param.put("append","j."); param.put(Constants.PARAM_TABLE_FLAG,Constants.TABLE_FLAG_JN); String jnWHere=fileDicBo.toChSqlByPageInfo(pageInfo, param); where=ajWHere+jnWHere; }else{ param.put("CURRTABLEFLAG",DATATABLEFLAG); where=fileDicBo.toChSqlByPageInfo(pageInfo, param); } } catch (BusinessException e1) { throw new ServerException(e1); } //得到门类对应的字段 FileDic fileDic = new FileDic(); fileDic.setFileTypeId(fileTypeId); fileDic.setTableFlag(tableFlag); fileDic.setStatus(Constants.STATUS_TRUE); fileDic.setOrderByString("columnOrder"); List<FileDic> dataSourceList= new ArrayList<FileDic>(); try { List<FileDic> fileDics = fileDicBo.queryFileDic(fileDic); //拼接列。 Map<String,String> columnMap = new HashMap<String, String>(); //列Map <columnChName,columnName> String tableName =fileTypeBo.getTableName(fileTypeId, tableFlag); StringBuffer fieldBuss = new StringBuffer(""); //字段 id,ninadu Map<String,String> colMap = new HashMap<String, String>(); //<columnName,columnChName> for (Iterator iterator = fileDics.iterator(); iterator.hasNext();) { fileDic = (FileDic) iterator.next(); if(fieldBuss.length()>0){ fieldBuss.append(","); } if(Constants.TABLE_FLAG_JN.equals(tableFlag)){ //是卷内 fieldBuss.append("j."); existsAddColumns.add("j."+fileDic.getColumnName()); }else{ existsAddColumns.add(fileDic.getColumnName()); } fieldBuss.append(fileDic.getColumnName()); colMap.put(StringUtil.getNotNullString(fileDic.getColumnName()).toUpperCase(),fileDic.getColumnChName()); if(Constants.TABLE_FLAG_JN.equals(tableFlag)){ //是卷内 columnMap.put(fileDic.getColumnChName(),"j."+fileDic.getColumnName()); }else{ columnMap.put(fileDic.getColumnChName(),fileDic.getColumnName()); } if(!Constants.FILE_DIC_DATA_TYPE_DATE.equals(fileDic.getColDataInputType()) && !Constants.FILE_DIC_DATA_TYPE_REMARK.equals(fileDic.getColDataInputType()) && (Constants.FILE_DIC_INPUT_TYPE_select.equals(fileDic.getInputType()) || Constants.FILE_DIC_INPUT_TYPE_inSe.equals(fileDic.getInputType())) && !StringUtil.isEmptyString(fileDic.getDataSource())){ //如是日期和备注类型。 且是下拉框的 且DataSource不是空 dataSourceList.add(fileDic); } } //是卷内 if(Constants.TABLE_FLAG_JN.equals(tableFlag)){ fileDic= new FileDic(); fileDic.setFileTypeId(fileTypeId); fileDic.setTableFlag(Constants.TABLE_FLAG_AJ); fileDic.setStatus(Constants.STATUS_TRUE); fileDic.setOrderByString("columnOrder"); List<FileDic> fileDics2 = fileDicBo.queryFileDic(fileDic); for (Iterator iterator = fileDics2.iterator(); iterator.hasNext();) { fileDic = (FileDic) iterator.next(); columnMap.put(fileDic.getColumnChName(),"a."+fileDic.getColumnName()); colMap.put(StringUtil.getNotNullString(fileDic.getColumnName()).toUpperCase(),fileDic.getColumnChName()); if("AJ,JN".equals(oldTableFlag)){ if(fieldBuss.length()>0){ fieldBuss.append(","); } fieldBuss.append("a."+fileDic.getColumnName()); existsAddColumns.add("a."+fileDic.getColumnName()); if(!Constants.FILE_DIC_DATA_TYPE_DATE.equals(fileDic.getColDataInputType()) && !Constants.FILE_DIC_DATA_TYPE_REMARK.equals(fileDic.getColDataInputType()) && (Constants.FILE_DIC_INPUT_TYPE_select.equals(fileDic.getInputType()) || Constants.FILE_DIC_INPUT_TYPE_inSe.equals(fileDic.getInputType())) && !StringUtil.isEmptyString(fileDic.getDataSource())){ //如是日期和备注类型。 且是下拉框的 且DataSource不是空 dataSourceList.add(fileDic); } } } //如果是卷内且 是否分组 if(Constants.STATUS_TRUE.equals(print.getGroupBy())){ String[] fileds=print.getGroupField().split(","); for (int j = 0; j < fileds.length; j++) { String string=columnMap.get(fileds[j]); if(string!=null){ if(!existsAddColumns.contains(string)){ if(fieldBuss.length()>0){ fieldBuss.append(","); } fieldBuss.append(string); existsAddColumns.add(string); } } } } } //循环获得列 排序 StringBuffer order =new StringBuffer(""); if(!StringUtil.isEmptyString(print.getOrderString())){ String[] arr =print.getOrderString().split(","); for (int i = 0; i < arr.length; i++) { String[] ar=arr[i].split("-"); if(ar.length>=0){ boolean bool=false; String str=StringUtil.getNotNullString(columnMap.get(ar[0])); if(str.length()>0){ bool=true; if(order.length()>0){ order.append(","); } } if(bool && !existsAddColumns.contains(str)){ existsAddColumns.add(str); if(fieldBuss.length()>0){ fieldBuss.append(","); } fieldBuss.append(str); } if(bool && ar.length>2){ if("1".equals(ar[2])){ order.append(" "+printBo.getDbNumberFunction(str)); }else{ order.append(" "+str); } order.append(" "+ar[1]); }else if(bool && ar.length>1){ order.append(" "+str); order.append(" "+ar[1]); } }else{ order.append(StringUtil.getNotNullString(columnMap.get(arr[i]))); } } } //生成 Data 对象,和 设置表 名, 条件 ,排序 Data data =new Data(); data.setColumns(fieldBuss.toString()); if(Constants.TABLE_FLAG_JN.equals(tableFlag)){ //是卷内 String ajTableName=fileTypeBo.getTableName(fileTypeId, Constants.TABLE_FLAG_AJ); data.setTableName(tableName+" j left join "+ajTableName+" a on j.AJID=a.id "); data.setWhere(where+StringUtil.getPageInfoSql("j.STATUS@@1@@2@_@a.STATUS@@1@@2@_@")); }else{ data.setTableName(tableName); data.setWhere(where+StringUtil.getPageInfoSql("STATUS@@1@@2@_@")); } data.setOrderString(order.toString()); //查询数据结果 List<Map<String,String>> maps = dataBo.queryNotPageDataList(data); //真正的返回数据List List result = new ArrayList (); //是否拆分行 Map<String,String[]> splitMap = new HashMap<String, String[]>(); //splitMap if(Constants.STATUS_TRUE.equals(print.getIsSplit())){ String splitString=StringUtil.getNotNullString(print.getSplitString()); String[] arr=splitString.split("@_@"); if(arr!=null){ for (int i = 0; i < arr.length; i++) { if(StringUtil.isEmptyString(arr[i])){ continue; } String[] ar=arr[i].split("@,@"); if(ar!=null && ar.length>1 && StringUtil.isNumeric(ar[1]) && !"0".equals(ar[1])){ splitMap.put(ar[0],ar); } } } } //end //不分组数据 if(!Constants.STATUS_TRUE.equals(print.getGroupBy())){ printNotGroup(dataSourceList, colMap, maps, result,columnMap,print,splitMap,fileTypeId); }else{ //分组数据 printGroup(print, dataSourceList, columnMap, colMap, maps, result,splitMap); } return result; } catch (BusinessException e) { throw new ServerException("printGDWJML()",e); } } /*不分组*/ private void printNotGroup(List<FileDic> dataSourceList,Map<String, String> colMap, List<Map<String, String>> maps, List result,Map<String,String> columnMap,Print print,Map<String,String[]> splitMap,String fileTypeId) throws ServerException { FileType fileType=null; try { fileType=fileTypeBo.queryFileTypeById(fileTypeId); } catch (BusinessException e1) { throw new ServerException(e1); } Map<String,Map<String, Code>> allCodeMap = new HashMap<String, Map<String,Code>>(); //全部AllCode int index=0; int dataSourceSize=dataSourceList.size(); for (Iterator iterator = maps.iterator(); iterator.hasNext();) { index++; Map<String,String> map = (Map<String, String>) iterator.next(); Map<String,String> map2 = new HashMap<String, String>(); map2.put("序号_1",index+""); map2.put("门类名称",fileType.getChname()); for (Iterator iterator2 = map.keySet().iterator(); iterator2 .hasNext();) { String key = (String) iterator2.next(); boolean start=false; if(dataSourceSize>0 && !start){ start=true; for (Iterator iterator3 = dataSourceList.iterator(); iterator3.hasNext();) { FileDic dataSourceFileDic = (FileDic) iterator3.next(); if(key.equals(dataSourceFileDic.getColumnName())){ Map<String, Code> codeMap=allCodeMap.get(dataSourceFileDic.getDataSource()); try { if(codeMap==null){ codeMap = codeBo.queryCodeValMapByType(dataSourceFileDic.getDataSource()); allCodeMap.put(dataSourceFileDic.getDataSource(),codeMap); } } catch (BusinessException e) { throw new ServerException(e); } String value=map.get(key); if(codeMap!=null && codeMap.get(value)!=null && Constants.STATUS_TRUE.equals(codeMap.get(value).getStatus())){ map2.put(colMap.get(key)+"_1",value); map.put(key,codeMap.get(value).getCodeKey()); } } } } String value=map.get(key); map2.put(colMap.get(key),value); } result.add(map2); } } /*分组*/ private void printGroup(Print print, List<FileDic> dataSourceList,Map<String, String> columnMap, Map<String, String> colMap,List<Map<String, String>> maps, List result,Map<String,String[]> splitMap) throws ServerException { String[] fileds=print.getGroupField().split(","); List<String> groupFileDicList = new ArrayList<String>(); for (int j = 0; j < fileds.length; j++) { String string=columnMap.get(fileds[j]); if(string!=null){ groupFileDicList.add(string); } } List<String> keyList = new ArrayList<String>(); Map<String,List<Map<String,String>>> dataMap = new HashMap<String, List<Map<String,String>>>(); for (Iterator iterator = maps.iterator(); iterator.hasNext();) { Map<String,String> map = (Map<String, String>) iterator.next(); /*Map<String,String> map2 = new HashMap<String, String>(); for (Iterator iterator2 = map.keySet().iterator(); iterator2.hasNext();) { String key = (String) iterator2.next(); map2.put(colMap.get(key),map.get(key)); } result.add(map2);*/ StringBuffer strKey=new StringBuffer(""); for (Iterator iterator2 = groupFileDicList.iterator(); iterator2.hasNext();) { String groupFieldStr = (String) iterator2.next(); if(groupFieldStr.indexOf(".")>=0){ groupFieldStr=groupFieldStr.substring(groupFieldStr.indexOf(".")+1); } String value=StringUtil.getNotNullString(map.get(groupFieldStr)); if(!StringUtil.isEmptyString(value)){ strKey.append(value); } } List<Map<String,String>> rowList=dataMap.get(strKey.toString()); if(rowList==null){ rowList =new ArrayList<Map<String,String>>(); dataMap.put(strKey.toString(),rowList); } if(!keyList.contains(strKey.toString())){ keyList.add(strKey.toString()); } rowList.add(map); } int index=0; int indexId=0; boolean isSplitNext=splitMap.size()>0?true:false; //是否有下一个 拆分行 List<Map<String,String>> newAddList = null; //拆分行 List if(isSplitNext){ newAddList = new ArrayList<Map<String,String>>(); } Map<String,Map<String, Code>> allCodeMap = new HashMap<String, Map<String,Code>>(); //全部CODEMAP for (Iterator iterator = keyList.iterator(); iterator.hasNext();) { //Key s String key = (String) iterator.next(); index=0; indexId=0; List<Map<String,String>> listMap = dataMap.get(key); //得到每一组数据 int groupIndex=0; //每组的下标 Map<String,String> groupTop1Map =null; //分组的第一个Map<String,String> int dataSourceSize=dataSourceList.size(); for (Iterator iterator2 = listMap.iterator(); iterator2.hasNext();) { // get(key) Map<String, String> map = (Map<String, String>) iterator2.next(); // old Row Map<String,String> map2 = new HashMap<String, String>(); //new Row groupIndex++; if(groupIndex==1){ //如果是分组的第一个就把这个句本给保存起来。 groupTop1Map=map2; } map2.put("序号_1",(indexId+1)+""); for (Iterator iterator4 = map.keySet().iterator(); iterator4.hasNext();) { // for TD String kyes = (String) iterator4.next(); boolean start=false; if(dataSourceSize>0 && !start){ start=true; for (Iterator iterator3 = dataSourceList.iterator(); iterator3.hasNext();) { FileDic dataSourceFileDic = (FileDic) iterator3.next(); if(kyes.equals(dataSourceFileDic.getColumnName())){ Map<String, Code> codeMap=allCodeMap.get(dataSourceFileDic.getDataSource()); try { if(codeMap==null){ codeMap = codeBo.queryCodeValMapByType(dataSourceFileDic.getDataSource()); allCodeMap.put(dataSourceFileDic.getDataSource(),codeMap); } } catch (BusinessException e) { throw new ServerException(e); } String value=map.get(kyes); if(codeMap!=null && codeMap.get(value)!=null && Constants.STATUS_TRUE.equals(codeMap.get(value).getStatus())){ String abc=codeMap.get(value).getCodeKey(); map2.put(colMap.get(kyes)+"_1",abc); map.put(kyes,abc); } } } } String value=map.get(kyes); map2.put(colMap.get(kyes),value); } result.add(map2); if(isSplitNext){ //拆分行 split row List<Map<String,String>> tempResultList = new ArrayList<Map<String,String>>(); //table 装行 for (Iterator iterator3 = splitMap.keySet().iterator(); iterator3.hasNext();) { try { String splitKey = (String) iterator3.next(); String[] paramArr=splitMap.get(splitKey); int leng=Integer.parseInt(paramArr[1]); String val=StringUtil.getNotNullString(map2.get(splitKey)); if(paramArr.length==3 && val.length()>0){ //判断是否有拆分的字符串 比如[ int cuIndex=val.indexOf(paramArr[2]); if(cuIndex>0){ //>0 String temp=val.substring(0,cuIndex); StringBuffer buff= new StringBuffer(temp); int tempLength=StringUtil.getCodeLen(temp); for (int i = tempLength; i < leng; i++) { buff.append(" "); } buff.append(val.substring(cuIndex)); val=buff.toString(); } } if(StringUtil.getCodeLen(val)>leng){ String val1=StringUtil.subStringCode(val,0,leng); //原来数据 String newSTr=StringUtil.subStringCode(val,leng+1); //后面的数据 int newLen=StringUtil.getCodeLen(newSTr); int size=newLen/leng; //后面的数据的长度 if(newLen%leng!=0){ size++; } if(tempResultList.size()<size){ //判定有几行 就添加几个 for (int i = tempResultList.size(); i <size; i++) { Map<String,String> tempMap=new HashMap<String, String>(); tempMap.put("序号_2",""+(index+i+1)); tempResultList.add(tempMap); } } map2.put(splitKey,val1); //重新赋值 int start=0; int end=0; for (int i = 0; i <size; i++) { Map<String,String> tempDataMap=null; if(tempResultList.size()>i){ tempDataMap=tempResultList.get(i); } if(tempDataMap!=null){ end+=leng; if(end>newLen){ end=newLen; } String tempVal=StringUtil.subStringCode(newSTr,start,end); tempDataMap.put(splitKey,tempVal); //赋值 start=end; } } } } catch (Exception e) { e.printStackTrace(); throw new ServerException(e); } } if(tempResultList.size()>0){ //判定》0 就添加 result.addAll(tempResultList); index+=tempResultList.size(); newAddList.addAll(tempResultList); } } index++; indexId++; //给每一条增加分页 int pageSize=Integer.parseInt(print.getPageSize()); int pageCont=listMap.size()/pageSize; if(listMap.size()%pageSize!=0){ pageCont++; } map2.put("总页数_1",pageCont+""); pageCont=index/pageSize; if(index%pageSize!=0){ pageCont++; } map2.put("当前页数_1",pageCont+""); //end } if(Constants.STATUS_TRUE.equals(print.getIsjoinTop1())){ //分组数据最后一条是否连接第一个条数据 [p]int len=result.size(); if(len>0 && groupTop1Map!=null){ Map<String,String> map = (Map<String, String>) result.get(len-1); //得到最后一个 Map<String,String> tempMap=new HashMap<String,String>(groupTop1Map); Iterator iterator2 = tempMap.keySet().iterator(); for (; iterator2.hasNext();) { String top1Key = (String) iterator2.next(); map.put(top1Key+"_TOP1",groupTop1Map.get(top1Key)); } } [/p] } //给拆分行的添加分页 if(isSplitNext){ int maxSize=listMap.size()+newAddList.size(); int pageSize=Integer.parseInt(print.getPageSize()); for (Iterator iterator11 = newAddList.iterator(); iterator.hasNext();) { Map<String, String> map = (Map<String, String>) iterator11.next(); int tempIndex=Integer.parseInt(map.get("序号_2")); int pageCont=maxSize/pageSize; if(maxSize%pageSize!=0){ pageCont++; } map.put("总页数_1",pageCont+""); pageCont=tempIndex/pageSize; if(tempIndex%pageSize!=0){ pageCont++; } map.put("当前页数_1",pageCont+""); } } //给不足的行添加空格。 int pageSize=Integer.parseInt(print.getPageSize()); int pageCont=index/pageSize; if(index%pageSize!=0){ pageCont++; } for(int i=index;i<(pageCont*pageSize);i++){ Map map2= new HashMap(); map2.put("总页数_1",pageCont+""); result.add(map2); pageCont=i/pageSize; if(i%pageSize!=0){ pageCont++; } map2.put("当前页数_1",pageCont+""); } } } public FileTypeBo getFileTypeBo() { return fileTypeBo; } public void setFileTypeBo(FileTypeBo fileTypeBo) { this.fileTypeBo = fileTypeBo; } public FileDicBo getFileDicBo() { return fileDicBo; } public void setFileDicBo(FileDicBo fileDicBo) { this.fileDicBo = fileDicBo; } public DataBo getDataBo() { return dataBo; } public void setDataBo(DataBo dataBo) { this.dataBo = dataBo; } public void insertPrint(Print print) throws ServerException { try { printBo.insertPrint(print); } catch (BusinessException e) { throw new ServerException(e); } } public CacheManage getCacheManage() { return cacheManage; } public void setCacheManage(CacheManage cacheManage) { this.cacheManage = cacheManage; } public String getDbNumberFunction(String str) throws ServerException { try { return printBo.getDbNumberFunction(str); } catch (BusinessException e) { throw new ServerException(e); } } public CodeBo getCodeBo() { return codeBo; } public void setCodeBo(CodeBo codeBo) { this.codeBo = codeBo; } public Logger getLogger() { return logger; } public void setLogger(Logger logger) { this.logger = logger; } public String getDb() throws ServerException { try { return printBo.getDb(); } catch (BusinessException e) { throw new ServerException(e); } } }