package com.freetmp.mbg.shellcallback;

import com.freetmp.mbg.merge.JavaSourceUtils;
import com.github.javaparser.ast.CompilationUnit;
import org.apache.commons.io.FileUtils;
import org.mybatis.generator.exception.ShellException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/*
 * Created by pin on 2015/2/7.
 */
public class MergeSupportedShellCallback extends DefaultShellCallback {

    static final Logger LOGGER = LoggerFactory.getLogger(MergeSupportedShellCallback.class);

    /*
     * @param overwrite if true overwrite the existed file
     */
    public MergeSupportedShellCallback(boolean overwrite) {
        super(overwrite);
    }

    @Override
    public boolean isMergeSupported() {
        return true;
    }

    @Override
    public String mergeJavaFile(String newFileSource, String existingFileFullPath, String[] javadocTags, String fileEncoding) throws ShellException {
        String mergedFileSource = newFileSource;
        LOGGER.info("merge java source file for {}",existingFileFullPath);
        try {
            CompilationUnit source = JavaSourceUtils.generateAst(newFileSource);
            CompilationUnit destination = JavaSourceUtils.generateAst(FileUtils.readFileToString(new File(existingFileFullPath),fileEncoding));
            if(destination != null && source != null) {
                mergedFileSource = JavaSourceUtils.mergeContent(source, destination);
            }
        } catch (Exception e) {
            LOGGER.info("java source merge failed: {}",e);
            throw new ShellException(e);
        }

        return mergedFileSource;
    }

}

@@ -66,113 +66,29 @@
 rge.
-JavaSourceUtils;%0Aimport com.github.javaparser.ast.CompilationUnit;%0Aimport org.apache.commons.io.FileUtils
+CompilationUnitMerger
 ;%0Aim
@@ -262,30 +262,8 @@
 y;%0A%0A
-import java.io.File;%0A%0A
 /*%0A 
@@ -369,18 +369,16 @@
 back %7B%0A%0A
-  
   static
@@ -465,17 +465,13 @@
 );%0A%0A
-  
   /*%0A
-  
    *
@@ -530,15 +530,11 @@
 %0A   
-  
 */%0A
-  
   pu
@@ -591,20 +591,16 @@
 ) %7B%0A    
-  
-  
 super(ov
@@ -609,25 +609,21 @@
 write);%0A
-  
   %7D%0A%0A
-  
   @Overr
@@ -618,34 +618,32 @@
  %7D%0A%0A  @Override%0A
-  
   public boolean
@@ -660,28 +660,24 @@
 pported() %7B%0A
-    
     return t
@@ -681,25 +681,21 @@
 n true;%0A
-  
   %7D%0A%0A
-  
   @Overr
@@ -698,18 +698,16 @@
 verride%0A
-  
   public
@@ -846,20 +846,16 @@
 ption %7B%0A
-    
     Stri
@@ -891,28 +891,24 @@
 Source;%0A    
-  
-  
 LOGGER.info(
@@ -939,16 +939,17 @@
 for %7B%7D%22,
+ 
 existing
@@ -963,20 +963,16 @@
 lPath);%0A
-    
     try 
@@ -983,189 +983,70 @@
     
-     
- CompilationUnit source = JavaSourceUtils.generateAst(newFileSource);%0A            CompilationUnit destination = JavaSourceUtils.generateAst(FileUtils.readFileToString
+mergedFileSource =  CompilationUnitMerger.merge
 (new
- 
 File
-(
+Source,
 exis
@@ -1066,185 +1066,10 @@
 ath)
-,fileEncoding));%0A            if(destination != null && source != null) %7B%0A                mergedFileSource = JavaSourceUtils.mergeContent(source, destination);%0A            %7D%0A    
+;%0A
     
@@ -1092,22 +1092,16 @@
 on e) %7B%0A
-      
       LO
@@ -1141,16 +1141,17 @@
 ed: %7B%7D%22,
+ 
 e);%0A    
@@ -1152,22 +1152,16 @@
 ;%0A      
-    
-  
 throw ne
@@ -1185,23 +1185,15 @@
 e);%0A
-    
     %7D%0A%0A
-    
     
@@ -1217,17 +1217,15 @@
 Source;%0A
-  
   %7D%0A%0A%7D%0A

make shell callback invoke the merger instead of utils