/src/com/google/appengine/datanucleus/mapping/DatastoreProperty.java

http://datanucleus-appengine.googlecode.com/ · Java · 202 lines · 121 code · 41 blank · 40 comment · 9 complexity · def55d4bfa0e579e9a24be144e1185ce MD5 · raw file

  1. /**********************************************************************
  2. Copyright (c) 2009 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. **********************************************************************/
  13. package com.google.appengine.datanucleus.mapping;
  14. import org.datanucleus.metadata.AbstractMemberMetaData;
  15. import org.datanucleus.metadata.ColumnMetaData;
  16. import org.datanucleus.store.mapped.DatastoreField;
  17. import org.datanucleus.store.mapped.DatastoreIdentifier;
  18. import org.datanucleus.store.mapped.MappedStoreManager;
  19. import org.datanucleus.store.mapped.mapping.DatastoreMapping;
  20. import org.datanucleus.store.mapped.mapping.JavaTypeMapping;
  21. /**
  22. * Describes a property in the datastore.
  23. *
  24. * Mostly copied from Column.
  25. *
  26. * @author Max Ross <maxr@google.com>
  27. */
  28. class DatastoreProperty implements DatastoreField {
  29. /** Identifier for the column in the datastore. */
  30. private DatastoreIdentifier identifier;
  31. /** ColumnMetaData for this column. */
  32. private ColumnMetaData columnMetaData;
  33. /** Table containing this column in the datastore. */
  34. private final DatastoreTable table;
  35. /** Datastore mapping for this column. */
  36. private DatastoreMapping datastoreMapping = null;
  37. /** Java type that this column is storing. (can we just get this from the mapping above ?) */
  38. private final String storedJavaType;
  39. /** Manager for the store into which we are persisting. */
  40. private final MappedStoreManager storeMgr;
  41. /** Flag indicating whether or not this is a pk */
  42. private boolean isPrimaryKey;
  43. /** Flag indicating whether the user wants the property to be nullable or not */
  44. private boolean isNullable;
  45. /**
  46. * {@link #getMemberMetaData()} typically derives this from the parent of
  47. * {@link #columnMetaData} but if this member is set it just returns
  48. * it directly.
  49. */
  50. private AbstractMemberMetaData ammd;
  51. public DatastoreProperty(DatastoreTable table, String javaType,
  52. DatastoreIdentifier identifier, ColumnMetaData colmd) {
  53. this.table = table;
  54. this.storedJavaType = javaType;
  55. this.storeMgr = table.getStoreManager();
  56. setIdentifier(identifier);
  57. if (colmd == null) {
  58. // Create a default ColumnMetaData since none provided
  59. columnMetaData = new ColumnMetaData();
  60. } else {
  61. columnMetaData = colmd;
  62. }
  63. // if not specified by the user the getAllowsNull is null
  64. isNullable = !Boolean.FALSE.equals(columnMetaData.getAllowsNull());
  65. // Uniqueness
  66. if (columnMetaData.getUnique()) {
  67. // MetaData requires it to be unique
  68. throw new UnsupportedOperationException("No support for uniqueness constraints");
  69. }
  70. }
  71. public String getStoredJavaType() {
  72. return storedJavaType;
  73. }
  74. public void setAsPrimaryKey() {
  75. isPrimaryKey = true;
  76. }
  77. public boolean isPrimaryKey() {
  78. return isPrimaryKey;
  79. }
  80. public boolean isNullable() {
  81. return isNullable;
  82. }
  83. public DatastoreMapping getDatastoreMapping() {
  84. return datastoreMapping;
  85. }
  86. public void setDatastoreMapping(DatastoreMapping mapping) {
  87. this.datastoreMapping = mapping;
  88. }
  89. public DatastoreTable getDatastoreContainerObject() {
  90. return table;
  91. }
  92. public String applySelectFunction(String replacementValue) {
  93. throw new UnsupportedOperationException("Select function not supported");
  94. }
  95. public void copyConfigurationTo(DatastoreField field) {
  96. DatastoreProperty prop = (DatastoreProperty) field;
  97. prop.isPrimaryKey = this.isPrimaryKey;
  98. }
  99. public DatastoreField setNullable() {
  100. // all properties are nullable
  101. return this;
  102. }
  103. public DatastoreField setDefaultable() {
  104. throw new UnsupportedOperationException("Default values not supported");
  105. }
  106. public void setIdentifier(DatastoreIdentifier identifier) {
  107. this.identifier = identifier;
  108. }
  109. public MappedStoreManager getStoreManager() {
  110. return storeMgr;
  111. }
  112. public DatastoreIdentifier getIdentifier() {
  113. return identifier;
  114. }
  115. public boolean isDefaultable() {
  116. return false;
  117. }
  118. public DatastoreField setUnique() {
  119. return this;
  120. }
  121. public boolean isUnique() {
  122. return false;
  123. }
  124. public DatastoreField setIdentity(boolean b) {
  125. isPrimaryKey = b;
  126. return this;
  127. }
  128. public boolean isIdentity() {
  129. return isPrimaryKey();
  130. }
  131. public void setDefaultValue(Object o) {
  132. throw new UnsupportedOperationException("Default values not supported.");
  133. }
  134. public Object getDefaultValue() {
  135. return null;
  136. }
  137. public void setColumnMetaData(ColumnMetaData columnMetaData) {
  138. this.columnMetaData = columnMetaData;
  139. }
  140. public ColumnMetaData getColumnMetaData() {
  141. return columnMetaData;
  142. }
  143. public JavaTypeMapping getJavaTypeMapping() {
  144. return datastoreMapping.getJavaTypeMapping();
  145. }
  146. public AbstractMemberMetaData getMemberMetaData() {
  147. if (ammd != null) {
  148. return ammd;
  149. }
  150. if (columnMetaData != null && columnMetaData.getParent() instanceof AbstractMemberMetaData) {
  151. return (AbstractMemberMetaData)columnMetaData.getParent();
  152. }
  153. return null;
  154. }
  155. void setMemberMetaData(AbstractMemberMetaData ammd) {
  156. this.ammd = ammd;
  157. }
  158. }