Skip to content

Method: lambda$getEntities$1(Map, Map.Entry)

1: /**
2: * Copyright (C) 2016 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.model.metamodel;
16:
17: import cz.cvut.kbss.jopa.exception.MetamodelInitializationException;
18: import cz.cvut.kbss.jopa.model.annotations.Inheritance;
19: import cz.cvut.kbss.jopa.query.NamedQueryManager;
20: import cz.cvut.kbss.jopa.utils.Constants;
21: import org.slf4j.Logger;
22: import org.slf4j.LoggerFactory;
23:
24: import java.lang.reflect.Field;
25: import java.util.*;
26:
27: public class MetamodelBuilder {
28:
29: private static final Logger LOG = LoggerFactory.getLogger(MetamodelBuilder.class);
30:
31: private final NamedNativeQueryProcessor queryProcessor;
32:
33: private final Map<Class<?>, AbstractIdentifiableType<?>> typeMap = new HashMap<>();
34: private final Set<Class<?>> inferredClasses = new HashSet<>();
35: private final NamedQueryManager namedQueryManager = new NamedQueryManager();
36:
37: public MetamodelBuilder() {
38: this.queryProcessor = new NamedNativeQueryProcessor(namedQueryManager);
39: }
40:
41: /**
42: * Builds persistence unit metamodel from the specified set of entities.
43: *
44: * @param entities Entities declared for the persistence unit
45: */
46: public void buildMetamodel(Set<Class<?>> entities) {
47: entities.forEach(this::processOWLClass);
48: }
49:
50: private <X> void processOWLClass(final Class<X> cls) {
51: if (typeMap.containsKey(cls)) {
52: return;
53: }
54:
55: LOG.debug("Processing OWL class: {}", cls);
56:
57: final TypeBuilderContext<X> et = ManagedClassProcessor.processManagedType(cls);
58:
59: processManagedType(et);
60: }
61:
62: private <X> void processManagedType(TypeBuilderContext<X> context) {
63: final AbstractIdentifiableType<X> type = context.getType();
64: final Class<X> cls = type.getJavaType();
65: typeMap.put(cls, type);
66:
67: final AbstractIdentifiableType<? super X> supertype = processSupertypes(cls);
68: if (supertype != null) {
69: type.setSupertype(supertype);
70: }
71:
72: final ClassFieldMetamodelProcessor<X> fieldProcessor = new ClassFieldMetamodelProcessor<>(context, this);
73:
74: for (Field f : cls.getDeclaredFields()) {
75: fieldProcessor.processField(f);
76: }
77:
78: if (type.getPersistenceType() == Type.PersistenceType.ENTITY) {
79: try {
80: type.getIdentifier();
81: } catch (IllegalArgumentException e) {
82: throw new MetamodelInitializationException("Missing identifier field in entity " + cls);
83: }
84: resolveInheritanceType((EntityTypeImpl<X>) type);
85: }
86:
87: queryProcessor.processClass(cls);
88: }
89:
90: private <X> AbstractIdentifiableType<? super X> processSupertypes(Class<X> cls) {
91: final Class<? super X> managedSupertype = ManagedClassProcessor.getManagedSupertype(cls);
92: if (managedSupertype != null) {
93: if (typeMap.containsKey(managedSupertype)) {
94: return (AbstractIdentifiableType<? super X>) typeMap.get(managedSupertype);
95: }
96: final TypeBuilderContext<? super X> context = ManagedClassProcessor.processManagedType(managedSupertype);
97: processManagedType(context);
98: return context.getType();
99: }
100: return null;
101: }
102:
103: private <X> void resolveInheritanceType(EntityTypeImpl<X> et) {
104: final Class<X> cls = et.getJavaType();
105: final Inheritance inheritance = cls.getDeclaredAnnotation(Inheritance.class);
106: if (inheritance != null) {
107: if (et.getSupertype() != null &&
108: et.getSupertype().getPersistenceType() != Type.PersistenceType.MAPPED_SUPERCLASS) {
109: throw new MetamodelInitializationException("Class " + cls +
110: " cannot declare inheritance strategy, because it already inherits it from its supertype.");
111: }
112: et.setInheritanceType(inheritance.strategy());
113: } else {
114: et.setInheritanceType(Constants.DEFAULT_INHERITANCE_TYPE);
115: }
116: }
117:
118: public Map<Class<?>, ManagedType<?>> getTypeMap() {
119: return Collections.unmodifiableMap(typeMap);
120: }
121:
122: public Map<Class<?>, EntityType<?>> getEntities() {
123: final Map<Class<?>, EntityType<?>> map = new HashMap<>();
124: typeMap.entrySet().stream().filter(e -> e.getValue().getPersistenceType() == Type.PersistenceType.ENTITY)
125: .forEach(e -> map.put(e.getKey(), (EntityType<?>) e.getValue()));
126: return map;
127: }
128:
129: public Set<Class<?>> getInferredClasses() {
130: return Collections.unmodifiableSet(inferredClasses);
131: }
132:
133: public NamedQueryManager getNamedQueryManager() {
134: return namedQueryManager;
135: }
136:
137: void addInferredClass(Class<?> cls) {
138: inferredClasses.add(cls);
139: }
140:
141: @SuppressWarnings("unchecked")
142: <X> ManagedType<X> getEntityClass(Class<X> cls) {
143: if (!typeMap.containsKey(cls)) {
144: processOWLClass(cls);
145: }
146: return (ManagedType<X>) typeMap.get(cls);
147: }
148: }