ZEST-151 : Removed Conversion Library
Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/5229303a Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/5229303a Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/5229303a Branch: refs/heads/ValueSerializationCleaning Commit: 5229303a2394233af083f81e2e46aa4bcac5356f Parents: e4736b0 Author: Niclas Hedhman <[email protected]> Authored: Sun Jun 12 16:56:17 2016 +0800 Committer: Niclas Hedhman <[email protected]> Committed: Sun Jun 12 22:51:05 2016 +0800 ---------------------------------------------------------------------- libraries/conversion/build.gradle | 32 - libraries/conversion/dev-status.xml | 38 - libraries/conversion/src/docs/conversion.txt | 129 ---- .../conversion/values/EntityToValue.java | 463 ------------ .../values/EntityToValueAssembler.java | 45 -- .../conversion/values/EntityToValueService.java | 55 -- .../values/PropertyNotPresentException.java | 46 -- .../zest/library/conversion/values/Shared.java | 71 -- .../library/conversion/values/Unqualified.java | 35 - .../conversion/values/ValueToEntity.java | 137 ---- .../values/ValueToEntityAssembler.java | 45 -- .../conversion/values/ValueToEntityMixin.java | 730 ------------------- .../conversion/values/ValueToEntityService.java | 33 - .../zest/library/conversion/values/package.html | 24 - .../conversion/values/EntityToValueTest.java | 232 ------ .../values/NestedValuesConversionTest.java | 130 ---- .../library/conversion/values/TestModel.java | 202 ----- .../conversion/values/ValueToEntityTest.java | 355 --------- settings.gradle | 1 - 19 files changed, 2803 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-java/blob/5229303a/libraries/conversion/build.gradle ---------------------------------------------------------------------- diff --git a/libraries/conversion/build.gradle b/libraries/conversion/build.gradle deleted file mode 100644 index 7443b58..0000000 --- a/libraries/conversion/build.gradle +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ - -description = "Apache Zest⢠Conversion Library has various generic services to manipulate Apache Zest⢠ValueComposites, such as converting an Entity to a Value." - -jar { manifest { name = "Apache Zest⢠Library - Conversion" }} - -dependencies { - compile(project(":org.apache.zest.core:org.apache.zest.core.bootstrap")) - - testCompile(project(":org.apache.zest.core:org.apache.zest.core.testsupport")) - - testRuntime(project(":org.apache.zest.core:org.apache.zest.core.runtime")) - testRuntime(libraries.logback) -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/5229303a/libraries/conversion/dev-status.xml ---------------------------------------------------------------------- diff --git a/libraries/conversion/dev-status.xml b/libraries/conversion/dev-status.xml deleted file mode 100644 index 5b78a6d..0000000 --- a/libraries/conversion/dev-status.xml +++ /dev/null @@ -1,38 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" ?> -<!-- - ~ Licensed to the Apache Software Foundation (ASF) under one - ~ or more contributor license agreements. See the NOTICE file - ~ distributed with this work for additional information - ~ regarding copyright ownership. The ASF licenses this file - ~ to you under the Apache License, Version 2.0 (the - ~ "License"); you may not use this file except in compliance - ~ with the License. You may obtain a copy of the License at - ~ - ~ http://www.apache.org/licenses/LICENSE-2.0 - ~ - ~ Unless required by applicable law or agreed to in writing, software - ~ distributed under the License is distributed on an "AS IS" BASIS, - ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - ~ See the License for the specific language governing permissions and - ~ limitations under the License. - ~ - ~ - --> -<module xmlns="http://zest.apache.org/schemas/2008/dev-status/1" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://zest.apache.org/schemas/2008/dev-status/1 - http://zest.apache.org/schemas/2008/dev-status/1/dev-status.xsd"> - <status> - <!--none,early,beta,stable,mature--> - <codebase>stable</codebase> - - <!-- none, brief, good, complete --> - <documentation>brief</documentation> - - <!-- none, some, good, complete --> - <unittests>some</unittests> - </status> - <licenses> - <license>ALv2</license> - </licenses> -</module> http://git-wip-us.apache.org/repos/asf/zest-java/blob/5229303a/libraries/conversion/src/docs/conversion.txt ---------------------------------------------------------------------- diff --git a/libraries/conversion/src/docs/conversion.txt b/libraries/conversion/src/docs/conversion.txt deleted file mode 100644 index a8eb3cf..0000000 --- a/libraries/conversion/src/docs/conversion.txt +++ /dev/null @@ -1,129 +0,0 @@ -/////////////////////////////////////////////////////////////// - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. -/////////////////////////////////////////////////////////////// - -[[library-conversion, Conversion Library]] -= Conversion - DEPRECATED = - -[devstatus] --------------- -source=libraries/conversion/dev-status.xml --------------- - -The Conversion Library provides support for converting composite types. - -include::../../build/docs/buildinfo/artifact.txt[] - - -[WARNING] -.DEPRECATED -==== -This functionality is now present in <<def-unitofwork>> as -the two methods toEntity() and toValue(). Since this library was written -assocations of all kinds are now fully supported in Values. -==== - - -== Entities to Values == - -To convert Entities to Values, use the EntityToValueService. It is easily assembled: - -[snippet,java] ----- -source=libraries/conversion/src/test/java/org/apache/zest/library/conversion/values/EntityToValueTest.java -tag=assembly ----- - -Let's say we have an interface defining state: - -[snippet,java] ----- -source=libraries/conversion/src/test/java/org/apache/zest/library/conversion/values/TestModel.java -tag=state ----- - -An EntityComposite using the state as a Private Mixin: - -[snippet,java] ----- -source=libraries/conversion/src/test/java/org/apache/zest/library/conversion/values/TestModel.java -tag=entity ----- - -And a ValueComposite extending this very same state; - -[snippet,java] ----- -source=libraries/conversion/src/test/java/org/apache/zest/library/conversion/values/TestModel.java -tag=value ----- - -Here is how to convert an EntityComposite to a ValueComposite: - -[snippet,java] ----- -source=libraries/conversion/src/test/java/org/apache/zest/library/conversion/values/EntityToValueTest.java -tag=conversion ----- - - -== Values to Entities == - -Using the ValueToEntity service one can create new Entities or update existing ones from Values. -It is easy assembled: - -[snippet,java] ----- -source=libraries/conversion/src/test/java/org/apache/zest/library/conversion/values/ValueToEntityTest.java -tag=assembly ----- - -Let's say we have the exact same model as described above. - -Here is how to create an EntityComposite from a ValueComposite: - -[snippet,java] ----- -source=libraries/conversion/src/test/java/org/apache/zest/library/conversion/values/ValueToEntityTest.java -tag=creation ----- - -Here is how to update an EntityComposite from a ValueComposite: - -[snippet,java] ----- -source=libraries/conversion/src/test/java/org/apache/zest/library/conversion/values/ValueToEntityTest.java -tag=update ----- - - -== Associations are converted to Identity strings == - -If your Entities and Values cannot use the same state type, you can annotate the Value that is the target of the -conversion with the `@Unqualified` annotation. Then, the lookup of the Value Property will be performed using the -*unqualified* name only, and not via the default of the full qualified name. In other words, this means that the -Property may be declared in the different interfaces and still be matched. - -Here is an example: - -[snippet,java] ----- -source=libraries/conversion/src/test/java/org/apache/zest/library/conversion/values/TestModel.java -tag=unqualified ----- - http://git-wip-us.apache.org/repos/asf/zest-java/blob/5229303a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/EntityToValue.java ---------------------------------------------------------------------- diff --git a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/EntityToValue.java b/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/EntityToValue.java deleted file mode 100644 index bdddaa4..0000000 --- a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/EntityToValue.java +++ /dev/null @@ -1,463 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.library.conversion.values; - -import java.lang.reflect.AccessibleObject; -import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; -import org.apache.zest.api.association.Association; -import org.apache.zest.api.association.AssociationDescriptor; -import org.apache.zest.api.association.AssociationStateDescriptor; -import org.apache.zest.api.association.AssociationStateHolder; -import org.apache.zest.api.association.ManyAssociation; -import org.apache.zest.api.association.NamedAssociation; -import org.apache.zest.api.entity.EntityComposite; -import org.apache.zest.api.entity.EntityDescriptor; -import org.apache.zest.api.entity.EntityReference; -import org.apache.zest.api.entity.Identity; -import org.apache.zest.api.injection.scope.Structure; -import org.apache.zest.api.mixin.Mixins; -import org.apache.zest.api.property.PropertyDescriptor; -import org.apache.zest.api.structure.Module; -import org.apache.zest.api.value.NoSuchValueException; -import org.apache.zest.api.value.ValueBuilder; -import org.apache.zest.api.value.ValueDescriptor; -import org.apache.zest.functional.Iterables; -import org.apache.zest.spi.ZestSPI; - -import static org.apache.zest.library.conversion.values.Shared.STRING_COLLECTION_TYPE_SPEC; -import static org.apache.zest.library.conversion.values.Shared.STRING_MAP_TYPE_SPEC; -import static org.apache.zest.library.conversion.values.Shared.STRING_TYPE_SPEC; - -/** - * @deprecated Please use {@link org.apache.zest.api.unitofwork.UnitOfWork#toValue(Class, Identity)} instead. - */ -@Mixins( EntityToValue.EntityToValueMixin.class ) -public interface EntityToValue -{ - /** - * Convert an entity to a value. - * - * @param <T> parametrized type of the value - * @param valueType type of the value - * @param entity the entity to convert to a value - * @return the resulting value - */ - <T> T convert( Class<T> valueType, Object entity ); - - /** - * Convert an entity to a value with an opportunity to customize its prototype. - * - * @param <T> parametrized type of the value - * @param valueType type of the value - * @param entity the entity to convert to a value - * @param prototypeOpportunity a Function that will be mapped on the value prototype before instanciantion - * @return the resulting value - */ - <T> T convert( Class<T> valueType, Object entity, Function<T, T> prototypeOpportunity ); - - /** - * Convert an iterable of entities to an iterable of values. - * - * @param <T> parametrized type of the value - * @param valueType type of the value - * @param entities the entities to convert to values - * @return the resulting values - */ - <T> Iterable<T> convert( Class<T> valueType, Iterable<Object> entities ); - - /** - * Convert an iterable of entities to an iterable of values with an opportunity to customize their prototypes. - * - * @param <T> parametrized type of the value - * @param valueType type of the value - * @param entities the entities to convert to values - * @param prototypeOpportunity a Function that will be mapped on each of the value prototypes before instanciation. - * @return the resulting values - */ - <T> Iterable<T> convert( Class<T> valueType, Iterable<Object> entities, Function<T, T> prototypeOpportunity ); - - static class EntityToValueMixin - implements EntityToValue - { - - @Structure - private ZestSPI spi; - @Structure - private Module module; - - @Override - public <T> T convert( final Class<T> valueType, Object entity ) - { - return createInstance( doConversion( valueType, entity ) ); - } - - @Override - @SuppressWarnings( "unchecked" ) - public <T> T convert( final Class<T> valueType, Object entity, Function<T, T> prototypeOpportunity ) - { - ValueBuilder<?> builder = doConversion( valueType, entity ); - prototypeOpportunity.apply( (T) builder.prototype() ); - return createInstance( builder ); - } - - @Override - public <T> Iterable<T> convert( final Class<T> valueType, Iterable<Object> entities ) - { - return Iterables.map( - new Function<Object, T>() - { - @Override - public T apply( Object entity ) - { - return convert( valueType, entity ); - } - }, entities ); - } - - @Override - public <T> Iterable<T> convert( final Class<T> valueType, Iterable<Object> entities, final Function<T, T> prototypeOpportunity ) - { - return Iterables.map( - new Function<Object, T>() - { - @Override - public T apply( Object entity ) - { - return convert( valueType, entity, prototypeOpportunity ); - } - }, entities ); - } - - private <T> ValueBuilder<?> doConversion( final Class<T> valueType, Object entity ) - { - ValueDescriptor valueDescriptor = module.descriptor().valueDescriptor( valueType.getName() ); - if( valueDescriptor == null ) - { - throw new NoSuchValueException( valueType.getName(), module.name(), module.typeLookup() ); - } - Unqualified unqualified = valueDescriptor.metaInfo( Unqualified.class ); -// Iterable<? extends PropertyDescriptor> properties = valueDescriptor.state().properties(); - final EntityComposite composite = (EntityComposite) entity; - final EntityDescriptor entityDescriptor = spi.entityDescriptorFor( composite ); - final AssociationStateHolder associationState = spi.stateOf( composite ); - ValueBuilder<?> builder; - - if( unqualified == null || !unqualified.value() ) - { - // Copy state using qualified names - builder = module.newValueBuilderWithState( - valueType, - new Function<PropertyDescriptor, Object>() - { - @Override - public Object apply( PropertyDescriptor descriptor ) - { - try - { - return associationState.propertyFor( descriptor.accessor() ).get(); - } - catch( IllegalArgumentException e ) - { - AssociationStateDescriptor entityState = entityDescriptor.state(); - String associationName = descriptor.qualifiedName().name(); - if( STRING_TYPE_SPEC.test( descriptor.valueType() ) ) - { - // Find Association and convert to string - AssociationDescriptor associationDescriptor; - try - { - associationDescriptor = entityState.getAssociationByName( associationName ); - } - catch( IllegalArgumentException e1 ) - { - return null; - } - AccessibleObject associationMethod = associationDescriptor.accessor(); - Object entity = associationState.associationFor( associationMethod ).get(); - if( entity != null ) - { - return ( (Identity) entity ).identity().get(); - } - else - { - return null; - } - } - else if( STRING_COLLECTION_TYPE_SPEC.test( descriptor.valueType() ) ) - { - AssociationDescriptor associationDescriptor; - try - { - associationDescriptor = entityState.getManyAssociationByName( associationName ); - } - catch( IllegalArgumentException e1 ) - { - return Collections.emptyList(); - } - - ManyAssociation<?> state = associationState.manyAssociationFor( associationDescriptor.accessor() ); - List<String> entities = new ArrayList<>( state.count() ); - for( Object entity : state ) - { - entities.add( ( (Identity) entity ).identity().get() ); - } - return entities; - } - else if( STRING_MAP_TYPE_SPEC.test( descriptor.valueType() ) ) - { - AssociationDescriptor associationDescriptor; - try - { - associationDescriptor = entityState.getNamedAssociationByName( associationName ); - } - catch( IllegalArgumentException e1 ) - { - return Collections.emptyMap(); - } - - NamedAssociation<?> state = associationState.namedAssociationFor( associationDescriptor.accessor() ); - Map<String, String> entities = new LinkedHashMap<>( state.count() ); - for( String name : state ) - { - entities.put( name, ( (Identity) state.get( name ) ).identity().get() ); - } - return entities; - } - - return null; - } - } - }, - new Function<AssociationDescriptor, EntityReference>() - { - @Override - public EntityReference apply( AssociationDescriptor associationDescriptor ) - { - return EntityReference.entityReferenceFor( - associationState.associationFor( associationDescriptor.accessor() ).get() ); - } - }, - new Function<AssociationDescriptor, Iterable<EntityReference>>() - { - @Override - public Iterable<EntityReference> apply( AssociationDescriptor associationDescriptor ) - { - ManyAssociation<?> state = associationState.manyAssociationFor( associationDescriptor.accessor() ); - List<EntityReference> refs = new ArrayList<>( state.count() ); - for( Object entity : state ) - { - refs.add( EntityReference.entityReferenceFor( entity ) ); - } - return refs; - } - }, - new Function<AssociationDescriptor, Map<String, EntityReference>>() - { - @Override - public Map<String, EntityReference> apply( AssociationDescriptor associationDescriptor ) - { - NamedAssociation<?> assoc = associationState.namedAssociationFor( associationDescriptor.accessor() ); - Map<String, EntityReference> refs = new LinkedHashMap<>( assoc.count() ); - for( String name : assoc ) - { - refs.put( name, EntityReference.entityReferenceFor( assoc.get( name ) ) ); - } - return refs; - } - } ); - } - else - { - builder = module.newValueBuilderWithState(valueType, - new Function<PropertyDescriptor, Object>() - { - @Override - public Object apply( final PropertyDescriptor descriptor ) - { - AssociationStateDescriptor entityState = entityDescriptor.state(); - String propertyName = descriptor.qualifiedName().name(); - try - { - PropertyDescriptor propertyDescriptor = entityState.findPropertyModelByName( propertyName ); - return associationState.propertyFor( propertyDescriptor.accessor() ).get(); - } - catch( IllegalArgumentException e ) - { - if( STRING_TYPE_SPEC.test( descriptor.valueType() ) ) - { - // Find Association and convert to string - AssociationDescriptor associationDescriptor; - try - { - associationDescriptor = entityState.getAssociationByName( propertyName ); - } - catch( IllegalArgumentException e1 ) - { - return null; - } - - AccessibleObject associationMethod = associationDescriptor.accessor(); - Object entity = associationState.associationFor( associationMethod ).get(); - if( entity != null ) - { - return ( (Identity) entity ).identity().get(); - } - else - { - return null; - } - } - else if( STRING_COLLECTION_TYPE_SPEC.test( descriptor.valueType() ) ) - { - AssociationDescriptor associationDescriptor; - try - { - associationDescriptor = entityState.getManyAssociationByName( propertyName ); - } - catch( IllegalArgumentException e1 ) - { - return null; - } - - AccessibleObject associationMethod = associationDescriptor.accessor(); - ManyAssociation<?> state = associationState.manyAssociationFor( associationMethod ); - List<String> entities = new ArrayList<>( state.count() ); - for( Object entity : state ) - { - entities.add( ( (Identity) entity ).identity().get() ); - } - return entities; - } - else if( STRING_MAP_TYPE_SPEC.test( descriptor.valueType() ) ) - { - AssociationDescriptor associationDescriptor; - try - { - associationDescriptor = entityState.getNamedAssociationByName( propertyName ); - } - catch( IllegalArgumentException e1 ) - { - return null; - } - - AccessibleObject associationMethod = associationDescriptor.accessor(); - NamedAssociation<?> state = associationState.namedAssociationFor( associationMethod ); - Map<String, String> entities = new LinkedHashMap<>( state.count() ); - for( String name : state ) - { - entities.put( name, ( (Identity) state.get( name ) ).identity().get() ); - } - return entities; - } - return null; - } - } - }, - new Function<AssociationDescriptor, EntityReference>() - { - @Override - public EntityReference apply( AssociationDescriptor descriptor ) - { - AssociationDescriptor associationDescriptor; - try - { - associationDescriptor = entityDescriptor.state() - .getAssociationByName( descriptor.qualifiedName().name() ); - } - catch( IllegalArgumentException e ) - { - return null; - } - - AccessibleObject associationMethod = associationDescriptor.accessor(); - Association<Object> association = associationState.associationFor( associationMethod ); - return EntityReference.entityReferenceFor( association.get() ); - } - }, - new Function<AssociationDescriptor, Iterable<EntityReference>>() - { - @Override - public Iterable<EntityReference> apply( final AssociationDescriptor descriptor ) - { - AssociationDescriptor associationDescriptor; - try - { - String associationName = descriptor.qualifiedName().name(); - AssociationStateDescriptor entityState = entityDescriptor.state(); - associationDescriptor = entityState.getManyAssociationByName( associationName ); - } - catch( IllegalArgumentException e ) - { - return Iterables.empty(); - } - - ManyAssociation<?> state = associationState.manyAssociationFor( associationDescriptor.accessor() ); - List<EntityReference> refs = new ArrayList<>( state.count() ); - for( Object entity : state ) - { - refs.add( EntityReference.entityReferenceFor( entity ) ); - } - return refs; - } - }, - new Function<AssociationDescriptor, Map<String, EntityReference>>() - { - @Override - public Map<String, EntityReference> apply( AssociationDescriptor descriptor ) - { - AssociationDescriptor associationDescriptor; - try - { - String associationName = descriptor.qualifiedName().name(); - AssociationStateDescriptor entityState = entityDescriptor.state(); - associationDescriptor = entityState.getNamedAssociationByName( associationName ); - } - catch( IllegalArgumentException e ) - { - return Collections.emptyMap(); - } - AccessibleObject associationMethod = associationDescriptor.accessor(); - NamedAssociation<Object> assoc = associationState.namedAssociationFor( associationMethod ); - Map<String, EntityReference> refs = new LinkedHashMap<>( assoc.count() ); - for( String name : assoc ) - { - refs.put( name, EntityReference.entityReferenceFor( assoc.get( name ) ) ); - } - return refs; - } - } ); - } - return builder; - } - - @SuppressWarnings( "unchecked" ) - private <T> T createInstance( ValueBuilder<?> builder ) - { - return (T) builder.newInstance(); - } - } - -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/5229303a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/EntityToValueAssembler.java ---------------------------------------------------------------------- diff --git a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/EntityToValueAssembler.java b/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/EntityToValueAssembler.java deleted file mode 100644 index 3f071cb..0000000 --- a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/EntityToValueAssembler.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.library.conversion.values; - -import org.apache.zest.api.entity.Identity; -import org.apache.zest.bootstrap.Assemblers; -import org.apache.zest.bootstrap.AssemblyException; -import org.apache.zest.bootstrap.ModuleAssembly; -import org.apache.zest.bootstrap.ServiceDeclaration; - -/** - * EntityToValue Service Assembler. - * @deprecated Please use {@link org.apache.zest.api.unitofwork.UnitOfWork#toValue(Class, Identity)} instead. - */ -public class EntityToValueAssembler - extends Assemblers.VisibilityIdentity<EntityToValueAssembler> -{ - @Override - public void assemble( ModuleAssembly module ) - throws AssemblyException - { - ServiceDeclaration service = module.services( EntityToValueService.class ).visibleIn( visibility() ); - if( hasIdentity() ) - { - service.identifiedBy( identity() ); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/5229303a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/EntityToValueService.java ---------------------------------------------------------------------- diff --git a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/EntityToValueService.java b/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/EntityToValueService.java deleted file mode 100644 index 8bdaa01..0000000 --- a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/EntityToValueService.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.library.conversion.values; - -import org.apache.zest.api.entity.Identity; -import org.apache.zest.api.service.ServiceComposite; - -/** The EntityToValueService converts Entities to matching Values. - * <p> - * The main purpose of this service is to provide convenience to map entities to serializable values, for instance - * to be transported across a network. The ValueComposite to convert the Entity to must closely match the Entity - * for this mapping to work. The rules are fairly straight forward; - * </p> - * <ol> - * <li>Any Property in the EntityComposite to be mapped, must (by default) exist in the ValueComposite with the same - * fully qualified name, i.e. method declared in the same interface. If the ValueComposite is annotated with - * @Unqualified then the Property method look up will only locate the properties with the name only, - * i.e. the methods may defined in different interfaces.</li> - * <li>For any Association in the EntityComposite, a Property<String> with the same <strong>unqualified</strong> - * name will be looked up in the ValueComposite. If found, the EntityReference of the Association will be - * converted to an URI and written to the String property.</li> - * <li>For any ManyAssociation in the EntityComposite, a Property<List<String>> with the same <strong> - * unqualified</strong> name will be looked up in the ValueComposite. If found, the EntityReferences in the - * ManyAssociation will be converted to URIs and placed into a List and set to the Property in ValueComposite.</li> - * <li>For any NamedAssociation in the EntityComposite, a Property<Map<String,String>> with the same - * <strong>unqualified</strong> name will be looked up in the ValueComposite. If found, the EntityReferences in - * the NamedAssociation will be converted to URIs and placed into a Map and set to the Property in ValueComposite. - * </li> - * - * </ol> - * <p> - * If a Property from the Entity is not found in the Value, then it is ignored. - * </p> - * @deprecated Please use {@link org.apache.zest.api.unitofwork.UnitOfWork#toValue(Class, Identity)} instead. - */ -public interface EntityToValueService extends EntityToValue, ServiceComposite -{ -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/5229303a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/PropertyNotPresentException.java ---------------------------------------------------------------------- diff --git a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/PropertyNotPresentException.java b/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/PropertyNotPresentException.java deleted file mode 100644 index 1450852..0000000 --- a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/PropertyNotPresentException.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ - -package org.apache.zest.library.conversion.values; - -import org.apache.zest.api.entity.EntityComposite; - -public class PropertyNotPresentException extends RuntimeException -{ - private Class valueType; - private Class<? extends EntityComposite> entityType; - - public PropertyNotPresentException( String message, Class valueType, Class<? extends EntityComposite> entityType ) - { - super(message); - this.valueType = valueType; - this.entityType = entityType; - } - - public Class valueType() - { - return valueType; - } - - public Class entityType() - { - return entityType; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/5229303a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/Shared.java ---------------------------------------------------------------------- diff --git a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/Shared.java b/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/Shared.java deleted file mode 100644 index 936d086..0000000 --- a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/Shared.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.library.conversion.values; - -import java.util.function.Predicate; -import org.apache.zest.api.type.CollectionType; -import org.apache.zest.api.type.MapType; -import org.apache.zest.api.type.ValueType; - -/** - * Shared. - */ -final class Shared -{ - static final Predicate<ValueType> STRING_TYPE_SPEC; - static final Predicate<ValueType> STRING_COLLECTION_TYPE_SPEC; - static final Predicate<ValueType> STRING_MAP_TYPE_SPEC; - - static - { - // Type Specifications - STRING_TYPE_SPEC = new Predicate<ValueType>() - { - @Override - public boolean test( ValueType valueType ) - { - return valueType.mainType().equals( String.class ); - } - }; - STRING_COLLECTION_TYPE_SPEC = new Predicate<ValueType>() - { - @Override - public boolean test( ValueType valueType ) - { - return valueType instanceof CollectionType - && ( (CollectionType) valueType ).collectedType().mainType().equals( String.class ); - } - }; - STRING_MAP_TYPE_SPEC = new Predicate<ValueType>() - { - @Override - public boolean test( ValueType valueType ) - { - return valueType instanceof MapType - && ( (MapType) valueType ).keyType().mainType().equals( String.class ) - && ( (MapType) valueType ).valueType().mainType().equals( String.class ); - } - }; - } - - private Shared() - { - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/5229303a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/Unqualified.java ---------------------------------------------------------------------- diff --git a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/Unqualified.java b/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/Unqualified.java deleted file mode 100644 index 8e3497f..0000000 --- a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/Unqualified.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ - -package org.apache.zest.library.conversion.values; - -import java.lang.annotation.*; - -/** If the ValueComposite is annotated with this, it means that the lookup of the Value Property should be performed - * using the <strong>unqualified</strong> name only, and not via the default of the full qualified name. This - * means that the Property may be declared in the different interfaces and still be matched. - */ -@Retention( RetentionPolicy.RUNTIME ) -@Target( ElementType.TYPE ) -@Documented -public @interface Unqualified -{ - boolean value() default true; -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/5229303a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntity.java ---------------------------------------------------------------------- diff --git a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntity.java b/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntity.java deleted file mode 100644 index 70ec04f..0000000 --- a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntity.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.library.conversion.values; - -import java.util.function.Function; -import org.apache.zest.api.entity.EntityComposite; -import org.apache.zest.api.entity.Identity; -import org.apache.zest.api.unitofwork.NoSuchEntityException; -import org.apache.zest.api.value.ValueComposite; - -/** - * Create or update Entities from matching Values. - * @deprecated Please use {@link org.apache.zest.api.unitofwork.UnitOfWork#toEntity(Class, Identity)} instead. - */ -public interface ValueToEntity -{ - /** - * Create an Entity from a Value. - * <p> - * If the Value extends {@link Identity} the Entity identity is taken from the Value's state. - * Else, if the Value's state for {@code Identity} is absent, a new Identity is generated. - * - * @param <T> Value Type - * @param entityType Entity Type - * @param value Value - * - * @return the created Entity - */ - <T> T create( Class<T> entityType, Object value ); - - /** - * Create an Entity from a Value. - * <p> - * If {@code identity} is not null, it is used as Entity identity. - * Else, if the Value extends {@link Identity} the Entity identity is taken from the Value's state. - * Else, if the Value's state for {@code Identity} is absent, a new Identity is generated. - * - * @param <T> Value Type - * @param entityType Entity Type - * @param identity Entity Identity, may be null - * @param value Value - * - * @return the created Entity - */ - <T> T create( Class<T> entityType, String identity, Object value ); - - /** - * Create an Entity from a Value. - * <p> - * If the Value extends {@link Identity} the Entity identity is taken from the Value's state. - * Else, if the Value's state for {@code Identity} is absent, a new Identity is generated. - * - * @param <T> Value Type - * @param entityType Entity Type - * @param value Value - * @param prototypeOpportunity A Function that will be mapped on the Entity prototype before instanciation - * - * @return the created Entity - */ - <T> T create( Class<T> entityType, Object value, Function<T, T> prototypeOpportunity ); - - /** - * Create an Entity from a Value. - * <p> - * If {@code identity} is not null, it is used as Entity identity. - * Else, if the Value extends {@link Identity} the Entity identity is taken from the Value's state. - * Else, if the Value's state for {@code Identity} is absent, a new Identity is generated. - * - * @param <T> Value Type - * @param entityType Entity Type - * @param identity Entity Identity, may be null - * @param value Value - * @param prototypeOpportunity A Function that will be mapped on the Entity prototype before instanciation - * - * @return the created Entity - */ - <T> T create( Class<T> entityType, String identity, Object value, Function<T, T> prototypeOpportunity ); - - /** - * Create an Iterable of Entities from an Iterable of Values. - * <p> - * If a Value extends {@link Identity} the Entity identity is taken from the Value's state. - * Else, if a Value's state for {@code Identity} is absent, a new Identity is generated. - * - * @param <T> Value Type - * @param entityType Entity Type - * @param values An Iterable of Values - * - * @return the Iterable of created Entities - */ - <T> Iterable<T> create( Class<T> entityType, Iterable<Object> values ); - - /** - * Create an Iterable of Entities from an Iterable of Values. - * <p> - * If a Value extends {@link Identity} the Entity identity is taken from the Value's state. - * Else, if a Value's state for {@code Identity} is absent, a new Identity is generated. - * - * @param <T> Value Type - * @param entityType Entity Type - * @param values An Iterable of Values - * @param prototypeOpportunity A Function that will be mapped on each Entity prototype before instanciation - * - * @return the Iterable of created Entities - */ - <T> Iterable<T> create( Class<T> entityType, Iterable<Object> values, Function<T, T> prototypeOpportunity ); - - /** - * Update an Entity from a Value. - * - * @param entity Entity - * @param value Value - * - * @throws ClassCastException If {@code entity} is not an {@link EntityComposite} - * or if {@code value} is not a {@link ValueComposite} - * @throws NoSuchEntityException If some associated Entity is absent from the EntityStore/UoW - */ - void update( Object entity, Object value ) - throws ClassCastException, NoSuchEntityException; -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/5229303a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntityAssembler.java ---------------------------------------------------------------------- diff --git a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntityAssembler.java b/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntityAssembler.java deleted file mode 100644 index 6774712..0000000 --- a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntityAssembler.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.library.conversion.values; - -import org.apache.zest.api.entity.Identity; -import org.apache.zest.bootstrap.Assemblers; -import org.apache.zest.bootstrap.AssemblyException; -import org.apache.zest.bootstrap.ModuleAssembly; -import org.apache.zest.bootstrap.ServiceDeclaration; - -/** - * ValueToEntity Service Assembler. - * @deprecated Please use {@link org.apache.zest.api.unitofwork.UnitOfWork#toEntity(Class, Identity)} instead. - */ -public class ValueToEntityAssembler - extends Assemblers.VisibilityIdentity<ValueToEntityAssembler> -{ - @Override - public void assemble( ModuleAssembly module ) - throws AssemblyException - { - ServiceDeclaration service = module.services( ValueToEntityService.class ).visibleIn( visibility() ); - if( hasIdentity() ) - { - service.identifiedBy( identity() ); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/5229303a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntityMixin.java ---------------------------------------------------------------------- diff --git a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntityMixin.java b/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntityMixin.java deleted file mode 100644 index 52ec6da..0000000 --- a/libraries/conversion/src/main/java/org/apache/zest/library/conversion/values/ValueToEntityMixin.java +++ /dev/null @@ -1,730 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ -package org.apache.zest.library.conversion.values; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; -import org.apache.zest.api.association.Association; -import org.apache.zest.api.association.AssociationDescriptor; -import org.apache.zest.api.association.AssociationStateDescriptor; -import org.apache.zest.api.association.AssociationStateHolder; -import org.apache.zest.api.association.ManyAssociation; -import org.apache.zest.api.association.NamedAssociation; -import org.apache.zest.api.common.QualifiedName; -import org.apache.zest.api.entity.EntityBuilder; -import org.apache.zest.api.entity.EntityComposite; -import org.apache.zest.api.entity.EntityDescriptor; -import org.apache.zest.api.entity.EntityReference; -import org.apache.zest.api.entity.Identity; -import org.apache.zest.api.injection.scope.Structure; -import org.apache.zest.api.property.PropertyDescriptor; -import org.apache.zest.api.structure.ModuleDescriptor; -import org.apache.zest.api.unitofwork.NoSuchEntityTypeException; -import org.apache.zest.api.unitofwork.NoSuchEntityException; -import org.apache.zest.api.unitofwork.UnitOfWorkFactory; -import org.apache.zest.api.value.ValueComposite; -import org.apache.zest.api.value.ValueDescriptor; -import org.apache.zest.functional.Iterables; -import org.apache.zest.spi.ZestSPI; - -import static org.apache.zest.library.conversion.values.Shared.STRING_COLLECTION_TYPE_SPEC; -import static org.apache.zest.library.conversion.values.Shared.STRING_MAP_TYPE_SPEC; -import static org.apache.zest.library.conversion.values.Shared.STRING_TYPE_SPEC; - -/** - * ValueToEntity Mixin. - * - * @deprecated Please use {@link org.apache.zest.api.unitofwork.UnitOfWork#toEntity(Class, Identity)} instead. - */ -public class ValueToEntityMixin - implements ValueToEntity -{ - private static final QualifiedName IDENTITY_STATE_NAME; - private static final Function<ManyAssociation<?>, Iterable<EntityReference>> MANY_ASSOC_TO_ENTITY_REF_ITERABLE; - private static final Function<NamedAssociation<?>, Map<String, EntityReference>> NAMED_ASSOC_TO_ENTITY_REF_MAP; - private static final Function<Collection<String>, Iterable<EntityReference>> STRING_COLLEC_TO_ENTITY_REF_ITERABLE; - private static final Function<Map<String, String>, Map<String, EntityReference>> STRING_MAP_TO_ENTITY_REF_MAP; - - static - { - try - { - IDENTITY_STATE_NAME = QualifiedName.fromAccessor( Identity.class.getMethod( "identity" ) ); - } - catch( NoSuchMethodException e ) - { - throw new InternalError( "Zest Core Runtime codebase is corrupted. Contact Zest team: ValueToEntityMixin" ); - } - MANY_ASSOC_TO_ENTITY_REF_ITERABLE = manyAssoc -> { - if( manyAssoc == null ) - { - return Iterables.empty(); - } - List<EntityReference> refs = new ArrayList<>( manyAssoc.count() ); - for( Object entity : manyAssoc ) - { - refs.add( EntityReference.entityReferenceFor( entity ) ); - } - return refs; - }; - NAMED_ASSOC_TO_ENTITY_REF_MAP = namedAssoc -> { - if( namedAssoc == null ) - { - return Collections.emptyMap(); - } - Map<String, EntityReference> refs = new LinkedHashMap<>( namedAssoc.count() ); - for( String name : namedAssoc ) - { - refs.put( name, EntityReference.entityReferenceFor( namedAssoc.get( name ) ) ); - } - return refs; - }; - STRING_COLLEC_TO_ENTITY_REF_ITERABLE = stringCollec -> { - if( stringCollec == null ) - { - return Iterables.empty(); - } - return stringCollec.stream() - .map( EntityReference::parseEntityReference ) - .collect( Collectors.toList() ); - }; - STRING_MAP_TO_ENTITY_REF_MAP = stringMap -> { - if( stringMap == null ) - { - return Collections.emptyMap(); - } - Map<String, EntityReference> refMap = new LinkedHashMap<>( stringMap.size() ); - for( Map.Entry<String, String> entry : stringMap.entrySet() ) - { - refMap.put( entry.getKey(), EntityReference.parseEntityReference( entry.getValue() ) ); - } - return refMap; - }; - } - - @Structure - private ZestSPI spi; - - @Structure - private UnitOfWorkFactory uowf; - - @Structure - private ModuleDescriptor module; - - @Override - public <T> T create( Class<T> entityType, Object value ) - { - return createInstance( doConversion( entityType, null, value ) ); - } - - @Override - public <T> T create( Class<T> entityType, String identity, Object value ) - { - return createInstance( doConversion( entityType, identity, value ) ); - } - - @Override - public <T> T create( Class<T> entityType, Object value, Function<T, T> prototypeOpportunity ) - { - EntityBuilder<?> builder = doConversion( entityType, null, value ); - //noinspection unchecked - prototypeOpportunity.apply( (T) builder.instance() ); - return createInstance( builder ); - } - - @Override - public <T> T create( Class<T> entityType, String identity, Object value, Function<T, T> prototypeOpportunity ) - { - EntityBuilder<?> builder = doConversion( entityType, identity, value ); - //noinspection unchecked - prototypeOpportunity.apply( (T) builder.instance() ); - return createInstance( builder ); - } - - @Override - public <T> Iterable<T> create( final Class<T> entityType, final Iterable<Object> values ) - { - return Iterables.map( - value -> create( entityType, value ), - values - ); - } - - @Override - public <T> Iterable<T> create( final Class<T> entityType, - final Iterable<Object> values, - final Function<T, T> prototypeOpportunity - ) - { - return Iterables.map( - value -> create( entityType, value, prototypeOpportunity ), - values - ); - } - - private <T> EntityBuilder<?> doConversion( Class<T> entityType, String identity, Object value ) - { - EntityDescriptor eDesc = module.entityDescriptor( entityType.getName() ); - if( eDesc == null ) - { - throw new NoSuchEntityTypeException( entityType.getName(), module.name(), module.typeLookup() ); - } - - ValueComposite vComposite = (ValueComposite) value; - - ValueDescriptor vDesc = spi.valueDescriptorFor( vComposite ); - AssociationStateHolder vState = spi.stateOf( vComposite ); - AssociationStateDescriptor vStateDesc = vDesc.state(); - - Unqualified unqualified = vDesc.metaInfo( Unqualified.class ); - if( unqualified == null || !unqualified.value() ) - { - return doQualifiedConversion( entityType, identity, vState, vStateDesc ); - } - return doUnqualifiedConversion( entityType, identity, vState, vStateDesc ); - } - - private <T> EntityBuilder<?> doQualifiedConversion( - Class<T> entityType, String identity, - final AssociationStateHolder vState, final AssociationStateDescriptor vStateDesc - ) - { - Function<PropertyDescriptor, Object> props = ePropDesc -> { - try - { - return vState.propertyFor( ePropDesc.accessor() ).get(); - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - // Property not found - return null; - } - }; - Function<AssociationDescriptor, EntityReference> assocs = eAssocDesc -> { - try - { - return EntityReference.entityReferenceFor( vState.associationFor( eAssocDesc.accessor() ) ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // Find String Property and convert to Association - String propName = eAssocDesc.qualifiedName().name(); - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( propName ); - if( STRING_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - String assocState = (String) vState.propertyFor( vPropDesc.accessor() ).get(); - return EntityReference.parseEntityReference( assocState ); - } - return null; - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - return null; - } - } - }; - Function<AssociationDescriptor, Iterable<EntityReference>> manyAssocs = eAssocDesc -> { - try - { - ManyAssociation<Object> vAssocState = vState.manyAssociationFor( eAssocDesc.accessor() ); - return MANY_ASSOC_TO_ENTITY_REF_ITERABLE.apply( vAssocState ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // Find Collection<String> Property and convert to ManyAssociation - String propName = eAssocDesc.qualifiedName().name(); - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( propName ); - if( STRING_COLLECTION_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - @SuppressWarnings( "unchecked" ) - Collection<String> vAssocState = (Collection) vState.propertyFor( vPropDesc.accessor() ).get(); - return STRING_COLLEC_TO_ENTITY_REF_ITERABLE.apply( vAssocState ); - } - return Iterables.empty(); - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - return Iterables.empty(); - } - } - }; - Function<AssociationDescriptor, Map<String, EntityReference>> namedAssocs = eAssocDesc -> { - try - { - NamedAssociation<?> vAssocState = vState.namedAssociationFor( eAssocDesc.accessor() ); - return NAMED_ASSOC_TO_ENTITY_REF_MAP.apply( vAssocState ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // Find Map<String,String> Property and convert to NamedAssociation - String propName = eAssocDesc.qualifiedName().name(); - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( propName ); - if( STRING_MAP_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - //noinspection unchecked - Map<String, String> vAssocState = (Map) vState.propertyFor( vPropDesc.accessor() ).get(); - return STRING_MAP_TO_ENTITY_REF_MAP.apply( vAssocState ); - } - return Collections.EMPTY_MAP; - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - return Collections.EMPTY_MAP; - } - } - }; - return uowf.currentUnitOfWork().newEntityBuilderWithState( - entityType, identity, props, assocs, manyAssocs, namedAssocs - ); - } - - private <T> EntityBuilder<?> doUnqualifiedConversion( - Class<T> entityType, String identity, - final AssociationStateHolder vState, final AssociationStateDescriptor vStateDesc - ) - { - Function<PropertyDescriptor, Object> props = ePropDesc -> { - String propName = ePropDesc.qualifiedName().name(); - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( propName ); - return vState.propertyFor( vPropDesc.accessor() ).get(); - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - // Property not found on Value - return null; - } - }; - Function<AssociationDescriptor, EntityReference> assocs = eAssocDesc -> { - String assocName = eAssocDesc.qualifiedName().name(); - try - { - AssociationDescriptor vAssocDesc = vStateDesc.getAssociationByName( assocName ); - Object assocEntity = vState.associationFor( vAssocDesc.accessor() ).get(); - return assocEntity == null ? null : EntityReference.entityReferenceFor( assocEntity ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // Association not found on Value, find Property<String> and convert to Association - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( assocName ); - if( STRING_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - String assocId = (String) vState.propertyFor( vPropDesc.accessor() ).get(); - return assocId == null ? null : EntityReference.parseEntityReference( assocId ); - } - return null; - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - return null; - } - } - }; - Function<AssociationDescriptor, Iterable<EntityReference>> manyAssocs = eAssocDesc -> { - String assocName = eAssocDesc.qualifiedName().name(); - try - { - AssociationDescriptor vAssocDesc = vStateDesc.getManyAssociationByName( assocName ); - ManyAssociation<Object> vManyAssoc = vState.manyAssociationFor( vAssocDesc.accessor() ); - return MANY_ASSOC_TO_ENTITY_REF_ITERABLE.apply( vManyAssoc ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // ManyAssociation not found on Value, find List<String> and convert to ManyAssociation - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( assocName ); - if( STRING_COLLECTION_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - @SuppressWarnings( "unchecked" ) - Collection<String> vAssocState = (Collection) vState.propertyFor( vPropDesc.accessor() ).get(); - return STRING_COLLEC_TO_ENTITY_REF_ITERABLE.apply( vAssocState ); - } - return Iterables.empty(); - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - return Iterables.empty(); - } - } - }; - Function<AssociationDescriptor, Map<String, EntityReference>> namedAssocs = eAssocDesc -> { - String assocName = eAssocDesc.qualifiedName().name(); - try - { - AssociationDescriptor vAssocDesc = vStateDesc.getNamedAssociationByName( assocName ); - NamedAssociation<Object> vAssocState = vState.namedAssociationFor( vAssocDesc.accessor() ); - return NAMED_ASSOC_TO_ENTITY_REF_MAP.apply( vAssocState ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // Find Map<String,String> Property and convert to NamedAssociation - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( assocName ); - if( STRING_MAP_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - @SuppressWarnings( "unchecked" ) - Map<String, String> vAssocState = (Map) vState.propertyFor( vPropDesc.accessor() ).get(); - return STRING_MAP_TO_ENTITY_REF_MAP.apply( vAssocState ); - } - return Collections.EMPTY_MAP; - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - return Collections.EMPTY_MAP; - } - } - }; - return uowf.currentUnitOfWork().newEntityBuilderWithState( - entityType, identity, props, assocs, manyAssocs, namedAssocs - ); - } - - protected <T> T createInstance( EntityBuilder<?> builder ) - { - //noinspection unchecked - return (T) builder.newInstance(); - } - - @Override - public void update( Object entity, Object value ) - throws NoSuchEntityException - { - EntityComposite eComposite = (EntityComposite) entity; - ValueComposite vComposite = (ValueComposite) value; - - EntityDescriptor eDesc = spi.entityDescriptorFor( eComposite ); - AssociationStateHolder eState = spi.stateOf( eComposite ); - AssociationStateDescriptor eStateDesc = eDesc.state(); - - ValueDescriptor vDesc = spi.valueDescriptorFor( vComposite ); - AssociationStateHolder vState = spi.stateOf( vComposite ); - AssociationStateDescriptor vStateDesc = vDesc.state(); - - Unqualified unqualified = vDesc.metaInfo( Unqualified.class ); - if( unqualified == null || !unqualified.value() ) - { - doQualifiedUpdate( eState, eStateDesc, vState, vStateDesc ); - } - else - { - doUnQualifiedUpdate( eState, eStateDesc, vState, vStateDesc ); - } - } - - private void doQualifiedUpdate( - AssociationStateHolder eState, AssociationStateDescriptor eStateDesc, - AssociationStateHolder vState, AssociationStateDescriptor vStateDesc - ) - throws NoSuchEntityException - { - eStateDesc.properties().forEach( ePropDesc -> - { - if( ! IDENTITY_STATE_NAME.equals( ePropDesc.qualifiedName() ) ) - { - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByQualifiedName( ePropDesc.qualifiedName() ); - eState.propertyFor( ePropDesc.accessor() ).set( vState.propertyFor( vPropDesc.accessor() ).get() ); - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - // Property not found on Value, do nothing - } - } - } ); - eStateDesc.associations().forEach( eAssocDesc -> - { - Association<Object> eAssoc = eState.associationFor( eAssocDesc.accessor() ); - try - { - AssociationDescriptor vAssocDesc - = vStateDesc.getAssociationByQualifiedName( eAssocDesc.qualifiedName() ); - eAssoc.set( vState.associationFor( vAssocDesc.accessor() ).get() ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // Association not found on Value, find Property<String> and load associated Entity - try - { - PropertyDescriptor vPropDesc - = vStateDesc.findPropertyModelByName( eAssocDesc.qualifiedName().name() ); - if( STRING_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - String assocId = (String) vState.propertyFor( vPropDesc.accessor() ).get(); - if( assocId != null ) - { - eAssoc.set( uowf.currentUnitOfWork().get( (Class) eAssocDesc.type(), assocId ) ); - } - else - { - eAssoc.set( null ); - } - } - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - // Do nothing - } - } - }); - eStateDesc.manyAssociations().forEach( eAssocDesc -> - { - ManyAssociation<Object> eManyAssoc = eState.manyAssociationFor( eAssocDesc.accessor() ); - try - { - AssociationDescriptor vAssocDesc - = vStateDesc.getManyAssociationByQualifiedName( eAssocDesc.qualifiedName() ); - ManyAssociation<Object> vManyAssoc = vState.manyAssociationFor( vAssocDesc.accessor() ); - eManyAssoc.toList().forEach( eManyAssoc::remove ); - vManyAssoc.toList().forEach( eManyAssoc::add ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // ManyAssociation not found on Value, find Property<List<String>> and load associated Entities - try - { - PropertyDescriptor vPropDesc - = vStateDesc.findPropertyModelByName( eAssocDesc.qualifiedName().name() ); - if( STRING_COLLECTION_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - @SuppressWarnings( "unchecked" ) - Collection<String> vAssocState = (Collection) vState.propertyFor( vPropDesc.accessor() ).get(); - eManyAssoc.toList().forEach( eManyAssoc::remove ); - if( vAssocState != null ) - { - for( String eachAssoc : vAssocState ) - { - eManyAssoc.add( - uowf.currentUnitOfWork().get( (Class) eAssocDesc.type(), eachAssoc ) - ); - } - } - } - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - // Do nothing - } - } - }); - eStateDesc.namedAssociations().forEach( eAssocDesc -> - { - NamedAssociation<Object> eNamedAssoc = eState.namedAssociationFor( eAssocDesc.accessor() ); - try - { - AssociationDescriptor vAssocDesc - = vStateDesc.getNamedAssociationByQualifiedName( eAssocDesc.qualifiedName() ); - NamedAssociation<Object> vNamedAssoc = vState.namedAssociationFor( vAssocDesc.accessor() ); - Iterables.toList( eNamedAssoc ).forEach( eNamedAssoc::remove ); - for( Map.Entry<String, Object> assocEntry : vNamedAssoc.toMap().entrySet() ) - { - eNamedAssoc.put( assocEntry.getKey(), assocEntry.getValue() ); - } - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // NamedAssociation not found on Value, find Property<Map<String,String>> and load associated Entities - try - { - PropertyDescriptor vPropDesc - = vStateDesc.findPropertyModelByName( eAssocDesc.qualifiedName().name() ); - if( STRING_MAP_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - @SuppressWarnings( "unchecked" ) - Map<String, String> vAssocState = (Map) vState.propertyFor( vPropDesc.accessor() ).get(); - Iterables.toList( eNamedAssoc ).forEach( eNamedAssoc::remove ); - if( vAssocState != null ) - { - for( Map.Entry<String, String> assocEntry : vAssocState.entrySet() ) - { - eNamedAssoc.put( - assocEntry.getKey(), - uowf.currentUnitOfWork().get( (Class) eAssocDesc.type(), assocEntry.getValue() ) - ); - } - } - } - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - // Do nothing - } - } - } ); - } - - private void doUnQualifiedUpdate( - AssociationStateHolder eState, AssociationStateDescriptor eStateDesc, - AssociationStateHolder vState, AssociationStateDescriptor vStateDesc - ) - { - eStateDesc.properties().forEach( ePropDesc -> - { - if( ! IDENTITY_STATE_NAME.equals( ePropDesc.qualifiedName() ) ) - { - try - { - PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( ePropDesc.qualifiedName().name() ); - eState.propertyFor( ePropDesc.accessor() ).set( vState.propertyFor( vPropDesc.accessor() ).get() ); - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - // Property not found on Value, do nothing - } - } - } ); - eStateDesc.associations().forEach( eAssocDesc -> - { - Association<Object> eAssoc = eState.associationFor( eAssocDesc.accessor() ); - try - { - AssociationDescriptor vAssocDesc = vStateDesc.getAssociationByName( eAssocDesc.qualifiedName().name() ); - eAssoc.set( vState.associationFor( vAssocDesc.accessor() ).get() ); - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // Association not found on Value, find Property<String> and load associated Entity - try - { - PropertyDescriptor vPropDesc - = vStateDesc.findPropertyModelByName( eAssocDesc.qualifiedName().name() ); - if( STRING_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - String assocId = (String) vState.propertyFor( vPropDesc.accessor() ).get(); - if( assocId != null ) - { - eAssoc.set( uowf.currentUnitOfWork().get( (Class) eAssocDesc.type(), assocId ) ); - } - else - { - eAssoc.set( null ); - } - } - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - // Do nothing - } - } - } ); - eStateDesc.manyAssociations().forEach( eAssocDesc -> - { - ManyAssociation<Object> eManyAssoc = eState.manyAssociationFor( eAssocDesc.accessor() ); - try - { - AssociationDescriptor vAssDesc - = vStateDesc.getManyAssociationByName( eAssocDesc.qualifiedName().name() ); - ManyAssociation<Object> vManyAss = vState.manyAssociationFor( vAssDesc.accessor() ); - eManyAssoc.toList().forEach( eManyAssoc::remove ); - vManyAss.toList().forEach( eManyAssoc::add ); - } - catch( IllegalArgumentException assNotFoundOnValue ) - { - // ManyAssociation not found on Value, find Property<List<String>> and load associated Entities - try - { - PropertyDescriptor vPropDesc - = vStateDesc.findPropertyModelByName( eAssocDesc.qualifiedName().name() ); - if( STRING_COLLECTION_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - @SuppressWarnings( "unchecked" ) - Collection<String> vAssocState = (Collection) vState.propertyFor( vPropDesc.accessor() ).get(); - eManyAssoc.toList().forEach( eManyAssoc::remove ); - if( vAssocState != null ) - { - vAssocState.forEach( eachAssoc -> - { - eManyAssoc.add( - uowf.currentUnitOfWork().get( (Class<?>) eAssocDesc.type(), eachAssoc ) - ); - } ); - } - } - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - // Do nothing - } - } - } ); - eStateDesc.namedAssociations().forEach( eAssocDesc -> - { - NamedAssociation<Object> eNamedAssoc = eState.namedAssociationFor( eAssocDesc.accessor() ); - try - { - AssociationDescriptor vAssocDesc - = vStateDesc.getNamedAssociationByName( eAssocDesc.qualifiedName().name() ); - NamedAssociation<Object> vNamedAssoc = vState.namedAssociationFor( vAssocDesc.accessor() ); - Iterables.toList( eNamedAssoc ).forEach( eNamedAssoc::remove ); - for( Map.Entry<String, Object> assocEntry : vNamedAssoc.toMap().entrySet() ) - { - eNamedAssoc.put( assocEntry.getKey(), assocEntry.getValue() ); - } - } - catch( IllegalArgumentException assocNotFoundOnValue ) - { - // NamedAssociation not found on Value, find Property<Map<String,String>> and load associated Entities - try - { - PropertyDescriptor vPropDesc - = vStateDesc.findPropertyModelByName( eAssocDesc.qualifiedName().name() ); - if( STRING_MAP_TYPE_SPEC.test( vPropDesc.valueType() ) ) - { - @SuppressWarnings( "unchecked" ) - Map<String, String> vAssocState = - (Map<String,String>) vState.propertyFor( vPropDesc.accessor() ).get(); - Iterables.toList( eNamedAssoc ).forEach( eNamedAssoc::remove ); - if( vAssocState != null ) - { - for( Map.Entry<String, String> assocEntry : vAssocState.entrySet() ) - { - eNamedAssoc.put( - assocEntry.getKey(), - uowf.currentUnitOfWork().get( (Class) eAssocDesc.type(), assocEntry.getValue() ) - ); - } - } - } - } - catch( IllegalArgumentException propNotFoundOnValue ) - { - // Do nothing - } - } - } ); - } -}
