org.dbflute.infra.doc.decomment.DfDecoMapFile Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dbflute-runtime Show documentation
Show all versions of dbflute-runtime Show documentation
The runtime library of DBFlute
/*
* Copyright 2014-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.dbflute.infra.doc.decomment;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.dbflute.helper.HandyDate;
import org.dbflute.helper.mapstring.MapListFile;
import org.dbflute.helper.message.ExceptionMessageBuilder;
import org.dbflute.infra.doc.decomment.exception.DfDecoMapFileReadFailureException;
import org.dbflute.infra.doc.decomment.exception.DfDecoMapFileWriteFailureException;
import org.dbflute.infra.doc.decomment.parts.DfDecoMapColumnPart;
import org.dbflute.infra.doc.decomment.parts.DfDecoMapPropertyPart;
import org.dbflute.infra.doc.decomment.parts.DfDecoMapTablePart;
import org.dbflute.optional.OptionalThing;
import org.dbflute.util.DfStringUtil;
import org.dbflute.util.DfTypeUtil;
// done cabos DfDecoMapFile by jflute (2017/07/27)
// done cabos add copyright in source file header like this class to classes of infra.doc.decomment by jflute (2017/11/11)
/**
* @author cabos
* @author hakiba
* @author jflute
*/
public class DfDecoMapFile {
// ===================================================================================
// Definition
// ==========
// e.g. dbflute_maihamadb/scheme/decomment/
private static final String BASE_DECOMMENT_DIR_PATH = "/schema/decomment/";
// e.g. dbflute_maihamadb/scheme/decomment/piece/
private static final String BASE_PICKUP_DIR_PATH = BASE_DECOMMENT_DIR_PATH + "piece/";
// e.g. dbflute_maihamadb/scheme/decomment/pickup/decomment-pickup.dfmap
private static final String BASE_PIECE_FILE_PATH = BASE_DECOMMENT_DIR_PATH + "pickup/decomment-pickup.dfmap";
private static final Map REPLACE_CHAR_MAP;
static {
// done cabos add spaces and replaceChar should be underscore? by jflute (2017/09/07)
List notAvailableCharList = Arrays.asList("/", "\\", "<", ">", "*", "?", "\"", "|", ":", ";", "\0", " ");
String replaceChar = "_";
REPLACE_CHAR_MAP = notAvailableCharList.stream().collect(Collectors.toMap(ch -> ch, ch -> replaceChar));
}
// ===================================================================================
// Read
// ======
// -----------------------------------------------------
// Piece
// -----
// done yuto write e.g. (2017/11/11)
// map:{
// ; formatVersion = 1.0
// ; tableName = MEMBER
// ; columnName = null
// ; targetType = TABLE
// ; decomment = loginable user, my name is deco
// ; databaseComment = loginable user
// ; commentVersion = 0
// ; authorList = list:{ deco }
// ; pieceCode = AL3OR1P
// ; pieceDatetime = 2017-12-31T12:34:56.789
// ; pieceOwner = deco
// ; previousPieceList = list:{}
// }
// map:{
// ; formatVersion = 1.0
// ; tableName = MEMBER
// ; columnName = MEMBER_NAME
// ; targetType = COLUMN
// ; decomment = sea mystic land oneman
// ; databaseComment = sea mystic
// ; commentVersion = 1
// ; authorList = list:{ cabos ; hakiba ; deco ; jflute }
// ; pieceCode = HF7ELSE
// ; pieceDatetime = 2017-10-15T16:17:18.199
// ; pieceOwner = jflute
// ; previousPieceList = list:{ FE893L1 }
// }
// done cabos I just noticed that this should be readPieceList()... by jflute (2017/11/18)
// done cabos write javadoc by jflute (2017/11/18)
/**
* Read all decomment piece map file in "clientDirPath/schema/decomment/piece/".
* @param clientDirPath The path of DBFlute client directory (NotNull)
* @return List of all decomment piece map (NotNull: If piece map file not exists, returns empty list)
*/
public List readPieceList(String clientDirPath) {
assertClientDirPath(clientDirPath);
String pieceDirPath = buildPieceDirPath(clientDirPath);
if (Files.notExists(Paths.get(pieceDirPath))) {
return Collections.emptyList();
}
try {
return Files.list(Paths.get(pieceDirPath)).filter(path -> path.toString().endsWith(".dfmap")).map(path -> {
return doReadPiece(path);
}).collect(Collectors.toList());
} catch (IOException e) {
throwDecoMapReadFailureException(pieceDirPath, e);
return Collections.emptyList(); // unreachable
}
}
// done cabos DBFlute uses doRead...() style for internal process so please change it by jflute (2017/11/18)
private DfDecoMapPiece doReadPiece(Path path) {
final MapListFile mapListFile = createMapListFile();
try {
Map map = mapListFile.readMap(Files.newInputStream(path));
return mappingToDecoMapPiece(map);
} catch (RuntimeException e) {
throwDecoMapReadFailureException(path.toString(), e);
return null; // unreachable
} catch (IOException e) {
throwDecoMapReadFailureException(path.toString(), e);
return null; // unreachable
}
}
// done hakiba cast check by hakiba (2017/07/29)
private DfDecoMapPiece mappingToDecoMapPiece(Map map) {
String formatVersion = (String) map.get("formatVersion");
String tableName = (String) map.get("tableName");
String columnName = (String) map.get("columnName");
DfDecoMapPieceTargetType targetType = DfDecoMapPieceTargetType.of(map.get("targetType")).get();
String decomment = (String) map.get("decomment");
String databaseComment = (String) map.get("databaseComment");
Long commentVersion = Long.valueOf(map.get("commentVersion").toString());
@SuppressWarnings("unchecked")
List authorList = (List) map.get("authorList");
String pieceCode = (String) map.get("pieceCode");
LocalDateTime pieceDatetime = new HandyDate((String) map.get("pieceDatetime")).getLocalDateTime();
String pieceOwner = (String) map.get("pieceOwner");
@SuppressWarnings("unchecked")
List previousPieceList = (List) map.get("previousPieceList");
return new DfDecoMapPiece(formatVersion, tableName, columnName, targetType, decomment, databaseComment, commentVersion, authorList,
pieceCode, pieceDatetime, pieceOwner, previousPieceList);
}
// -----------------------------------------------------
// Pickup
// ------
// map:{
// ; formatVersion = 1.0
// ; pickupDatetime = 2017-11-09T09:09:09.009
// ; decoMap = map:{
// ; tableList = list:{
// ; map:{
// ; tableName = MEMBER
// ; propertyList = list:{
// ; map:{
// ; decomment = first decomment
// ; databaseComment = ...
// ; commentVersion = ...
// ; authorList = list:{ deco }
// ; pieceCode = DECO0000
// ; pieceDatetime = 2017-11-05T00:38:13.645
// ; pieceOwner = cabos
// ; previousPieceList = list:{}
// }
// ; map:{ // propertyList size is more than 2 if decomment conflicts exists
// ; ...
// }
// }
// ; columnList = list:{
// ; map:{
// ; columnName = MEMBER_NAME
// ; propertyList = list:{
// ; map:{
// ; decomment = sea mystic land oneman
// ; databaseComment = sea mystic
// ; commentVersion = 1
// ; authorList = list:{ cabos, hakiba, deco, jflute }
// ; pieceCode = HAKIBA00
// ; pieceDatetime = 2017-11-05T00:38:13.645
// ; pieceOwner = cabos
// ; previousPieceList = list:{ JFLUTE00, CABOS000 }
// }
// }
// }
// ; ... // more other columns
// }
// }
// ; map:{ // Of course, other table decomment info is exists that
// ; tableName = MEMBER_LOGIN
// ; ...
// }
// }
// }
// }
// done hakiba sub tag comment by jflute (2017/08/17)
/**
* Read decomment pickup map file at "clientDirPath/schema/decomment/pickup/decomment-pickup.dfmap".
* @param clientDirPath The path of DBFlute client directory (NotNull)
* @return pickup decomment map (NotNull: If pickup map file not exists, returns empty)
*/
public OptionalThing readPickup(String clientDirPath) {
assertClientDirPath(clientDirPath);
String filePath = buildPickupFilePath(clientDirPath);
if (Files.notExists(Paths.get(filePath))) {
// done hakiba null pointer so use optional thing and stream empty by jflute (2017/10/05)
return OptionalThing.empty();
}
return OptionalThing.ofNullable(doReadPickup(Paths.get(filePath)), () -> {});
}
private DfDecoMapPickup doReadPickup(Path path) {
MapListFile mapListFile = createMapListFile();
try {
Map map = mapListFile.readMap(Files.newInputStream(path));
return mappingToDecoMapPickup(map);
} catch (RuntimeException e) {
throwDecoMapReadFailureException(path.toString(), e);
return null; // unreachable
} catch (IOException e) {
throwDecoMapReadFailureException(path.toString(), e);
return null; // unreachable
}
}
private DfDecoMapPickup mappingToDecoMapPickup(Map map) {
String formatVersion = (String) map.getOrDefault("formatVersion", DfDecoMapPickup.DEFAULT_FORMAT_VERSION);
LocalDateTime pickupDatetime = DfTypeUtil.toLocalDateTime(map.get("pickupDatetime"));
DfDecoMapPickup pickup = new DfDecoMapPickup(formatVersion);
pickup.setPickupDatetime(pickupDatetime);
@SuppressWarnings("unchecked")
Map>> decoMap =
(Map>>) map.getOrDefault("decoMap", new LinkedHashMap<>());
if (decoMap.isEmpty()) {
return pickup;
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy