List of usage examples for org.springframework.data.mapping PersistentProperty getField
@Nullable Field getField();
From source file:com.turbospaces.serialization.PropertiesSerializer.java
/** * create new properties serialized with the provided entity meta-data information. * /* ww w. ja v a 2 s. c o m*/ * @param kryo * kryo serialization provider * @param entityMetadata * class meta data provider */ public PropertiesSerializer(final DecoratedKryo kryo, final BO entityMetadata) { super(kryo, new ArrayList<CachedSerializationProperty>(entityMetadata.getOrderedProperties().length) { private static final long serialVersionUID = 1L; { PersistentProperty[] orderedProperties = entityMetadata.getOrderedProperties(); for (PersistentProperty orderedProperty : orderedProperties) add(new CachedSerializationProperty(orderedProperty.getType(), orderedProperty.getField())); } }.toArray(new CachedSerializationProperty[entityMetadata.getOrderedProperties().length])); this.entityMetadata = entityMetadata; }
From source file:com.turbospaces.model.BO.java
/** * create business object over actual basic persistent entity * //from w w w . ja v a2 s . c o m * @param delegate * the actual persistent entity meta-data provider * @throws NoSuchMethodException * re-throw cglib exception * @throws SecurityException * re-throw cglib exception * @throws IntrospectionException * re-throw exceptions */ public BO(final BasicPersistentEntity delegate) throws SecurityException, NoSuchMethodException, IntrospectionException { this.delegate = delegate; this.fastConstructor = FastClass.create(delegate.getType()) .getConstructor(delegate.getType().getConstructor()); // find optimistic lock version/routing fields { final Collection<PersistentProperty> versionCandidates = Lists.newLinkedList(); final Collection<PersistentProperty> routingCandidates = Lists.newLinkedList(); delegate.doWithProperties(new PropertyHandler() { @Override public void doWithPersistentProperty(final PersistentProperty persistentProperty) { PropertyDescriptor propertyDescriptor = persistentProperty.getPropertyDescriptor(); Field field = persistentProperty.getField(); if (hasAnnotation(propertyDescriptor, field, Version.class)) versionCandidates.add(persistentProperty); if (hasAnnotation(propertyDescriptor, field, Routing.class)) routingCandidates.add(persistentProperty); } private boolean hasAnnotation(final PropertyDescriptor descriptor, final Field field, final Class annotation) { if (descriptor != null && descriptor.getReadMethod() != null && descriptor.getReadMethod().getAnnotation(annotation) != null) return true; if (field != null && field.getAnnotation(annotation) != null) return true; return false; } }); Preconditions.checkArgument(versionCandidates.size() <= 1, "too many fields marked with @Version annotation, candidates = " + versionCandidates.toString()); Preconditions.checkArgument(routingCandidates.size() <= 1, "too many fields marked with @Routing annotation, candidates = " + routingCandidates.toString()); if (!versionCandidates.isEmpty()) optimisticLockVersionProperty = versionCandidates.iterator().next(); if (!routingCandidates.isEmpty()) routingProperty = routingCandidates.iterator().next(); } { // Java Beans convention marker AtomicBoolean propertyAccess = new AtomicBoolean(true); List<String> setters = Lists.newLinkedList(); List<String> getters = Lists.newLinkedList(); List<Class<?>> types = Lists.newLinkedList(); for (PersistentProperty<?> persistentProperty : getOrderedProperties()) { PropertyDescriptor propertyDescriptor = persistentProperty.getPropertyDescriptor(); if (propertyDescriptor != null) { if (propertyDescriptor.getReadMethod() != null && propertyDescriptor.getWriteMethod() != null) { setters.add(propertyDescriptor.getWriteMethod().getName()); getters.add(propertyDescriptor.getReadMethod().getName()); types.add(persistentProperty.getType()); } } else { propertyAccess.set(false); brokenProperties.add(persistentProperty); } } if (propertyAccess.get()) // create properties extract for all persistent properties bulkBean = BulkBean.create(delegate.getType(), getters.toArray(new String[getters.size()]), setters.toArray(new String[setters.size()]), types.toArray(new Class[types.size()])); else Log.warn(String.format( "PropetiesSerializer-%s unable to use getters-setters access optimization. Suspected/Corrupted properties = %s", delegate.getType().getSimpleName(), getBrokenProperties())); boolean canOptimizeIdProperty = hasReadWriteMethods(delegate.getIdProperty()); boolean canOptimizeVersionProperty = hasReadWriteMethods(getOptimisticLockVersionProperty()); boolean canOptimizeRoutingProperty = hasReadWriteMethods(getRoutingProperty()); // create id/version/routing bulk fields extractor if (canOptimizeIdProperty && canOptimizeVersionProperty && canOptimizeRoutingProperty) { String[] g = new String[] { delegate.getIdProperty().getPropertyDescriptor().getReadMethod().getName(), getOptimisticLockVersionProperty().getPropertyDescriptor().getReadMethod().getName(), getRoutingProperty().getPropertyDescriptor().getReadMethod().getName() }; String[] s = new String[] { delegate.getIdProperty().getPropertyDescriptor().getWriteMethod().getName(), getOptimisticLockVersionProperty().getPropertyDescriptor().getWriteMethod().getName(), getRoutingProperty().getPropertyDescriptor().getWriteMethod().getName() }; Class<?>[] c = new Class[] { delegate.getIdProperty().getType(), getOptimisticLockVersionProperty().getType(), getRoutingProperty().getType() }; idVersionRoutingBulkBean = BulkBean.create(delegate.getType(), g, s, c); } } }
From source file:org.lightadmin.core.web.ApplicationController.java
private Object cloneEntityOfDomain(String entityId, String domainTypeName) { DomainTypeAdministrationConfiguration domainTypeConfiguration = configuration.forEntityName(domainTypeName); DynamicJpaRepository repository = domainTypeConfiguration.getRepository(); PersistentEntity persistentEntity = domainTypeConfiguration.getPersistentEntity(); Serializable id = (Serializable) conversionService.convert(entityId, persistentEntity.getIdProperty().getActualType()); Object found = repository.findOne(id); if (found != null) { try {/* www.j a va 2 s. c o m*/ Object newInstance = null; if (found instanceof CloneableEntity) { newInstance = CloneableEntity.class.cast(found).clone(); } else { newInstance = domainTypeConfiguration.getDomainType().newInstance(); BeanUtils.copyProperties(found, newInstance, persistentEntity.getIdProperty().getName()); PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(newInstance); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getReadMethod() != null && propertyDescriptor.getWriteMethod() != null) { Object value = propertyDescriptor.getReadMethod().invoke(newInstance); Object newValue = null; try { if (value instanceof SortedSet) { newValue = new TreeSet(SortedSet.class.cast(value)); } else if (value instanceof Set) { newValue = new HashSet(Set.class.cast(value)); } else if (value instanceof SortedMap) { newValue = new TreeMap(SortedMap.class.cast(value)); } else if (value instanceof Collection) { newValue = new ArrayList(Collection.class.cast(value)); } else if (value instanceof Map) { newValue = new HashMap(Map.class.cast(value)); } } catch (Throwable t) { if (logger.isWarnEnabled()) { logger.warn("Can't clone " + propertyDescriptor.getName(), t); } } if (newValue != null) { propertyDescriptor.getWriteMethod().invoke(newInstance, newValue); } } } } Object saved = repository.saveAndFlush(newInstance); PersistentProperty idProperty = persistentEntity.getIdProperty(); Field idField = idProperty.getField(); idField.setAccessible(true); return idProperty.usePropertyAccess() ? idProperty.getGetter().invoke(saved) : idField.get(saved); } catch (Throwable t) { throw new RuntimeException(t); } } else { return null; } }