标签:需要 file alt pattern 低耦合 应用 文件类型 过程 sim
1 /** 2 * 根据机构给定文件名模式,按照批次日期生成对应机构的交互文件名 3 * @param institution 4 * @param fileNamePattern 5 * @param batchDate 6 */ 7 public String fetchFileName(String institution, String fileNamePattern, Date batchDate) { 8 String fileName = null; 9 if(institution.equal("TA")) { 10 //交易所文件,8位日期,3位批次号 11 String fileKeyPatternWithDate = String 12 .format(fileKeyPattern.replace("(\\d{8})", "%1$tY%1$tm%1$td"), new Date()); 13 int seq = sequenceDAO.fetchSequence(fileKeyPatternWithDate); 14 fileName = fileKeyPatternWithDate.replace("(\\d{3})", String.format("%03d", seq)); 15 fileName = fileName.substring(fileName.lastIndexOf(File.separator)+1); 16 } else if(institution.equal("Com")){ 17 //渠道文件,8位日期,4位渠道号 18 fileName = String.format(fileKeyPattern.replace("(\\d{8})", "%1$tY%1$tm%1$td"), new Date()); 19 fileName.replace("(\\d{4})", InstitutionCode.Com.code()); 20 } 21 return fileName; 22 } 23 24 /** 25 * 可能有各种原因,公司没有为所有应用部署集中的ftp服务器或者纯粹只是建立一个临时方案,导致你需要针对不同的机构配置不同的ftp交互策略。 26 * 于是你可能有如下两个获取ftp download和upload目录的方法。 27 */ 28 public String getDownloadFileDir(String institution, Date batchDate){ 29 String date = new SimpleDateFormat("yyyyMMdd").format(batchDate); 30 //sftpBaseDir是使用map存放的关于各个机构交互文件的sftp父级目录 31 if(institution.equal("TA")){ 32 return sftpBaseDir.get(institution) + "/" + date; 33 } else if(institution.equal("Com")){ 34 return sftpBaseDir.get(institution) + "/download/" + date; 35 } 36 37 return null; 38 } 39 public String getUploadFileDir(String institution, Date batchDate){ 40 String date = new SimpleDateFormat("yyyyMMdd").format(batchDate); 41 //sftpBaseDir是使用map存放的关于各个机构交互文件的sftp父级目录 42 if(institution.equal("TA")){ 43 return sftpBaseDir.get(institution) + "/" + date; 44 } else if(institution.equal("Com")){ 45 return sftpBaseDir.get(institution) + "/upload/" + date; 46 } 47 return null; 48 } 49 50 /** 51 * 由于文件模式固定,你的业务需求里很可能期望能通过对应机构的文件名就能推断该文件的一些详细信息. 52 */ 53 public InstitutionFile inferFileInfoByFilename(String institution, String filename){ 54 String filepattern = null; 55 if(institution.equal("TA")){ 56 String patterns[] = fileName.split("\\d{8}",2); 57 String tmpPattern = patterns[0] + "(\\d{8})" + patterns[1]; 58 patterns = tmpPattern.split("\\d{3}[.]", 2); 59 filepattern = patterns[0] + "(\\d{3})." + patterns[1]; 60 } else if(institutuion.equal("Com")){ 61 String patterns[] = fileName.split("\\d{8}", 2); 62 String tmpPattern = patterns[0] + "(\\d{8})" + patterns[1]; 63 patterns = tmpPattern.split("\\d{4}", 2); 64 filepattern = patterns[0] + "(\\d{4})" + patterns[1]; 65 } 66 InstitutionFile institutionFile = institutionRepository. 67 findFileByIdentity(institution.filePattern(file.getName()), institution); 68 return institutionFile; 69 }
1 public enum Institution implements IInstitution, IFile { 2 3 TA("交易所"){ 4 @Override 5 public Type type() { 6 return Type.TA; 7 } 8 9 @Override 10 public String fileNameWithoutSeq(String filePattern, Date batchDate) { 11 return String.format(filePattern.replace("(\\d{8})", "%1$tY%1$tm%1$td"), batchDate); 12 } 13 14 @Override 15 public String filePattern(String fileName) { 16 String patterns[] = fileName.split("\\d{8}",2); 17 String filePattern = patterns[0] + "(\\d{8})" + patterns[1]; 18 patterns = filePattern.split("\\d{3}[.]", 2); 19 return patterns[0] + "(\\d{3})." + patterns[1]; 20 } 21 22 @Override 23 public String downloadFilePath(String basePath, Date batchDate) { 24 String date = new SimpleDateFormat("yyyyMMdd").format(batchDate); 25 return basePath + "/" + date; 26 } 27 28 @Override 29 public String uploadFilePath(String basePath, Date batchDate) { 30 String date = new SimpleDateFormat("yyyyMMdd").format(batchDate); 31 return basePath + "/" + date; 32 } 33 34 @Override 35 public boolean multiBatch() { 36 return true; 37 } 38 39 @Override 40 public String fileNameWithSeq(String fileNamePatternWithDate, int seq) { 41 if(!multiBatch()) 42 throw new UnsupportedOperationException("该机构不支持一天多批次文件"); 43 return fileNamePatternWithDate.replace("(\\d{3})", String.format("%03d", seq)); 44 } 45 }, 46 Com("渠道") { 47 @Override 48 public Type type() { 49 return Type.Channel; 50 } 51 52 @Override 53 public String fileNameWithoutSeq(String filePattern, Date batchDate) { 54 String fileKeyPatternWithDate = String.format(filePattern.replace("(\\d{8})", "%1$tY%1$tm%1$td"), 55 batchDate); 56 return fileKeyPatternWithDate.replace("(\\d{4})", ChannelCode.typeOf(this).getCode()); 57 } 58 59 @Override 60 public String filePattern(String fileName) { 61 String patterns[] = fileName.split("\\d{8}", 2); 62 String filePattern = patterns[0] + "(\\d{8})" + patterns[1]; 63 patterns = filePattern.split("\\d{4}", 2); 64 return patterns[0] + "(\\d{4})" + patterns[1]; 65 } 66 67 @Override 68 public String fileNameWithSeq(String fileNamePatternWithDate, int seq) { 69 throw new UnsupportedOperationException("该机构不支持一天多批次文件"); 70 } 71 }; 72 73 private String text; 74 Institution(String text){ 75 this.text = text; 76 } 77 public String getText(){ 78 return this.text; 79 } 80 public String getAbbr(){ 81 return this.toString().toLowerCase(); 82 } 83 84 @Override 85 public Institution type() { 86 return this; 87 } 88 89 @Override 90 public InstitutionCode institutionCode() { 91 return InstitutionCode.typeOf(this); 92 } 93 94 public static Institution codeOf(String institution){ 95 for(Institution ins : Institution.values()){ 96 if(ins.getAbbr().equals(institution.toLowerCase())){ 97 return ins; 98 } 99 } 100 throw new IllegalArgumentException("不支持机构"); 101 } 102 103 @Override 104 public DateFormat fileNameDateFormat() { 105 return new SimpleDateFormat("yyyyMMdd"); 106 } 107 108 @Override 109 public boolean multiBatch() { 110 return false; 111 } 112 113 @Override 114 public String downloadFilePath(String basePath, Date batchDate) { 115 String date = new SimpleDateFormat("yyyyMMdd").format(batchDate); 116 return basePath + "/download/" + date; 117 } 118 119 @Override 120 public String uploadFilePath(String basePath, Date batchDate) { 121 String date = new SimpleDateFormat("yyyyMMdd").format(batchDate); 122 return basePath + "/upload/" + date; 123 } 124 125 }
1 public String fetchFileName(Institution institution, String fileKeyPattern, Date batchDate) { 2 String fileNamePatternWithDate = institution.fileNameWithoutSeq(fileKeyPattern, batchDate); 3 if(institution.multiBatch()) { 4 int seq = sequenceDAO.fetchSequence(institution + File.separator + fileNamePatternWithDate); 5 return institution.fileNameWithSeq(fileNamePatternWithDate, seq); 6 } 7 return fileNamePatternWithDate; 8 } 9 10 public String getUploadFileDir(Institution institution, Date batchDate){ 11 return institution.uploadFilePath(sftpBaseDir.get(institution.getAbbr()), batchDate); 12 } 13 public String getDownloadFileDir(Institution institution, Date batchDate){ 14 return institution.downloadFilePath(sftpBaseDir.get(institution.getAbbr()), batchDate); 15 } 16 17 public InstitutionFile inferFileInfoByFilename(Institution institution, String filename){ 18 InstitutionFile institutionFile = institutionRepository. 19 findFileByIdentity(institution.filePattern(filename), institution); 20 return institutionFile; 21 }
消除if-else/switch语句块来聚合模型的设计与实现
标签:需要 file alt pattern 低耦合 应用 文件类型 过程 sim
原文地址:http://www.cnblogs.com/shenjixiaodao/p/7230123.html