Save This Page
Home » jdo2-model-2.3-ea-src » org.apache.jdo.impl.model.jdo » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    * 
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software 
   12    * distributed under the License is distributed on an "AS IS" BASIS, 
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
   14    * See the License for the specific language governing permissions and 
   15    * limitations under the License.
   16    */
   17   
   18   package org.apache.jdo.impl.model.jdo;
   19   
   20   import org.apache.jdo.impl.model.jdo.util.TypeSupport;
   21   import org.apache.jdo.model.ModelFatalException;
   22   import org.apache.jdo.model.java.JavaType;
   23   import org.apache.jdo.model.jdo.JDOClass;
   24   import org.apache.jdo.model.jdo.JDOField;
   25   import org.apache.jdo.model.jdo.JDOMap;
   26   import org.apache.jdo.model.jdo.JDOModel;
   27   import org.apache.jdo.util.I18NHelper;
   28   
   29   /**
   30    * An instance of this class represents the JDO relationship metadata 
   31    * (the treatment of keys and values) of a map relationship field. 
   32    * This dynamic implementation only stores property values explicitly
   33    * set by setter method.
   34    *
   35    * @author Michael Bouschen
   36    * @since 1.1
   37    * @version 1.1
   38    */
   39   public class JDOMapImplDynamic extends JDORelationshipImpl implements JDOMap {
   40       
   41       /** Property embeddedKey. */
   42       protected Boolean embeddedKey;
   43   
   44       /** Property keyType. No default. */
   45       protected transient JavaType keyType;
   46   
   47       /** Property keyTypeName. Defaults to java.lang.Object. */
   48       private String keyTypeName = "java.lang.Object"; //NOI18N
   49   
   50       /** Property embeddedValue. */
   51       protected Boolean embeddedValue;
   52   
   53       /** Property valueType. No default. */
   54       protected transient JavaType valueType;
   55   
   56       /** Property valueTypeName. Defaults to java.lang.Object. */
   57       private String valueTypeName = "java.lang.Object"; //NOI18N
   58   
   59       /** I18N support */
   60       private final static I18NHelper msg =  
   61           I18NHelper.getInstance(JDOMapImplDynamic.class);
   62   
   63       /**
   64        * Determines whether the keys of the map should be stored if possible as 
   65        * part of the instance instead of as their own instances in the datastore.
   66        * @return <code>true</code> if the keys are stored as part of this instance;
   67        * <code>false</code> otherwise
   68        */
   69       public boolean isEmbeddedKey() {
   70           if (embeddedKey != null) {
   71               // return embeddedKey, if explicitly set by the setter
   72               return embeddedKey.booleanValue();
   73           }
   74           
   75           // not set => calculate
   76           JavaType type = getKeyType();
   77           return (type != null) ? 
   78               TypeSupport.isEmbeddedElementType(type) : false;
   79       }
   80       
   81       /**
   82        * Set whether the keys of the map should be stored if possible as part 
   83        * of the instance instead of as their own instances in the datastore.
   84        * @param embeddedKey <code>true</code> if the keys are stored as part of
   85        * this instance; <code>false</code> otherwise
   86        */
   87       public void setEmbeddedKey(boolean embeddedKey) {
   88           this.embeddedKey = (embeddedKey ? Boolean.TRUE : Boolean.FALSE);
   89       }
   90   
   91       /**
   92        * Get the type representation of the keys for this JDOMap.
   93        * @return the type of the keys of this JDOMap  
   94        */
   95       public JavaType getKeyType() {
   96           if (keyType != null) {
   97               // return keyType, if explicitly set by the setter
   98               return keyType;
   99           }
  100       
  101           // not set => calculate
  102           JavaType type = null;
  103           if (keyTypeName != null) {
  104               JDOField jdoField = getDeclaringField();
  105               JDOClass jdoClass = jdoField.getDeclaringClass();
  106               JDOModel jdoModel = jdoClass.getDeclaringModel();
  107               type = TypeSupport.resolveType(jdoModel, keyTypeName,
  108                                              jdoClass.getPackagePrefix());
  109               if (type == null) {
  110                   throw new ModelFatalException(
  111                       msg.msg("EXC_CannotResolveKeyType", keyTypeName, //NOI18N
  112                               jdoField.getName(), jdoClass.getName()));
  113               }
  114           }
  115           
  116           return type;
  117       }
  118   
  119       /**
  120        * Set the type representation of the keys for this JDOMap.
  121        * @param keyType the type representation of the keys
  122        */
  123       public void setKeyType(JavaType keyType) {
  124           this.keyType = keyType;
  125           if (keyType != null) {
  126               setKeyTypeName(keyType.getName());
  127           }
  128       }
  129   
  130       /**
  131        * Get the string representation of the type of the keys for this JDOMap.
  132        * @return the key type as string
  133        */
  134       public String getKeyTypeName() {
  135           return keyTypeName;
  136       }
  137   
  138       /**
  139        * Set string representation of the type of the keys for this JDOMap.
  140        * @param keyTypeName the name of the key type
  141        */
  142       public void setKeyTypeName(String keyTypeName) {
  143           this.keyTypeName = keyTypeName;
  144       }
  145   
  146       /**
  147        * Determines whether the values of the map should be stored if possible as 
  148        * part of the instance instead of as their own instances in the datastore.
  149        * @return <code>true</code> if the values are stored as part of this 
  150        * instance; <code>false</code> otherwise
  151        */
  152       public boolean isEmbeddedValue() {
  153           if (embeddedValue != null) {
  154               // return embeddedKey, if explicitly set by the setter
  155               return embeddedValue.booleanValue();
  156           }
  157           
  158           // not set => calculate
  159           JavaType type = getValueType();
  160           return (type != null) ? 
  161               TypeSupport.isEmbeddedElementType(type) : false;
  162       }
  163       
  164       /**
  165        * Set whether the values of the map should be stored if possible as part 
  166        * of the instance instead of as their own instances in the datastore.
  167        * @param embeddedValue <code>true</code> if the values are stored as part 
  168        * of this instance; <code>false</code> otherwise
  169        */
  170       public void setEmbeddedValue(boolean embeddedValue) {
  171           this.embeddedValue = (embeddedValue ? Boolean.TRUE : Boolean.FALSE);
  172       }
  173   
  174       /**
  175        * Get the type representation of the values for this JDOMap.
  176        * @return the type of the values of this JDOMap  
  177        */
  178       public JavaType getValueType() {
  179           if (valueType != null) {
  180               // return valueType, if explicitly set by the setter
  181               return valueType;
  182           }
  183       
  184           // not set => calculate
  185           JavaType type = null;
  186           if (valueTypeName != null) {
  187               JDOField jdoField = getDeclaringField();
  188               JDOClass jdoClass = jdoField.getDeclaringClass();
  189               JDOModel jdoModel = jdoClass.getDeclaringModel();
  190               type = TypeSupport.resolveType(jdoModel, valueTypeName,
  191                                              jdoClass.getPackagePrefix());
  192               if (type == null) {
  193                   throw new ModelFatalException(
  194                       msg.msg("EXC_CannotResolveValueType", valueTypeName, //NOI18N
  195                               jdoField.getName(), jdoClass.getName()));
  196               }
  197           }
  198           
  199           return type;
  200       }
  201   
  202       /**
  203        * Set the type representation of the values for this JDOMap.
  204        * @param valueType the type representation of the values
  205        */
  206       public void setValueType(JavaType valueType) {
  207           this.valueType = valueType;
  208           if (valueType != null) {
  209               setKeyTypeName(valueType.getName());
  210           }
  211       }
  212   
  213       /**
  214        * Get the string representation of the type of the values for this JDOMap.
  215        * @return the key value as string
  216        */
  217       public String getValueTypeName() {
  218           return valueTypeName;
  219       }
  220   
  221       /**
  222        * Set string representation of the type of the values for this JDOMap.
  223        * @param valueTypeName the name of the value type
  224        */
  225       public void setValueTypeName(String valueTypeName) {
  226           this.valueTypeName = valueTypeName;
  227       }
  228   
  229       /**
  230        * Determines whether this JDORelationship represents a map 
  231        * relationship or not. A return of <code>true</code> means this
  232        * JDORelationship is a JDOMap instance.
  233        * @return <code>true</code> if this JDORelationship represents a
  234        * map relationship; <code>false</code> otherwise.
  235        */
  236       public boolean isJDOMap() {
  237           return true;
  238       }
  239   
  240       //========= Internal helper methods ==========
  241   
  242       /** 
  243        * Get the type representation of the relationship. This will be 
  244        * the JavaType for references, the element type for collections
  245        * and arrays, and the value type for maps.
  246        * @return the relationship type
  247        */
  248       public JavaType getRelatedJavaType() {
  249           return getValueType();
  250       }
  251   
  252   }

Save This Page
Home » jdo2-model-2.3-ea-src » org.apache.jdo.impl.model.jdo » [javadoc | source]