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);
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations