📄 genericdelegator.java
字号:
* either does an insert or an update as appropriate. * <br/>These updates all happen in one transaction, so they will either all succeed or all fail, * if the data source supports transactions. This is just like to othersToStore feature * of the GenericEntity on a create or store. *@param values List of GenericValue instances containing the entities to store *@param doCacheClear boolean that specifies whether or not to automatically clear cache entries related to this operation *@return int representing number of rows effected by this operation */ public int storeAll(List values, boolean doCacheClear) throws GenericEntityException { return this.storeAll(values, doCacheClear, false); } /** Store the Entities from the List GenericValue instances to the persistent store. * <br/>This is different than the normal store method in that the store method only does * an update, while the storeAll method checks to see if each entity exists, then * either does an insert or an update as appropriate. * <br/>These updates all happen in one transaction, so they will either all succeed or all fail, * if the data source supports transactions. This is just like to othersToStore feature * of the GenericEntity on a create or store. *@param values List of GenericValue instances containing the entities to store *@param doCacheClear boolean that specifies whether or not to automatically clear cache entries related to this operation *@param createDummyFks boolean that specifies whether or not to automatically create "dummy" place holder FKs *@return int representing number of rows effected by this operation */ public int storeAll(List values, boolean doCacheClear, boolean createDummyFks) throws GenericEntityException { if (values == null) { return 0; } int numberChanged = 0; boolean beganTransaction = false; try { beganTransaction = TransactionUtil.begin(); Iterator viter = values.iterator(); while (viter.hasNext()) { GenericValue value = (GenericValue) viter.next(); String entityName = value.getEntityName(); GenericPK primaryKey = value.getPrimaryKey(); Map ecaEventMap = this.getEcaEntityEventMap(entityName); GenericHelper helper = getEntityHelper(entityName); // exists? // NOTE: don't use findByPrimaryKey because we don't want to the ECA events to fire and such if (!primaryKey.isPrimaryKey()) { throw new GenericModelException("[GenericDelegator.storeAll] One of the passed primary keys is not a valid primary key: " + primaryKey); } GenericValue existing = null; try { existing = helper.findByPrimaryKey(primaryKey); this.decryptFields(existing); } catch (GenericEntityNotFoundException e) { existing = null; } if (existing == null) { if (createDummyFks) { value.checkFks(true); } this.create(value, doCacheClear); numberChanged++; } else { // don't send fields that are the same, and if no fields have changed, update nothing ModelEntity modelEntity = value.getModelEntity(); GenericValue toStore = GenericValue.create(modelEntity, value.getPrimaryKey()); toStore.setDelegator(this); boolean atLeastOneField = false; Iterator nonPksIter = modelEntity.getNopksIterator(); while (nonPksIter.hasNext()) { ModelField modelField = (ModelField) nonPksIter.next(); String fieldName = modelField.getName(); if (value.containsKey(fieldName)) { Object fieldValue = value.get(fieldName); Object oldValue = existing.get(fieldName); if ((fieldValue == null && oldValue != null) || (fieldValue != null && !fieldValue.equals(oldValue))) { toStore.put(fieldName, fieldValue); atLeastOneField = true; } } } if (atLeastOneField) { if (createDummyFks) { value.checkFks(true); } numberChanged += this.store(toStore, doCacheClear); } } } return numberChanged; } catch (GenericEntityException e) { String errMsg = "Failure in storeAll operation: " + e.toString() + ". Rolling back transaction."; Debug.logError(e, errMsg, module); try { // only rollback the transaction if we started one... TransactionUtil.rollback(beganTransaction, errMsg, e); } catch (GenericEntityException e2) { Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); } // after rolling back, rethrow the exception throw e; } finally { // only commit the transaction if we started one... this will throw an exception if it fails TransactionUtil.commit(beganTransaction); } } /** Remove the Entities from the List from the persistent store. * <br/>The List contains GenericEntity objects, can be either GenericPK or GenericValue. * <br/>If a certain entity contains a complete primary key, the entity in the datasource corresponding * to that primary key will be removed, this is like a removeByPrimary Key. * <br/>On the other hand, if a certain entity is an incomplete or non primary key, * if will behave like the removeByAnd method. * <br/>These updates all happen in one transaction, so they will either all succeed or all fail, * if the data source supports transactions. *@param dummyPKs Collection of GenericEntity instances containing the entities or by and fields to remove *@return int representing number of rows effected by this operation */ public int removeAll(List dummyPKs) throws GenericEntityException { return this.removeAll(dummyPKs, true); } /** Remove the Entities from the List from the persistent store. * <br/>The List contains GenericEntity objects, can be either GenericPK or GenericValue. * <br/>If a certain entity contains a complete primary key, the entity in the datasource corresponding * to that primary key will be removed, this is like a removeByPrimary Key. * <br/>On the other hand, if a certain entity is an incomplete or non primary key, * if will behave like the removeByAnd method. * <br/>These updates all happen in one transaction, so they will either all succeed or all fail, * if the data source supports transactions. *@param dummyPKs Collection of GenericEntity instances containing the entities or by and fields to remove *@param doCacheClear boolean that specifies whether or not to automatically clear cache entries related to this operation *@return int representing number of rows effected by this operation */ public int removeAll(List dummyPKs, boolean doCacheClear) throws GenericEntityException { if (dummyPKs == null) { return 0; } boolean beganTransaction = false; int numRemoved = 0; try { Iterator viter = dummyPKs.iterator(); while (viter.hasNext()) { GenericEntity value = (GenericEntity) viter.next(); if (value.containsPrimaryKey()) { numRemoved += this.removeByPrimaryKey(value.getPrimaryKey(), doCacheClear); } else { numRemoved += this.removeByAnd(value.getEntityName(), value.getAllFields(), doCacheClear); } } return numRemoved; } catch (GenericEntityException e) { String errMsg = "Failure in removeAll operation: " + e.toString() + ". Rolling back transaction."; Debug.logError(e, errMsg, module); try { // only rollback the transaction if we started one... TransactionUtil.rollback(beganTransaction, errMsg, e); } catch (GenericEntityException e2) { Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); } // after rolling back, rethrow the exception throw e; } finally { // only commit the transaction if we started one... this will throw an exception if it fails TransactionUtil.commit(beganTransaction); } } // ====================================== // ======= Find Methods ================= // ====================================== /** Find a Generic Entity by its Primary Key *@param primaryKey The primary key to find by. *@return The GenericValue corresponding to the primaryKey */ public GenericValue findByPrimaryKey(GenericPK primaryKey) throws GenericEntityException { boolean beganTransaction = false; try { if (alwaysUseTransaction) { beganTransaction = TransactionUtil.begin(); } Map ecaEventMap = this.getEcaEntityEventMap(primaryKey.getEntityName()); this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false); GenericHelper helper = getEntityHelper(primaryKey.getEntityName()); GenericValue value = null; if (!primaryKey.isPrimaryKey()) { throw new GenericModelException("[GenericDelegator.findByPrimaryKey] Passed primary key is not a valid primary key: " + primaryKey); } this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false); try { value = helper.findByPrimaryKey(primaryKey); } catch (GenericEntityNotFoundException e) { value = null; } if (value != null) { value.setDelegator(this); this.decryptFields(value); } this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false); return value; } catch (GenericEntityException e) { String errMsg = "Failure in findByPrimaryKey operation for entity [" + primaryKey.getEntityName() + "]: " + e.toString() + ". Rolling back transaction."; Debug.logError(e, errMsg, module); try { // only rollback the transaction if we started one... TransactionUtil.rollback(beganTransaction, errMsg, e); } catch (GenericEntityException e2) { Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); } // after rolling back, rethrow the exception throw e; } finally { // only commit the transaction if we started one... this will throw an exception if it fails TransactionUtil.commit(beganTransaction); } } /** Find a CACHED Generic Entity by its Primary Key *@param primaryKey The primary key to find by. *@return The GenericValue corresponding to the primaryKey */ public GenericValue findByPrimaryKeyCache(GenericPK primaryKey) throws GenericEntityException { Map ecaEventMap = this.getEcaEntityEventMap(primaryKey.getEntityName()); this.evalEcaRules(EntityEcaHandler.EV_CACHE_CHECK, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false); GenericValue value = this.getFromPrimaryKeyCache(primaryKey); if (value instanceof GenericEntity.NULL) return null; if (value == null) { value = findByPrimaryKey(primaryKey); if (value != null) { this.evalEcaRules(EntityEcaHandler.EV_CACHE_PUT, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false); this.putInPrimaryKeyCache(primaryKey, value); } else { this.putInPrimaryKeyCache(primaryKey, GenericValue.NULL_VALUE); } } return value; } /** Find a 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 *@return The GenericValue corresponding to the primaryKey */ public GenericValue findByPrimaryKey(String entityName, Map fields) throws GenericEntityException { return findByPrimaryKey(makePK(entityName, fields)); } /** Find a CACHED Generic Entity by its Primary Key *@param entityName
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -