001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.relation;
017
018import com.mybatisflex.core.row.Row;
019
020import java.lang.reflect.Field;
021import java.util.List;
022
023class ToOneRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
024
025
026    public ToOneRelation(String selfField, String targetSchema, String targetTable, String targetField,
027                         String joinTable, String joinSelfColumn, String joinTargetColumn,
028                         String dataSource, Class<SelfEntity> selfEntityClass, Field relationField, String[] selectColumns) {
029        super(selfField, targetSchema, targetTable, targetField,
030            joinTable, joinSelfColumn, joinTargetColumn,
031            dataSource, selfEntityClass, relationField,
032            null, selectColumns
033        );
034    }
035
036
037    @Override
038    public void join(List<SelfEntity> selfEntities, List<?> targetObjectList, List<Row> mappingRows) {
039        selfEntities.forEach(selfEntity -> {
040            Object selfValue = selfFieldWrapper.get(selfEntity);
041            if (selfValue != null) {
042                selfValue = selfValue.toString();
043                String targetMappingValue = null;
044                if (mappingRows != null) {
045                    targetMappingValue = getTargetMappingValue(mappingRows, selfValue);
046                    if (targetMappingValue == null) {
047                        return;
048                    }
049                } else {
050                    targetMappingValue = (String) selfValue;
051                }
052
053                for (Object targetObject : targetObjectList) {
054                    Object targetValue = targetFieldWrapper.get(targetObject);
055                    if (targetValue != null && targetMappingValue.equals(targetValue.toString())) {
056                        relationFieldWrapper.set(targetObject, selfEntity);
057                        break;
058                    }
059                }
060            }
061        });
062    }
063
064
065    private String getTargetMappingValue(List<Row> mappingRows, Object selfValue) {
066        for (Row mappingRow : mappingRows) {
067            if (selfValue.equals(String.valueOf(mappingRow.getIgnoreCase(joinSelfColumn)))) {
068                Object joinValue = mappingRow.getIgnoreCase(joinTargetColumn);
069                if (joinValue != null) {
070                    return joinValue.toString();
071                }
072            }
073        }
074        return null;
075    }
076
077}