Author: doogie
Date: Wed Oct 17 16:51:41 2007 New Revision: 585759 URL: http://svn.apache.org/viewvc?rev=585759&view=rev Log: Lots of entities have a single field as their primary key. Add some find variants that allow for specifying just the value for database lookup. Closes https://issues.apache.org/jira/browse/OFBIZ-1292 Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/DelegatorInterface.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericDelegator.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericPK.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/DelegatorInterface.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/DelegatorInterface.java?rev=585759&r1=585758&r2=585759&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/DelegatorInterface.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/DelegatorInterface.java Wed Oct 17 16:51:41 2007 @@ -69,12 +69,18 @@ GenericValue makeValue(String entityName, Map fields); + GenericValue makeValueSingle(String entityName, Object singlePkValue); + GenericValue makeValidValue(String entityName, Map fields); GenericPK makePK(String entityName, Map fields); + GenericPK makePKSingle(String entityName, Object singlePkValue); + GenericValue create(String entityName, Map fields) throws GenericEntityException; + GenericValue createSingle(String entityName, Object singlePkValue) throws GenericEntityException; + GenericValue create(GenericValue value) throws GenericEntityException; GenericValue create(GenericValue value, boolean doCacheClear) throws GenericEntityException; @@ -93,7 +99,11 @@ GenericValue findByPrimaryKey(String entityName, Map fields) throws GenericEntityException; + GenericValue findByPrimaryKeySingle(String entityName, Object singlePkValue) throws GenericEntityException; + GenericValue findByPrimaryKeyCache(String entityName, Map fields) throws GenericEntityException; + + GenericValue findByPrimaryKeyCacheSingle(String entityName, Object singlePkValue) throws GenericEntityException; GenericValue findByPrimaryKeyPartial(GenericPK primaryKey, Set keys) throws GenericEntityException; Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericDelegator.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericDelegator.java?rev=585759&r1=585758&r2=585759&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericDelegator.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericDelegator.java Wed Oct 17 16:51:41 2007 @@ -444,6 +444,17 @@ return value; } + /** Creates a Entity in the form of a GenericValue without persisting it */ + public GenericValue makeValueSingle(String entityName, Object singlePkValue) { + ModelEntity entity = this.getModelEntity(entityName); + if (entity == null) { + throw new IllegalArgumentException("[GenericDelegator.makeValue] could not find entity for entityName: " + entityName); + } + GenericValue value = GenericValue.create(entity, singlePkValue); + value.setDelegator(this); + return value; + } + /** Creates a Entity in the form of a GenericValue without persisting it; only valid fields will be pulled from the fields Map */ public GenericValue makeValidValue(String entityName, Map fields) { ModelEntity entity = this.getModelEntity(entityName); @@ -469,6 +480,18 @@ return pk; } + /** Creates a Primary Key in the form of a GenericPK without persisting it */ + public GenericPK makePKSingle(String entityName, Object singlePkValue) { + ModelEntity entity = this.getModelEntity(entityName); + if (entity == null) { + throw new IllegalArgumentException("[GenericDelegator.makePK] could not find entity for entityName: " + entityName); + } + GenericPK pk = GenericPK.create(entity, singlePkValue); + + pk.setDelegator(this); + return pk; + } + /** Creates a Entity in the form of a GenericValue and write it to the datasource *@param primaryKey The GenericPK to create a value in the datasource from *@return GenericValue instance containing the new instance @@ -503,6 +526,19 @@ return this.create(genericValue, true); } + /** Creates a Entity in the form of a GenericValue and write it to the database + *@return GenericValue instance containing the new instance + */ + public GenericValue createSingle(String entityName, Object singlePkValue) throws GenericEntityException { + if (entityName == null || singlePkValue == null) { + return null; + } + ModelEntity entity = this.getModelReader().getModelEntity(entityName); + GenericValue genericValue = GenericValue.create(entity, singlePkValue); + + return this.create(genericValue, true); + } + /** Creates a Entity in the form of a GenericValue and write it to the datasource *@param value The GenericValue to create a value in the datasource from *@return GenericValue instance containing the new instance @@ -1353,6 +1389,15 @@ return findByPrimaryKey(makePK(entityName, fields)); } + /** Find a Generic Entity by its Primary Key + *@param entityName The Name of the Entity as defined in the entity XML file + *@param singlePkValue + *@return The GenericValue corresponding to the primaryKey + */ + public GenericValue findByPrimaryKeySingle(String entityName, Object singlePkValue) throws GenericEntityException { + return findByPrimaryKey(makePKSingle(entityName, singlePkValue)); + } + /** Find a CACHED Generic Entity by its Primary Key *@param entityName The Name of the Entity as defined in the entity XML file *@param fields The fields of the named entity to query by with their corresponging values @@ -1362,6 +1407,15 @@ return findByPrimaryKeyCache(makePK(entityName, fields)); } + /** Find a CACHED Generic Entity by its Primary Key + *@param entityName The Name of the Entity as defined in the entity XML file + *@param singlePkValue + *@return The GenericValue corresponding to the primaryKey + */ + public GenericValue findByPrimaryKeyCacheSingle(String entityName, Object singlePkValue) throws GenericEntityException { + return findByPrimaryKeyCache(makePKSingle(entityName, singlePkValue)); + } + /** Find a Generic Entity by its Primary Key and only returns the values requested by the passed keys (names) *@param primaryKey The primary key to find by. *@param keys The keys, or names, of the values to retrieve; only these values will be retrieved @@ -1892,6 +1946,10 @@ eli.setDelegator(this); //TODO: add decrypt fields return eli; + } + + public long findCountByAnd(String entityName) throws GenericEntityException { + return findCountByAnd(entityName, (Map<String, Object>) null); } public long findCountByAnd(String entityName, Map fields) throws GenericEntityException { Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java?rev=585759&r1=585758&r2=585759&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java Wed Oct 17 16:51:41 2007 @@ -167,6 +167,24 @@ } } + /** Creates new GenericEntity from existing Map */ + protected void init(ModelEntity modelEntity, Object singlePkValue) { + if (modelEntity == null) { + throw new IllegalArgumentException("Cannont create a GenericEntity with a null modelEntity parameter"); + } + if (modelEntity.getPksSize() != 1) { + throw new IllegalArgumentException("Cannot create a GenericEntity with more than one primary key field"); + } + this.modelEntity = modelEntity; + this.entityName = modelEntity.getEntityName(); + set(modelEntity.getOnlyPk().getName(), singlePkValue); + + // check some things + if (this.entityName == null) { + throw new IllegalArgumentException("Cannont create a GenericEntity with a null entityName in the modelEntity parameter"); + } + } + /** Copy Constructor: Creates new GenericEntity from existing GenericEntity */ protected void init(GenericEntity value) { if (value.modelEntity == null) { Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericPK.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericPK.java?rev=585759&r1=585758&r2=585759&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericPK.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericPK.java Wed Oct 17 16:51:41 2007 @@ -52,6 +52,13 @@ return newPK; } + /** Creates new GenericPK from existing Map */ + public static GenericPK create(ModelEntity modelEntity, Object singlePkValue) { + GenericPK newPK = (GenericPK) genericPKFactory.object(); + newPK.init(modelEntity, singlePkValue); + return newPK; + } + /** Creates new GenericPK from existing GenericPK */ public static GenericPK create(GenericPK value) { GenericPK newPK = (GenericPK) genericPKFactory.object(); Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java?rev=585759&r1=585758&r2=585759&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java Wed Oct 17 16:51:41 2007 @@ -79,6 +79,13 @@ return newValue; } + /** Creates new GenericValue from existing Map */ + public static GenericValue create(ModelEntity modelEntity, Object singlePkValue) { + GenericValue newValue = (GenericValue) genericValueFactory.object(); + newValue.init(modelEntity, singlePkValue); + return newValue; + } + /** Creates new GenericValue from existing GenericValue */ public static GenericValue create(GenericValue value) { GenericValue newValue = (GenericValue) genericValueFactory.object(); |
Free forum by Nabble | Edit this page |