Search in sources :

Example 11 with ModelEntity

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

the class GenericDelegator method getRelatedDummyPK.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#getRelatedDummyPK(java.lang.String, java.util.Map, org.apache.ofbiz.entity.GenericValue)
     */
@Override
public GenericPK getRelatedDummyPK(String relationName, Map<String, ? extends Object> byAndFields, GenericValue value) 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);
    }
    ModelEntity relatedEntity = getModelReader().getModelEntity(relation.getRelEntityName());
    // 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 GenericPK.create(this, relatedEntity, fields);
}
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 12 with ModelEntity

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

the class GenericDelegator method find.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#find(java.lang.String, org.apache.ofbiz.entity.condition.EntityCondition, org.apache.ofbiz.entity.condition.EntityCondition, java.util.Set, java.util.List, org.apache.ofbiz.entity.util.EntityFindOptions)
     */
@Override
public EntityListIterator find(String entityName, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, Set<String> fieldsToSelect, List<String> orderBy, EntityFindOptions findOptions) throws GenericEntityException {
    // if there is no transaction throw an exception, we don't want to create a transaction here since closing it would mess up the ELI
    if (!TransactionUtil.isTransactionInPlace()) {
        // throw new GenericEntityException("ERROR: Cannot do a find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.");
        // throwing an exception is a little harsh for now, just display a really big error message since we want to get all of these fixed...
        Exception newE = new Exception("Stack Trace");
        Debug.logError(newE, "ERROR: Cannot do a find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.", module);
    }
    ModelEntity modelEntity = getModelReader().getModelEntity(entityName);
    GenericValue dummyValue = GenericValue.create(modelEntity);
    EntityEcaRuleRunner<?> ecaRunner = this.getEcaRuleRunner(modelEntity.getEntityName());
    ecaRunner.evalRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_FIND, dummyValue, false);
    if (whereEntityCondition != null) {
        whereEntityCondition.checkCondition(modelEntity);
    }
    if (havingEntityCondition != null) {
        havingEntityCondition.checkCondition(modelEntity);
    }
    ecaRunner.evalRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_FIND, dummyValue, false);
    GenericHelper helper = getEntityHelper(modelEntity.getEntityName());
    EntityListIterator eli = helper.findListIteratorByCondition(this, modelEntity, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderBy, findOptions);
    eli.setDelegator(this);
    ecaRunner.evalRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, dummyValue, false);
    return eli;
}
Also used : ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) GenericHelper(org.apache.ofbiz.entity.datasource.GenericHelper) 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)

Example 13 with ModelEntity

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

the class ScriptHelperImpl method findOne.

public Map<String, Object> findOne(String entityName, Map<String, ? extends Object> fields, Map<String, ? extends Object> args) throws ScriptException {
    Assert.notNull("entityName", entityName);
    if (args == null) {
        args = EMPTY_ARGS;
    }
    boolean useCache = "true".equals(args.get("useCache"));
    boolean autoFieldMap = !"false".equals(args.get("autoFieldMap"));
    List<String> selectFieldList = UtilGenerics.checkList(args.get("selectFieldList"));
    ModelEntity modelEntity = ctxHelper.getDelegator().getModelEntity(entityName);
    if (modelEntity == null) {
        throw new ScriptException("Error running script " + ctxHelper.getScriptName() + " - no entity definition found for entity name [" + entityName + "]");
    }
    return runFindByPrimaryKey(modelEntity, ctxHelper, useCache, autoFieldMap, fields, selectFieldList);
}
Also used : ScriptException(javax.script.ScriptException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 14 with ModelEntity

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

the class GenericDAO method delete.

public int delete(GenericEntity entity, SQLProcessor sqlP) throws GenericEntityException {
    ModelEntity modelEntity = entity.getModelEntity();
    if (modelEntity instanceof ModelViewEntity) {
        throw new org.apache.ofbiz.entity.GenericNotImplementedException("Operation delete not supported yet for view entities");
    }
    StringBuilder sql = new StringBuilder().append("DELETE FROM ").append(modelEntity.getTableName(datasource)).append(" WHERE ");
    SqlJdbcUtil.makeWhereStringFromFields(sql, modelEntity.getPkFieldsUnmodifiable(), entity, "AND");
    int retVal;
    sqlP.prepareStatement(sql.toString());
    SqlJdbcUtil.setPkValues(sqlP, modelEntity, entity, modelFieldTypeReader);
    retVal = sqlP.executeUpdate();
    entity.removedFromDatasource();
    return retVal;
}
Also used : ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) GenericNotImplementedException(org.apache.ofbiz.entity.GenericNotImplementedException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 15 with ModelEntity

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

the class GenericDAO method select.

public void select(GenericEntity entity, SQLProcessor sqlP) throws GenericEntityException {
    ModelEntity modelEntity = entity.getModelEntity();
    if (modelEntity.getPksSize() <= 0) {
        throw new GenericEntityException("Entity has no primary keys, cannot select by primary key");
    }
    StringBuilder sqlBuffer = new StringBuilder("SELECT ");
    if (modelEntity.getNopksSize() > 0) {
        modelEntity.colNameString(modelEntity.getNopksCopy(), sqlBuffer, "", ", ", "", datasource.getAliasViewColumns());
    } else {
        sqlBuffer.append("*");
    }
    sqlBuffer.append(SqlJdbcUtil.makeFromClause(modelEntity, modelFieldTypeReader, datasource));
    sqlBuffer.append(SqlJdbcUtil.makeWhereClause(modelEntity, modelEntity.getPkFieldsUnmodifiable(), entity, "AND", datasource.getJoinStyle()));
    sqlP.prepareStatement(sqlBuffer.toString(), true, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    SqlJdbcUtil.setPkValues(sqlP, modelEntity, entity, modelFieldTypeReader);
    sqlP.executeQuery();
    if (sqlP.next()) {
        int idx = 1;
        Iterator<ModelField> nopkIter = modelEntity.getNopksIterator();
        while (nopkIter.hasNext()) {
            ModelField curField = nopkIter.next();
            SqlJdbcUtil.getValue(sqlP.getResultSet(), idx, curField, entity, modelFieldTypeReader);
            idx++;
        }
        entity.synchronizedWithDatasource();
    } else {
        // Debug.logWarning("[GenericDAO.select]: select failed, result set was empty for entity: " + entity.toString(), module);
        throw new GenericEntityNotFoundException("Result set was empty for entity: " + entity.toString());
    }
}
Also used : ModelField(org.apache.ofbiz.entity.model.ModelField) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericEntityNotFoundException(org.apache.ofbiz.entity.GenericEntityNotFoundException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

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