Search in sources :

Example 1 with DfClassificationSqlResourceCloser

use of org.dbflute.properties.assistant.classification.DfClassificationSqlResourceCloser in project dbflute-core by dbflute.

the class DfClassificationProperties method setupTableClassification.

protected void setupTableClassification(DfClassificationTop classificationTop, List<DfClassificationElement> elementList, DfClassificationElement metaElement, Set<String> exceptCodeSet, Connection conn, String sql) {
    final String classificationName = classificationTop.getClassificationName();
    final String[] sisters = metaElement.getSisters();
    final Map<String, Object> subItemPropMap = metaElement.getSubItemMap();
    Statement st = null;
    ResultSet rs = null;
    try {
        st = conn.createStatement();
        _log.info("...Selecting for " + classificationName + " classification" + ln() + sql);
        rs = st.executeQuery(sql);
        final Set<String> duplicateCheckSet = StringSet.createAsCaseInsensitive();
        while (rs.next()) {
            final String code = rs.getString("cls_code");
            final String name = rs.getString("cls_name");
            final String alias = rs.getString("cls_alias");
            final String comment = rs.getString("cls_comment");
            if (exceptCodeSet.contains(code)) {
                _log.info("  exceptd: " + code);
                continue;
            }
            if (duplicateCheckSet.contains(code)) {
                _log.info("  duplicate: " + code);
                continue;
            } else {
                duplicateCheckSet.add(code);
            }
            final Map<String, Object> selectedMap = newLinkedHashMap();
            selectedMap.put(DfClassificationElement.KEY_CODE, code);
            selectedMap.put(DfClassificationElement.KEY_NAME, filterTableClassificationName(classificationTop, name));
            selectedMap.put(DfClassificationElement.KEY_ALIAS, filterTableClassificationLiteralOutput(alias));
            if (Srl.is_NotNull_and_NotTrimmedEmpty(comment)) {
                // because of not required
                selectedMap.put(DfClassificationElement.KEY_COMMENT, comment);
            }
            if (sisters != null && sisters.length > 0) {
                final String sisterValue = rs.getString("cls_sister");
                selectedMap.put(DfClassificationElement.KEY_SISTER_CODE, sisterValue);
            }
            if (subItemPropMap != null && !subItemPropMap.isEmpty()) {
                final Map<String, Object> subItemMap = new LinkedHashMap<String, Object>();
                for (String subItemKey : subItemPropMap.keySet()) {
                    final String clsKey = "cls_" + subItemKey;
                    final String subItemValue = rs.getString(clsKey);
                    final String subItemVeloFriendlyValue;
                    if (subItemValue != null) {
                        subItemVeloFriendlyValue = filterTableClassificationLiteralOutput(subItemValue);
                    } else {
                        // for determination in templates
                        subItemVeloFriendlyValue = "null";
                    }
                    subItemMap.put(subItemKey, subItemVeloFriendlyValue);
                }
                selectedMap.put(DfClassificationElement.KEY_SUB_ITEM_MAP, subItemMap);
            }
            final DfClassificationElement element = new DfClassificationElement();
            element.setClassificationName(classificationName);
            element.acceptBasicItemMap(selectedMap);
            elementList.add(element);
        }
    } catch (SQLException e) {
        throwTableClassificationSelectSQLFailureException(classificationName, sql, e);
        throw new SQLFailureException("Failed to execute the SQL:" + ln() + sql, e);
    } finally {
        new DfClassificationSqlResourceCloser().closeStatement(st, rs);
    }
}
Also used : DfClassificationElement(org.dbflute.properties.assistant.classification.DfClassificationElement) DfClassificationSqlResourceCloser(org.dbflute.properties.assistant.classification.DfClassificationSqlResourceCloser) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) LinkedHashMap(java.util.LinkedHashMap) SQLFailureException(org.dbflute.exception.SQLFailureException)

Example 2 with DfClassificationSqlResourceCloser

use of org.dbflute.properties.assistant.classification.DfClassificationSqlResourceCloser in project dbflute-core by dbflute.

the class DfClassificationProperties method getClassificationDefinitionMap.

// -----------------------------------------------------
// Native Definition
// -----------------
/**
 * Get the map of classification definition.
 * @return The map of classification definition. (NotNull)
 */
protected Map<String, DfClassificationTop> getClassificationDefinitionMap() {
    if (_classificationTopMap != null) {
        return _classificationTopMap;
    }
    _classificationTopMap = newLinkedHashMap();
    final Map<String, Object> plainDefinitionMap;
    {
        final String mapName = KEY_classificationDefinitionMap;
        final String propKey = "torque." + mapName;
        plainDefinitionMap = resolveSplit(mapName, mapProp(propKey, DEFAULT_EMPTY_MAP));
    }
    final DfClassificationLiteralArranger literalArranger = new DfClassificationLiteralArranger();
    String allInOneSql = null;
    Connection conn = null;
    try {
        for (Entry<String, Object> entry : plainDefinitionMap.entrySet()) {
            final String classificationName = entry.getKey();
            final Object objValue = entry.getValue();
            // handle special elements
            if (classificationName.equalsIgnoreCase("$$SQL$$")) {
                allInOneSql = (String) objValue;
                continue;
            }
            // check duplicate classification
            if (_classificationTopMap.containsKey(classificationName)) {
                String msg = "Duplicate classification: " + classificationName;
                throw new DfIllegalPropertySettingException(msg);
            }
            final DfClassificationTop classificationTop = new DfClassificationTop();
            classificationTop.setClassificationName(classificationName);
            _classificationTopMap.put(classificationName, classificationTop);
            // handle classification elements
            if (!(objValue instanceof List<?>)) {
                throwClassificationMapValueIllegalListTypeException(objValue);
            }
            final List<?> plainList = (List<?>) objValue;
            final List<DfClassificationElement> elementList = new ArrayList<DfClassificationElement>();
            boolean tableClassification = false;
            for (Object element : plainList) {
                if (!(element instanceof Map<?, ?>)) {
                    throwClassificationListElementIllegalMapTypeException(element);
                }
                @SuppressWarnings("unchecked") final Map<String, Object> elementMap = (Map<String, Object>) element;
                // from table
                final String table = (String) elementMap.get(DfClassificationElement.KEY_TABLE);
                if (Srl.is_NotNull_and_NotTrimmedEmpty(table)) {
                    tableClassification = true;
                    if (conn == null) {
                        // on demand
                        conn = createMainSchemaConnection();
                    }
                    processTableClassification(classificationTop, elementMap, table, elementList, conn);
                    continue;
                }
                // from literal
                if (isElementMapClassificationTop(elementMap)) {
                    // top definition
                    processClassificationTopFromLiteralIfNeeds(classificationTop, elementMap);
                } else {
                    literalArranger.arrange(classificationName, elementMap);
                    final DfClassificationElement classificationElement = new DfClassificationElement();
                    classificationElement.setClassificationName(classificationName);
                    classificationElement.acceptBasicItemMap(elementMap);
                    elementList.add(classificationElement);
                }
            }
            // adjust classification top
            classificationTop.addClassificationElementAll(elementList);
            classificationTop.setTableClassification(tableClassification);
            _classificationTopMap.put(classificationName, classificationTop);
        }
        if (allInOneSql != null) {
            if (conn == null) {
                // on demand
                conn = createMainSchemaConnection();
            }
            processAllInOneTableClassification(conn, allInOneSql);
        }
        // *Classification Resource Point!
        reflectClassificationResourceToDefinition();
        filterUseDocumentOnly();
        checkClassificationConstraintsIfNeeds();
        prepareSuppressedDBAccessClassTableSet();
    } finally {
        new DfClassificationSqlResourceCloser().closeConnection(conn);
    }
    return _classificationTopMap;
}
Also used : DfClassificationSqlResourceCloser(org.dbflute.properties.assistant.classification.DfClassificationSqlResourceCloser) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DfIllegalPropertySettingException(org.dbflute.exception.DfIllegalPropertySettingException) DfClassificationLiteralArranger(org.dbflute.properties.assistant.classification.DfClassificationLiteralArranger) DfClassificationElement(org.dbflute.properties.assistant.classification.DfClassificationElement) DfClassificationTop(org.dbflute.properties.assistant.classification.DfClassificationTop) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) StringKeyMap(org.dbflute.helper.StringKeyMap)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)2 DfClassificationElement (org.dbflute.properties.assistant.classification.DfClassificationElement)2 DfClassificationSqlResourceCloser (org.dbflute.properties.assistant.classification.DfClassificationSqlResourceCloser)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 DfIllegalPropertySettingException (org.dbflute.exception.DfIllegalPropertySettingException)1 SQLFailureException (org.dbflute.exception.SQLFailureException)1 StringKeyMap (org.dbflute.helper.StringKeyMap)1 DfClassificationLiteralArranger (org.dbflute.properties.assistant.classification.DfClassificationLiteralArranger)1 DfClassificationTop (org.dbflute.properties.assistant.classification.DfClassificationTop)1