Search in sources :

Example 1 with EntityFieldMap

use of org.apache.ofbiz.entity.condition.EntityFieldMap in project ofbiz-framework by apache.

the class GenericEntity method checkFks.

/**
 * Checks to see if all foreign key records exist in the database. Will create a dummy value for
 * those missing when specified.
 *
 * @param insertDummy Create a dummy record using the provided fields
 * @return true if all FKs exist (or when all missing are created)
 * @throws GenericEntityException
 */
public boolean checkFks(boolean insertDummy) throws GenericEntityException {
    ModelEntity model = this.getModelEntity();
    Iterator<ModelRelation> relItr = model.getRelationsIterator();
    while (relItr.hasNext()) {
        ModelRelation relation = relItr.next();
        if ("one".equalsIgnoreCase(relation.getType())) {
            // see if the related value exists
            Map<String, Object> fields = new HashMap<>();
            for (ModelKeyMap keyMap : relation.getKeyMaps()) {
                fields.put(keyMap.getRelFieldName(), this.get(keyMap.getFieldName()));
            }
            EntityFieldMap ecl = EntityCondition.makeCondition(fields);
            long count = this.getDelegator().findCountByCondition(relation.getRelEntityName(), ecl, null, null);
            if (count == 0) {
                if (insertDummy) {
                    // create the new related value (dummy)
                    GenericValue newValue = this.getDelegator().makeValue(relation.getRelEntityName());
                    boolean allFieldsSet = true;
                    for (ModelKeyMap mkm : relation.getKeyMaps()) {
                        if (this.get(mkm.getFieldName()) != null) {
                            newValue.set(mkm.getRelFieldName(), this.get(mkm.getFieldName()));
                            if (Debug.infoOn()) {
                                Debug.logInfo("Set [" + mkm.getRelFieldName() + "] to - " + this.get(mkm.getFieldName()), module);
                            }
                        } else {
                            allFieldsSet = false;
                        }
                    }
                    if (allFieldsSet) {
                        if (Debug.infoOn()) {
                            Debug.logInfo("Creating place holder value : " + newValue, module);
                        }
                        // inherit create and update times from this value in order to make this not seem like new/fresh data
                        newValue.put(ModelEntity.CREATE_STAMP_FIELD, this.get(ModelEntity.CREATE_STAMP_FIELD));
                        newValue.put(ModelEntity.CREATE_STAMP_TX_FIELD, this.get(ModelEntity.CREATE_STAMP_TX_FIELD));
                        newValue.put(ModelEntity.STAMP_FIELD, this.get(ModelEntity.STAMP_FIELD));
                        newValue.put(ModelEntity.STAMP_TX_FIELD, this.get(ModelEntity.STAMP_TX_FIELD));
                        // set isFromEntitySync so that create/update stamp fields set above will be preserved
                        newValue.setIsFromEntitySync(true);
                        // check the FKs for the newly created entity
                        newValue.checkFks(true);
                        newValue.create();
                    }
                } else {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : ModelKeyMap(org.apache.ofbiz.entity.model.ModelKeyMap) HashMap(java.util.HashMap) ModelRelation(org.apache.ofbiz.entity.model.ModelRelation) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) EntityFieldMap(org.apache.ofbiz.entity.condition.EntityFieldMap)

Aggregations

HashMap (java.util.HashMap)1 EntityFieldMap (org.apache.ofbiz.entity.condition.EntityFieldMap)1 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)1 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)1 ModelRelation (org.apache.ofbiz.entity.model.ModelRelation)1