@SuppressWarnings("unchecked") void addMappedPath(MappedPath path) { if (path.getPredicatePath().size() > 0) { if (!path.get(0).inv()) { mappedPredicates.add(path.get(0).getUID()); } else { invMappedPredicates.add(path.get(0).getUID()); } else if (path.getMappedProperty().isAnnotationPresent(Mixin.class)) { mixinProperties.add(path.getMappedProperty()); MappedProperty property = path.getMappedProperty(); MappedPath existingPath = properties.get(property.getName()); if (path.getMappedProperty().isIdReference()) { if (idProperty != null) { throw new IllegalArgumentException("Duplicate ID property: " + idProperty + " and " + path.getMappedProperty()); idProperty = path.getMappedProperty(); properties.put(path.getName(), path); MappedProperty existingProperty = existingPath.getMappedProperty(); if (property instanceof FieldProperty) { if (existingProperty instanceof FieldProperty) { properties.put(path.getName(), path); path.merge(existingPath); existingPath.merge(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())); }
/** * 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); } } } }
public int getOrder() { if (isSimpleProperty()) { if (!isReference()) { return 1; } else { return 2; } } else if (size() == 1) { return 3; } else { return 4; } }
private void loadReferences(MappedClass mappedClass, Map<ID, Multimap<UID, STMT>> directProps, Set<ID> handled) { Map<UID, Class<?>> directToType = new HashMap<UID, Class<?>>(); for (MappedPath mappedPath : mappedClass.getProperties()) { if (mappedPath.isReference() && !mappedPath.getPredicatePath().isEmpty()) { MappedProperty<?> property = mappedPath.getMappedProperty(); if (!property.isList()) { Class<?> type = property.getType(); type = property.getComponentType(); if (!type.isEnum() && !mappedPath.isInverse(0)) { directToType.put(mappedPath.get(0).getUID(), type);
@SuppressWarnings("unchecked") private Operation<?> transformPathEqNeConstant(Operation<?> operation) { Path<?> path = (Path<?>) operation.getArg(0); Constant<?> constant = (Constant<?>) operation.getArg(1); MappedPath mappedPath = getMappedPath(path); // id property if (path.getMetadata().getPathType() == PathType.PROPERTY && constant.getType().equals(String.class) && mappedPath.getPredicatePath().isEmpty()) { operation = PredicateOperation.create((Operator) operation.getOperator(), path, new ConstantImpl<ID>(session.getId(new LID(constant.toString())))); // localized property } else if (mappedPath.getMappedProperty().isLocalized()) { Locale locale; if (path.getMetadata().getPathType() == PathType.PROPERTY) { locale = session.getCurrentLocale(); } else { locale = (Locale) path.getMetadata().getElement(); } operation = PredicateOperation.create((Operator) operation.getOperator(), path, new ConstantImpl<LIT>(new LIT(constant.toString(), locale))); } return operation; }
@SuppressWarnings("unchecked") @Nullable private Object convertSingleValue(MappedPath propertyPath, Set<? extends NODE> values) { MappedProperty<?> propertyDefinition = propertyPath.getMappedProperty(); Class targetType = propertyDefinition.getType(); if (!values.isEmpty()) { return convertValue(values.iterator().next(), targetType, propertyPath); } else { return null; } }
@SuppressWarnings("unchecked") private void processProperty(Session session, Map<UID, RDFSResource> resources, OWLClass owlClass, UID cuid, MappedPath mappedPath) { MappedProperty<?> mappedProperty = mappedPath.getMappedProperty(); MappedPredicate mappedPredicate = mappedPath.get(0); UID puid = mappedPredicate.getUID(); String predicateNs = puid.ns(); if (mappedPath.isReference()) { if (!(property instanceof ObjectProperty)) { throw new IllegalArgumentException("Expected ObjectProperty for: " + mappedPath); if (mappedProperty.isAnyResource()) { property = new RDFProperty(puid); } else if (mappedPath.isReference() || mappedProperty.isCollection()) { property = new ObjectProperty(puid); } else { } else if (mappedPath.isReference()) { if (property.getRange().isEmpty()) { RDFSClass<?> range = processClass(session,
MappedProperty<?> property = path.getMappedProperty(); if (path.isSimpleProperty()) { MappedPredicate mappedPredicate = path.get(0); UID predicate = mappedPredicate.getUID(); if (mappedPredicate.getContext() != null) {
private Object convertValue(NODE value, Class<?> targetClass, MappedPath propertyPath) { Object convertedValue; MappedProperty mappedProperty = propertyPath.getMappedProperty(); try { if (targetClass.isAssignableFrom(value.getClass())) { else if (MappedPath.isWildcard(targetClass) && value.isResource()) { convertedValue = convertMappedObject((ID) value, Object.class, true, mappedProperty.isInjection()); if (propertyPath.isIgnoreInvalid()) { logger.debug(e.getMessage(), e); convertedValue = null;
@Test public void VverrideWithinClass() { Configuration configuration = new DefaultConfiguration(TEST.NS, WithinClass.class); MappedClass mappedClass = configuration.getMappedClass(WithinClass.class); MappedPath path = mappedClass.getMappedPath("reference"); assertEquals(new UID(TEST.NS, "reference"), path.get(0).getUID()); MappedProperty<?> property = path.getMappedProperty(); assertTrue(property.isInjection()); }
assertEquals(Parent.class, path.getMappedProperty().getType()); assertFalse(path.isInherited()); assertEquals(Object.class, path.getMappedProperty().getType()); assertFalse(path.getMappedProperty().isRequired()); assertFalse(path.isInherited()); assertEquals(Parent.class, path.getMappedProperty().getComponentType()); assertFalse(path.isInherited()); assertFalse(path.isInherited()); assertEquals(FirstChild.class, path.getMappedProperty().getType()); assertFalse(path.isInherited()); assertEquals(Integer.class, path.getMappedProperty().getType()); assertFalse(path.getMappedProperty().isRequired()); assertFalse(path.isInherited()); assertEquals(FirstChild.class, path.getMappedProperty().getComponentType()); assertFalse(path.isInherited()); assertTrue(path.isInherited()); assertEquals(NestedChild.class, path.getMappedProperty().getType()); assertFalse(path.isInherited()); assertEquals(String.class, path.getMappedProperty().getType()); assertTrue(path.getMappedProperty().isRequired());
private Set<NODE> getPathValue(MappedPath path, ID subject, PropertiesMap properties, UID context) { if (configuration.allowRead(path)) { Set<NODE> values; MappedProperty<?> property = path.getMappedProperty(); if (property.isMixin()) { values = Collections.<NODE> singleton(subject); } else if (path.size() > 0) { values = findPathValues(subject, path, 0, properties, context); } else { values = new LinkedHashSet<NODE>(); } if (values.isEmpty()) { for (UID uri : property.getDefaults()) { values.add(uri); } } return values; } return Collections.emptySet(); }
@Test public void Inverse() { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.addClass(TEST.NS, Company.class) .addId("id") .addProperty("departments", new UID(TEST.NS, "company"), true) .addProperties(); Configuration configuration = builder.build(); // company MappedClass company = configuration.getMappedClass(Company.class); MappedPath company_departments = company.getMappedPath("departments"); assertEquals(TEST.NS, company_departments.getPredicatePath().get(0).getUID().ns()); assertEquals("company", company_departments.getPredicatePath().get(0).getUID().ln()); assertTrue(company_departments.getPredicatePath().get(0).inv()); }
if (!mappedPath.isInherited() && mappedPath.isSimpleProperty()) { processProperty(session, resources, owlClass, cuid, mappedPath);
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())); } } }
private Object getValue(MappedPath propertyPath, Set<? extends NODE> values, UID context) throws InstantiationException, IllegalAccessException { MappedProperty<?> mappedProperty = propertyPath.getMappedProperty(); Object convertedValue; else if (values.size() <= 1 || propertyPath.isIgnoreInvalid()) { convertedValue = convertSingleValue(propertyPath, values);
if (!path.isConstructorParameter()) { MappedProperty<?> property = path.getMappedProperty(); if (!property.isVirtual()) { Object convertedValue;
@Test public void ToString() throws SecurityException, NoSuchMethodException { Configuration configuration = new DefaultConfiguration(TEST.NS, MappedPathTest.class); MappedClass mappedClass = configuration.getMappedClass(MappedPathTest.class); MethodProperty property = new MethodProperty(MappedPathTest.class.getMethod("getProperty"), mappedClass); MappedPredicate predicate = new MappedPredicate(TEST.NS, new DummyPredicate(RDF.type), null); MappedPath path = new MappedPath(property, Collections.<MappedPredicate> singletonList(predicate), false); assertEquals("public java.lang.String com.mysema.rdfbean.object.MappedPathTest.getProperty() { http://www.w3.org/1999/02/22-rdf-syntax-ns#type }", path.toString()); }
private Set<NODE> findPathValues(ID resource, MappedPath path, int index, PropertiesMap properties, UID context) { MappedPredicate predicate = path.get(index); if (predicate.getContext() != null) { context = predicate.getContext(); } Set<NODE> values; if (!predicate.inv() && properties.getDirect() != null) { values = findValues(predicate.getUID(), properties.getDirect(), context, predicate.inv()); } else if (predicate.inv() && properties.getInverse() != null) { values = findValues(predicate.getUID(), properties.getInverse(), null, predicate.inv()); } else { values = findValues(resource, predicate.getUID(), predicate.inv(), predicate.includeInferred(), context); } if (path.size() > index + 1) { Set<NODE> nestedValues = new LinkedHashSet<NODE>(); for (NODE value : values) { if (value.isResource()) { nestedValues.addAll(findPathValues((ID) value, path, index + 1, new PropertiesMap(null, null), context)); } } return nestedValues; } return values; }