Skip to content

Method: OneLevelRemoveCascadeExplorer(Consumer)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.model;
19:
20: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
21: import cz.cvut.kbss.jopa.proxy.lazy.LazyLoadingProxy;
22: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
23: import cz.cvut.kbss.jopa.utils.JOPALazyUtils;
24:
25: import java.util.Collection;
26: import java.util.HashSet;
27: import java.util.function.Consumer;
28:
29: public class OneLevelRemoveCascadeExplorer extends OneLevelCascadeExplorer {
30:
31: private final Consumer<Object> removeOperation;
32:
33: public OneLevelRemoveCascadeExplorer(Consumer<Object> removeOperation) {
34: this.removeOperation = removeOperation;
35: }
36:
37: @Override
38: protected void exploreCascaded(Attribute<?, ?> at, Object o) {
39: Object attVal = EntityPropertiesUtils.getAttributeValue(at, o);
40: if (attVal == null) {
41: return;
42: }
43: if (JOPALazyUtils.isLazyLoadingProxy(attVal)) {
44: attVal = ((LazyLoadingProxy<?>) attVal).triggerLazyLoading();
45: }
46: if (at.isCollection()) {
47: for (final Object ox2 : new HashSet<>((Collection<?>) attVal)) {
48: removeOperation.accept(ox2);
49: }
50: } else {
51: removeOperation.accept(attVal);
52: }
53: }
54: }