public EntityValueEncoderFactory(SessionFactory sessionFactory, Configuration rdfBeanConfiguration, Class<T> cl) { this.sessionFactory = sessionFactory; this.cl = cl; this.idProperty = rdfBeanConfiguration.getMappedClass(cl).getIdProperty(); }
@Nullable private MappedPath getPathMapping(MappedClass mappedClass, Constructor<?> constructor, int parameterIndex, String property) { boolean reference = mappedClass.hasProperty(property); ConstructorParameter constructorParameter = new ConstructorParameter(constructor, parameterIndex, mappedClass, property, reference); if (mappedClass.hasProperty(property)) { return mappedClass.getMappedPath(property); } else { List<MappedPredicate> path = getPredicatePath(mappedClass.getClassNs(), constructorParameter); return getMappedPath(constructorParameter, path); } }
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 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()); } } }
private void collectMethodPaths(Class<?> clazz, MappedClass mappedClass) { String classNs = mappedClass.getClassNs(); for (Method method : clazz.getDeclaredMethods()) { MappedPath path = getPathMapping(classNs, method, mappedClass); if (path != null) { mappedClass.addMappedPath(path); } } }
Context context = clazz.getAnnotation(Context.class); List<MappedClass> superclasses = getMappedSuperClasses(clazz); mappedClass = new MappedClass(clazz, uid, context != null ? new UID(context.value()) : null, superclasses); if (!clazz.isEnum()) { for (MappedClass mappedSuperClass : mappedClass.getMappedSuperClasses()) { if (mappedSuperClass != null) { 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()))); collectDynamicFieldProperties(clazz, mappedClass); assignConstructor(clazz, mappedClass); mappedClass.close(); mappedClasses.put(clazz, mappedClass);
private MappedPath getMappedPath(Path<?> path) { PathMetadata<?> md = path.getMetadata(); if (path.getMetadata().getPathType() != PathType.PROPERTY) { md = md.getParent().getMetadata(); } MappedClass mc = configuration.getMappedClass(md.getParent().getType()); return mc.getMappedPath(md.getElement().toString()); }
@SuppressWarnings("unchecked") private void toRDF(Object instance, ID subject, UID parentContext, MappedClass mappedClass, boolean update) { UID uri = mappedClass.getUID(); UID context = parentContext; if (!update && uri != null) { for (MappedPath path : mappedClass.getProperties()) { MappedProperty<?> property = path.getMappedProperty(); if (path.isSimpleProperty()) { for (MappedProperty<?> property : mappedClass.getDynamicProperties()) { Map<?, ?> properties = (Map) property.getValue(beanMap); if (properties != null) {
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()); } } }
MappedClass mappedClass = configuration.getMappedClass(clazz); OWLClass owlClass = null; UID cuid = mappedClass.getUID(); if (cuid == null) { return null; if (mappedClass.isEnum()) { List<RDFSResource> oneOf = new ArrayList<RDFSResource>(); for (Object constant : clazz.getEnumConstants()) { for (MappedPath mappedPath : mappedClass.getProperties()) { if (!mappedPath.isInherited() && mappedPath.isSimpleProperty()) { processProperty(session, resources, owlClass, cuid, mappedPath);
private <T> void setId(MappedClass mappedClass, ID subject, BeanMap instance) { MappedProperty<?> idProperty = mappedClass.getIdProperty(); if (idProperty != null && !mappedClass.isEnum() && !idProperty.isVirtual()) { Object id = null; Identifier identifier; for (MappedProperty<?> mixinProperty : mappedClass.getMixinProperties()) { Object mixinValue = mixinProperty.getValue(instance); if (mixinValue != null) {
private RDFQuery createQuery(MappedClass mappedClass, @Nullable UID type, boolean polymorphic) { RDFQuery query = new RDFQueryImpl(connection); if (type != null) { query.where(Blocks.S_TYPE); if (mappedClass.getContext() != null) { query.set(QNODE.typeContext, mappedClass.getContext()); } if (polymorphic) { Collection<UID> types = ontology.getSubtypes(type); if (types.size() > 1 && connection.getInferenceOptions().subClassOf()) { query.where(QNODE.type.in(types)); } else { query.set(QNODE.type, type); } } else { query.set(QNODE.type, type); } } query.where(Blocks.SPOC); if (!polymorphic && mappedClass.getDynamicProperties().isEmpty() && mappedClass.getMappedPredicates().size() < 5) { query.where(QNODE.p.in(mappedClass.getMappedPredicates())); } return query; }
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())); } } }
@Test public void Without_namespace() { ConfigurationBuilder builder = new ConfigurationBuilder(); for (Class<?> cl : Arrays.<Class<?>> asList(Person.class, Department.class, Company.class, Labeled.class)) { builder.addClass(cl).addProperties(); } Configuration configuration = builder.build(); MappedClass person = configuration.getMappedClass(Person.class); String ns = "java:com.mysema.rdfbean.object.ConfigurationBuilderTest.Person#"; assertEquals(new UID(ns, "Person"), person.getUID()); assertEquals(new UID(ns, "labeled"), person.getMappedPath("labeled").getPredicatePath().get(0).getUID()); }
if (!mappedClass.getDynamicProperties().isEmpty()) { bindDynamicProperties(subject, properties.getDirect(), beanMap, mappedClass); for (MappedPath path : mappedClass.getProperties()) { if (!path.isConstructorParameter()) { MappedProperty<?> property = path.getMappedProperty();
@SuppressWarnings("unchecked") private void bindDynamicProperties(ID subject, Multimap<UID, STMT> properties, BeanMap beanMap, MappedClass mappedClass) { for (MappedProperty<?> property : mappedClass.getDynamicProperties()) { UID context = property.getContext(); Map<UID, Object> values = new HashMap<UID, Object>(); if (property.isIncludeMapped() || !mappedClass.isMappedPredicate(stmt.getPredicate())) { Class<?> componentType; if (property.isDynamicCollection()) { for (STMT typeStmt : typeStmts) { for (MappedClass cl : configuration.getMappedClasses(typeStmt.getObject().asURI())) { if (componentType.isAssignableFrom(cl.getJavaClass())) { matched = true;
public ConfigurationOntology(Configuration configuration) { Set<UID> types = new HashSet<UID>(); Multimap<UID, UID> directSubtypes = MultimapFactory.<UID, UID> createWithSet(); Multimap<UID, UID> directSupertypes = MultimapFactory.<UID, UID> createWithSet(); for (MappedClass mappedClass : configuration.getMappedClasses()) { types.add(mappedClass.getUID()); for (MappedClass superClass : mappedClass.getMappedSuperClasses()) { directSupertypes.put(mappedClass.getUID(), superClass.getUID()); directSubtypes.put(superClass.getUID(), mappedClass.getUID()); } // ClassMapping classMapping = // mappedClass.getJavaClass().getAnnotation(ClassMapping.class); // if (!classMapping.parent().equals("")) { // UID parent = new UID(classMapping.parent()); // directSupertypes.put(mappedClass.getUID(), parent); // directSubtypes.put(parent, mappedClass.getUID()); // } } initializeTypeHierarchy(types, directSubtypes, directSupertypes); }
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; }
/** * Replace the references of the given entity with persisted ones * * @param entity */ private void replaceReferences(Configuration configuration, Object entity) { BeanMap beanMap = new BeanMap(entity); MappedClass mappedClass = configuration.getMappedClass(entity.getClass()); for (MappedPath mappedPath : mappedClass.getProperties()) { if (mappedPath.isReference() && mappedPath.isSimpleProperty()) { Object value = mappedPath.getMappedProperty().getValue(beanMap); if (value != null && persisted.containsKey(value)) { value = persisted.get(value); mappedPath.getMappedProperty().setValue(beanMap, value); } } } }