Skip to contentMethod: getMerger(FieldSpecification)
1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.sessions.merge;
16:
17: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
19: import cz.cvut.kbss.jopa.sessions.ChangeRecord;
20: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
21:
22: import java.util.Collection;
23: import java.util.Map;
24:
25: public class DetachedValueMerger implements ValueMerger {
26:
27: private final UnitOfWorkImpl uow;
28:
29: private final ValueMerger defaultValueMerger;
30: private final ManagedTypeValueMerger managedTypeMerger;
31: private final CollectionValueMerger collectionMerger;
32: private final MapValueMerger mapValueMerger;
33:
34: public DetachedValueMerger(UnitOfWorkImpl uow) {
35: assert uow != null;
36: this.uow = uow;
37: this.defaultValueMerger = new DefaultValueMerger();
38: this.managedTypeMerger = new ManagedTypeValueMerger(uow);
39: this.collectionMerger = new CollectionValueMerger(uow, managedTypeMerger);
40: this.mapValueMerger = new MapValueMerger();
41: }
42:
43: @Override
44: public void mergeValue(Object target, ChangeRecord changeRecord, Descriptor attributeDescriptor) {
45: getMerger(changeRecord.getAttribute()).mergeValue(target, changeRecord, attributeDescriptor);
46: }
47:
48: private ValueMerger getMerger(FieldSpecification<?, ?> att) {
49:• if (uow.isEntityType(att.getJavaType())) {
50: return managedTypeMerger;
51:• } else if (Collection.class.isAssignableFrom(att.getJavaType())) {
52: return collectionMerger;
53:• } else if (Map.class.isAssignableFrom(att.getJavaType())) {
54: return mapValueMerger;
55: } else {
56: return defaultValueMerger;
57: }
58: }
59: }