001    package org.tynamo.descriptor.extension;
002    
003    import org.tynamo.internal.InternalConstants;
004    import org.tynamo.descriptor.Descriptor;
005    import org.tynamo.util.BeanModelUtils;
006    
007    import java.util.HashMap;
008    import java.util.Map;
009    
010    public class BeanModelExtension implements DescriptorExtension
011    {
012    
013            private Map<String, String> reorderMap = new HashMap<String, String>();
014            private Map<String, String> includeMap = new HashMap<String, String>();
015            private Map<String, String> excludeMap = new HashMap<String, String>();
016    
017            private BeanModelExtension()
018            {
019            }
020    
021            public String getReorderPropertyNames(String contextKey)
022            {
023                    return reorderMap.containsKey(contextKey) ? reorderMap.get(contextKey) : getReorder();
024            }
025    
026            public String getIncludePropertyNames(String contextKey)
027            {
028                    return includeMap.containsKey(contextKey) ? includeMap.get(contextKey) : getInclude();
029            }
030    
031            public String getExcludePropertyNames(String contextKey)
032            {
033                    return excludeMap.containsKey(contextKey) ? excludeMap.get(contextKey) : getExclude();
034            }
035    
036            public void setReorderPropertyNames(String contextKey, String properties)
037            {
038                    reorderMap.put(contextKey, properties);
039            }
040    
041            public void setIncludePropertyNames(String contextKey, String properties)
042            {
043                    includeMap.put(contextKey, properties);
044            }
045    
046            public void setExcludePropertyNames(String contextKey, String properties)
047            {
048                    excludeMap.put(contextKey, properties);
049            }
050    
051    
052            public String getReorder()
053            {
054                    return reorderMap.get(InternalConstants.DEFAULT_BEANMODEL_CONTEXT_KEY);
055            }
056    
057            public String getInclude()
058            {
059                    return includeMap.get(InternalConstants.DEFAULT_BEANMODEL_CONTEXT_KEY);
060            }
061    
062            public String getExclude()
063            {
064                    return excludeMap.get(InternalConstants.DEFAULT_BEANMODEL_CONTEXT_KEY);
065            }
066    
067            public void addToIncludeMap(String contextKey, String newProperty)
068            {
069                    String includePropertyNames = includeMap.get(contextKey);
070                    includePropertyNames = includePropertyNames == null ? newProperty : BeanModelUtils.join(includePropertyNames, newProperty);
071                    setIncludePropertyNames(contextKey, includePropertyNames);
072            }
073    
074            public void addToExcludeMap(String contextKey, String newProperty)
075            {
076                    String excludePropertyNames = excludeMap.get(contextKey);
077                    excludePropertyNames = excludePropertyNames == null ? newProperty : BeanModelUtils.join(excludePropertyNames, newProperty);
078                    setExcludePropertyNames(contextKey, excludePropertyNames);
079            }
080    
081            public static BeanModelExtension obtainBeanModelExtension(Descriptor descriptor)
082            {
083                    BeanModelExtension beanModelExtension = descriptor.getExtension(BeanModelExtension.class);
084                    if (beanModelExtension == null)
085                    {
086                            beanModelExtension = new BeanModelExtension();
087                            descriptor.addExtension(BeanModelExtension.class, beanModelExtension);
088                    }
089    
090                    return beanModelExtension;
091            }
092    }