Search in sources :

Example 1 with ModelAlias

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

the class GenericEntity method get.

/**
 * call by the previous method to be able to read with View entityName and entity Field and after for real entity
 * @param modelEntity the modelEntity, for a view it's the ViewEntity
 * @param modelEntityToUse, same as before except if it's a second call for a view, and so it's the real modelEntity
 * @return null or resourceValue
 */
private Object get(ModelEntity modelEntity, ModelEntity modelEntityToUse, String name, String resource, Locale locale) {
    if (UtilValidate.isEmpty(resource)) {
        resource = modelEntityToUse.getDefaultResourceName();
        // still empty? return null
        if (UtilValidate.isEmpty(resource)) {
            return null;
        }
    }
    if (UtilProperties.isPropertiesResourceNotFound(resource, locale, false)) {
        // Properties do not exist for this resource+locale combination
        return null;
    }
    ResourceBundle bundle = null;
    try {
        bundle = UtilProperties.getResourceBundle(resource, locale);
    } catch (IllegalArgumentException e) {
        bundle = null;
    }
    if (bundle == null) {
        return null;
    }
    StringBuilder keyBuffer = new StringBuilder();
    // start with the Entity Name
    keyBuffer.append(modelEntityToUse.getEntityName());
    // next add the Field Name
    keyBuffer.append('.');
    keyBuffer.append(name);
    // finish off by adding the values of all PK fields
    if (modelEntity instanceof ModelViewEntity) {
        // retrieve pkNames of realEntity
        ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntity;
        List<String> pkNamesToUse = new LinkedList<>();
        // iterate on realEntity for pkField
        Iterator<ModelField> iter = modelEntityToUse.getPksIterator();
        while (iter != null && iter.hasNext()) {
            ModelField curField = iter.next();
            String pkName = null;
            Iterator<ModelAlias> iterAlias = modelViewEntity.getAliasesIterator();
            // search aliasName for pkField of realEntity
            while (iterAlias != null && iterAlias.hasNext()) {
                ModelAlias aliasField = iterAlias.next();
                if (aliasField.getField().equals(curField.getName())) {
                    ModelEntity memberModelEntity = modelViewEntity.getMemberModelEntity(aliasField.getEntityAlias());
                    if (memberModelEntity.getEntityName().equals(modelEntityToUse.getEntityName())) {
                        pkName = aliasField.getName();
                        break;
                    }
                }
            }
            if (pkName == null) {
                pkName = curField.getName();
            }
            pkNamesToUse.add(pkName);
        }
        // read value with modelEntity name of pkNames
        for (String pkName : pkNamesToUse) {
            if (this.containsKey(pkName)) {
                keyBuffer.append('.');
                keyBuffer.append(this.get(pkName));
            }
        }
    } else {
        Iterator<ModelField> iter = modelEntity.getPksIterator();
        while (iter != null && iter.hasNext()) {
            ModelField curField = iter.next();
            keyBuffer.append('.');
            keyBuffer.append(this.get(curField.getName()));
        }
    }
    String bundleKey = keyBuffer.toString();
    Object resourceValue = null;
    try {
        resourceValue = bundle.getObject(bundleKey);
    } catch (MissingResourceException e) {
        return null;
    }
    return resourceValue;
}
Also used : ModelAlias(org.apache.ofbiz.entity.model.ModelViewEntity.ModelAlias) MissingResourceException(java.util.MissingResourceException) LinkedList(java.util.LinkedList) ModelField(org.apache.ofbiz.entity.model.ModelField) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) ResourceBundle(java.util.ResourceBundle) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 2 with ModelAlias

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

the class ModelUtil method isPotentialLocalizedFields.

/**
 * Check is a ModelEntity have a default resource associate to resolve localized value
 * When the ModelEntity is a ModelViewEntity, check with the list fields to resolve these related entities
 * @param modelEntity
 * @param fieldNames
 * @return
 */
public static boolean isPotentialLocalizedFields(ModelEntity modelEntity, List<String> fieldNames) {
    if (modelEntity == null)
        return false;
    if (modelEntity instanceof ModelViewEntity) {
        // now try to retrieve with the field heading from the real entity linked to the view
        ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntity;
        Iterator<ModelAlias> it = modelViewEntity.getAliasesIterator();
        while (it.hasNext()) {
            ModelAlias modelAlias = it.next();
            if (fieldNames.contains(modelAlias.getName())) {
                ModelEntity memberModelEntity = modelViewEntity.getMemberModelEntity(modelAlias.getEntityAlias());
                if (UtilValidate.isNotEmpty(memberModelEntity.getDefaultResourceName())) {
                    return true;
                }
            }
        }
        return false;
    } else {
        return UtilValidate.isNotEmpty(modelEntity.getDefaultResourceName());
    }
}
Also used : ModelAlias(org.apache.ofbiz.entity.model.ModelViewEntity.ModelAlias)

Example 3 with ModelAlias

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

the class DynamicViewEntity method getViewElement.

public Element getViewElement(Document doc, String entityName) {
    Element viewElement = doc.createElement("view-entity");
    viewElement.setAttribute("entity-name", entityName);
    for (ModelMemberEntity member : memberModelMemberEntities.values()) {
        Element memberElement = doc.createElement("member-entity");
        memberElement.setAttribute("entity-alias", member.getEntityAlias());
        memberElement.setAttribute("entity-name", member.getEntityName());
        viewElement.appendChild(memberElement);
    }
    for (ModelAliasAll aliasAll : aliasAlls) {
        Element aliasAllElement = doc.createElement("alias-all");
        aliasAllElement.setAttribute("entity-alias", aliasAll.getEntityAlias());
        if (UtilValidate.isNotEmpty(aliasAll.getPrefix()))
            aliasAllElement.setAttribute("prefix", aliasAll.getPrefix());
        if (aliasAll.getGroupBy())
            aliasAllElement.setAttribute("group-by", "true");
        if (UtilValidate.isNotEmpty(aliasAll.getFunction()))
            aliasAllElement.setAttribute("function", aliasAll.getFunction());
        for (String excludeField : aliasAll) {
            Element excludeElement = doc.createElement("exclude");
            excludeElement.setAttribute("field", excludeField);
            aliasAllElement.appendChild(excludeElement);
        }
        viewElement.appendChild(aliasAllElement);
    }
    for (ModelAlias alias : aliases) {
        Element aliasElement = doc.createElement("alias");
        aliasElement.setAttribute("entity-alias", alias.getEntityAlias());
        aliasElement.setAttribute("name", alias.getName());
        if (!alias.getName().equals(alias.getField()))
            aliasElement.setAttribute("field", alias.getField());
        String colAlias = ModelUtil.dbNameToVarName(alias.getColAlias());
        if (!alias.getName().equals(colAlias))
            aliasElement.setAttribute("col-alias", colAlias);
        if (alias.getIsPk() != null)
            aliasElement.setAttribute("prim-key", alias.getIsPk().toString());
        if (alias.getGroupBy())
            aliasElement.setAttribute("group-by", "true");
        if (UtilValidate.isNotEmpty(alias.getFunction()))
            aliasElement.setAttribute("function", alias.getFunction());
        // TODO: description, complex-alias
        viewElement.appendChild(aliasElement);
    }
    for (ModelViewLink viewLink : viewLinks) {
        Element viewLinkElement = doc.createElement("view-link");
        viewLinkElement.setAttribute("entity-alias", viewLink.getEntityAlias());
        if (viewLink.isRelOptional())
            viewLinkElement.setAttribute("rel-optional", "true");
        viewLinkElement.setAttribute("rel-entity-alias", viewLink.getRelEntityAlias());
        for (ModelKeyMap keyMap : viewLink) {
            Element keyMapElement = doc.createElement("key-map");
            keyMapElement.setAttribute("field-name", keyMap.getFieldName());
            if (!keyMap.getFieldName().equals(keyMap.getRelFieldName()))
                keyMapElement.setAttribute("rel-field-name", keyMap.getRelFieldName());
            viewLinkElement.appendChild(keyMapElement);
        }
        // TODO: conditions
        viewElement.appendChild(viewLinkElement);
    }
    for (ModelRelation relation : relations) {
        viewElement.appendChild(relation.toXmlElement(doc));
    }
    return viewElement;
}
Also used : ModelMemberEntity(org.apache.ofbiz.entity.model.ModelViewEntity.ModelMemberEntity) ModelViewLink(org.apache.ofbiz.entity.model.ModelViewEntity.ModelViewLink) Element(org.w3c.dom.Element) ModelAlias(org.apache.ofbiz.entity.model.ModelViewEntity.ModelAlias) ModelAliasAll(org.apache.ofbiz.entity.model.ModelViewEntity.ModelAliasAll)

Example 4 with ModelAlias

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

the class DynamicViewEntity method addAlias.

public void addAlias(String entityAlias, String name, String field, String colAlias, Boolean primKey, Boolean groupBy, String function, String fieldSet, ComplexAliasMember complexAliasMember) {
    if (entityAlias == null && complexAliasMember == null) {
        throw new IllegalArgumentException("entityAlias cannot be null if this is not a complex alias in call to DynamicViewEntity.addAlias");
    }
    if (name == null) {
        throw new IllegalArgumentException("name cannot be null in call to DynamicViewEntity.addAlias");
    }
    ModelAlias alias = new ModelAlias(entityAlias, name, field, colAlias, primKey, groupBy, function, fieldSet);
    if (complexAliasMember != null) {
        alias.setComplexAliasMember(complexAliasMember);
    }
    this.aliases.add(alias);
}
Also used : ModelAlias(org.apache.ofbiz.entity.model.ModelViewEntity.ModelAlias)

Example 5 with ModelAlias

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

the class GenericEntity method get.

/**
 * Same as the getResource method that does not take resource name, but instead allows manually
 *    specifying the resource name. In general you should use the other method for more consistent
 *    naming and use of the corresponding properties files.
 * @param name The name of the field on the entity
 * @param resource The name of the resource to get the value from; if null defaults to the
 *    default-resource-name on the entity definition, if specified there
 * @param locale The locale to use when finding the ResourceBundle, if null uses the default
 *    locale for the current instance of Java
 * @return If the specified resource is found and contains a key as described above, then that
 *    property value is returned; otherwise returns the field value
 */
public Object get(String name, String resource, Locale locale) {
    Object fieldValue = get(name);
    // In case of view entity first try to retrieve with View field names
    ModelEntity modelEntityToUse = this.getModelEntity();
    Object resourceValue = get(this.getModelEntity(), modelEntityToUse, name, resource, locale);
    if (resourceValue == null) {
        if (modelEntityToUse instanceof ModelViewEntity) {
            // now try to retrieve with the field heading from the real entity linked to the view
            ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntityToUse;
            Iterator<ModelAlias> it = modelViewEntity.getAliasesIterator();
            while (it.hasNext()) {
                ModelAlias modelAlias = it.next();
                if (modelAlias.getName().equalsIgnoreCase(name)) {
                    modelEntityToUse = modelViewEntity.getMemberModelEntity(modelAlias.getEntityAlias());
                    name = modelAlias.getField();
                    break;
                }
            }
            resourceValue = get(this.getModelEntity(), modelEntityToUse, name, resource, locale);
            if (resourceValue == null) {
                return fieldValue;
            }
            return resourceValue;
        }
        return fieldValue;
    }
    return resourceValue;
}
Also used : ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) ModelAlias(org.apache.ofbiz.entity.model.ModelViewEntity.ModelAlias) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Aggregations

ModelAlias (org.apache.ofbiz.entity.model.ModelViewEntity.ModelAlias)7 ModelViewEntity (org.apache.ofbiz.entity.model.ModelViewEntity)3 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)2 LinkedList (java.util.LinkedList)1 MissingResourceException (java.util.MissingResourceException)1 ResourceBundle (java.util.ResourceBundle)1 ModelField (org.apache.ofbiz.entity.model.ModelField)1 ModelAliasAll (org.apache.ofbiz.entity.model.ModelViewEntity.ModelAliasAll)1 ModelMemberEntity (org.apache.ofbiz.entity.model.ModelViewEntity.ModelMemberEntity)1 ModelViewLink (org.apache.ofbiz.entity.model.ModelViewEntity.ModelViewLink)1 Element (org.w3c.dom.Element)1