@Override public Object apply( PropertyDescriptor propertyDescriptor ) { return getEntityState( entity ).propertyValueOf( propertyDescriptor.qualifiedName() ); } }
private Object findValue( Map<String, Object> attributes, PropertyDescriptor descriptor ) { String name = descriptor.qualifiedName().name(); if( name.equals( "reference" ) ) { Object id = attributes.get( "id" ); if( id == null ) { throw new IllegalArgumentException( resourceType.getName() + " implements Identity and must have an {id} attribute in the path template." ); } return id; } return attributes.get( name ); }
private <T> T getNumber( Preferences prefs, ModuleDescriptor module, PropertyDescriptor pDesc, NumberParser<T> parser ) { Object initialValue = pDesc.resolveInitialValue( module ); String str = prefs.get( pDesc.qualifiedName().name(), initialValue == null ? null : initialValue.toString() ); T result = null; if( str != null ) { result = parser.parse( str ); } return result; }
private <T> T getNumber( Preferences prefs, ModuleDescriptor module, PropertyDescriptor pDesc, NumberParser<T> parser ) { Object initialValue = pDesc.resolveInitialValue( module ); String str = prefs.get( pDesc.qualifiedName().name(), initialValue == null ? null : initialValue.toString() ); T result = null; if( str != null ) { result = parser.parse( str ); } return result; }
@Override public TemplateCollectionModel keys() throws TemplateModelException { List<String> names = descriptor.state().properties() .map( descriptor -> descriptor.qualifiedName().name() ) .collect( Collectors.toList() ); return (TemplateCollectionModel) wrapper.wrap( names.iterator() ); }
@Override public TemplateCollectionModel keys() throws TemplateModelException { List<String> names = descriptor.state().properties() .map( descriptor -> descriptor.qualifiedName().name() ) .collect( Collectors.toList() ); return (TemplateCollectionModel) wrapper.wrap( names.iterator() ); }
private void serializeProperties( EntityState entityState, Map<String, String> props ) { Stream<? extends PropertyDescriptor> properties = entityState.entityDescriptor().state().properties(); properties.forEach( descriptor -> { Object value = entityState.propertyValueOf( descriptor.qualifiedName() ); if( value != null ) { String serialized = valueSerialization.serialize( value ); props.put( descriptor.qualifiedName().name(), serialized ); } } ); }
@Override public void exportDataSources() throws MalformedObjectNameException, MBeanRegistrationException, InstanceAlreadyExistsException, NotCompliantMBeanException { for ( ServiceReference<DataSource> dataSource : dataSources ) { ModuleDescriptor module = spi.moduleOf( dataSource ); EntityDescriptor descriptor = module.entityDescriptor( DataSourceConfiguration.class.getName() ); List<MBeanAttributeInfo> attributes = new ArrayList<>(); Map<String, AccessibleObject> properties = new LinkedHashMap<>(); descriptor.state().properties().forEach(persistentProperty -> { if ( !persistentProperty.isImmutable() ) { String propertyName = persistentProperty.qualifiedName().name(); String type = persistentProperty.valueType().primaryType().getName(); attributes.add( new MBeanAttributeInfo( propertyName, type, propertyName, true, true, type.equals( "java.lang.Boolean" ) ) ); properties.put( propertyName, persistentProperty.accessor() ); } } ); List<MBeanOperationInfo> operations = new ArrayList<>(); operations.add( new MBeanOperationInfo( "restart", "Restart DataSource", new MBeanParameterInfo[ 0 ], "void", MBeanOperationInfo.ACTION_INFO ) ); String mbeanName = dataSource.identity().toString(); MBeanInfo mbeanInfo = new MBeanInfo( DataSourceConfiguration.class.getName(), mbeanName, attributes.toArray( new MBeanAttributeInfo[ attributes.size() ] ), null, operations.toArray( new MBeanOperationInfo[ operations.size() ] ), null ); Object mbean = new ConfigurableDataSource( dataSourceService, mbeanInfo, mbeanName, properties ); ObjectName configurableDataSourceName = new ObjectName( "Polygene:application=" + application.name() + ",class=Datasource,name=" + mbeanName ); server.registerMBean( mbean, configurableDataSourceName ); configurationNames.add( configurableDataSourceName ); } }
@Override public void exportDataSources() throws MalformedObjectNameException, MBeanRegistrationException, InstanceAlreadyExistsException, NotCompliantMBeanException { for ( ServiceReference<DataSource> dataSource : dataSources ) { ModuleDescriptor module = spi.moduleOf( dataSource ); EntityDescriptor descriptor = module.entityDescriptor( DataSourceConfiguration.class.getName() ); List<MBeanAttributeInfo> attributes = new ArrayList<>(); Map<String, AccessibleObject> properties = new LinkedHashMap<>(); descriptor.state().properties().forEach(persistentProperty -> { if ( !persistentProperty.isImmutable() ) { String propertyName = persistentProperty.qualifiedName().name(); String type = persistentProperty.valueType().primaryType().getName(); attributes.add( new MBeanAttributeInfo( propertyName, type, propertyName, true, true, type.equals( "java.lang.Boolean" ) ) ); properties.put( propertyName, persistentProperty.accessor() ); } } ); List<MBeanOperationInfo> operations = new ArrayList<>(); operations.add( new MBeanOperationInfo( "restart", "Restart DataSource", new MBeanParameterInfo[ 0 ], "void", MBeanOperationInfo.ACTION_INFO ) ); String mbeanName = dataSource.identity().toString(); MBeanInfo mbeanInfo = new MBeanInfo( DataSourceConfiguration.class.getName(), mbeanName, attributes.toArray( new MBeanAttributeInfo[ attributes.size() ] ), null, operations.toArray( new MBeanOperationInfo[ operations.size() ] ), null ); Object mbean = new ConfigurableDataSource( dataSourceService, mbeanInfo, mbeanName, properties ); ObjectName configurableDataSourceName = new ObjectName( "Polygene:application=" + application.name() + ",class=Datasource,name=" + mbeanName ); server.registerMBean( mbean, configurableDataSourceName ); configurationNames.add( configurableDataSourceName ); } }
private void serializeProperties( EntityState entityState, Graph graph, Resource subject, EntityDescriptor entityType, boolean includeNonQueryable ) { // Properties entityType.state().properties().forEach( persistentProperty -> { Object property = entityState.propertyValueOf( persistentProperty.qualifiedName() ); if( property != null ) { serializeProperty( persistentProperty, property, subject, graph, includeNonQueryable ); } } ); }
@Override public void write( OutputStream outputStream ) throws IOException { if( representation != null ) { representation.write( outputStream ); } else if( object != null ) { AssociationStateHolder state = spi.stateOf( (ValueComposite) object ); ValueDescriptor descriptor = (ValueDescriptor) spi.modelDescriptorFor( object ); descriptor.state().properties().forEach( property -> { String name = property.qualifiedName().name(); String value = state.propertyFor( property.accessor() ).get().toString(); try { outputStream.write( name.getBytes( "UTF-8" ) ); outputStream.write( "=".getBytes( "UTF-8" ) ); outputStream.write( value.getBytes( "UTF-8" ) ); outputStream.write( '\n' ); } catch( IOException e ) { // TODO; not sure what could happen here. } } ); } } }
@Override public Function<PropertyDescriptor, Object> properties( Object entityComposite, Function<PropertyDescriptor, Object> defaultFn ) { return descriptor -> { Object value = defaultFn.apply( descriptor ); QualifiedName name = QualifiedName.fromClass( SomeType.class, "name" ); if( name.equals( descriptor.qualifiedName() ) ) { return "[" + value + "]"; } return value; }; }
@Override public Function<PropertyDescriptor, Object> properties( Object entityComposite, Function<PropertyDescriptor, Object> defaultFn ) { return descriptor -> { QualifiedName name = QualifiedName.fromClass( SomeType.class, "name" ); Object value = defaultFn.apply( descriptor ); if( descriptor.qualifiedName().equals( name ) ) { return "[" + value + "]"; } return value; }; }
private T createObject() { //noinspection MismatchedQueryAndUpdateOfCollection final Form form = new Form( representation ); ValueBuilder<T> builder = this.vbf.newValueBuilderWithState( objectClass, descriptor -> form.getFirstValue( descriptor.qualifiedName().name() ), descriptor -> null, descriptor -> null, descriptor -> null ); return builder.newInstance(); }
private Object mapField( RestForm form, PropertyDescriptor propertyName ) { String name = propertyName.qualifiedName().name(); FormField field = form.field( name ); if( field == null ) { UnitOfWork uow = uowf.currentUnitOfWork(); String usecase = ""; if( uow != null ) { usecase = uow.usecase().name(); } throw new IllegalArgumentException( "Field named '" + name + "' is required and not present in usecase " + usecase ); } return field.value().get(); } }
private void serializePropertyTypes( final EntityDescriptor entityDescriptor, final Graph graph, final URI entityTypeUri ) { ValueFactory values = graph.getValueFactory(); // Properties entityDescriptor.state().properties().forEach( persistentProperty -> { URI propertyURI = values.createURI( persistentProperty.qualifiedName().toURI() ); graph.add( propertyURI, Rdfs.DOMAIN, entityTypeUri ); graph.add( propertyURI, Rdfs.TYPE, Rdfs.PROPERTY ); // TODO Support more types URI type = dataTypes.get( persistentProperty.valueType().primaryType().getName() ); if( type != null ) { graph.add( propertyURI, Rdfs.RANGE, type ); } } ); } }
private void serializeProperty( PropertyDescriptor persistentProperty, Object property, Resource subject, Graph graph, boolean includeNonQueryable ) { if( !( includeNonQueryable || persistentProperty.queryable() ) ) { return; // Skip non-queryable } ValueType valueType = persistentProperty.valueType(); final ValueFactory valueFactory = graph.getValueFactory(); String propertyURI = persistentProperty.qualifiedName().toURI(); URI predicate = valueFactory.createURI( propertyURI ); String baseURI = propertyURI.substring( 0, propertyURI.indexOf( '#' ) ) + "/"; if( valueType instanceof ValueCompositeType ) { serializeValueComposite( subject, predicate, (ValueComposite) property, valueType, graph, baseURI, includeNonQueryable ); } else { String stringProperty = serializer.serialize( Serializer.Options.NO_TYPE_INFO, property ); final Literal object = valueFactory.createLiteral( stringProperty ); graph.add( subject, predicate, object ); } }
RestForm.class, descriptor -> { if( descriptor.qualifiedName().name().equals( "fields" ) )
private Object createParametersComposite( RestForm form, Class<?> argType ) { ValueBuilder<?> vb = vbf.newValueBuilderWithState( argType, descriptor -> { FormField field = form.field( descriptor.qualifiedName().name() ); if( field == null ) { return null; } Class<?> propertyType = descriptor.valueType().primaryType(); Property<String> value = field.value(); if( value == null ) { return null; } return convertPropertyValue( value.get(), propertyType ); }, descriptor -> null, descriptor -> null, descriptor -> null ); return vb.newInstance(); }
public EntityState newEntityState( EntityStoreUnitOfWork store, EntityReference reference ) throws ConstraintViolationException, EntityStoreException { try { // New EntityState EntityState entityState = store.newEntityState( reference, this ); // Set reference property PropertyDescriptor persistentPropertyDescriptor = state().propertyModelFor( IDENTITY_METHOD ); entityState.setPropertyValue( persistentPropertyDescriptor.qualifiedName(), reference.identity() ); return entityState; } catch( EntityAlreadyExistsException e ) { throw new EntityCompositeAlreadyExistsException( reference ); } catch( EntityStoreException e ) { throw new ConstructionException( "Could not create new entity in store", e ); } catch( ConstraintViolationException e ) { e.setCompositeDescriptor( this ); e.setIdentity( reference.identity() ); throw e; } }