Search in sources :

Example 11 with GenericHelper

use of org.apache.ofbiz.entity.datasource.GenericHelper in project ofbiz-framework by apache.

the class GenericDelegator method removeByPrimaryKey.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#removeByPrimaryKey(org.apache.ofbiz.entity.GenericPK)
     */
@Override
public int removeByPrimaryKey(GenericPK primaryKey) throws GenericEntityException {
    boolean beganTransaction = false;
    try {
        if (alwaysUseTransaction) {
            beganTransaction = TransactionUtil.begin();
        }
        EntityEcaRuleRunner<?> ecaRunner = this.getEcaRuleRunner(primaryKey.getEntityName());
        ecaRunner.evalRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_REMOVE, primaryKey, false);
        GenericHelper helper = getEntityHelper(primaryKey.getEntityName());
        ecaRunner.evalRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_REMOVE, primaryKey, false);
        // if audit log on for any fields, save old value before removing so it's still there
        if (primaryKey.getModelEntity().getHasFieldWithAuditLog()) {
            createEntityAuditLogAll(this.findOne(primaryKey.getEntityName(), primaryKey, false), true, true);
        }
        GenericValue removedEntity = null;
        if (testMode) {
            removedEntity = this.findOne(primaryKey.getEntityName(), primaryKey, false);
        }
        int num = helper.removeByPrimaryKey(primaryKey);
        // doCacheClear
        ecaRunner.evalRules(EntityEcaHandler.EV_CACHE_CLEAR, EntityEcaHandler.OP_REMOVE, primaryKey, false);
        this.clearCacheLine(primaryKey);
        this.saveEntitySyncRemoveInfo(primaryKey);
        if (testMode) {
            if (removedEntity != null) {
                storeForTestRollback(new TestOperation(OperationType.DELETE, removedEntity));
            }
        }
        ecaRunner.evalRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_REMOVE, primaryKey, false);
        TransactionUtil.commit(beganTransaction);
        return num;
    } catch (IllegalStateException | GenericEntityException e) {
        String errMsg = "Failure in removeByPrimaryKey operation for entity [" + primaryKey.getEntityName() + "]: " + e.toString() + ". Rolling back transaction.";
        Debug.logError(e, errMsg, module);
        TransactionUtil.rollback(beganTransaction, errMsg, e);
        throw new GenericEntityException(e);
    }
}
Also used : GenericHelper(org.apache.ofbiz.entity.datasource.GenericHelper)

Example 12 with GenericHelper

use of org.apache.ofbiz.entity.datasource.GenericHelper in project ofbiz-framework by apache.

the class GenericDelegator method removeValue.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#removeValue(org.apache.ofbiz.entity.GenericValue)
     */
@Override
public int removeValue(GenericValue value) 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();
        }
        EntityEcaRuleRunner<?> ecaRunner = this.getEcaRuleRunner(value.getEntityName());
        ecaRunner.evalRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_REMOVE, value, false);
        GenericHelper helper = getEntityHelper(value.getEntityName());
        ecaRunner.evalRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_REMOVE, value, false);
        // if audit log on for any fields, save old value before actual remove
        if (value.getModelEntity().getHasFieldWithAuditLog()) {
            createEntityAuditLogAll(value, true, true);
        }
        GenericValue removedValue = null;
        if (testMode) {
            removedValue = this.findOne(value.getEntityName(), value.getPrimaryKey(), false);
        }
        int num = helper.removeByPrimaryKey(value.getPrimaryKey());
        // Need to call removedFromDatasource() here because the helper calls removedFromDatasource() on the PK instead of the GenericEntity.
        value.removedFromDatasource();
        // doCacheClear
        ecaRunner.evalRules(EntityEcaHandler.EV_CACHE_CLEAR, EntityEcaHandler.OP_REMOVE, value, false);
        this.clearCacheLine(value);
        if (testMode) {
            if (removedValue != null) {
                storeForTestRollback(new TestOperation(OperationType.DELETE, removedValue));
            }
        }
        this.saveEntitySyncRemoveInfo(value.getPrimaryKey());
        ecaRunner.evalRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_REMOVE, value, false);
        TransactionUtil.commit(beganTransaction);
        return num;
    } catch (IllegalStateException | GenericEntityException e) {
        String errMsg = "Failure in removeValue operation for entity [" + value.getEntityName() + "]: " + e.toString() + ". Rolling back transaction.";
        Debug.logError(e, errMsg, module);
        TransactionUtil.rollback(beganTransaction, errMsg, e);
        throw new GenericEntityException(e);
    }
}
Also used : GenericHelper(org.apache.ofbiz.entity.datasource.GenericHelper)

Example 13 with GenericHelper

use of org.apache.ofbiz.entity.datasource.GenericHelper in project ofbiz-framework by apache.

the class GenericDelegator method createSetNextSeqId.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#createSetNextSeqId(org.apache.ofbiz.entity.GenericValue)
     */
@Override
public GenericValue createSetNextSeqId(GenericValue value) throws GenericEntityException {
    if (value == null) {
        throw new GenericEntityException("Cannot create a null value");
    }
    GenericHelper helper = getEntityHelper(value.getEntityName());
    // just make sure it is this delegator...
    value.setDelegator(this);
    // this will throw an IllegalArgumentException if the entity for the value does not have one pk field, or if it already has a value set for the one pk field
    value.setNextSeqId();
    boolean beganTransaction = false;
    try {
        if (alwaysUseTransaction) {
            beganTransaction = TransactionUtil.begin();
        }
        EntityEcaRuleRunner<?> ecaRunner = this.getEcaRuleRunner(value.getEntityName());
        ecaRunner.evalRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_CREATE, value, false);
        ecaRunner.evalRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_CREATE, value, false);
        value.setDelegator(this);
        // if audit log on for any fields, save new value with no old value because it's a create
        if (value.getModelEntity().getHasFieldWithAuditLog()) {
            createEntityAuditLogAll(value, false, false);
        }
        try {
            value = helper.create(value);
            if (testMode) {
                storeForTestRollback(new TestOperation(OperationType.INSERT, value));
            }
        } catch (IllegalStateException | GenericEntityException e) {
            // see if this was caused by an existing record before resetting the sequencer and trying again
            // NOTE: use the helper directly so ECA rules, etc won't be run
            GenericValue existingValue = null;
            try {
                existingValue = helper.findByPrimaryKey(value.getPrimaryKey());
            } catch (GenericEntityException e1) {
            // ignore this error, if not found it'll probably be a GenericEntityNotFoundException
            // it is important to not let this get thrown because it will mask the original exception
            }
            if (existingValue == null) {
                throw e;
            }
            if (Debug.infoOn()) {
                Debug.logInfo("Error creating entity record with a sequenced value [" + value.getPrimaryKey() + "], trying again about to refresh bank for entity [" + value.getEntityName() + "]", module);
            }
            // found an existing value... was probably a duplicate key, so clean things up and try again
            this.AtomicRefSequencer.get().forceBankRefresh(value.getEntityName(), 1);
            value.setNextSeqId();
            value = helper.create(value);
            if (Debug.infoOn()) {
                Debug.logInfo("Successfully created new entity record on retry with a sequenced value [" + value.getPrimaryKey() + "], after getting refreshed bank for entity [" + value.getEntityName() + "]", module);
            }
            if (testMode) {
                storeForTestRollback(new TestOperation(OperationType.INSERT, value));
            }
        }
        if (value != null) {
            value.setDelegator(this);
            if (value.lockEnabled()) {
                refresh(value);
            } else {
                // doCacheClear
                ecaRunner.evalRules(EntityEcaHandler.EV_CACHE_CLEAR, EntityEcaHandler.OP_CREATE, value, false);
                this.clearCacheLine(value);
            }
            ecaRunner.evalRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_CREATE, value, false);
        }
        TransactionUtil.commit(beganTransaction);
        return value;
    } catch (GenericEntityException e) {
        String entityName = value != null ? value.getEntityName() : "invalid Generic Value";
        String errMsg = "Failure in createSetNextSeqId operation for entity [" + entityName + "]: " + e.toString() + ". Rolling back transaction.";
        Debug.logError(e, errMsg, module);
        TransactionUtil.rollback(beganTransaction, errMsg, e);
        throw new GenericEntityException(e);
    }
}
Also used : GenericHelper(org.apache.ofbiz.entity.datasource.GenericHelper)

Example 14 with GenericHelper

use of org.apache.ofbiz.entity.datasource.GenericHelper in project ofbiz-framework by apache.

the class GenericDelegator method create.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#create(org.apache.ofbiz.entity.GenericValue)
     */
@Override
public GenericValue create(GenericValue value) throws GenericEntityException {
    boolean beganTransaction = false;
    try {
        if (alwaysUseTransaction) {
            beganTransaction = TransactionUtil.begin();
        }
        if (value == null) {
            throw new GenericEntityException("Cannot create a null value");
        }
        EntityEcaRuleRunner<?> ecaRunner = this.getEcaRuleRunner(value.getEntityName());
        ecaRunner.evalRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_CREATE, value, false);
        GenericHelper helper = getEntityHelper(value.getEntityName());
        ecaRunner.evalRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_CREATE, value, false);
        value.setDelegator(this);
        // if audit log on for any fields, save new value with no old value because it's a create
        if (value.getModelEntity().getHasFieldWithAuditLog()) {
            createEntityAuditLogAll(value, false, false);
        }
        value = helper.create(value);
        if (testMode) {
            storeForTestRollback(new TestOperation(OperationType.INSERT, value));
        }
        if (value != null) {
            value.setDelegator(this);
            if (value.lockEnabled()) {
                refresh(value);
            } else {
                // doCacheClear
                ecaRunner.evalRules(EntityEcaHandler.EV_CACHE_CLEAR, EntityEcaHandler.OP_CREATE, value, false);
                this.clearCacheLine(value);
            }
            ecaRunner.evalRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_CREATE, value, false);
        }
        TransactionUtil.commit(beganTransaction);
        return value;
    } catch (IllegalStateException | GenericEntityException e) {
        String errMsg = "Failure in create operation for entity [" + (value != null ? value.getEntityName() : "value is null") + "]: " + e.toString() + ". Rolling back transaction.";
        Debug.logError(errMsg, module);
        TransactionUtil.rollback(beganTransaction, errMsg, e);
        throw new GenericEntityException(e);
    }
}
Also used : GenericHelper(org.apache.ofbiz.entity.datasource.GenericHelper)

Example 15 with GenericHelper

use of org.apache.ofbiz.entity.datasource.GenericHelper in project ofbiz-framework by apache.

the class GenericDelegator method storeAll.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#storeAll(java.util.List, org.apache.ofbiz.entity.util.EntityStoreOptions)
     */
@Override
public int storeAll(List<GenericValue> values, EntityStoreOptions storeOptions) throws GenericEntityException {
    if (values == null) {
        return 0;
    }
    // if no store options passed, use default
    if (storeOptions == null) {
        storeOptions = new EntityStoreOptions();
    }
    int numberChanged = 0;
    boolean beganTransaction = false;
    try {
        beganTransaction = TransactionUtil.begin();
        for (GenericValue value : values) {
            String entityName = value.getEntityName();
            GenericPK primaryKey = value.getPrimaryKey();
            GenericHelper helper = getEntityHelper(entityName);
            // 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);
            } catch (GenericEntityNotFoundException e) {
                existing = null;
            }
            if (existing == null) {
                if (storeOptions.isCreateDummyFks()) {
                    value.checkFks(true);
                }
                this.create(value);
                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(this, modelEntity, value.getPrimaryKey());
                boolean atLeastOneField = false;
                Iterator<ModelField> nonPksIter = modelEntity.getNopksIterator();
                while (nonPksIter.hasNext()) {
                    ModelField modelField = nonPksIter.next();
                    String fieldName = modelField.getName();
                    if (value.containsKey(fieldName)) {
                        Object fieldValue = value.get(fieldName);
                        Object oldValue = existing.get(fieldName);
                        if (!UtilObject.equalsHelper(oldValue, fieldValue)) {
                            toStore.put(fieldName, fieldValue);
                            atLeastOneField = true;
                        }
                    }
                }
                if (atLeastOneField) {
                    if (storeOptions.isCreateDummyFks()) {
                        value.checkFks(true);
                    }
                    numberChanged += this.store(toStore);
                }
            }
        }
        TransactionUtil.commit(beganTransaction);
        return numberChanged;
    } catch (GenericEntityException e) {
        String errMsg = "Failure in storeAll operation: " + e.toString() + ". Rolling back transaction.";
        Debug.logError(e, errMsg, module);
        TransactionUtil.rollback(beganTransaction, errMsg, e);
        throw new GenericEntityException(e);
    }
}
Also used : GenericHelper(org.apache.ofbiz.entity.datasource.GenericHelper) EntityStoreOptions(org.apache.ofbiz.entity.util.EntityStoreOptions) ModelField(org.apache.ofbiz.entity.model.ModelField) UtilObject(org.apache.ofbiz.base.util.UtilObject) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Aggregations

GenericHelper (org.apache.ofbiz.entity.datasource.GenericHelper)16 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)7 IOException (java.io.IOException)3 SAXException (org.xml.sax.SAXException)3 ExecutionException (java.util.concurrent.ExecutionException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 GeneralRuntimeException (org.apache.ofbiz.base.util.GeneralRuntimeException)2 ModelField (org.apache.ofbiz.entity.model.ModelField)2 SerializeException (org.apache.ofbiz.entity.serialize.SerializeException)2 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)2 NodeModel (freemarker.ext.dom.NodeModel)1 Configuration (freemarker.template.Configuration)1 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 TemplateHashModel (freemarker.template.TemplateHashModel)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 StringWriter (java.io.StringWriter)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1