private boolean isPolymorphic(MappedClass mappedClass) { return configuration.isPolymorphic(mappedClass.getJavaClass()); }
public SimpleConfiguration( ConverterRegistry converterRegistry, Set<MappedClass> mappedClasses) { this.converterRegistry = converterRegistry; this.mappedClasses = mappedClasses; for (MappedClass mappedClass : mappedClasses) { uidToMappedClass.put(mappedClass.getUID(), mappedClass); classToMappedClass.put(mappedClass.getJavaClass(), mappedClass); for (MappedClass superClass : mappedClass.getMappedSuperClasses()) { polymorphicClasses.add(superClass.getJavaClass()); } } }
@Nullable private Class<?> convertClassReference(UID uid, Class<?> targetClass) { List<MappedClass> mappedClasses = configuration.getMappedClasses(uid); boolean foundMatch = false; for (MappedClass mappedClass : mappedClasses) { Class<?> clazz = mappedClass.getJavaClass(); if (targetClass.isAssignableFrom(clazz)) { targetClass = clazz; foundMatch = true; } } if (foundMatch) { return targetClass; } else { return null; } }
@Nullable @SuppressWarnings("unchecked") private <T> Class<? extends T> matchType(Collection<ID> types, Class<T> targetType) { if (types.isEmpty()) { return targetType; } else { Class<? extends T> result = targetType; boolean foundMatch = false; for (ID type : types) { if (type instanceof UID) { UID uid = (UID) type; List<MappedClass> classes = configuration.getMappedClasses(uid); if (classes != null) { for (MappedClass mappedClass : classes) { Class<?> clazz = mappedClass.getJavaClass(); if ((result == null || result.isAssignableFrom(clazz)) && !clazz.isInterface()) { foundMatch = true; result = (Class<? extends T>) clazz; } } } } } if (foundMatch) { return result; } else { return null; } } }
public MappedClassBuilder addProperties() { String ns = mappedClass.getUID().getNamespace(); for (Field field : mappedClass.getJavaClass().getDeclaredFields()) { if (!handled.contains(field.getName())) { Predicate predicate = new PredicateImpl("", ns, field.getName(), false); MappedPredicate mappedPredicate = new MappedPredicate(ns, predicate, null); addMappedPath(field, Collections.singletonList(mappedPredicate), predicate); } } return this; }
public MappedClassBuilder addMixin(String propertyName) { try { handled.add(propertyName); Field field = mappedClass.getJavaClass().getDeclaredField(propertyName); Mixin mixin = new MixinImpl(); return addMappedPath(field, Collections.<MappedPredicate> emptyList(), mixin); } catch (SecurityException e) { throw new ConfigurationException(e); } catch (NoSuchFieldException e) { throw new ConfigurationException(e); } }
private MappedClass resolveMappedClass(MappedClass mappedClass, PropertiesMap properties) { for (STMT stmt : properties.getDirect().get(RDF.type)) { if (stmt.getObject().isURI()) { List<MappedClass> mappedClasses = configuration.getMappedClasses(stmt.getObject().asURI()); for (MappedClass mc : mappedClasses) { if (!mc.equals(mappedClass) && mappedClass.getJavaClass().isAssignableFrom(mc.getJavaClass())) { return mc; } } } } return mappedClass; }
public MappedClassBuilder addId(String propertyName) { try { handled.add(propertyName); Field field = mappedClass.getJavaClass().getDeclaredField(propertyName); Id id; if (field.getType().equals(ID.class)) { id = new IdImpl(IDType.RESOURCE); } else if (field.getType().equals(UID.class)) { id = new IdImpl(IDType.URI); } else { id = new IdImpl(IDType.LOCAL); } return addMappedPath(field, Collections.<MappedPredicate> emptyList(), id); } catch (SecurityException e) { throw new ConfigurationException(e); } catch (NoSuchFieldException e) { throw new ConfigurationException(e); } }
private MappedClassBuilder add(String propertyName, UID uid, boolean inv, Annotation... ann) { try { handled.add(propertyName); Field field = mappedClass.getJavaClass().getDeclaredField(propertyName); List<Annotation> annotations = new ArrayList<Annotation>(); Predicate predicate = new PredicateImpl("", uid.ns(), uid.ln(), inv); annotations.add(predicate); annotations.addAll(Arrays.asList(ann)); MappedPredicate mappedPredicate = new MappedPredicate(defaultNamespace, predicate, null); return addMappedPath(field, Collections.singletonList(mappedPredicate), annotations.toArray(new Annotation[annotations.size()])); } catch (SecurityException e) { throw new ConfigurationException(e); } catch (NoSuchFieldException e) { throw new ConfigurationException(e); } }
public Configuration build() { if (converterRegistry == null) { converterRegistry = new ConverterRegistryImpl(); } // populate mappedSuperClasses for (MappedClass mappedClass : mappedClasses.values()) { if (!mappedClass.getJavaClass().getSuperclass().equals(Object.class)) { MappedClass mappedSuperClass = mappedClasses.get(mappedClass.getJavaClass().getSuperclass()); if (mappedSuperClass != null) { mappedClass.getMappedSuperClasses().add(mappedSuperClass); } } } // merge data for (MappedClass mappedClass : mappedClasses.values()) { Deque<MappedClass> supers = new ArrayDeque<MappedClass>(mappedClass.getMappedSuperClasses()); while (!supers.isEmpty()) { MappedClass mappedSuperClass = supers.pop(); supers.addAll(mappedSuperClass.getMappedSuperClasses()); for (MappedPath path : mappedSuperClass.getProperties()) { MappedProperty<?> property = (MappedProperty<?>) path.getMappedProperty().clone(); property.resolve(mappedClass); mappedClass.addMappedPath(new MappedPath(property, path.getPredicatePath(), !mappedClass.equals(property.getDeclaringClass()))); } } } return new SimpleConfiguration(converterRegistry, new HashSet<MappedClass>(mappedClasses.values())); }
public final void addClasses(Class<?>... classes) { for (Class<?> clazz : classes) { if (clazz.getAnnotation(ClassMapping.class) != null) { MappedClass mappedClass = mappedClassFactory.getMappedClass(clazz); if (mappedClass.getUID() != null) { ClassMapping classMapping = clazz.getAnnotation(ClassMapping.class); if (clazz.isEnum() && !classMapping.parent().equals(Object.class)) { MappedClass parentClass = mappedClassFactory.getMappedClass(classMapping .parent()); for (Object constant : clazz.getEnumConstants()) { UID instance = new UID(mappedClass.getClassNs(), ((Enum) constant).name()); addClass(instance, parentClass); } } addClass(mappedClass.getUID(), mappedClass); } for (MappedClass superClass : mappedClass.getMappedSuperClasses()) { polymorphicClasses.add(superClass.getJavaClass()); } mappedClasses.add(mappedClass); } else { throw new IllegalArgumentException("No @ClassMapping annotation for " + clazz.getName()); } } }
public void export() { SessionFactoryImpl sessionFactory = new SessionFactoryImpl(); sessionFactory.setConfiguration(new DefaultConfiguration(RDFSClass.class.getPackage(), OWLClass.class.getPackage())); sessionFactory.setRepository(repository); sessionFactory.initialize(); Session session = sessionFactory.openSession(); if (ontology != null) { Ontology ont = new Ontology(new UID(ontology)); if (ontologyImports != null) { for (String oimport : ontologyImports) { ont.addImport(new Ontology(new UID(oimport))); } } session.save(ont); } Map<UID, RDFSResource> resources = new HashMap<UID, RDFSResource>(); resources.put(RDF.List, new RDFSClass<RDFSResource>(RDF.List)); resources.put(RDF.first, new RDFProperty(RDF.first)); resources.put(RDF.rest, new RDFProperty(RDF.rest)); resources.put(RDF.type, new RDFProperty(RDF.type)); resources.put(RDFS.label, new RDFProperty(RDFS.label)); resources.put(RDFS.comment, new RDFProperty(RDFS.comment)); resources.put(RDFS.Resource, new RDFSClass<Object>(RDFS.Resource)); // process classes for (MappedClass mappedClass : configuration.getMappedClasses()) { processClass(session, mappedClass.getJavaClass(), resources); } // save resources session.saveAll(resources.values().toArray()); }
for (STMT typeStmt : typeStmts) { for (MappedClass cl : configuration.getMappedClasses(typeStmt.getObject().asURI())) { if (componentType.isAssignableFrom(cl.getJavaClass())) { matched = true;
private void collectTypes() { for (MappedClass mappedClass : configuration.getMappedClasses()) { if (mappedClass.isEnum()) { // skip enum types continue; } EntityType entityType = (EntityType) createType(mappedClass.getJavaClass()); for (MappedPath mappedPath : mappedClass.getProperties()) { MappedProperty<?> mappedProperty = mappedPath.getMappedProperty(); Class<?> type = mappedPath.getMappedProperty().getType(); Type propertyType = null; if (type.equals(Map.class)) { propertyType = new SimpleType(Types.MAP, createType(mappedProperty.getKeyType()), createType(mappedProperty.getComponentType())); } else if (type.equals(List.class)) { propertyType = new SimpleType(Types.LIST, createType(mappedProperty.getComponentType())); } else if (type.equals(Set.class)) { propertyType = new SimpleType(Types.SET, createType(mappedProperty.getComponentType())); } else if (type.equals(Collection.class)) { propertyType = new SimpleType(Types.COLLECTION, createType(mappedProperty.getComponentType())); } else { propertyType = createType(type); } entityType.addProperty(createProperty(entityType, mappedPath.getName(), propertyType, mappedProperty.getAnnotations())); } } }