ToOne.java
/*
* Copyright 2025-present 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
*
* https://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 io.github.simplejdbcmapper.relationship;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import io.github.simplejdbcmapper.exception.MapperException;
/**
* This handles the toOne relationship.
*
* @author Antony Joseph
*/
class ToOne {
private Class<?> mainType;
private Class<?> relatedType;
private String mainObjJoinProperty;
private String relatedObjJoinProperty;
private String mainObjPropertyToPopulate;
ToOne(Class<?> mainType, Class<?> relatedType) {
Assert.notNull(mainType, "mainType must not be null");
Assert.notNull(relatedType, "relatedType must not be null");
this.mainType = mainType;
this.relatedType = relatedType;
}
void joinOn(String mainObjJoinProperty, String relatedObjJoinProperty) {
Assert.notNull(mainObjJoinProperty, "mainObjJoinProperty must not be null");
Assert.notNull(relatedObjJoinProperty, "relatedObjJoinProperty must not be null");
Class<?> mainObjJoinPropertyType = RelationshipMapper.getPropertyType(mainType, mainObjJoinProperty);
Class<?> relatedObjJoinPropertyType = RelationshipMapper.getPropertyType(relatedType, relatedObjJoinProperty);
if (mainObjJoinPropertyType != relatedObjJoinPropertyType) {
throw new IllegalArgumentException("Conflicting property types. Property type of "
+ mainType.getSimpleName() + "." + mainObjJoinProperty + " and " + relatedType.getSimpleName() + "."
+ relatedObjJoinProperty + " are not the same.");
}
this.mainObjJoinProperty = mainObjJoinProperty;
this.relatedObjJoinProperty = relatedObjJoinProperty;
}
void populate(String mainObjPropertyToPopulate) {
Assert.notNull(mainObjPropertyToPopulate, "mainObjPropertyToPopulate must not be null");
this.mainObjPropertyToPopulate = mainObjPropertyToPopulate;
}
<T, U> void process(List<T> mainObjList, List<U> relatedObjList) {
if (CollectionUtils.isEmpty(mainObjList) || CollectionUtils.isEmpty(relatedObjList)) {
return;
}
Method mainObjJoinPropertyReadMethod = RelationshipMapper.getReadMethod(mainType, mainObjJoinProperty);
Method relatedObjJoinPropertyReadMethod = RelationshipMapper.getReadMethod(relatedType, relatedObjJoinProperty);
Method mainObjPropertyToPopulateWriteMethod = RelationshipMapper.getWriteMethod(mainType,
mainObjPropertyToPopulate);
try {
Map<Object, U> joinPropToRelatedObjMap = getJoinPropToRelatedObjMap(relatedObjList,
relatedObjJoinPropertyReadMethod);
for (T mainObj : mainObjList) {
if (mainObj != null) {
Object mainObjJoinPropertyValue = mainObjJoinPropertyReadMethod.invoke(mainObj);
U relatedObj = joinPropToRelatedObjMap.get(mainObjJoinPropertyValue);
try {
mainObjPropertyToPopulateWriteMethod.invoke(mainObj, relatedObj);
} catch (Exception e) {
throw new MapperException(e.getMessage() + ". Invoking " + mainObjPropertyToPopulateWriteMethod
+ " with value " + relatedObj, e);
}
}
}
} catch (Exception e) {
throw new MapperException(e.getMessage(), e);
}
}
private <U> Map<Object, U> getJoinPropToRelatedObjMap(List<U> relatedObjList,
Method relatedObjJoinPropertyReadMethod) throws IllegalAccessException, InvocationTargetException {
Map<Object, U> joinPropToRelatedObjMap = new HashMap<>();
for (U relatedObj : relatedObjList) {
if (relatedObj != null) {
Object relatedObjJoinPropertyValue = relatedObjJoinPropertyReadMethod.invoke(relatedObj);
if (relatedObjJoinPropertyValue != null) {
joinPropToRelatedObjMap.put(relatedObjJoinPropertyValue, relatedObj);
}
}
}
return joinPropToRelatedObjMap;
}
}