package org.dbflute.intro.app.web.document.decomment;

import static org.dbflute.intro.app.web.document.decomment.DecommentPostBody.DecommentTablePart;

import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.dbflute.intro.app.logic.document.decomment.DocumentDecommentPhysicalLogic;
import org.dbflute.intro.app.model.document.decomment.DfDecoMapPiece;
import org.dbflute.intro.app.model.document.decomment.parts.DfDecoMapColumnPart;
import org.dbflute.intro.app.model.document.decomment.parts.DfDecoMapTablePart;
import org.dbflute.intro.app.web.base.IntroBaseAction;
import org.lastaflute.core.time.TimeManager;
import org.lastaflute.web.Execute;
import org.lastaflute.web.response.JsonResponse;

// TODO done cabos decoment to decomment by jflute (2017/07/27)
/**
 * @author cabos
 */
public class DocumentDecommentAction extends IntroBaseAction {

    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    @Resource
    private TimeManager timeManager;
    @Resource
    private DocumentDecommentPhysicalLogic decommentPhysicalLogic;

    // ===================================================================================
    //                                                                           Piece Map
    //                                                                           =========
    // TODO done cabos post to save, get to diff by jflute (2017/07/27)
    @Execute(urlPattern = "{}/@word")
    public JsonResponse<Void> save(String projectName, DecommentPostBody body) {
        validate(body, messages -> {});
        decommentPhysicalLogic.saveDecommentPieceMap(projectName, convertBodyToDecoMapPiece(body));
        return JsonResponse.asEmptyBody();
    }

    private DfDecoMapPiece convertBodyToDecoMapPiece(DecommentPostBody body) {
        DfDecoMapPiece pieceMap = new DfDecoMapPiece();
        pieceMap.setFormatVersion("1.0");
        pieceMap.setAuthor(getAuthor());
        pieceMap.setDecommentDatetime(timeManager.currentDateTime());
        pieceMap.setMerged(body.merged);
        pieceMap.setDecoMap(convertTablePartToDecoMapPiece(body.table));
        return pieceMap;
    }

    private String getAuthor() {
        return decommentPhysicalLogic.getAuthorFromGitSystem();
    }

    private DfDecoMapTablePart convertTablePartToDecoMapPiece(DecommentTablePart tablePart) {
        DfDecoMapTablePart tablePartMap = new DfDecoMapTablePart();
        tablePartMap.setTableName(tablePart.tableName);
        tablePartMap.setColumns(tablePart.columns.stream().map(columnPart -> {
            DfDecoMapColumnPart columnPartMap = new DfDecoMapColumnPart();
            columnPartMap.setColumnName(columnPart.columnName);
            columnPartMap.setDecomment(columnPart.decomment);
            columnPartMap.setDatabaseComment(columnPart.databaseComment);
            columnPartMap.setPreviousWholeComment(columnPart.previousWholeComment);
            columnPartMap.setCommentVersion(columnPart.commentVersion);
            columnPartMap.setAuthorList(columnPart.authorList);
            return columnPartMap;
        }).collect(Collectors.toList()));
        return tablePartMap;
    }

    // ===================================================================================
    //                                                                          Pickup Map
    //                                                                          ==========
    @Execute(urlPattern = "{}/@word")
    public JsonResponse<Void> piece(String projectName) {
        // TODO hakiba create new decomment response (2017/07/20)
        return JsonResponse.asEmptyBody();
    }
}

@@ -144,16 +144,133 @@
 blePart;
+%0Aimport static org.dbflute.intro.app.web.document.decomment.DecommentPostBody.DecommentTablePart.DecommentColumnPart;
 %0A%0Aimport
@@ -2873,28 +2873,212 @@
 map(
-columnPart -%3E %7B%0A    
+this::convertColumnPartToDecoMapPiece).collect(Collectors.toList()));%0A        return tablePartMap;%0A    %7D%0A%0A    private DfDecoMapColumnPart convertColumnPartToDecoMapPiece(DecommentColumnPart columnPart) %7B%0A
     
@@ -3144,36 +3144,32 @@
 Part();%0A        
-    
 columnPartMap.se
@@ -3204,36 +3204,32 @@
 nName);%0A        
-    
 columnPartMap.se
@@ -3262,36 +3262,32 @@
 mment);%0A        
- 
-   
 columnPartMap.se
@@ -3324,36 +3324,32 @@
 tabaseComment);%0A
-    
         columnPa
@@ -3412,36 +3412,32 @@
 mment);%0A        
-  
-  
 columnPartMap.se
@@ -3484,28 +3484,24 @@
 n);%0A        
-    
 columnPartMa
@@ -3544,28 +3544,24 @@
 t);%0A        
-   
- 
 return colum
@@ -3574,79 +3574,8 @@
 ap;%0A
-        %7D).collect(Collectors.toList()));%0A        return tablePartMap;%0A
     

refactor DocumentDecommentAction