public List<AllInfoInPackage> queryClassAndMethodInfoByPackage3(String appName,String packageName1) { AlpacaCodeRelationExample example = new AlpacaCodeRelationExample(); example.createCriteria().andAppNameEqualTo(appName).andPackageNameEqualTo(packageName1).andIsDeletedEqualTo("n").andIsHiddenEqualTo("n"); List<AlpacaCodeRelation> acrList = alpacaCodeRelationDAO.selectByExample(example); if(ListUtils.isBlank(acrList)) { return null; } List<AllInfoInPackage> topClassList = new ArrayList<AllInfoInPackage>(); for(AlpacaCodeRelation acr : acrList) { String className = acr.getClassName(); String methodName = acr.getMethodName(); Long classDetailId = acr.getClassDetailsId(); Long methodDetailId = acr.getMethodDetailsId(); Long dataId = acr.getId(); if(topClassList.size()==0) { if(classDetailId==null||classDetailId<=0) { classDetailId = insertInitClassOrMethodDetail();//首先在detail表中插入一条class数据,返回插入的id号 } if(methodDetailId==null||methodDetailId<=0) { methodDetailId = insertInitClassOrMethodDetail(); } acr.setClassDetailsId(classDetailId); acr.setMethodDetailsId(methodDetailId); if(acr.getClassDetailsId()==null||acr.getMethodDetailsId()==null) updateClassOrMethodId(dataId,classDetailId,methodDetailId); addNewMethodData(acr, topClassList);//最终返回数据插入初值 } else { boolean isClassNameExist = false; for(AllInfoInPackage allInfoInPackage:topClassList) { List<MethodInfoDto> methodLists; MethodInfoDto newMethodInfoDto = new MethodInfoDto(); if(className.equals(allInfoInPackage.getClassName())) { Long publicClassId = allInfoInPackage.getClassDetailId(); methodLists = allInfoInPackage.getMethodInfos(); newMethodInfoDto.setMethodName(methodName); if(methodDetailId==null||methodDetailId<=0) { methodDetailId = insertInitClassOrMethodDetail(); updateClassOrMethodId(dataId,publicClassId,methodDetailId); } newMethodInfoDto.setMethodDetailId(methodDetailId); newMethodInfoDto.setMethodAttr(this.queryMethodOrClassDescriptionById(methodDetailId).getAttribute());//设置属性 methodLists.add(newMethodInfoDto); isClassNameExist = true; break; } } if(!isClassNameExist) { if(classDetailId==null||classDetailId<=0) { classDetailId = insertInitClassOrMethodDetail();//首先在detail表中插入一条class数据,返回插入的id号 } if(methodDetailId==null||methodDetailId<=0) { methodDetailId = insertInitClassOrMethodDetail(); } acr.setClassDetailsId(classDetailId); acr.setMethodDetailsId(methodDetailId); if(acr.getClassDetailsId()==null||acr.getMethodDetailsId()==null) updateClassOrMethodId(dataId,classDetailId,methodDetailId); addNewMethodData(acr, topClassList);//最终返回数据插入初值 } } } return topClassList; }
private void addNewMethodData(AlpacaCodeRelation acr, List<AllInfoInPackage> topClassList) { String className = acr.getClassName(); String methodName = acr.getMethodName(); Long classDetailId = acr.getClassDetailsId(); Long methodDetailId = acr.getMethodDetailsId(); AllInfoInPackage allInfoInPackage = new AllInfoInPackage(); allInfoInPackage.setClassName(className); allInfoInPackage.setClassDetailId(classDetailId); allInfoInPackage.setClassDesc(this.queryMethodOrClassDescriptionById(classDetailId).getDescription());//加入类描述信息 MethodInfoDto methodInfoDto = new MethodInfoDto(); methodInfoDto.setMethodName(methodName); methodInfoDto.setMethodDetailId(methodDetailId); methodInfoDto.setMethodAttr(this.queryMethodOrClassDescriptionById(methodDetailId).getAttribute()); List<MethodInfoDto> methodInfos = new ArrayList<MethodInfoDto>(); methodInfos.add(methodInfoDto); allInfoInPackage.setMethodInfos(methodInfos); topClassList.add(allInfoInPackage); } private Long insertInitClassOrMethodDetail() { AlpacaCodeRelationDetails acrd = new AlpacaCodeRelationDetails(); acrd.setGmtCreate(new Date()); acrd.setGmtModified(new Date()); acrd.setIsDeleted("n"); Long detailId = alpacaCodeRelationDetailsDAO.insert(acrd); return detailId; } //通过id来更新相应表中的classDetailId或者methodDetailId字段 private int updateClassOrMethodId(Long id,Long classDetailId,Long methodDetailId) { AlpacaCodeRelation record = new AlpacaCodeRelation(); record.setId(id); record.setGmtModified(new Date()); record.setClassDetailsId(classDetailId); record.setMethodDetailsId(methodDetailId); int row = alpacaCodeRelationDAO.updateByPrimaryKeySelective(record); return row; }
测试代码部分:
(1)
package test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; public class ListTest { public static void main(String[] args) { List<List<String>> maa = new ArrayList<List<String>>(); List<AlpacaCodeRelation> acrList = new ArrayList<AlpacaCodeRelation>(); for(int i=0;i<6;i++) { AlpacaCodeRelation acr = new AlpacaCodeRelation(); if(i==1||i==4) { acr.setClassName("lxp0"); } else acr.setClassName("lxp"+i); acr.setMethodName("lxpm"+i); acr.setPackageName("lxpp"+i); acrList.add(acr); } List<AllInfoInPackage> topClassList = new ArrayList<AllInfoInPackage>(); for(AlpacaCodeRelation acr : acrList) { String packageName = acr.getPackageName(); String className = acr.getClassName(); String methodName = acr.getMethodName(); if(topClassList==null||topClassList.size()==0) { /* AllInfoInPackage allInfoInPackage = new AllInfoInPackage(); allInfoInPackage.setClassName(className); MethodInfoDto methodInfoDto = new MethodInfoDto(); methodInfoDto.setMethodName(methodName); List<MethodInfoDto> methodInfos = new ArrayList<MethodInfoDto>(); methodInfos.add(methodInfoDto); allInfoInPackage.setMethodInfos(methodInfos); topClassList.add(allInfoInPackage); System.out.println("haha:"+topClassList.toString());*/ addNewMethodData(acr, topClassList); } else { boolean isClassNameExist = false; for(AllInfoInPackage allInfoInPackage:topClassList) { List<MethodInfoDto> methodLists = new ArrayList<MethodInfoDto>(); MethodInfoDto newMethodInfoDto = new MethodInfoDto(); if(className.equals(allInfoInPackage.getClassName())) { methodLists = allInfoInPackage.getMethodInfos(); newMethodInfoDto.setMethodName(methodName); methodLists.add(newMethodInfoDto); // System.out.println("topClassList:"+topClassList.toString()); isClassNameExist = true; break; } } if(!isClassNameExist) { /* List<MethodInfoDto> methodLists = new ArrayList<MethodInfoDto>(); AllInfoInPackage allInfoInPackage = new AllInfoInPackage(); allInfoInPackage.setClassName(className); MethodInfoDto methodInfoDto = new MethodInfoDto(); methodInfoDto.setMethodName(methodName); methodLists.add(methodInfoDto); allInfoInPackage.setMethodInfos(methodLists); topClassList.add(allInfoInPackage);*/ addNewMethodData(acr,topClassList); System.out.println("topClassList2:"+topClassList.toString()); } } } } private void testtest() { String sql = "{'topic':'haha','priority':'p1','url':'http://sss'}"; JSON json = JSONObject.parseObject(sql); } private static void addNewMethodData(AlpacaCodeRelation acr,List<AllInfoInPackage> topClassList) { String className = acr.getClassName(); String methodName = acr.getMethodName(); AllInfoInPackage allInfoInPackage = new AllInfoInPackage(); allInfoInPackage.setClassName(className); MethodInfoDto methodInfoDto = new MethodInfoDto(); methodInfoDto.setMethodName(methodName); List<MethodInfoDto> methodInfos = new ArrayList<MethodInfoDto>(); methodInfos.add(methodInfoDto); allInfoInPackage.setMethodInfos(methodInfos); topClassList.add(allInfoInPackage); } }(2)
package test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSONObject; public class CopyOfListTest { public static void main(String[] args) { CopyOfListTest ll = new CopyOfListTest(); /** * 测试数据 * 1、全部都不同,也就是6个包名,6个class名,6个method名 * 2、有2个包名相同,但是类名和方法名不同的:p1==p2=1, * 3、有2个包名相同,类名也相同,但是2个方法不同 * 4、有2个包名相同,类名相同,方法相同 */ ll.test(3); } private void test(int k) { List<List<String>> maa = new ArrayList<List<String>>(); List<AlpacaCodeRelation> acrList = new ArrayList<AlpacaCodeRelation>(); if(k==1) { for(int i=0;i<6;i++) { AlpacaCodeRelation acr = new AlpacaCodeRelation(); acr.setPackageName("p"+i); acr.setClassName("c"+i); acr.setMethodName("m"+i); acrList.add(acr); } } else if(k==2) { for(int i=0;i<6;i++) { AlpacaCodeRelation acr = new AlpacaCodeRelation(); if(i==1||i==3) { acr.setPackageName("p1"); acr.setClassName("c"+i); acr.setMethodName("m"+i); } else { acr.setPackageName("p"+i); acr.setClassName("c"+i); acr.setMethodName("m"+i); } acrList.add(acr); } } else if(k==3) { for(int i=0;i<6;i++) { AlpacaCodeRelation acr = new AlpacaCodeRelation(); if(i==1||i==3) { acr.setPackageName("p1"); acr.setClassName("c"+i); acr.setMethodName("m"+i); } else if (i==2||i==4) { acr.setPackageName("p2"); acr.setClassName("c2"); acr.setMethodName("m"+i); } else { acr.setPackageName("p"+i); acr.setClassName("c"+i); acr.setMethodName("m"+i); } acrList.add(acr); } } else { for(int i=0;i<6;i++) { AlpacaCodeRelation acr = new AlpacaCodeRelation(); if(i==1||i==3) { acr.setPackageName("p1"); acr.setClassName("c"+i); acr.setMethodName("m"+i); } else if (i==2||i==4) { acr.setPackageName("p2"); acr.setClassName("c2"); acr.setMethodName("m2"); } else { acr.setPackageName("p"+i); acr.setClassName("c"+i); acr.setMethodName("m"+i); } acrList.add(acr); } } List<ReturnClassInfoDto> topClassList = new ArrayList<ReturnClassInfoDto>(); for(AlpacaCodeRelation acr : acrList) { String packageName = acr.getPackageName(); String className = acr.getClassName(); String methodName = acr.getMethodName(); if(topClassList==null||topClassList.size()==0) { ReturnClassInfoDto returnClassInfoDto = new ReturnClassInfoDto(); returnClassInfoDto.setClassName(className); returnClassInfoDto.setPackageName(packageName); List<String> methodLists = new ArrayList<String>(); methodLists.add(methodName); returnClassInfoDto.setMethodNames(methodLists); topClassList.add(returnClassInfoDto); System.out.println("haha:"+topClassList.toString()); } else { boolean isClassNameExist = false; for(ReturnClassInfoDto returnClassInfoDto:topClassList) { List<String> methodLists = new ArrayList<String>(); if(className.equals(returnClassInfoDto.getClassName())&&packageName.equals(returnClassInfoDto.getPackageName())) { methodLists = returnClassInfoDto.getMethodNames(); methodLists.add(methodName); returnClassInfoDto.setMethodNames(methodLists); System.out.println(returnClassInfoDto.toString()); System.out.println("topClassList:"+topClassList.toString()); isClassNameExist = true; break; } } if(!isClassNameExist) { List<String> methodLists = new ArrayList<String>(); ReturnClassInfoDto returnClassInfoDto = new ReturnClassInfoDto(); returnClassInfoDto.setClassName(className); returnClassInfoDto.setPackageName(packageName); methodLists.add(methodName); returnClassInfoDto.setMethodNames(methodLists); topClassList.add(returnClassInfoDto); System.out.println("topClassList2:"+topClassList.toString()); } } } List<ReturnPackageInfoDto> haha = changeToPackageListInfo(topClassList); String json = JSONObject.toJSONString(haha); System.out.println("haha"+k+":"+json); } private List<ReturnPackageInfoDto> changeToPackageListInfo(List<ReturnClassInfoDto> returnClassInfoList) { List<ReturnPackageInfoDto> topPackageList = new ArrayList<ReturnPackageInfoDto>(); for(ReturnClassInfoDto returnClassInfoDto:returnClassInfoList) { String packageName = returnClassInfoDto.getPackageName(); if(topPackageList==null||topPackageList.size()==0) { List<ReturnClassInfoDto> dataStore = new ArrayList<ReturnClassInfoDto>(); ReturnPackageInfoDto infoDto = new ReturnPackageInfoDto(); dataStore.add(returnClassInfoDto); infoDto.setPackageName(packageName); infoDto.setClassInfos(dataStore); topPackageList.add(infoDto); } else { boolean isPackageNameExist = false; for(ReturnPackageInfoDto returnPackageInfoDto:topPackageList) { List<ReturnClassInfoDto> middleProcess = new ArrayList<ReturnClassInfoDto>(); if(packageName.equals(returnPackageInfoDto.getPackageName())) { middleProcess = returnPackageInfoDto.getClassInfos(); middleProcess.add(returnClassInfoDto); isPackageNameExist = true; break; } } if(!isPackageNameExist) { List<ReturnClassInfoDto> middleProcess = new ArrayList<ReturnClassInfoDto>(); ReturnPackageInfoDto returnPackageInfoDto = new ReturnPackageInfoDto(); middleProcess.add(returnClassInfoDto); returnPackageInfoDto.setPackageName(packageName); returnPackageInfoDto.setClassInfos(middleProcess); topPackageList.add(returnPackageInfoDto); } } } return topPackageList; } }(3)
package test; import java.util.Date; public class AlpacaCodeRelation { private Long id; private Date gmtCreate; private Date gmtModified; private Integer crid; private String moduleName; private String job; private String methodName; private String methodDesc; private String className; private String packageName; private Integer env; private String appName; private Long methodDetailsId; private Long classDetailsId; private String isHidden; private String isDeleted; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Date getGmtCreate() { return gmtCreate; } public void setGmtCreate(Date gmtCreate) { this.gmtCreate = gmtCreate; } public Date getGmtModified() { return gmtModified; } public void setGmtModified(Date gmtModified) { this.gmtModified = gmtModified; } public Integer getCrid() { return crid; } public void setCrid(Integer crid) { this.crid = crid; } public String getModuleName() { return moduleName; } public void setModuleName(String moduleName) { this.moduleName = moduleName == null ? null : moduleName.trim(); } public String getJob() { return job; } public void setJob(String job) { this.job = job == null ? null : job.trim(); } public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName == null ? null : methodName.trim(); } public String getMethodDesc() { return methodDesc; } public void setMethodDesc(String methodDesc) { this.methodDesc = methodDesc == null ? null : methodDesc.trim(); } public String getClassName() { return className; } public void setClassName(String className) { this.className = className == null ? null : className.trim(); } public String getPackageName() { return packageName; } public void setPackageName(String packageName) { this.packageName = packageName == null ? null : packageName.trim(); } public Integer getEnv() { return env; } public void setEnv(Integer env) { this.env = env; } public String getAppName() { return appName; } public void setAppName(String appName) { this.appName = appName == null ? null : appName.trim(); } public Long getMethodDetailsId() { return methodDetailsId; } public void setMethodDetailsId(Long methodDetailsId) { this.methodDetailsId = methodDetailsId; } public Long getClassDetailsId() { return classDetailsId; } public void setClassDetailsId(Long classDetailsId) { this.classDetailsId = classDetailsId; } public String getIsHidden() { return isHidden; } public void setIsHidden(String isHidden) { this.isHidden = isHidden == null ? null : isHidden.trim(); } public String getIsDeleted() { return isDeleted; } public void setIsDeleted(String isDeleted) { this.isDeleted = isDeleted == null ? null : isDeleted.trim(); } }
package test; import java.util.List; /** * Created by zouping.lxp on 2016/1/20. */ public class AllInfoInPackage { private Long classDetailId; private String className; private String classDesc; private String classAttr; private List<MethodInfoDto> methodInfos; public Long getClassDetailId() { return classDetailId; } public void setClassDetailId(Long classDetailId) { this.classDetailId = classDetailId; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getClassDesc() { return classDesc; } public void setClassDesc(String classDesc) { this.classDesc = classDesc; } public String getClassAttr() { return classAttr; } public void setClassAttr(String classAttr) { this.classAttr = classAttr; } public List<MethodInfoDto> getMethodInfos() { return methodInfos; } public void setMethodInfos(List<MethodInfoDto> methodInfos) { this.methodInfos = methodInfos; } @Override public String toString() { return "{classDetailId=" + classDetailId + ", className=" + className + ", classDesc=" + classDesc + ", classAttr=" + classAttr + ", methodInfos=" + methodInfos + "}"; } }