Search in sources :

Example 1 with DfSPolicyParsedPolicy

use of org.dbflute.logic.doc.spolicy.parsed.DfSPolicyParsedPolicy in project dbflute-core by dbflute.

the class DfSPolicyChecker method parsePolicy.

// ===================================================================================
// Parse Policy
// ============
protected DfSPolicyParsedPolicy parsePolicy() {
    _log.info("...Parsing schema policy map: " + _policyMap.keySet());
    DfSPolicyParsedPolicyPart wholePolicyPart = null;
    DfSPolicyParsedPolicyPart tablePolicyPart = null;
    DfSPolicyParsedPolicyPart columnPolicyPart = null;
    for (Entry<String, Object> entry : _policyMap.entrySet()) {
        final String key = entry.getKey();
        final Object value = entry.getValue();
        if (key.equals("wholeMap")) {
            @SuppressWarnings("unchecked") final Map<String, Object> wholeMap = (Map<String, Object>) value;
            final List<String> themeList = extractThemeList(wholeMap);
            wholePolicyPart = new DfSPolicyParsedPolicyPart(themeList, Collections.emptyList());
        } else if (key.equals("tableMap")) {
            @SuppressWarnings("unchecked") final Map<String, Object> tableMap = (Map<String, Object>) value;
            final List<String> themeList = extractThemeList(tableMap);
            tablePolicyPart = new DfSPolicyParsedPolicyPart(themeList, extractStatementList(tableMap));
        } else if (key.equals("columnMap")) {
            @SuppressWarnings("unchecked") final Map<String, Object> columnMap = (Map<String, Object>) value;
            final List<String> themeList = extractThemeList(columnMap);
            columnPolicyPart = new DfSPolicyParsedPolicyPart(themeList, extractStatementList(columnMap));
        } else {
            if (!Srl.equalsPlain(key, "tableExceptList", "tableTargetList", "columnExceptMap", "isMainSchemaOnly")) {
                _logicalSecretary.throwSchemaPolicyCheckUnknownPropertyException(key);
            }
        }
    }
    if (wholePolicyPart == null) {
        wholePolicyPart = new DfSPolicyParsedPolicyPart(Collections.emptyList(), Collections.emptyList());
    }
    if (tablePolicyPart == null) {
        tablePolicyPart = new DfSPolicyParsedPolicyPart(Collections.emptyList(), Collections.emptyList());
    }
    if (columnPolicyPart == null) {
        columnPolicyPart = new DfSPolicyParsedPolicyPart(Collections.emptyList(), Collections.emptyList());
    }
    return new DfSPolicyParsedPolicy(wholePolicyPart, tablePolicyPart, columnPolicyPart);
}
Also used : DfSPolicyParsedPolicyPart(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyParsedPolicy.DfSPolicyParsedPolicyPart) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) DfSPolicyParsedPolicy(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyParsedPolicy)

Example 2 with DfSPolicyParsedPolicy

use of org.dbflute.logic.doc.spolicy.parsed.DfSPolicyParsedPolicy in project dbflute-core by dbflute.

the class DfSPolicyChecker method checkPolicyIfNeeds.

// ===================================================================================
// Check Policy
// ============
public DfSPolicyResult checkPolicyIfNeeds() {
    // null allowed if no policy
    if (_policyMap.isEmpty()) {
        return null;
    }
    _log.info("");
    _log.info("...Beginning schema policy check");
    // map:{
    // ; tableExceptList = list:{}
    // ; tableTargetList = list:{}
    // ; columnExceptMap = map:{}
    // ; isMainSchemaOnly = false
    // ; wholeMap = map:{
    // ; themeList = list:{ uniqueTableAlias ; sameColumnAliasIfSameColumnName }
    // }
    // ; tableMap = map:{
    // ; themeList = list:{ hasPK ; upperCaseBasis ; identityIfPureIDPK }
    // }
    // ; columnMap = map:{
    // ; themeList = list:{ upperCaseBasis }
    // ; statementList = list:{
    // ; if columnName is suffix:_FLAG then bad
    // ; if columnName is suffix:_FLG then notNull
    // ; if columnName is suffix:_FLG then dbType is INTEGER
    // }
    // }
    // }
    final long before = System.currentTimeMillis();
    final DfSPolicyParsedPolicy policy = parsePolicy();
    final String dispPolicy = showParsedPolicy(policy);
    final DfSPolicyResult result = createPolicyResult(policy);
    result.acceptPolicyMessage(dispPolicy);
    final List<Table> tableList = _database.getTableList();
    doCheckWhole(policy, result, tableList);
    for (Table table : tableList) {
        if (!isTargetTable(table)) {
            continue;
        }
        doCheckTableColumn(policy, result, table);
    }
    final String violationMessage = _logicalSecretary.buildSchemaPolicyCheckViolationMessage(result);
    result.acceptViolationMessage(violationMessage);
    result.acceptEndingHandler(() -> {
        // lazy handling for display of SchemaHTML
        _log.info("...Ending schema policy check: " + result);
        if (result.hasViolation()) {
            _logicalSecretary.throwSchemaPolicyCheckViolationException(violationMessage);
        } else {
            final long after = System.currentTimeMillis();
            // for tuning
            final String performanceView = DfTraceViewUtil.convertToPerformanceView(after - before);
            _log.info(" -> No violation of schema policy. Good DB design! [" + performanceView + "]");
            _log.info("");
        }
    });
    // not ending yet
    return result;
}
Also used : Table(org.apache.torque.engine.database.model.Table) DfSPolicyResult(org.dbflute.logic.doc.spolicy.result.DfSPolicyResult) DfSPolicyParsedPolicy(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyParsedPolicy)

Aggregations

DfSPolicyParsedPolicy (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyParsedPolicy)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Table (org.apache.torque.engine.database.model.Table)1 DfSPolicyParsedPolicyPart (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyParsedPolicy.DfSPolicyParsedPolicyPart)1 DfSPolicyResult (org.dbflute.logic.doc.spolicy.result.DfSPolicyResult)1