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