Search in sources :

Example 81 with GenericEntityException

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

the class ModelForm method addAutoFieldsFromEntity.

private void addAutoFieldsFromEntity(AutoFieldsEntity autoFieldsEntity, ModelReader entityModelReader, Set<String> useWhenFields, List<ModelFormFieldBuilder> fieldBuilderList, Map<String, ModelFormFieldBuilder> fieldBuilderMap) {
    // read entity def and auto-create fields
    ModelEntity modelEntity = null;
    try {
        modelEntity = entityModelReader.getModelEntity(autoFieldsEntity.entityName);
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    if (modelEntity == null) {
        throw new IllegalArgumentException("Error finding Entity with name " + autoFieldsEntity.entityName + " for auto-fields-entity in a form widget");
    }
    Iterator<ModelField> modelFieldIter = modelEntity.getFieldsIterator();
    while (modelFieldIter.hasNext()) {
        ModelField modelField = modelFieldIter.next();
        if (modelField.getIsAutoCreatedInternal()) {
            // don't ever auto-add these, should only be added if explicitly referenced
            continue;
        }
        ModelFormFieldBuilder builder = new ModelFormFieldBuilder();
        builder.setModelForm(this);
        builder.setName(modelField.getName());
        builder.setEntityName(modelEntity.getEntityName());
        builder.setFieldName(modelField.getName());
        builder.induceFieldInfoFromEntityField(modelEntity, modelField, autoFieldsEntity.defaultFieldType);
        builder.setPosition(autoFieldsEntity.defaultPosition);
        if (UtilValidate.isNotEmpty(autoFieldsEntity.mapName)) {
            builder.setMapName(autoFieldsEntity.mapName);
        }
        addUpdateField(builder, useWhenFields, fieldBuilderList, fieldBuilderMap);
    }
}
Also used : ModelField(org.apache.ofbiz.entity.model.ModelField) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 82 with GenericEntityException

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

the class PortalPageWorker method getPortalPage.

/**
 * Returns the PortalPage with the specified portalPageId.
 * If a specific PortalPage exists for the current userLogin it is returned instead of the original one.
 */
public static GenericValue getPortalPage(String portalPageId, Map<String, Object> context) {
    GenericValue portalPage = null;
    if (UtilValidate.isNotEmpty(portalPageId)) {
        Delegator delegator = WidgetWorker.getDelegator(context);
        try {
            // Get the current userLoginId
            String userLoginId = "_NA_";
            if (UtilValidate.isNotEmpty(context.get("userLogin"))) {
                // check if a user is logged in
                userLoginId = ((GenericValue) context.get("userLogin")).getString("userLoginId");
            }
            // Get the PortalPage ensuring that it is either owned by the user or a system page
            EntityCondition cond = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("portalPageId", EntityOperator.EQUALS, portalPageId), EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("ownerUserLoginId", EntityOperator.EQUALS, "_NA_"), EntityCondition.makeCondition("ownerUserLoginId", EntityOperator.EQUALS, userLoginId)), EntityOperator.OR)), EntityOperator.AND);
            List<GenericValue> portalPages = EntityQuery.use(delegator).from("PortalPage").where(cond).queryList();
            if (UtilValidate.isNotEmpty(portalPages)) {
                portalPage = EntityUtil.getFirst(portalPages);
            }
            // If a derived PortalPage private to the user exists, returns this instead of the system one
            cond = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("originalPortalPageId", EntityOperator.EQUALS, portalPageId), EntityCondition.makeCondition("ownerUserLoginId", EntityOperator.EQUALS, userLoginId)), EntityOperator.AND);
            List<GenericValue> privateDerivedPortalPages = EntityQuery.use(delegator).from("PortalPage").where(cond).queryList();
            if (UtilValidate.isNotEmpty(privateDerivedPortalPages)) {
                portalPage = EntityUtil.getFirst(privateDerivedPortalPages);
            }
        } catch (GenericEntityException e) {
            Debug.logError("Could not retrieve portalpage:" + e.getMessage(), module);
        }
    }
    return portalPage;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition)

Example 83 with GenericEntityException

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

the class GeoWorker method expandGeoGroup.

public static List<GenericValue> expandGeoGroup(GenericValue geo) {
    if (geo == null) {
        return new LinkedList<GenericValue>();
    }
    if (!"GROUP".equals(geo.getString("geoTypeId"))) {
        return UtilMisc.toList(geo);
    }
    List<GenericValue> geoList = new LinkedList<GenericValue>();
    List<GenericValue> thisGeoAssoc = null;
    try {
        thisGeoAssoc = geo.getRelated("AssocGeoAssoc", UtilMisc.toMap("geoAssocTypeId", "GROUP_MEMBER"), null, false);
    } catch (GenericEntityException e) {
        Debug.logError(e, "Unable to get associated Geo GROUP_MEMBER relationship(s)", module);
    }
    if (UtilValidate.isNotEmpty(thisGeoAssoc)) {
        for (GenericValue nextGeoAssoc : thisGeoAssoc) {
            GenericValue nextGeo = null;
            try {
                nextGeo = nextGeoAssoc.getRelatedOne("MainGeo", false);
            } catch (GenericEntityException e) {
                Debug.logError(e, "Unable to get related Geo", module);
            }
            geoList.addAll(expandGeoGroup(nextGeo));
        }
    }
    return geoList;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) LinkedList(java.util.LinkedList)

Example 84 with GenericEntityException

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

the class LdapAuthenticationServices method userLogin.

public static boolean userLogin(DispatchContext ctx, Map<String, ?> context) {
    if (Debug.verboseOn())
        Debug.logVerbose("Starting LDAP authentication", module);
    Properties env = UtilProperties.getProperties("jndiLdap");
    String username = (String) context.get("login.username");
    if (username == null) {
        username = (String) context.get("username");
    }
    String password = (String) context.get("login.password");
    if (password == null) {
        password = (String) context.get("password");
    }
    String dn = null;
    Delegator delegator = ctx.getDelegator();
    boolean isServiceAuth = context.get("isServiceAuth") != null && ((Boolean) context.get("isServiceAuth")).booleanValue();
    GenericValue userLogin = null;
    try {
        userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", username).cache(isServiceAuth).queryOne();
    } catch (GenericEntityException e) {
        Debug.logWarning(e, "", module);
    }
    if (userLogin != null) {
        dn = userLogin.getString("userLdapDn");
    }
    if (UtilValidate.isEmpty(dn)) {
        String dnTemplate = (String) env.get("ldap.dn.template");
        if (dnTemplate != null) {
            dn = dnTemplate.replace("%u", username);
        }
        if (Debug.verboseOn())
            Debug.logVerbose("Using DN template: " + dn, module);
    } else {
        if (Debug.verboseOn())
            Debug.logVerbose("Using UserLogin.userLdapDn: " + dn, module);
    }
    env.put(Context.SECURITY_PRINCIPAL, dn);
    env.put(Context.SECURITY_CREDENTIALS, password);
    try {
        // Create initial context
        DirContext ldapCtx = new InitialDirContext(env);
        ldapCtx.close();
    } catch (NamingException e) {
        if (Debug.verboseOn())
            Debug.logVerbose("LDAP authentication failed: " + e.getMessage(), module);
        return false;
    }
    if (Debug.verboseOn())
        Debug.logVerbose("LDAP authentication succeeded", module);
    if (!"true".equals(env.get("ldap.synchronize.passwords"))) {
        return true;
    }
    // Synchronize user's OFBiz password with user's LDAP password
    if (userLogin != null) {
        boolean useEncryption = "true".equals(EntityUtilProperties.getPropertyValue("security", "password.encrypt", delegator));
        String currentPassword = userLogin.getString("currentPassword");
        boolean samePassword;
        if (useEncryption) {
            samePassword = HashCrypt.comparePassword(currentPassword, LoginServices.getHashType(), password);
        } else {
            samePassword = currentPassword.equals(password);
        }
        if (!samePassword) {
            if (Debug.verboseOn())
                Debug.logVerbose("Starting password synchronization", module);
            userLogin.set("currentPassword", useEncryption ? HashCrypt.cryptUTF8(LoginServices.getHashType(), null, password) : password, false);
            Transaction parentTx = null;
            boolean beganTransaction = false;
            try {
                try {
                    parentTx = TransactionUtil.suspend();
                } catch (GenericTransactionException e) {
                    Debug.logError(e, "Could not suspend transaction: " + e.getMessage(), module);
                }
                try {
                    beganTransaction = TransactionUtil.begin();
                    userLogin.store();
                } catch (GenericEntityException e) {
                    Debug.logError(e, "Error saving UserLogin", module);
                    try {
                        TransactionUtil.rollback(beganTransaction, "Error saving UserLogin", e);
                    } catch (GenericTransactionException e2) {
                        Debug.logError(e2, "Could not rollback nested transaction: " + e2.getMessage(), module);
                    }
                } finally {
                    try {
                        TransactionUtil.commit(beganTransaction);
                        if (Debug.verboseOn())
                            Debug.logVerbose("Password synchronized", module);
                    } catch (GenericTransactionException e) {
                        Debug.logError(e, "Could not commit nested transaction: " + e.getMessage(), module);
                    }
                }
            } finally {
                if (parentTx != null) {
                    try {
                        TransactionUtil.resume(parentTx);
                        if (Debug.verboseOn())
                            Debug.logVerbose("Resumed the parent transaction.", module);
                    } catch (GenericTransactionException e) {
                        Debug.logError(e, "Could not resume parent nested transaction: " + e.getMessage(), module);
                    }
                }
            }
        }
    }
    return true;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) Transaction(javax.transaction.Transaction) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) Properties(java.util.Properties) UtilProperties(org.apache.ofbiz.base.util.UtilProperties) EntityUtilProperties(org.apache.ofbiz.entity.util.EntityUtilProperties)

Example 85 with GenericEntityException

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

the class StatusServices method getStatusItems.

public static Map<String, Object> getStatusItems(DispatchContext ctx, Map<String, ?> context) {
    Delegator delegator = ctx.getDelegator();
    List<String> statusTypes = checkList(context.get("statusTypeIds"), String.class);
    Locale locale = (Locale) context.get("locale");
    if (UtilValidate.isEmpty(statusTypes)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonStatusMandatory", locale));
    }
    List<GenericValue> statusItems = new LinkedList<GenericValue>();
    for (String statusTypeId : statusTypes) {
        try {
            List<GenericValue> myStatusItems = EntityQuery.use(delegator).from("StatusItem").where("statusTypeId", statusTypeId).orderBy("sequenceId").cache(true).queryList();
            statusItems.addAll(myStatusItems);
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
    }
    Map<String, Object> ret = new LinkedHashMap<String, Object>();
    ret.put("statusItems", statusItems);
    return ret;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)913 GenericValue (org.apache.ofbiz.entity.GenericValue)847 Delegator (org.apache.ofbiz.entity.Delegator)599 Locale (java.util.Locale)384 HashMap (java.util.HashMap)336 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)270 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)259 LinkedList (java.util.LinkedList)231 BigDecimal (java.math.BigDecimal)213 Timestamp (java.sql.Timestamp)171 Map (java.util.Map)109 GeneralException (org.apache.ofbiz.base.util.GeneralException)95 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)78 IOException (java.io.IOException)75 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)57 Security (org.apache.ofbiz.security.Security)54 ArrayList (java.util.ArrayList)48 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)47 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)39 LinkedHashMap (java.util.LinkedHashMap)37