001    package org.tynamo.services;
002    
003    import org.apache.tapestry5.ioc.Configuration;
004    import org.apache.tapestry5.ioc.MappedConfiguration;
005    import org.apache.tapestry5.ioc.OrderedConfiguration;
006    import org.apache.tapestry5.ioc.ServiceBinder;
007    import org.apache.tapestry5.ioc.annotations.InjectService;
008    import org.apache.tapestry5.ioc.services.CoercionTuple;
009    import org.apache.tapestry5.services.BeanBlockContribution;
010    import org.apache.tapestry5.services.DataTypeAnalyzer;
011    import org.apache.tapestry5.services.LibraryMapping;
012    import org.tynamo.VersionedModule;
013    import org.tynamo.blob.DefaultBlobManager;
014    import org.tynamo.blob.BlobManager;
015    import org.tynamo.builder.BuilderDirector;
016    import org.tynamo.descriptor.DescriptorFactory;
017    import org.tynamo.descriptor.MethodDescriptorFactory;
018    import org.tynamo.descriptor.MethodDescriptorFactoryImpl;
019    import org.tynamo.descriptor.PropertyDescriptorFactory;
020    import org.tynamo.descriptor.PropertyDescriptorFactoryImpl;
021    import org.tynamo.descriptor.ReflectionDescriptorFactory;
022    
023    public class TynamoCoreModule extends VersionedModule {
024    
025            public final static String PROPERTY_DISPLAY_BLOCKS = "tynamo/PropertyDisplayBlocks";
026            public final static String PROPERTY_EDIT_BLOCKS = "tynamo/Editors";
027    
028            public static void bind(ServiceBinder binder) {
029                    // Make bind() calls on the binder object to define most IoC services.
030                    // Use service builder methods (example below) when the implementation
031                    // is provided inline, or requires more initialization than simply
032                    // invoking the constructor.
033    
034                    binder.bind(BuilderDirector.class, BuilderDirector.class);
035                    binder.bind(DescriptorFactory.class, ReflectionDescriptorFactory.class);
036                    binder.bind(PropertyDescriptorFactory.class, PropertyDescriptorFactoryImpl.class);
037                    binder.bind(MethodDescriptorFactory.class, MethodDescriptorFactoryImpl.class);
038                    binder.bind(EntityCoercerService.class, EntityCoercerServiceImpl.class);
039                    binder.bind(DescriptorService.class, DescriptorServiceImpl.class);
040                    binder.bind(TynamoDataTypeAnalyzer.class, TynamoDataTypeAnalyzer.class);
041    
042                    binder.bind(BlobManager.class, DefaultBlobManager.class).withId("DefaultBlobManager");
043    
044            }
045    
046            /**
047             * Add our components and pages to the "Tynamo" library.
048             */
049            public static void contributeComponentClassResolver(Configuration<LibraryMapping> configuration) {
050                    configuration.add(new LibraryMapping("tynamo", "org.tynamo"));
051            }
052    
053            public static void contributeClasspathAssetAliasManager(MappedConfiguration<String, String> configuration) {
054                    configuration.add("tynamo/" + version, "org/tynamo");
055            }
056    
057    
058            /**
059             * Contribution to the BeanBlockSource service to tell the BeanEditForm component about the editors.
060             */
061            public static void contributeBeanBlockSource(Configuration<BeanBlockContribution> configuration) {
062                    configuration.add(new BeanBlockContribution("hidden", PROPERTY_EDIT_BLOCKS, "hidden", true));
063                    configuration.add(new BeanBlockContribution("dateEditor", PROPERTY_EDIT_BLOCKS, "date", true));
064                    configuration.add(new BeanBlockContribution("fckEditor", PROPERTY_EDIT_BLOCKS, "fckEditor", true));
065                    configuration.add(new BeanBlockContribution("readOnly", PROPERTY_EDIT_BLOCKS, "readOnly", true));
066                    configuration.add(new BeanBlockContribution("single-valued-association", PROPERTY_EDIT_BLOCKS, "select", true));
067                    configuration.add(new BeanBlockContribution("identifierEditor", PROPERTY_EDIT_BLOCKS, "identifierEditor", true));
068                    configuration.add(new BeanBlockContribution("many-valued-association", PROPERTY_EDIT_BLOCKS, "palette", true));
069                    configuration.add(new BeanBlockContribution("composition", PROPERTY_EDIT_BLOCKS, "editComposition", true));
070                    configuration.add(new BeanBlockContribution("embedded", PROPERTY_EDIT_BLOCKS, "embedded", true));
071                    configuration.add(new BeanBlockContribution("blob", PROPERTY_EDIT_BLOCKS, "blob", true));
072    
073                    configuration.add(new BeanBlockContribution("hidden", PROPERTY_DISPLAY_BLOCKS, "hidden", false));
074                    configuration.add(new BeanBlockContribution("single-valued-association", PROPERTY_DISPLAY_BLOCKS, "showPageLink", false));
075                    configuration.add(new BeanBlockContribution("many-valued-association", PROPERTY_DISPLAY_BLOCKS, "showPageLinks", false));
076                    configuration.add(new BeanBlockContribution("composition", PROPERTY_DISPLAY_BLOCKS, "showPageLinks", false));
077                    configuration.add(new BeanBlockContribution("blob", PROPERTY_DISPLAY_BLOCKS, "download", false));
078            }
079    
080            /**
081             * <dl>
082             * <dt>Annotation</dt>
083             * <dd>Checks for {@link org.apache.tapestry5.beaneditor.DataType} annotation</dd>
084             * <dt>Default (ordered last)</dt>
085             * <dd>{@link org.apache.tapestry5.internal.services.DefaultDataTypeAnalyzer} service (
086             * {@link #contributeDefaultDataTypeAnalyzer(org.apache.tapestry5.ioc.MappedConfiguration)} )</dd>
087             * </dl>
088             */
089            public static void contributeDataTypeAnalyzer(OrderedConfiguration<DataTypeAnalyzer> configuration,
090                            @InjectService("TynamoDataTypeAnalyzer") DataTypeAnalyzer tynamoDataTypeAnalyzer) {
091                    configuration.add("Tynamo", tynamoDataTypeAnalyzer, "before:Default");
092            }
093    
094            /**
095             * Contributes a set of standard type coercions to the {@link org.apache.tapestry5.ioc.services.TypeCoercer} service:
096             * <ul>
097             * <li>Class to String</li>
098             * <li>String to Double</li>
099             * </ul>
100             */
101            @SuppressWarnings("unchecked")
102            public static void contributeTypeCoercer(final Configuration<CoercionTuple> configuration,
103                            @InjectService("EntityCoercerService") EntityCoercerService entityCoercerService) {
104                    configuration.add(new CoercionTuple<Class, String>(Class.class, String.class, new ClassToStringCoercion(entityCoercerService)));
105                    configuration.add(new CoercionTuple<String, Class>(String.class, Class.class, new StringToClassCoercion(entityCoercerService)));
106            }
107    
108    }