Search in sources :

Example 81 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class GenericDelegator method storeByCondition.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#storeByCondition(java.lang.String, java.util.Map, org.apache.ofbiz.entity.condition.EntityCondition)
     */
@Override
public int storeByCondition(String entityName, Map<String, ? extends Object> fieldsToSet, EntityCondition condition) throws GenericEntityException {
    boolean beganTransaction = false;
    try {
        if (alwaysUseTransaction) {
            beganTransaction = TransactionUtil.begin();
        }
        ModelEntity modelEntity = getModelReader().getModelEntity(entityName);
        GenericHelper helper = getEntityHelper(entityName);
        List<GenericValue> updatedEntities = null;
        if (testMode) {
            updatedEntities = this.findList(entityName, condition, null, null, null, false);
        }
        int rowsAffected = helper.storeByCondition(this, modelEntity, fieldsToSet, condition);
        if (rowsAffected > 0) {
            this.clearCacheLine(entityName);
        }
        if (testMode) {
            for (GenericValue entity : updatedEntities) {
                storeForTestRollback(new TestOperation(OperationType.UPDATE, entity));
            }
        }
        TransactionUtil.commit(beganTransaction);
        return rowsAffected;
    } catch (IllegalStateException | GenericEntityException e) {
        String errMsg = "Failure in storeByCondition 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 : ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) GenericHelper(org.apache.ofbiz.entity.datasource.GenericHelper)

Example 82 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class GenericDelegator method findList.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#findList(java.lang.String, org.apache.ofbiz.entity.condition.EntityCondition, java.util.Set, java.util.List, org.apache.ofbiz.entity.util.EntityFindOptions, boolean)
     */
@Override
public List<GenericValue> findList(String entityName, EntityCondition entityCondition, Set<String> fieldsToSelect, List<String> orderBy, EntityFindOptions findOptions, boolean useCache) throws GenericEntityException {
    EntityEcaRuleRunner<?> ecaRunner = null;
    GenericValue dummyValue = null;
    if (useCache) {
        ecaRunner = this.getEcaRuleRunner(entityName);
        ModelEntity modelEntity = getModelReader().getModelEntity(entityName);
        dummyValue = GenericValue.create(modelEntity);
        ecaRunner.evalRules(EntityEcaHandler.EV_CACHE_CHECK, EntityEcaHandler.OP_FIND, dummyValue, false);
        List<GenericValue> cacheList = this.cache.get(entityName, entityCondition, orderBy);
        if (cacheList != null) {
            return cacheList;
        }
    }
    boolean beganTransaction = false;
    try {
        if (alwaysUseTransaction) {
            beganTransaction = TransactionUtil.begin();
        }
        List<GenericValue> list = null;
        try (EntityListIterator eli = this.find(entityName, entityCondition, null, fieldsToSelect, orderBy, findOptions)) {
            list = eli.getCompleteList();
        }
        if (useCache) {
            ecaRunner.evalRules(EntityEcaHandler.EV_CACHE_PUT, EntityEcaHandler.OP_FIND, dummyValue, false);
            this.cache.put(entityName, entityCondition, orderBy, list);
        }
        TransactionUtil.commit(beganTransaction);
        return list;
    } catch (GenericEntityException e) {
        String errMsg = "Failure in findByCondition 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 : ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator)

Example 83 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class GenericDelegator method getRelated.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#getRelated(java.lang.String, java.util.Map, java.util.List, org.apache.ofbiz.entity.GenericValue, boolean)
     */
@Override
public List<GenericValue> getRelated(String relationName, Map<String, ? extends Object> byAndFields, List<String> orderBy, GenericValue value, boolean useCache) throws GenericEntityException {
    ModelEntity modelEntity = value.getModelEntity();
    ModelRelation relation = modelEntity.getRelation(relationName);
    if (relation == null) {
        throw new GenericModelException("Could not find relation for relationName: " + relationName + " for value " + value);
    }
    // put the byAndFields (if not null) into the hash map first,
    // they will be overridden by value's fields if over-specified this is important for security and cleanliness
    Map<String, Object> fields = new HashMap<>();
    if (byAndFields != null) {
        fields.putAll(byAndFields);
    }
    for (ModelKeyMap keyMap : relation.getKeyMaps()) {
        fields.put(keyMap.getRelFieldName(), value.get(keyMap.getFieldName()));
    }
    return this.findByAnd(relation.getRelEntityName(), fields, orderBy, useCache);
}
Also used : ModelKeyMap(org.apache.ofbiz.entity.model.ModelKeyMap) HashMap(java.util.HashMap) ModelRelation(org.apache.ofbiz.entity.model.ModelRelation) UtilObject(org.apache.ofbiz.base.util.UtilObject) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 84 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class GenericDelegator method clearCacheLine.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#clearCacheLine(java.lang.String, java.util.Map)
     */
@Override
public void clearCacheLine(String entityName, Map<String, ? extends Object> fields) {
    // if no fields passed, do the all cache quickly and return
    if (fields == null) {
        cache.remove(entityName);
        return;
    }
    ModelEntity entity = this.getModelEntity(entityName);
    if (entity == null) {
        throw new IllegalArgumentException("[GenericDelegator.clearCacheLine] could not find entity for entityName: " + entityName);
    }
    // if never cached, then don't bother clearing
    if (entity.getNeverCache()) {
        return;
    }
    GenericValue dummyValue = GenericValue.create(this, entity, fields);
    this.clearCacheLineFlexible(dummyValue);
}
Also used : ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 85 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class GenericDelegator method getNextSeqIdLong.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#getNextSeqIdLong(java.lang.String, long)
     */
public Long getNextSeqIdLong(String seqName, long staggerMax) {
    try {
        SequenceUtil sequencer = this.AtomicRefSequencer.get();
        if (sequencer == null) {
            ModelEntity seqEntity = this.getModelEntity("SequenceValueItem");
            sequencer = new SequenceUtil(this.getEntityHelperInfo("SequenceValueItem"), seqEntity, "seqName", "seqId");
            if (!AtomicRefSequencer.compareAndSet(null, sequencer)) {
                sequencer = this.AtomicRefSequencer.get();
            }
        }
        ModelEntity seqModelEntity = null;
        try {
            seqModelEntity = getModelReader().getModelEntity(seqName);
        } catch (GenericEntityException e) {
            Debug.logInfo("Entity definition not found for sequence name " + seqName, module);
        }
        Long newSeqId = sequencer == null ? null : sequencer.getNextSeqId(seqName, staggerMax, seqModelEntity);
        return newSeqId;
    } catch (Exception e) {
        String errMsg = "Failure in getNextSeqIdLong operation for seqName [" + seqName + "]: " + e.toString() + ". Rolling back transaction.";
        Debug.logError(e, errMsg, module);
        throw new GeneralRuntimeException(errMsg, e);
    }
}
Also used : SequenceUtil(org.apache.ofbiz.entity.util.SequenceUtil) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) SerializeException(org.apache.ofbiz.entity.serialize.SerializeException) SAXException(org.xml.sax.SAXException) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)102 GenericValue (org.apache.ofbiz.entity.GenericValue)37 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)29 ModelField (org.apache.ofbiz.entity.model.ModelField)28 HashMap (java.util.HashMap)22 Delegator (org.apache.ofbiz.entity.Delegator)17 ModelViewEntity (org.apache.ofbiz.entity.model.ModelViewEntity)16 LinkedList (java.util.LinkedList)14 Locale (java.util.Locale)12 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)11 ArrayList (java.util.ArrayList)10 ModelRelation (org.apache.ofbiz.entity.model.ModelRelation)10 IOException (java.io.IOException)8 TreeSet (java.util.TreeSet)8 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)8 Map (java.util.Map)7 GeneralRuntimeException (org.apache.ofbiz.base.util.GeneralRuntimeException)7 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)7 ModelFieldType (org.apache.ofbiz.entity.model.ModelFieldType)7 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)7