📄 genericdelegator.java
字号:
/** Creates a Entity in the form of a GenericValue and write it to the database *@return GenericValue instance containing the new instance */ public GenericValue create(String entityName, Map fields) throws GenericEntityException { if (entityName == null || fields == null) { return null; } ModelEntity entity = this.getModelReader().getModelEntity(entityName); GenericValue genericValue = GenericValue.create(entity, fields); 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 */ public GenericValue create(GenericValue value) throws GenericEntityException { return this.create(value, 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 *@param doCacheClear boolean that specifies whether or not to automatically clear cache entries related to this operation *@return GenericValue instance containing the new instance */ public GenericValue create(GenericValue value, boolean doCacheClear) throws GenericEntityException { boolean beganTransaction = false; try { if (alwaysUseTransaction) { beganTransaction = TransactionUtil.begin(); } Map ecaEventMap = this.getEcaEntityEventMap(value.getEntityName()); this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_CREATE, value, ecaEventMap, (ecaEventMap == null), false); if (value == null) { throw new GenericEntityException("Cannot create a null value"); } GenericHelper helper = getEntityHelper(value.getEntityName()); this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_CREATE, value, ecaEventMap, (ecaEventMap == null), false); value.setDelegator(this); this.encryptFields(value); value = helper.create(value); if (value != null) { value.setDelegator(this); if (value.lockEnabled()) { refresh(value, doCacheClear); } else { if (doCacheClear) { this.evalEcaRules(EntityEcaHandler.EV_CACHE_CLEAR, EntityEcaHandler.OP_CREATE, value, ecaEventMap, (ecaEventMap == null), false); this.clearCacheLine(value); } } } this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_CREATE, value, ecaEventMap, (ecaEventMap == null), false); return value; } catch (GenericEntityException e) { String errMsg = "Failure in create operation for entity [" + value.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); } } /** Creates or stores an Entity *@param value The GenericValue instance containing the new or existing instance *@param doCacheClear boolean that specifies whether or not to automatically clear cache entries related to this operation *@return GenericValue instance containing the new or updated instance */ public GenericValue createOrStore(GenericValue value, boolean doCacheClear) throws GenericEntityException { boolean beganTransaction = false; try { if (alwaysUseTransaction) { beganTransaction = TransactionUtil.begin(); } GenericValue checkValue = this.findByPrimaryKey(value.getPrimaryKey()); if (checkValue != null) { this.store(value, doCacheClear); } else { this.create(value, doCacheClear); } if (value.lockEnabled()) { this.refresh(value); } return value; } catch (GenericEntityException e) { String errMsg = "Failure in createOrStore operation for entity [" + value.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); } } /** Creates or stores an Entity *@param value The GenericValue instance containing the new or existing instance *@return GenericValue instance containing the new or updated instance */ public GenericValue createOrStore(GenericValue value) throws GenericEntityException { return createOrStore(value, true); } protected void saveEntitySyncRemoveInfo(GenericEntity dummyPK) throws GenericEntityException { // don't store remove info on entities where it is disabled if (dummyPK.getModelEntity().getNoAutoStamp()) { return; } // don't store remove info on things removed on an entity sync if (dummyPK.getIsFromEntitySync()) { return; } String serializedPK = null; try { serializedPK = XmlSerializer.serialize(dummyPK); } catch (SerializeException e) { Debug.logError(e, "Could not serialize primary key to save EntitySyncRemove", module); } catch (FileNotFoundException e) { Debug.logError(e, "Could not serialize primary key to save EntitySyncRemove", module); } catch (IOException e) { Debug.logError(e, "Could not serialize primary key to save EntitySyncRemove", module); } if (serializedPK != null) { GenericValue entitySyncRemove = this.makeValue("EntitySyncRemove", null); entitySyncRemove.set("entitySyncRemoveId", this.getNextSeqId("EntitySyncRemove")); entitySyncRemove.set("primaryKeyRemoved", serializedPK); entitySyncRemove.create(); } } /** Remove a Generic Entity corresponding to the primaryKey *@param primaryKey The primary key of the entity to remove. *@return int representing number of rows effected by this operation */ public int removeByPrimaryKey(GenericPK primaryKey) throws GenericEntityException { int retVal = this.removeByPrimaryKey(primaryKey, true); return retVal; } /** Remove a Generic Entity corresponding to the primaryKey *@param primaryKey The primary key of the entity to remove. *@param doCacheClear boolean that specifies whether to clear cache entries for this primaryKey to be removed *@return int representing number of rows effected by this operation */ public int removeByPrimaryKey(GenericPK primaryKey, boolean doCacheClear) throws GenericEntityException { boolean beganTransaction = false; try { if (alwaysUseTransaction) { beganTransaction = TransactionUtil.begin(); } Map ecaEventMap = this.getEcaEntityEventMap(primaryKey.getEntityName()); this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_REMOVE, primaryKey, ecaEventMap, (ecaEventMap == null), false); GenericHelper helper = getEntityHelper(primaryKey.getEntityName()); if (doCacheClear) { // always clear cache before the operation this.evalEcaRules(EntityEcaHandler.EV_CACHE_CLEAR, EntityEcaHandler.OP_REMOVE, primaryKey, ecaEventMap, (ecaEventMap == null), false); this.clearCacheLine(primaryKey); } this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_REMOVE, primaryKey, ecaEventMap, (ecaEventMap == null), false); int num = helper.removeByPrimaryKey(primaryKey); this.saveEntitySyncRemoveInfo(primaryKey); this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_REMOVE, primaryKey, ecaEventMap, (ecaEventMap == null), false); return num; } catch (GenericEntityException e) { String errMsg = "Failure in removeByPrimaryKey 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); } } /** Remove a Generic Value from the database *@param value The GenericValue object of the entity to remove. *@return int representing number of rows effected by this operation */ public int removeValue(GenericValue value) throws GenericEntityException { return this.removeValue(value, true); } /** Remove a Generic Value from the database *@param value The GenericValue object of the entity to remove. *@param doCacheClear boolean that specifies whether to clear cache entries for this value to be removed *@return int representing number of rows effected by this operation */ public int removeValue(GenericValue value, boolean doCacheClear) throws GenericEntityException { // NOTE: this does not call the GenericDelegator.removeByPrimaryKey method because it has more information to pass to the ECA rule hander boolean beganTransaction = false; try { if (alwaysUseTransaction) { beganTransaction = TransactionUtil.begin(); } Map ecaEventMap = this.getEcaEntityEventMap(value.getEntityName()); this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_REMOVE, value, ecaEventMap, (ecaEventMap == null), false); GenericHelper helper = getEntityHelper(value.getEntityName()); if (doCacheClear) { this.evalEcaRules(EntityEcaHandler.EV_CACHE_CLEAR, EntityEcaHandler.OP_REMOVE, value, ecaEventMap, (ecaEventMap == null), false); this.clearCacheLine(value); } this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_REMOVE, value, ecaEventMap, (ecaEventMap == null), false); int num = helper.removeByPrimaryKey(value.getPrimaryKey()); this.saveEntitySyncRemoveInfo(value.getPrimaryKey()); this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_REMOVE, value, ecaEventMap, (ecaEventMap == null), false); return num; } catch (GenericEntityException e) { String errMsg = "Failure in removeValue operation for entity [" + value.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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -