2017-09-29 1 views
1

나는 다음과 같은 Domain 모델 파일은 당신이 좋아하는 경우에 제공 할 수있다 감안할 때 :읽기 파일 경로 섹션은 모델링하는

public class Report { 

    private String reportId; 
    private String reportName; 
    private String client; 
    private String format; 
    private Date cobDate; 
    private String filePath; 

} 

public enum ReportType { 

    PNL_REPORT("11", "pnlreport"), 
    BALANCE_SHEET("20", "balance"), 

    private final String reportId; 
    private final String name; 

    private ReportType(String reportId, String name) { 
     this.reportId = reportId; 
     this.name = name; 
    } 
} 

을 내가 아래 Service 구현이 제공 :

@Service 
public class FileService { 

    @Autowired 
    private FileRepository fileRepository; 


    public List<Report> getReport(String filePattern, String format) { 

     List<Report> reportDeliverables = new ArrayList<>(); 

     List<File> filesToSend = fileRepository.getFilesToSend(sourcePath, filePattern, format); 

     \\C:\pathToReports\ClientABC\COB28Sep2017\pnlreport.pdf 

     \\return list of report objects, initialised. 
    } 

} 

I을 Java 8을 사용하고 있으며 효율적으로 각 File 개체를 filesToSend ~ Report 개의 필드로 변환하는 방법을 알고 싶습니다.

reportID -> 열거가 아이디보고에서 파생

REPORTNAME (이름이 접미사없이 파일 이름과 일치) -> pnlreport.pdf을

클라이언트 -> clientABC

cobDate-> 28 9 월 2017

형식 -> PDF

적인 filePath -> C : \ pathToReports \ ClientABC \ COB28Sep2017 \ pnlreport.pdf

경로에는 항상 클라이언트, cob 날짜, 보고서 파일 이름 순으로 소스가 있습니다.

+0

reportID를 어떻게 작성합니까? 다른 정보는 regex 그룹을 사용하여 파일 경로에서 추출 할 수 있습니다. https://docs.oracle.com/javase/tutorial/essential/regex/groups.html –

+0

'ReportType' 열거 형'reportID'에 대한 매핑입니다. lookup에서'name'으로 변경되었습니다. – user2781389

답변

2

나는 당신의 문제를 undestand까지 그 해결책이 있습니다. ReportFile의 regex는 창과 비슷한 파일 경로 만 만족합니다. 다른 OS의 경우에는 다른 정규 표현식이 필요합니다.

enum ReportType { 

    PNL_REPORT("11", "pnlreport"), 
    BALANCE_SHEET("20", "balance"); 

    private final String reportId; 
    private final String name; 

    ReportType(String reportId, String name) { 
     this.reportId = reportId; 
     this.name = name; 
    } 

    static ReportType fromName(String name) { 
     return Arrays.stream(values()) 
      .filter(reportType -> reportType.name.equals(name)) 
      .findFirst() 
      .orElseThrow(RuntimeException::new); 
    } 

    public String getId() { 
     return reportId; 
    } 
} 

class ReportFile { 

    private static final Pattern WINDOWS_REGEX = 
     Pattern.compile(".+\\\\(Client[^\\\\]+)\\\\COB([^\\\\]+)\\\\([^\\\\.]+).([^\\\\.]+)"); 

    private static final Pattern UNIX_REGEX = 
     Pattern.compile(".+/(Client[^/]+)/COB([^/]+)/([^/.]+).([^/.]+)"); 

    private final File file; 

    public ReportFile(File file) { 
     this.file = file; 
    } 

    Report toReport() { 
     String filePath = file.getPath(); 
     Matcher matcher = WINDOWS_REGEX.matcher(filePath); 

     if (!matcher.find()) { 
      throw new RuntimeException("Invalid path for report: " + filePath); 
     } 

     String client = matcher.group(1); 
     Date cobDate; 
     try { 
      LocalDate cobLocalDate = 
       LocalDate.parse(matcher.group(2), DateTimeFormatter.ofPattern("dMMMy").withLocale(Locale.US)); 
      cobDate = Date.from(cobLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
     String reportName = matcher.group(3); 
     String id = ReportType.fromName(matcher.group(3)).getId(); 
     String extension = matcher.group(4); 

     return new Report(
      id, reportName, client, extension, cobDate, filePath 
     ); 
    } 
} 

class Ex { 
    public static void main(String[] args) { 
     List<Report> reportDeliverables = new ArrayList<>(); 

     List<File> filesToSend = ImmutableList.of(
      new File("C:\\pathToReports\\ClientABC\\COB28Sep2017\\pnlreport.pdf") 
     ); 

     System.out.println(filesToSend.stream() 
      .map(ReportFile::new) 
      .map(ReportFile::toReport) 
      .collect(Collectors.toList())); 
    } 
} 
+0

고맙습니다. :) 'ReportFile'을'Component' 클래스로 변환하고 config에서 regex를 읽는 것을 관리했습니다. 유닉스 경로 정규 표현식을 포함하고 문자열 "클라이언트"를 제거하지 않기 위해 답을 수정 해주시겠습니까 – user2781389

+0

유닉스 정규식을 추가했지만 클라이언트 문자열은 제거되지 않았습니다. '(Client [^ /] +)'는 Client 문자열이 첫 번째 그룹 안에 있음을 의미합니다. –

+0

@ user2781389 문제가 해결되면이 대답을 수락하십시오. –