Skip to content

Method: getEntityClass(Class)

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 AbstractIdentifiableType<X> et = ManagedClassProcessor.processManagedType(cls);
58:
59: processManagedType(et);
60: }
61:
62: private <X> void processManagedType(AbstractIdentifiableType<X> type) {
63: final Class<X> cls = type.getJavaType();
64: typeMap.put(cls, type);
65: final ClassFieldMetamodelProcessor<X> fieldProcessor = new ClassFieldMetamodelProcessor<>(cls, type, this);
66:
67: for (Field f : cls.getDeclaredFields()) {
68: fieldProcessor.processField(f);
69: }
70:
71: final AbstractIdentifiableType<? super X> supertype = processSupertypes(cls);
72: if (supertype != null) {
73: type.setSupertype(supertype);
74: }
75:
76: if (type.getPersistenceType() == Type.PersistenceType.ENTITY) {
77: try {
78: type.getIdentifier();
79: } catch (IllegalArgumentException e) {
80: throw new MetamodelInitializationException("Missing identifier field in entity class " + cls);
81: }
82: resolveInheritanceType((EntityTypeImpl<X>) type);
83: }
84:
85: queryProcessor.processClass(cls);
86: }
87:
88: private <X> AbstractIdentifiableType<? super X> processSupertypes(Class<X> cls) {
89: final Class<? super X> managedSupertype = ManagedClassProcessor.getManagedSupertype(cls);
90: if (managedSupertype != null) {
91: if (typeMap.containsKey(managedSupertype)) {
92: return (AbstractIdentifiableType<? super X>) typeMap.get(managedSupertype);
93: }
94: final AbstractIdentifiableType<? super X> type = ManagedClassProcessor.processManagedType(managedSupertype);
95: processManagedType(type);
96: return type;
97: }
98: return null;
99: }
100:
101: private <X> void resolveInheritanceType(EntityTypeImpl<X> et) {
102: final Class<X> cls = et.getJavaType();
103: final Inheritance inheritance = cls.getDeclaredAnnotation(Inheritance.class);
104: if (inheritance != null) {
105: if (et.getSupertype() != null &&
106: et.getSupertype().getPersistenceType() != Type.PersistenceType.MAPPED_SUPERCLASS) {
107: throw new MetamodelInitializationException("Class " + cls +
108: " cannot declare inheritance strategy, because it already inherits it from its supertype.");
109: }
110: et.setInheritanceType(inheritance.strategy());
111: } else {
112: et.setInheritanceType(Constants.DEFAULT_INHERITANCE_TYPE);
113: }
114: }
115:
116: public Map<Class<?>, ManagedType<?>> getTypeMap() {
117: return Collections.unmodifiableMap(typeMap);
118: }
119:
120: public Map<Class<?>, EntityType<?>> getEntities() {
121: final Map<Class<?>, EntityType<?>> map = new HashMap<>();
122: typeMap.entrySet().stream().filter(e -> e.getValue().getPersistenceType() == Type.PersistenceType.ENTITY)
123: .forEach(e -> map.put(e.getKey(), (EntityType<?>) e.getValue()));
124: return map;
125: }
126:
127: public Set<Class<?>> getInferredClasses() {
128: return Collections.unmodifiableSet(inferredClasses);
129: }
130:
131: public NamedQueryManager getNamedQueryManager() {
132: return namedQueryManager;
133: }
134:
135: void addInferredClass(Class<?> cls) {
136: inferredClasses.add(cls);
137: }
138:
139: @SuppressWarnings("unchecked")
140: <X> ManagedType<X> getEntityClass(Class<X> cls) {
141:• if (!typeMap.containsKey(cls)) {
142: processOWLClass(cls);
143: }
144: return (ManagedType<X>) typeMap.get(cls);
145: }
146: }