litesoft
@ 811
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.bo.views; import java.io.*; import java.util.*; import org.litesoft.bo.*; import org.litesoft.bo.attributes.*; import org.litesoft.core.typeutils.*; import org.litesoft.core.util.*; public abstract class AbstractVoMetaData<V extends ViewObject> implements VoMetaData<V>, VoNames, BuildAMDs { private final Class<V> mViewObjectClass; private final String mObjectName; private final VAMap<V> mAttributesByName = new VAMap<V>(); private final BoAttribute[] mAttributes; private final String[] mAttributeNames; protected AbstractVoMetaData( Class<V> pViewObjectClass, VoAttributeList<V> pAttributes ) { mObjectName = Objects.justClassName( mViewObjectClass = pViewObjectClass ); List<BoAttribute> zAttributes = new ArrayList<BoAttribute>(); List<String> zNames = new ArrayList<String>(); add( zAttributes, zNames, getAttrNewID() ); if ( pAttributes != null ) { for ( VoAttribute<V> zAttribute : pAttributes ) { if ( zAttribute != null ) { add( zAttributes, zNames, zAttribute ); } } } if ( zAttributes.size() == 1 ) { throw new IllegalStateException( "ViewObject Meta Data, with NO Attributes" ); } mAttributes = zAttributes.toArray( new BoAttribute[zAttributes.size()] ); mAttributeNames = zNames.toArray( new String[zNames.size()] ); } private void add( List<BoAttribute> pAttributes, List<String> pNames, VoAttribute<V> zAttribute ) { String zName = zAttribute.getName(); pNames.add( zName ); pAttributes.add( zAttribute ); mAttributesByName.put( zName, zAttribute ); } public Class<V> getViewObjectClass() { return mViewObjectClass; } // BoMetaData... public final String[] getAttributeNames() { return mAttributeNames; } public final BoAttribute getBoAttribute( String pAttributeName ) throws NoSuchElementException { return getVoAttribute( pAttributeName ); } public final BoAttribute[] getBoAttributes() { return mAttributes; } // ExtendedBoMetaData... public final VoAttribute<V> getVoAttribute( String pAttributeName ) throws NoSuchElementException { VoAttribute<V> zAttribute = mAttributesByName.get( pAttributeName ); if ( zAttribute != null ) { return zAttribute; } throw new NoSuchElementException( "No Attribute for name '" + pAttributeName + "'" ); } public String getDerivedFromAttributePath( String pAttributeName ) { VoAttribute<V> zAttribute = mAttributesByName.get( pAttributeName ); return (zAttribute != null) ? zAttribute.getDerivedFromAttributePath() : null; } public final String getObjectName() { return mObjectName; } public ObjectUniqueKey createObjectUniqueKey( BoAccessor pBoAccessor ) { return createNewObjectUniqueKey( pBoAccessor ); } protected ObjectUniqueKey createNewObjectUniqueKey( BoAccessor pBoAccessor ) { return ObjectUniqueKey.createNew( ((IViewObject) pBoAccessor).getNewID() ); } private static class VAMap<X extends ViewObject> extends HashMap<String, VoAttribute<X>> implements Serializable { public VAMap() { } } @SuppressWarnings({"unchecked"}) private VoAttribute<V> getAttrNewID() { return (VoAttribute<V>) ATTR_NewID; } private static final VoAttribute<ViewObject> ATTR_NewID = new SimpleVoAttribute<ViewObject>( aNewID, false, _Long.RO() ) { public String getDerivedFromAttributePath() { return null; } public boolean isQueryViewAttribute() { return false; } protected Object LLgetAttributeValue( ViewObject pVO ) { return pVO.getNewID(); } public void setAttributeValue( ViewObject pVO, Object pValue ) throws AttributeIllegalArgumentException { pVO.setNewID( TypeConverter.to_Long( pValue ) ); } }; protected UnsupportedOperationException unavailableMethod() { return new UnsupportedOperationException( "Method unavailable on Parent MetaData - should have been overiden / use appropriate child/leaf" ); } protected static class VoAttributeList<T extends IViewObject> extends ArrayList<VoAttribute<T>> { public VoAttributeList( VoAttribute... pAttributes ) { if ( pAttributes != null ) { for ( VoAttribute zAttribute : pAttributes ) { if ( zAttribute != null ) { addAttribute( zAttribute ); } } } } public VoAttributeList<T> augment( VoAttributeList<? extends T> pParentList ) { for ( VoAttribute<? extends T> zAttribute : pParentList ) { if ( "ID".equals( zAttribute.getName() ) ) { insertIDAttribute( zAttribute ); } else { addAttribute( zAttribute ); } } return this; } @SuppressWarnings({"unchecked"}) private void addAttribute( VoAttribute zAttribute ) { add( zAttribute ); } @SuppressWarnings({"unchecked"}) private void insertIDAttribute( VoAttribute zAttribute ) { add( 0, zAttribute ); } } } |