Wiki source code of Server-Side _Entity.java Template
Last modified by David Avendasora on 2009/04/07 07:19
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{code}} | ||
| 2 | // $ DO NOT EDIT. Make changes to ${entity.classNameWithOptionalPackage}.java instead. | ||
| 3 | #if ($entity.superclassPackageName) | ||
| 4 | package $entity.superclassPackageName; | ||
| 5 | |||
| 6 | #end | ||
| 7 | import com.webobjects.eoaccess.*; | ||
| 8 | import com.webobjects.eocontrol.*; | ||
| 9 | import com.webobjects.foundation.*; | ||
| 10 | import java.math.*; | ||
| 11 | import java.util.*; | ||
| 12 | import org.apache.log4j.Logger; | ||
| 13 | |||
| 14 | import er.extensions.eof.ERXKey; | ||
| 15 | |||
| 16 | @SuppressWarnings( { "serial", | ||
| 17 | "unused" }) | ||
| 18 | public abstract class ${entity.prefixClassNameWithoutPackage} extends #if ($entity.partialEntitySet)er.extensions.partials.ERXPartial<${entity.partialEntity.className}>#elseif ($entity.parentSet)${entity.parent.classNameWithDefault}#elseif ($EOGenericRecord)${EOGenericRecord}#else er.extensions.eof.ERXGenericRecord#end { | ||
| 19 | #if ($entity.partialEntitySet) | ||
| 20 | public static final String ENTITY_NAME = "$entity.partialEntity.name"; | ||
| 21 | #else | ||
| 22 | public static final String ENTITY_NAME = "$entity.name"; | ||
| 23 | #end | ||
| 24 | |||
| 25 | // Attributes | ||
| 26 | #foreach ($attribute in $entity.sortedClassAttributes) | ||
| 27 | public static final String ${attribute.uppercaseUnderscoreName}_KEY = "$attribute.name"; | ||
| 28 | public static final ERXKey<$attribute.javaClassName> ${attribute.uppercaseUnderscoreName} = new ERXKey<$attribute.javaClassName>(${attribute.uppercaseUnderscoreName}_KEY); | ||
| 29 | #end | ||
| 30 | |||
| 31 | // Relationships | ||
| 32 | #foreach ($relationship in $entity.sortedClassRelationships) | ||
| 33 | public static final String ${relationship.uppercaseUnderscoreName}_KEY = "$relationship.name"; | ||
| 34 | public static final ERXKey<$relationship.actualDestination.classNameWithDefault> ${relationship.uppercaseUnderscoreName} = new ERXKey<$relationship.actualDestination.classNameWithDefault>(${relationship.uppercaseUnderscoreName}_KEY); | ||
| 35 | #end | ||
| 36 | |||
| 37 | private static Logger LOG = Logger.getLogger(${entity.prefixClassNameWithoutPackage}.class.getName()); | ||
| 38 | |||
| 39 | #if (!$entity.partialEntitySet) | ||
| 40 | public $entity.classNameWithOptionalPackage localInstanceIn(EOEditingContext editingContext) { | ||
| 41 | $entity.classNameWithOptionalPackage localInstance = ($entity.classNameWithOptionalPackage)EOUtilities.localInstanceOfObject(editingContext, this); | ||
| 42 | if (localInstance == null) { | ||
| 43 | throw new IllegalStateException("You attempted to localInstance " + this + ", which has not yet committed."); | ||
| 44 | } | ||
| 45 | return localInstance; | ||
| 46 | } | ||
| 47 | |||
| 48 | #end | ||
| 49 | #foreach ($attribute in $entity.sortedClassAttributes) | ||
| 50 | #if (!$attribute.inherited) | ||
| 51 | #if ($attribute.userInfo.ERXConstantClassName) | ||
| 52 | public $attribute.userInfo.ERXConstantClassName ${attribute.name}() { | ||
| 53 | Number value = (Number)storedValueForKey(${attribute.uppercaseUnderscoreName}_KEY); | ||
| 54 | return ($attribute.userInfo.ERXConstantClassName)value; | ||
| 55 | } | ||
| 56 | |||
| 57 | public void set${attribute.capitalizedName}($attribute.userInfo.ERXConstantClassName ${attribute.name}) { | ||
| 58 | takeStoredValueForKey(${attribute.name}, ${attribute.uppercaseUnderscoreName}_KEY); | ||
| 59 | } | ||
| 60 | #else | ||
| 61 | public $attribute.javaClassName ${attribute.name}() { | ||
| 62 | /*if (${entity.prefixClassNameWithoutPackage}.LOG.isDebugEnabled()) { | ||
| 63 | ${entity.prefixClassNameWithoutPackage}.LOG.debug("${entity.prefixClassNameWithoutPackage}.${attribute.name}(): " + storedValueForKey(${attribute.uppercaseUnderscoreName}_KEY)); | ||
| 64 | }*/ | ||
| 65 | return ($attribute.javaClassName) storedValueForKey(${attribute.uppercaseUnderscoreName}_KEY); | ||
| 66 | } | ||
| 67 | |||
| 68 | public void set${attribute.capitalizedName}($attribute.javaClassName ${attribute.name}) { | ||
| 69 | #if ($attribute.javaClassName == "java.math.BigDecimal") | ||
| 70 | if (${attribute.name} != null) { | ||
| 71 | ${attribute.name} = ${attribute.name}.setScale(${attribute.scale}, RoundingMode.HALF_UP); | ||
| 72 | } | ||
| 73 | #end | ||
| 74 | if (${entity.prefixClassNameWithoutPackage}.LOG.isDebugEnabled()) { | ||
| 75 | ${entity.prefixClassNameWithoutPackage}.LOG.debug( "updating $attribute.name from " + storedValueForKey(${attribute.uppercaseUnderscoreName}_KEY) + " to " + ${attribute.name}); | ||
| 76 | } | ||
| 77 | takeStoredValueForKey(${attribute.name}, ${attribute.uppercaseUnderscoreName}_KEY); | ||
| 78 | } | ||
| 79 | #end | ||
| 80 | |||
| 81 | #end | ||
| 82 | #end | ||
| 83 | #foreach ($relationship in $entity.sortedClassToOneRelationships) | ||
| 84 | #if (!$relationship.inherited) | ||
| 85 | public $relationship.actualDestination.classNameWithDefault ${relationship.name}() { | ||
| 86 | /*if (${entity.prefixClassNameWithoutPackage}.LOG.isDebugEnabled()) { | ||
| 87 | ${entity.prefixClassNameWithoutPackage}.LOG.debug("${entity.prefixClassNameWithoutPackage}.${relationship.name}(): " + storedValueForKey(${relationship.uppercaseUnderscoreName}_KEY)); | ||
| 88 | }*/ | ||
| 89 | return ($relationship.actualDestination.classNameWithDefault)storedValueForKey(${relationship.uppercaseUnderscoreName}_KEY); | ||
| 90 | } | ||
| 91 | |||
| 92 | public void set${relationship.capitalizedName}Relationship($relationship.actualDestination.classNameWithDefault ${relationship.name}) { | ||
| 93 | if (${entity.prefixClassNameWithoutPackage}.LOG.isDebugEnabled()) { | ||
| 94 | ${entity.prefixClassNameWithoutPackage}.LOG.debug("updating $relationship.name from " + ${relationship.name}() + " to " + ${relationship.name}); | ||
| 95 | } | ||
| 96 | if (${relationship.name} == null) { | ||
| 97 | $relationship.actualDestination.classNameWithDefault old${relationship.capitalizedName} = ${relationship.name}(); | ||
| 98 | if (old${relationship.capitalizedName} != null) { | ||
| 99 | removeObjectFromBothSidesOfRelationshipWithKey(old${relationship.capitalizedName}, ${relationship.uppercaseUnderscoreName}_KEY); | ||
| 100 | } | ||
| 101 | } else { | ||
| 102 | addObjectToBothSidesOfRelationshipWithKey(${relationship.name}, ${relationship.uppercaseUnderscoreName}_KEY); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | #end | ||
| 107 | #end | ||
| 108 | #foreach ($relationship in $entity.sortedClassToManyRelationships) | ||
| 109 | #if (!$relationship.inherited) | ||
| 110 | @SuppressWarnings("unchecked") | ||
| 111 | public NSArray<${relationship.actualDestination.classNameWithDefault}> ${relationship.name}() { | ||
| 112 | return (NSArray<${relationship.actualDestination.classNameWithDefault}>)storedValueForKey(${relationship.uppercaseUnderscoreName}_KEY); | ||
| 113 | } | ||
| 114 | |||
| 115 | public Integer count${relationship.capitalizedName}() { | ||
| 116 | return countForRelationship(${relationship.uppercaseUnderscoreName}_KEY); | ||
| 117 | } | ||
| 118 | |||
| 119 | public EOQualifier qualifierFor${relationship.capitalizedName}() { | ||
| 120 | return qualifierForRelationshipWithKey(${relationship.uppercaseUnderscoreName}_KEY); | ||
| 121 | } | ||
| 122 | |||
| 123 | #if (!$relationship.inverseRelationship || $relationship.flattened || !$relationship.inverseRelationship.classProperty) | ||
| 124 | public NSArray<${relationship.actualDestination.classNameWithDefault}> ${relationship.name}(EOQualifier qualifier) { | ||
| 125 | return ${relationship.name}(qualifier, null); | ||
| 126 | } | ||
| 127 | #else | ||
| 128 | public NSArray<${relationship.actualDestination.classNameWithDefault}> ${relationship.name}(EOQualifier qualifier) { | ||
| 129 | return ${relationship.name}(qualifier, null, false); | ||
| 130 | } | ||
| 131 | |||
| 132 | public NSArray<${relationship.actualDestination.classNameWithDefault}> ${relationship.name}(EOQualifier qualifier, boolean fetch) { | ||
| 133 | return ${relationship.name}(qualifier, null, fetch); | ||
| 134 | } | ||
| 135 | #end | ||
| 136 | |||
| 137 | @SuppressWarnings("unchecked") | ||
| 138 | public NSArray<${relationship.actualDestination.classNameWithDefault}> ${relationship.name}(EOQualifier qualifier, NSArray<EOSortOrdering> sortOrderings#if ($relationship.inverseRelationship && !$relationship.flattened && $relationship.inverseRelationship.classProperty), boolean fetch#end) { | ||
| 139 | if (${entity.prefixClassNameWithoutPackage}.LOG.isDebugEnabled()) { | ||
| 140 | ${entity.prefixClassNameWithoutPackage}.LOG.debug("${relationship.name}(qualifier " + qualifier + ", sortOrderings " + sortOrderings + "#if ($relationship.inverseRelationship && !$relationship.flattened && $relationship.inverseRelationship.classProperty), fetch " + fetch + "#end) called."); | ||
| 141 | } | ||
| 142 | NSArray<${relationship.actualDestination.classNameWithDefault}> results; | ||
| 143 | #if ($relationship.inverseRelationship && !$relationship.flattened && $relationship.inverseRelationship.classProperty) | ||
| 144 | if (fetch) { | ||
| 145 | EOQualifier fullQualifier; | ||
| 146 | #if (${relationship.actualDestination.genericRecord}) | ||
| 147 | EOQualifier inverseQualifier = new EOKeyValueQualifier("${relationship.inverseRelationship.name}", EOQualifier.QualifierOperatorEqual, this); | ||
| 148 | #else | ||
| 149 | EOQualifier inverseQualifier = new EOKeyValueQualifier(${relationship.actualDestination.classNameWithDefault}.${relationship.inverseRelationship.uppercaseUnderscoreName}_KEY, EOQualifier.QualifierOperatorEqual, this); | ||
| 150 | #end | ||
| 151 | |||
| 152 | if (qualifier == null) { | ||
| 153 | fullQualifier = inverseQualifier; | ||
| 154 | } else { | ||
| 155 | NSMutableArray<EOQualifier> qualifiers = new NSMutableArray<EOQualifier>(); | ||
| 156 | qualifiers.addObject(qualifier); | ||
| 157 | qualifiers.addObject(inverseQualifier); | ||
| 158 | fullQualifier = new EOAndQualifier(qualifiers); | ||
| 159 | } | ||
| 160 | |||
| 161 | #if (${relationship.actualDestination.genericRecord}) | ||
| 162 | EOFetchSpecification fetchSpec = new EOFetchSpecification("${relationship.actualDestination.name}", qualifier, sortOrderings); | ||
| 163 | fetchSpec.setIsDeep(true); | ||
| 164 | results = (NSArray<${relationship.actualDestination.classNameWithDefault}>)editingContext().objectsWithFetchSpecification(fetchSpec); | ||
| 165 | #else | ||
| 166 | results = ${relationship.actualDestination.classNameWithDefault}.fetch${relationship.actualDestination.pluralName}(editingContext(), fullQualifier, sortOrderings); | ||
| 167 | #end | ||
| 168 | } else { | ||
| 169 | #end | ||
| 170 | results = ${relationship.name}(); | ||
| 171 | if (qualifier != null) { | ||
| 172 | results = (NSArray<${relationship.actualDestination.classNameWithDefault}>)EOQualifier.filteredArrayWithQualifier(results, qualifier); | ||
| 173 | } | ||
| 174 | if (sortOrderings != null) { | ||
| 175 | results = (NSArray<${relationship.actualDestination.classNameWithDefault}>)EOSortOrdering.sortedArrayUsingKeyOrderArray(results, sortOrderings); | ||
| 176 | } | ||
| 177 | #if ($relationship.inverseRelationship && !$relationship.flattened && $relationship.inverseRelationship.classProperty) | ||
| 178 | } | ||
| 179 | #end | ||
| 180 | return results; | ||
| 181 | } | ||
| 182 | |||
| 183 | public void addTo${relationship.capitalizedName}Relationship($relationship.actualDestination.classNameWithDefault ${relationship.actualDestination.initialLowercaseClassNameWithoutPackage}) { | ||
| 184 | if (${entity.prefixClassNameWithoutPackage}.LOG.isDebugEnabled()) { | ||
| 185 | ${entity.prefixClassNameWithoutPackage}.LOG.debug("adding " + ${relationship.actualDestination.initialLowercaseClassNameWithoutPackage} + " to ${relationship.name} relationship"); | ||
| 186 | } | ||
| 187 | addObjectToBothSidesOfRelationshipWithKey(${relationship.actualDestination.initialLowercaseClassNameWithoutPackage}, ${relationship.uppercaseUnderscoreName}_KEY); | ||
| 188 | } | ||
| 189 | |||
| 190 | public void removeFrom${relationship.capitalizedName}Relationship($relationship.actualDestination.classNameWithDefault ${relationship.actualDestination.initialLowercaseClassNameWithoutPackage}) { | ||
| 191 | if (${entity.prefixClassNameWithoutPackage}.LOG.isDebugEnabled()) { | ||
| 192 | ${entity.prefixClassNameWithoutPackage}.LOG.debug("Removing " + ${relationship.actualDestination.initialLowercaseClassNameWithoutPackage} + " from ${relationship.name} relationship"); | ||
| 193 | } | ||
| 194 | removeObjectFromBothSidesOfRelationshipWithKey(${relationship.actualDestination.initialLowercaseClassNameWithoutPackage}, ${relationship.uppercaseUnderscoreName}_KEY); | ||
| 195 | #if ($relationship.ownsDestination) | ||
| 196 | // editingContext().deleteObject(${relationship.actualDestination.initialLowercaseClassNameWithoutPackage}); | ||
| 197 | #end } | ||
| 198 | |||
| 199 | public $relationship.actualDestination.classNameWithDefault create${relationship.capitalizedName}Relationship() { | ||
| 200 | EOClassDescription eoClassDesc = EOClassDescription.classDescriptionForEntityName("${relationship.actualDestination.name}"); | ||
| 201 | EOEnterpriseObject eo = eoClassDesc.createInstanceWithEditingContext(editingContext(), null); | ||
| 202 | editingContext().insertObject(eo); | ||
| 203 | addObjectToBothSidesOfRelationshipWithKey(eo, ${relationship.uppercaseUnderscoreName}_KEY); | ||
| 204 | return ($relationship.actualDestination.classNameWithDefault) eo; | ||
| 205 | } | ||
| 206 | |||
| 207 | public void delete${relationship.capitalizedName}Relationship($relationship.actualDestination.classNameWithDefault ${relationship.actualDestination.initialLowercaseClassNameWithoutPackage}) { | ||
| 208 | if (${entity.prefixClassNameWithoutPackage}.LOG.isDebugEnabled()) { | ||
| 209 | ${entity.prefixClassNameWithoutPackage}.LOG.debug("Delete " + ${relationship.actualDestination.initialLowercaseClassNameWithoutPackage}); | ||
| 210 | } | ||
| 211 | removeObjectFromBothSidesOfRelationshipWithKey(${relationship.actualDestination.initialLowercaseClassNameWithoutPackage}, ${relationship.uppercaseUnderscoreName}_KEY); | ||
| 212 | #if (true || !$relationship.ownsDestination) | ||
| 213 | editingContext().deleteObject(${relationship.actualDestination.initialLowercaseClassNameWithoutPackage}); | ||
| 214 | #end } | ||
| 215 | |||
| 216 | public void deleteAll${relationship.capitalizedName}Relationships() { | ||
| 217 | if (${entity.prefixClassNameWithoutPackage}.LOG.isDebugEnabled()) { | ||
| 218 | ${entity.prefixClassNameWithoutPackage}.LOG.debug("Delete all " + this + ".${relationship.name}() objects"); | ||
| 219 | } | ||
| 220 | Enumeration<${relationship.actualDestination.classNameWithDefault}> objects = ${relationship.name}().immutableClone().objectEnumerator(); | ||
| 221 | while (objects.hasMoreElements()) { | ||
| 222 | delete${relationship.capitalizedName}Relationship(($relationship.actualDestination.classNameWithDefault)objects.nextElement()); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | #end | ||
| 227 | #end | ||
| 228 | |||
| 229 | #if (!$entity.abstractEntity) | ||
| 230 | public #if (!$entity.partialEntitySet)static #end${entity.classNameWithOptionalPackage}#if (!$entity.partialEntitySet) create#else init#end${entity.name}(EOEditingContext editingContext | ||
| 231 | #foreach ($attribute in $entity.sortedClassAttributes) | ||
| 232 | #if (!$attribute.allowsNull) | ||
| 233 | #set ($restrictingQualifierKey = 'false') | ||
| 234 | #foreach ($qualifierKey in $entity.restrictingQualifierKeys) | ||
| 235 | #if ($attribute.name == $qualifierKey)#set ($restrictingQualifierKey = 'true') | ||
| 236 | #end | ||
| 237 | #end | ||
| 238 | #if ($restrictingQualifierKey == 'false') #if ($attribute.userInfo.ERXConstantClassName) | ||
| 239 | , ${attribute.userInfo.ERXConstantClassName} #else | ||
| 240 | , ${attribute.javaClassName} #end | ||
| 241 | ${attribute.name} | ||
| 242 | #end | ||
| 243 | #end | ||
| 244 | #end | ||
| 245 | #foreach ($relationship in $entity.sortedClassToOneRelationships) #if ($relationship.mandatory && !$relationship.flattened && !($relationship.ownsDestination && $relationship.propagatesPrimaryKey)) | ||
| 246 | , ${relationship.actualDestination.classNameWithDefault} ${relationship.name} | ||
| 247 | #end | ||
| 248 | #end | ||
| 249 | ) { | ||
| 250 | if(LOG.isDebugEnabled()) { | ||
| 251 | LOG.debug("${entity.classNameWithOptionalPackage}#if (!$entity.partialEntitySet).create#else.init#end${entity.name}(EOEditingContext " + editingContext | ||
| 252 | #foreach ($attribute in $entity.sortedClassAttributes) | ||
| 253 | #if (!$attribute.allowsNull) | ||
| 254 | #set ($restrictingQualifierKey = 'false') | ||
| 255 | #foreach ($qualifierKey in $entity.restrictingQualifierKeys) | ||
| 256 | #if ($attribute.name == $qualifierKey)#set ($restrictingQualifierKey = 'true') | ||
| 257 | #end | ||
| 258 | #end | ||
| 259 | #if ($restrictingQualifierKey == 'false') #if ($attribute.userInfo.ERXConstantClassName) | ||
| 260 | + ", ${attribute.userInfo.ERXConstantClassName} #else | ||
| 261 | + ", ${attribute.name} #end | ||
| 262 | " + ${attribute.name} | ||
| 263 | #end | ||
| 264 | #end | ||
| 265 | #end | ||
| 266 | #foreach ($relationship in $entity.sortedClassToOneRelationships) #if ($relationship.mandatory && !$relationship.flattened && !($relationship.ownsDestination && $relationship.propagatesPrimaryKey)) | ||
| 267 | + ", ${relationship.name} " + ${relationship.name} | ||
| 268 | #end | ||
| 269 | #end ); | ||
| 270 | } | ||
| 271 | ${entity.classNameWithOptionalPackage} new${entity.name} = (${entity.classNameWithOptionalPackage})#if ($entity.partialEntitySet)this;#else EOUtilities.createAndInsertInstance(editingContext, ${entity.name}.ENTITY_NAME);#end | ||
| 272 | |||
| 273 | // Set Required Attributes | ||
| 274 | #foreach ($attribute in $entity.sortedClassAttributes) | ||
| 275 | #if (!$attribute.allowsNull) | ||
| 276 | #set ($restrictingQualifierKey = 'false') | ||
| 277 | #foreach ($qualifierKey in $entity.restrictingQualifierKeys) | ||
| 278 | #if ($attribute.name == $qualifierKey) | ||
| 279 | #set ($restrictingQualifierKey = 'true') | ||
| 280 | #end | ||
| 281 | #end | ||
| 282 | #if ($restrictingQualifierKey == 'false') | ||
| 283 | new${entity.name}.set${attribute.capitalizedName}(${attribute.name}); | ||
| 284 | #end | ||
| 285 | #end | ||
| 286 | #end | ||
| 287 | // Set Required Relationships | ||
| 288 | #foreach ($relationship in $entity.sortedClassToOneRelationships) | ||
| 289 | #if ($relationship.mandatory && !$relationship.flattened && !($relationship.ownsDestination && $relationship.propagatesPrimaryKey)) | ||
| 290 | new${entity.name}.set${relationship.capitalizedName}Relationship(${relationship.name}); | ||
| 291 | #end | ||
| 292 | #end | ||
| 293 | return new${entity.name}; | ||
| 294 | } | ||
| 295 | #end | ||
| 296 | |||
| 297 | #if (!$entity.partialEntitySet) | ||
| 298 | |||
| 299 | public static NSArray<${entity.classNameWithOptionalPackage}> fetchAll${entity.pluralName}(EOEditingContext editingContext) { | ||
| 300 | return ${entity.prefixClassNameWithoutPackage}.fetchAll${entity.pluralName}(editingContext, null); | ||
| 301 | } | ||
| 302 | |||
| 303 | public static NSArray<${entity.classNameWithOptionalPackage}> fetchAll${entity.pluralName}(EOEditingContext editingContext, NSArray<EOSortOrdering> sortOrderings) { | ||
| 304 | return ${entity.prefixClassNameWithoutPackage}.fetch${entity.pluralName}(editingContext, null, sortOrderings); | ||
| 305 | } | ||
| 306 | |||
| 307 | @SuppressWarnings("unchecked") | ||
| 308 | public static NSArray<${entity.classNameWithOptionalPackage}> fetch${entity.pluralName}(EOEditingContext editingContext, EOQualifier qualifier, NSArray<EOSortOrdering> sortOrderings) { | ||
| 309 | if(${entity.prefixClassNameWithoutPackage}.LOG.isDebugEnabled()) { | ||
| 310 | ${entity.prefixClassNameWithoutPackage}.LOG.debug("fetch${entity.pluralName}(editingContext " + editingContext + ", qualifier " + qualifier + ", sortOrderings " + sortOrderings + ")"); | ||
| 311 | } | ||
| 312 | EOFetchSpecification fetchSpec = new EOFetchSpecification(${entity.name}.ENTITY_NAME, qualifier, sortOrderings); | ||
| 313 | fetchSpec.setIsDeep(true); | ||
| 314 | NSArray<${entity.classNameWithOptionalPackage}> eoObjects = (NSArray<${entity.classNameWithOptionalPackage}>)editingContext.objectsWithFetchSpecification(fetchSpec); | ||
| 315 | return eoObjects; | ||
| 316 | } | ||
| 317 | |||
| 318 | public static ${entity.classNameWithOptionalPackage} fetch${entity.name}(EOEditingContext editingContext, String keyName, Object value) { | ||
| 319 | return ${entity.prefixClassNameWithoutPackage}.fetch${entity.name}(editingContext, new EOKeyValueQualifier(keyName, EOQualifier.QualifierOperatorEqual, value)); | ||
| 320 | } | ||
| 321 | |||
| 322 | public static ${entity.classNameWithOptionalPackage} fetch${entity.name}(EOEditingContext editingContext, EOQualifier qualifier) { | ||
| 323 | NSArray<${entity.classNameWithOptionalPackage}> eoObjects = ${entity.prefixClassNameWithoutPackage}.fetch${entity.pluralName}(editingContext, qualifier, null); | ||
| 324 | ${entity.classNameWithOptionalPackage} eoObject; | ||
| 325 | int count = eoObjects.count(); | ||
| 326 | if (count == 0) { | ||
| 327 | eoObject = null; | ||
| 328 | } | ||
| 329 | else if (count == 1) { | ||
| 330 | eoObject = (${entity.classNameWithOptionalPackage})eoObjects.objectAtIndex(0); | ||
| 331 | } | ||
| 332 | else { | ||
| 333 | throw new IllegalStateException("There was more than one ${entity.name} that matched the qualifier '" + qualifier + "'."); | ||
| 334 | } | ||
| 335 | return eoObject; | ||
| 336 | } | ||
| 337 | |||
| 338 | public static ${entity.classNameWithOptionalPackage} fetchRequired${entity.name}(EOEditingContext editingContext, String keyName, Object value) { | ||
| 339 | return ${entity.prefixClassNameWithoutPackage}.fetchRequired${entity.name}(editingContext, new EOKeyValueQualifier(keyName, EOQualifier.QualifierOperatorEqual, value)); | ||
| 340 | } | ||
| 341 | |||
| 342 | public static ${entity.classNameWithOptionalPackage} fetchRequired${entity.name}(EOEditingContext editingContext, EOQualifier qualifier) { | ||
| 343 | ${entity.classNameWithOptionalPackage} eoObject = ${entity.prefixClassNameWithoutPackage}.fetch${entity.name}(editingContext, qualifier); | ||
| 344 | if (eoObject == null) { | ||
| 345 | throw new NoSuchElementException("There was no ${entity.name} that matched the qualifier '" + qualifier + "'."); | ||
| 346 | } | ||
| 347 | return eoObject; | ||
| 348 | } | ||
| 349 | |||
| 350 | public static ${entity.classNameWithOptionalPackage} localInstanceIn(EOEditingContext editingContext, ${entity.classNameWithOptionalPackage} eo) { | ||
| 351 | ${entity.classNameWithOptionalPackage} localInstance = (eo == null) ? null : (${entity.classNameWithOptionalPackage})EOUtilities.localInstanceOfObject(editingContext, eo); | ||
| 352 | if (localInstance == null && eo != null) { | ||
| 353 | throw new IllegalStateException("You attempted to localInstance " + eo + ", which has not yet committed."); | ||
| 354 | } | ||
| 355 | return localInstance; | ||
| 356 | } | ||
| 357 | #end | ||
| 358 | |||
| 359 | #foreach ($fetchSpecification in $entity.sortedFetchSpecs) | ||
| 360 | @SuppressWarnings("unchecked") | ||
| 361 | public static NSArray<${entity.className}> fetch${fetchSpecification.capitalizedName}(EOEditingContext ec#foreach ($binding in $fetchSpecification.distinctBindings), | ||
| 362 | ${binding.javaClassName} ${binding.name}Binding#end) { | ||
| 363 | if (${entity.prefixClassNameWithoutPackage}.LOG.isDebugEnabled()) { | ||
| 364 | ${entity.prefixClassNameWithoutPackage}.LOG.debug("fetch${fetchSpecification.capitalizedName}(EOEditingContext " + ec + "#foreach ($binding in $fetchSpecification.distinctBindings), ${binding.javaClassName} " + ${binding.name}Binding + "#end)"); | ||
| 365 | } | ||
| 366 | EOFetchSpecification spec = EOFetchSpecification.fetchSpecificationNamed("${fetchSpecification.name}", ${entity.name}.ENTITY_NAME); | ||
| 367 | #if ($fetchSpecification.distinctBindings.size() > 0) | ||
| 368 | NSMutableDictionary<Object, String> bindings = new NSMutableDictionary<Object, String>(); | ||
| 369 | #foreach ($binding in $fetchSpecification.distinctBindings) | ||
| 370 | bindings.takeValueForKey(${binding.name}Binding, "${binding.name}"); | ||
| 371 | #end | ||
| 372 | spec = spec.fetchSpecificationWithQualifierBindings(bindings); | ||
| 373 | #end | ||
| 374 | return ec.objectsWithFetchSpecification(spec); | ||
| 375 | } | ||
| 376 | |||
| 377 | public static ${entity.classNameWithOptionalPackage} fetchOne${fetchSpecification.capitalizedName}(EOEditingContext ec#foreach ($binding in $fetchSpecification.distinctBindings), | ||
| 378 | ${binding.javaClassName} ${binding.name}Binding#end) { | ||
| 379 | if(${entity.prefixClassNameWithoutPackage}.LOG.isDebugEnabled()) { | ||
| 380 | ${entity.prefixClassNameWithoutPackage}.LOG.debug("fetchOne${fetchSpecification.capitalizedName}(ec " + ec#foreach ($binding in $fetchSpecification.distinctBindings) + "," | ||
| 381 | + "${binding.name}Binding " + ${binding.name}Binding#end + ")"); | ||
| 382 | } | ||
| 383 | ${entity.classNameWithOptionalPackage} result = null; | ||
| 384 | #if ($fetchSpecification.distinctBindings.size() > 0) | ||
| 385 | NSMutableDictionary<Object, String> bindings = new NSMutableDictionary<Object, String>(); | ||
| 386 | #foreach ($binding in $fetchSpecification.distinctBindings) | ||
| 387 | bindings.takeValueForKey(${binding.name}Binding, "${binding.name}"); | ||
| 388 | #end | ||
| 389 | #end | ||
| 390 | try { | ||
| 391 | result = (${entity.classNameWithOptionalPackage}) EOUtilities.objectWithFetchSpecificationAndBindings(ec, ${entity.name}.ENTITY_NAME, "${fetchSpecification.name}", #if ($fetchSpecification.distinctBindings.size() > 0) bindings #else null #end); | ||
| 392 | } catch ( EOObjectNotAvailableException e ) { | ||
| 393 | //Do Nothing. | ||
| 394 | } | ||
| 395 | return result; | ||
| 396 | } | ||
| 397 | |||
| 398 | |||
| 399 | #end | ||
| 400 | } | ||
| 401 | |||
| 402 | {{/code}} |