ToMany.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.ArrayList;
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 toMany relationship.
*
* @author Antony Joseph
*/
class ToMany {
private Class<?> mainType;
private Class<?> relatedType;
private String mainObjIdProperty;
private String relatedObjFkProperty;
private String mainObjPropertyToPopulate;
ToMany(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 mainObjIdProperty, String relatedObjFkProperty) {
Assert.notNull(mainObjIdProperty, "mainObjIdProperty must not be null");
Assert.notNull(relatedObjFkProperty, "relatedObjFkProperty must not be null");
Class<?> mainObjIdPropertyType = RelationshipMapper.getPropertyType(mainType, mainObjIdProperty);
Class<?> relatedObjFkPropertyType = RelationshipMapper.getPropertyType(relatedType, relatedObjFkProperty);
if (mainObjIdPropertyType != relatedObjFkPropertyType) {
throw new IllegalArgumentException("Conflicting property types. Property type of "
+ mainType.getSimpleName() + "." + mainObjIdProperty + " and " + relatedType.getSimpleName() + "."
+ relatedObjFkProperty + " are not the same.");
}
this.mainObjIdProperty = mainObjIdProperty;
this.relatedObjFkProperty = relatedObjFkProperty;
}
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 mainObjIdPropertyReadMethod = RelationshipMapper.getReadMethod(mainType, mainObjIdProperty);
Method relatedObjFkPropertyReadMethod = RelationshipMapper.getReadMethod(relatedType, relatedObjFkProperty);
Method mainObjPropertyToPopulateWriteMethod = RelationshipMapper.getWriteMethod(mainType,
mainObjPropertyToPopulate);
try {
Map<Object, List<U>> fkToRelatedObjListMap = getFkToRelatedObjListMap(relatedObjList,
relatedObjFkPropertyReadMethod);
for (T mainObj : mainObjList) {
if (mainObj != null) {
Object mainObjIdPropertyValue = mainObjIdPropertyReadMethod.invoke(mainObj);
List<U> populaterList = fkToRelatedObjListMap.get(mainObjIdPropertyValue);
if (populaterList == null) {
populaterList = new ArrayList<>();
}
try {
mainObjPropertyToPopulateWriteMethod.invoke(mainObj, populaterList);
} catch (Exception e) {
throw new MapperException(e.getMessage() + ". Invoking " + mainObjPropertyToPopulateWriteMethod
+ " with value " + populaterList, e);
}
}
}
} catch (Exception e) {
throw new MapperException(e.getMessage(), e);
}
}
private <U> Map<Object, List<U>> getFkToRelatedObjListMap(List<U> relatedObjList,
Method relatedObjFkPropertyReadMethod) throws IllegalAccessException, InvocationTargetException {
// relatedObjFk - List of relatedObj
Map<Object, List<U>> foreignKeyToListMap = new HashMap<>();
for (U relatedObj : relatedObjList) {
if (relatedObj != null) {
Object foreignKeyPropertyValue = relatedObjFkPropertyReadMethod.invoke(relatedObj);
if (foreignKeyPropertyValue != null) {
List<U> list = foreignKeyToListMap.get(foreignKeyPropertyValue);
if (list == null) {
list = new ArrayList<>();
list.add(relatedObj);
foreignKeyToListMap.put(foreignKeyPropertyValue, list);
} else {
list.add(relatedObj);
}
}
}
}
return foreignKeyToListMap;
}
}