org.dbmfs.DbmfsUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.dbmfs.DbmfsUtil.java

Source

package org.dbmfs;

import java.util.*;
import java.io.*;
import java.lang.reflect.*;
import java.math.BigDecimal;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.SerializationFeature;

import org.dbmfs.params.*;

/**
 * DbmFS??Util.br
 *
 * @author okuyamaoo
 * @license Apache License
 */
public class DbmfsUtil {

    public static String fileNameLastString = ".json";
    public static String pathSeparator = "/";

    /**
     * ???Top???
     *
     * @param path 
     * @return true:Top / false:Top????
     */
    public static boolean isTopDirectoryPath(String path) {
        if (path != null && path.trim().equals("/"))
            return true;
        return false;
    }

    /**
     * ?fstat????
     * @return ?1
     */
    public static String createDirectoryInfoTemplate() {
        StringBuilder strBuf = new StringBuilder();

        strBuf.append("dir");
        strBuf.append("\t");
        strBuf.append("1");
        strBuf.append("\t");
        strBuf.append("0");
        strBuf.append("\t");
        strBuf.append("0");
        strBuf.append("\t");
        strBuf.append("0");
        strBuf.append("\t");
        strBuf.append("1435098875");
        strBuf.append("\t");
        strBuf.append("0");
        strBuf.append("\t");
        strBuf.append("493");
        strBuf.append("\t");
        strBuf.append("0");
        strBuf.append("\t");
        strBuf.append("24576974580222");
        return strBuf.toString();
    }

    /**
     * ?fstat????
     * @return ?1
     */
    public static String createFileInfoTemplate(int dataSize) {
        StringBuilder strBuf = new StringBuilder();

        strBuf.append("file");
        strBuf.append("\t");
        strBuf.append("1");
        strBuf.append("\t");
        strBuf.append("0");
        strBuf.append("\t");
        strBuf.append("0");
        strBuf.append("\t");
        strBuf.append(dataSize);
        strBuf.append("\t");
        strBuf.append("1435098875");
        strBuf.append("\t");
        strBuf.append("1");
        strBuf.append("\t");
        strBuf.append("33188");
        strBuf.append("\t");
        strBuf.append("0");
        strBuf.append("\t");
        strBuf.append("24576974580222");
        strBuf.append("\t");
        strBuf.append("0");
        return strBuf.toString();
    }

    /**
     * ??????????????????
     * key = "/tbl1/Pkey1-AAA.json"<br>
     * ?= String[0]:tbl1, String[1]:Pkey1-AAA.json<br>
     *
     * key = "/tbl1"<br>
     * ?= String[0]:tbl1<br>
     *
     * @return ????????????????
     */
    public static String[] splitTableNameAndPKeyCharacter(String key) {
        String[] splitPath = key.split("/");
        List<String> replaceList = new ArrayList();
        for (int idx = 0; idx < splitPath.length; idx++) {
            if (!splitPath[idx].trim().equals("")) {
                replaceList.add(splitPath[idx]);
            }
        }

        splitPath = replaceList.toArray(new String[0]);
        return splitPath;
    }

    public static String[] deserializeInfomationString(String infomationString) {
        return infomationString.split("\t");
    }

    /**
     * ?MapJson.<br>
     *
     * @return Json
     */
    public static String jsonSerialize(Object target) throws JsonProcessingException {
        return jsonSerialize(target, true);
    }

    public static String jsonSerialize(Object target, boolean format) throws JsonProcessingException {
        ObjectMapper mapper = null;

        // 
        if (format) {

            mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
        } else {
            mapper = new ObjectMapper();
        }

        return mapper.writeValueAsString(target);
    }

    /**
     * ?JsonObject.<br>
     *
     * @param Json
     * @return ?Object
     */
    public static List<Map> jsonDeserialize(String jsonString) throws IOException, JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(jsonString, List.class);
    }

    /**
     * ?JsonObject.<br>
     *
     * @param Json
     * @return ?Object
     */
    public static Map<String, Object> jsonDeserializeSingleObject(String jsonString)
            throws IOException, JsonProcessingException {
        String[] strSplit = jsonString.split("\"__DBMFS_TABLE_META_INFOMATION\"");

        String convertDataString = strSplit[0].trim();
        convertDataString = convertDataString.substring(1, (convertDataString.length() - 1));

        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(convertDataString + "}", Map.class);
    }

    /**
     * fuse-j?????dbmfs????????
     * ??/tablename/100_200/file.json?????/tablename/file.json?????
     * /tablename/100_200/tablename/100_?/tablename?????
     * @param path
     * @return ?
     */
    public static String convertRealPath(String path) {
        // Top??????????????????
        if (path.equals("/") || path.matches(".*/[0-9]*$"))
            return path;

        // ?"/100_""/100_200"??????
        if (path.matches(".*/[0-9_]*$") || path.matches(".*/[0-9_]*/$")) {

            // ?/100_
            String[] pathSplit = path.split("/");
            StringBuilder pathBuf = new StringBuilder();
            pathBuf.append("/");
            pathBuf.append(pathSplit[1]);
            return pathBuf.toString();
        }

        if (path.matches(".*/[0-9_]*$_[0-9_]*$") || path.matches(".*/[0-9_]*$_[0-9_]*/$")) {
            // ?/100_
            String[] pathSplit = path.split("/");
            StringBuilder pathBuf = new StringBuilder();
            pathBuf.append("/");
            pathBuf.append(pathSplit[1]);
            return pathBuf.toString();
        }

        // ??offset limit ???????
        if (path.matches(".*/[0-9_]*/.*json$") == true || path.matches(".*/[0-9_]*_[0-9_]*/.*json$") == true) {
            // ??? offset limit?
            // offset limit
            // Path?? /tablename/100_/filename.json???????
            String[] pathSplit = path.split("/");
            StringBuilder pathBuf = new StringBuilder();
            for (int idx = 1; idx < pathSplit.length; idx++) {
                if (idx != 2) {
                    pathBuf.append("/");
                    pathBuf.append(pathSplit[idx]);
                }
            }
            return pathBuf.toString();
        }
        return path;
    }

    /**
     * ??limit/offset?????TargetDirectoryParams?
     *
     * @param pathCharacter (/dirname/100_??)
     * @return TargetDirectoryParams
     */
    public static TargetDirectoryParams parseTargetDirectoryPath(String pathCharacter) {
        if (pathCharacter.trim().equals("") || pathCharacter.trim().equals("/"))
            return new TargetDirectoryParams("/");

        String[] pathCharacterSplit = pathCharacter.split(pathSeparator);
        if (pathCharacterSplit[pathCharacterSplit.length - 1].matches("[0-9_]*$")) {

            // ?"_"?????????limit. offset?????
            String offsetLimitStr = pathCharacterSplit[pathCharacterSplit.length - 1];

            String[] offsetLimitSplit = offsetLimitStr.split("_");

            int limit = Integer.MAX_VALUE;
            int offset = 0;

            // ?"/100_200" ("100_"????)
            List<String> offsetLimitList = new ArrayList();
            for (String str : offsetLimitSplit) {
                if (!str.trim().equals("")) {
                    offsetLimitList.add(str);
                }
            }

            // ??????
            StringBuilder targetPath = new StringBuilder();
            for (int idx = 0; idx < pathCharacterSplit.length - 1; idx++) {
                if (!pathCharacterSplit[idx].trim().equals("")) {
                    targetPath.append("/");
                    targetPath.append(pathCharacterSplit[idx]);
                }
            }

            // offset limit ???????????
            if (offsetLimitList.size() == 1) {

                // offset??
                return new TargetDirectoryParams(targetPath.toString(),
                        new Integer(offsetLimitList.get(0)).intValue(), Integer.MAX_VALUE);
            } else if (offsetLimitList.size() == 2) {

                // offset, limit
                return new TargetDirectoryParams(targetPath.toString(),
                        new Integer(offsetLimitList.get(0)).intValue(),
                        (new Integer(offsetLimitList.get(1)).intValue()
                                - new Integer(offsetLimitList.get(0)).intValue()));
            }

            return new TargetDirectoryParams(pathCharacter);
        } else {

            // ?????????????????
            return new TargetDirectoryParams(pathCharacter);
        }
    }

    /**
     * ?????.<br>
     * tableName = "tbl1"<br>
     * pKeyConcatStr = "Pkey1-AAA"<br>
     * ?= /tbl1/Pkey1-AAA.json<br>
     *
     * @return 
     */
    public static String createFileFullPathString(String tableName, String pKeyConcatStr) {

        return "/" + tableName + "/" + pKeyConcatStr + DbmfsUtil.fileNameLastString;
    }

    /**
     * ????????????.<br>
     * fileName = "Pkey1-AAA.json"<br>
     * ?= Pkey1-AAA<br>
     *
     * @return ???????
     */
    public static String deletedFileTypeCharacter(String fileName) {
        String[] realPKeysplit = fileName.split(DbmfsUtil.fileNameLastString);
        return realPKeysplit[0];
    }

    public static Map<String, Object> convertJsonMap2TypeMap(Map<String, Object> target,
            Map<String, Map<String, Object>> meta) throws Exception {

        Map<String, Object> returnMap = new LinkedHashMap();

        for (Map.Entry<String, Object> ent : target.entrySet()) {

            String key = ent.getKey();
            Object value = ent.getValue();

            if (!key.equals(DatabaseAccessor.tableMetaInfoKey)
                    && !key.equals(DatabaseAccessor.tableMetaInfoPKeyKey)) { //???

                Map<String, Object> columnMeta = meta.get(key);
                String javaTypeName = (String) columnMeta.get("javaTypeName");
                returnMap.put(key, deserializeType(value, javaTypeName));
            }
        }
        return returnMap;
    }

    public static int countPathSeparator(String path) {
        if (path == null || path.equals(""))
            return 0;
        return ((path.length() - path.replaceAll(pathSeparator, "").length()) / pathSeparator.length());
    }

    public static DDLFolder jsonDeserializeDDLObject(String jsonBody) throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        List dataMapList = mapper.readValue(jsonBody, List.class);

        return DDLFolder.createDDLFolder((String) ((Map) dataMapList.get(0)).get("__DBMFS_TABLE_META_INFOMATION"));
    }

    /**
     * ????????
     * ????????????.<br>
     *
     *
     */
    public static boolean isTableName(String targetName) {

        if (targetName.matches(".*/[0-9]*$") == true) {
            return false;
        } else {
            return true;
        }
    }

    public static boolean isHiddenFile(String targetPath) {
        String[] pathList = splitTableNameAndPKeyCharacter(targetPath);
        if (pathList.length == 1) {

            return false;
        } else if (pathList.length == 2) {

            if (pathList[1].indexOf(".") == 0) {
                // ?
                return true;
            }
        }
        return false;
    }

    public static List<String> buildMountTableNames(String tableNames) {
        List<String> mountTableList = new ArrayList();
        String[] tableList = tableNames.split(",");

        for (String str : tableList) {
            if (!str.trim().equals("")) {
                mountTableList.add("/" + str.trim().toLowerCase());
                mountTableList.add("/" + str.trim().toLowerCase() + "/");
            }
        }
        mountTableList.add("/");
        return mountTableList;
    }

    public static Object deserializeType(String value, String javaTypeName) {
        try {
            if (javaTypeName.equals("java.lang.String")) {
                return value;
            } else if (javaTypeName.equals("java.lang.Long")) {
                return new Long(value);
            } else if (javaTypeName.equals("java.lang.Integer")) {
                return new Integer(value);
            } else if (javaTypeName.equals("java.lang.Double")) {
                return new Double(value);
            } else if (javaTypeName.equals("java.lang.Float")) {
                return new Double(value).floatValue();
            } else if (javaTypeName.equals("java.math.BigDecimal")) {
                return new BigDecimal(new Double(value).doubleValue());
            } else if (javaTypeName.equals("java.sql.Date")) {
                return new java.sql.Date((new Long(value)).longValue());
            } else if (javaTypeName.equals("java.sql.Timestamp")) {
                return new java.sql.Timestamp((new Long(value)).longValue());
            } else if (javaTypeName.equals("java.sql.Time")) {
                return new java.sql.Time((new Long(value)).longValue());
            } else if (javaTypeName.equals("java.lang.Boolean")) {
                try {
                    if ((new Integer(value)).intValue() == 1) {
                        return new Boolean("true");
                    } else {
                        return new Boolean("false");
                    }
                } catch (Exception e) {
                    if (value != null) {
                        if (value.trim().toLowerCase().equals("true")) {
                            return new Boolean("true");
                        } else if (value.trim().toLowerCase().equals("false")) {
                            return new Boolean("false");
                        }
                    }
                }
            }
        } catch (Exception ee) {

        }

        return value;
    }

    public static Object deserializeType(Long value, String javaTypeName) {
        if (javaTypeName.equals("java.lang.Long")) {
            return value;
        }

        if (javaTypeName.equals("java.sql.Date")) {
            return new java.sql.Date(value.longValue());
        }

        if (javaTypeName.equals("java.sql.Timestamp")) {
            return new java.sql.Timestamp(value.longValue());
        }

        if (javaTypeName.equals("java.sql.Time")) {
            return new java.sql.Time(value.longValue());
        }

        return value;
    }

    public static Object deserializeType(Integer value, String javaTypeName) {
        if (javaTypeName.equals("java.lang.Integer")) {
            return value;
        }

        if (javaTypeName.equals("java.lang.Boolean")) {
            if (value.intValue() == 1) {
                return new Boolean("true");
            } else {
                return new Boolean("false");
            }
        }
        return value;
    }

    public static Object deserializeType(Double value, String javaTypeName) {
        if (javaTypeName.equals("java.lang.Double")) {
            return value;
        }

        if (javaTypeName.equals("java.lang.Float")) {
            return value.floatValue();
        }

        if (javaTypeName.equals("java.math.BigDecimal")) {
            return new BigDecimal(value.doubleValue());
        }
        return value;
    }

    public static Object deserializeType(byte[] value, String javaTypeName) {
        return value;
    }

    public static Object deserializeType(Boolean value, String javaTypeName) {
        return value;
    }

    public static Object deserializeType(Object value, String javaTypeName) {
        if (value instanceof String) {
            return deserializeType((String) value, javaTypeName);
        } else if (value instanceof Long) {
            return deserializeType((Long) value, javaTypeName);
        } else if (value instanceof Double) {
            return deserializeType((Double) value, javaTypeName);
        } else if (value instanceof Boolean) {
            return deserializeType((Boolean) value, javaTypeName);
        }
        return value;
    }
}