use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class SqlJdbcUtil method makeViewTable.
public static String makeViewTable(ModelEntity modelEntity, ModelFieldTypeReader modelFieldTypeReader, Datasource datasourceInfo) throws GenericEntityException {
if (modelEntity instanceof ModelViewEntity) {
StringBuilder sql = new StringBuilder("(SELECT ");
Iterator<ModelField> fieldsIter = modelEntity.getFieldsIterator();
if (fieldsIter.hasNext()) {
ModelField curField = fieldsIter.next();
sql.append(curField.getColValue());
sql.append(" AS ");
sql.append(curField.getColName());
while (fieldsIter.hasNext()) {
curField = fieldsIter.next();
sql.append(", ");
sql.append(curField.getColValue());
sql.append(" AS ");
sql.append(curField.getColName());
}
}
sql.append(makeFromClause(modelEntity, modelFieldTypeReader, datasourceInfo));
String viewWhereClause = makeViewWhereClause(modelEntity, datasourceInfo.getJoinStyle());
ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntity;
List<EntityCondition> whereConditions = new LinkedList<EntityCondition>();
List<EntityCondition> havingConditions = new LinkedList<EntityCondition>();
List<String> orderByList = new LinkedList<String>();
modelViewEntity.populateViewEntityConditionInformation(modelFieldTypeReader, whereConditions, havingConditions, orderByList, null);
String viewConditionClause;
if (!whereConditions.isEmpty()) {
viewConditionClause = EntityCondition.makeCondition(whereConditions, EntityOperator.AND).makeWhereString(modelViewEntity, null, datasourceInfo);
} else {
viewConditionClause = null;
}
if (UtilValidate.isNotEmpty(viewWhereClause) || UtilValidate.isNotEmpty(viewConditionClause)) {
sql.append(" WHERE ");
if (UtilValidate.isNotEmpty(viewWhereClause)) {
sql.append("(").append(viewWhereClause).append(")");
if (UtilValidate.isNotEmpty(viewConditionClause)) {
sql.append(" AND ");
}
}
if (UtilValidate.isNotEmpty(viewConditionClause)) {
sql.append("(").append(viewConditionClause).append(")");
}
}
// FIXME: handling HAVING, don't need ORDER BY for nested views
modelViewEntity.colNameString(modelViewEntity.getGroupBysCopy(), sql, " GROUP BY ", ", ", "", false);
sql.append(")");
return sql.toString();
} else {
return modelEntity.getTableName(datasourceInfo);
}
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class ModelViewEntity method populateViewEntityConditionInformation.
public void populateViewEntityConditionInformation(ModelFieldTypeReader modelFieldTypeReader, List<EntityCondition> whereConditions, List<EntityCondition> havingConditions, List<String> orderByList, List<String> entityAliasStack) {
if (entityAliasStack == null) {
entityAliasStack = new LinkedList<>();
}
if (this.viewEntityCondition != null) {
EntityCondition whereCondition = this.viewEntityCondition.getWhereCondition(modelFieldTypeReader, entityAliasStack);
if (whereCondition != null) {
whereConditions.add(whereCondition);
}
EntityCondition havingCondition = this.viewEntityCondition.getHavingCondition(modelFieldTypeReader, entityAliasStack);
if (havingCondition != null) {
havingConditions.add(havingCondition);
}
// add the current one first so it overrides the lower level ones
List<String> currentOrderByList = this.viewEntityCondition.getOrderByList();
if (currentOrderByList != null) {
orderByList.addAll(currentOrderByList);
}
}
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class EntityCryptoTestSuite method testCryptoSubSelect.
public void testCryptoSubSelect() throws Exception {
String nanoTime = "" + System.nanoTime();
EntityCondition condition;
List<GenericValue> results;
delegator.removeByAnd("TestingCrypto", UtilMisc.toMap("testingCryptoTypeId", "SUB_SELECT_1"));
delegator.removeByAnd("TestingCrypto", UtilMisc.toMap("testingCryptoTypeId", "SUB_SELECT_2"));
delegator.removeByAnd("TestingCrypto", UtilMisc.toMap("testingCryptoTypeId", "SUB_SELECT_3"));
delegator.create("TestingCrypto", UtilMisc.toMap("testingCryptoId", "SUB_1", "testingCryptoTypeId", "SUB_SELECT_1", "encryptedValue", nanoTime));
delegator.create("TestingCrypto", UtilMisc.toMap("testingCryptoId", "SUB_2", "testingCryptoTypeId", "SUB_SELECT_2", "encryptedValue", nanoTime));
delegator.create("TestingCrypto", UtilMisc.toMap("testingCryptoId", "SUB_3", "testingCryptoTypeId", "SUB_SELECT_3", "encryptedValue", "constant"));
results = EntityQuery.use(delegator).from("TestingCrypto").where("encryptedValue", nanoTime).orderBy("testingCryptoId").queryList();
assertEquals(2, results.size());
assertEquals("SUB_1", results.get(0).get("testingCryptoId"));
assertEquals("SUB_2", results.get(1).get("testingCryptoId"));
results = EntityQuery.use(delegator).from("TestingCrypto").where(EntityCondition.makeCondition("testingCryptoTypeId", EntityOperator.IN, UtilMisc.toList("SUB_SELECT_1", "SUB_SELECT_3"))).orderBy("testingCryptoId").queryList();
assertEquals(2, results.size());
assertEquals("SUB_1", results.get(0).get("testingCryptoId"));
assertEquals("SUB_3", results.get(1).get("testingCryptoId"));
condition = makeSubSelectCondition(nanoTime);
results = EntityQuery.use(delegator).from("TestingCrypto").where(condition).orderBy("testingCryptoId").queryList();
assertEquals(1, results.size());
assertEquals("SUB_1", results.get(0).get("testingCryptoId"));
condition = EntityCondition.makeCondition("testingCryptoId", EntityOperator.EQUALS, makeSubSelect(nanoTime));
results = EntityQuery.use(delegator).from("TestingCrypto").where(condition).orderBy("testingCryptoId").queryList();
assertEquals(1, results.size());
assertEquals("SUB_1", results.get(0).get("testingCryptoId"));
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class EntityCacheServices method clearCacheLine.
/**
* Clear Cache Line Service: one of the following context parameters is required: value, dummyPK or primaryKey
* @param dctx The DispatchContext that this service is operating in
* @param context Map containing the input parameters
* @return Map with the result of the service, the output parameters
*/
public static Map<String, Object> clearCacheLine(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
Boolean distributeBool = (Boolean) context.get("distribute");
boolean distribute = false;
if (distributeBool != null)
distribute = distributeBool.booleanValue();
if (context.containsKey("value")) {
GenericValue value = (GenericValue) context.get("value");
if (Debug.infoOn())
Debug.logInfo("Got a clear cache line by value service call; entityName: " + value.getEntityName(), module);
if (Debug.verboseOn())
Debug.logVerbose("Got a clear cache line by value service call; value: " + value, module);
delegator.clearCacheLine(value, distribute);
} else if (context.containsKey("dummyPK")) {
GenericEntity dummyPK = (GenericEntity) context.get("dummyPK");
if (Debug.infoOn())
Debug.logInfo("Got a clear cache line by dummyPK service call; entityName: " + dummyPK.getEntityName(), module);
if (Debug.verboseOn())
Debug.logVerbose("Got a clear cache line by dummyPK service call; dummyPK: " + dummyPK, module);
delegator.clearCacheLineFlexible(dummyPK, distribute);
} else if (context.containsKey("primaryKey")) {
GenericPK primaryKey = (GenericPK) context.get("primaryKey");
if (Debug.infoOn())
Debug.logInfo("Got a clear cache line by primaryKey service call; entityName: " + primaryKey.getEntityName(), module);
if (Debug.verboseOn())
Debug.logVerbose("Got a clear cache line by primaryKey service call; primaryKey: " + primaryKey, module);
delegator.clearCacheLine(primaryKey, distribute);
} else if (context.containsKey("condition")) {
String entityName = (String) context.get("entityName");
EntityCondition condition = (EntityCondition) context.get("condition");
if (Debug.infoOn())
Debug.logInfo("Got a clear cache line by condition service call; entityName: " + entityName, module);
if (Debug.verboseOn())
Debug.logVerbose("Got a clear cache line by condition service call; condition: " + condition, module);
delegator.clearCacheLineByCondition(entityName, condition, distribute);
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class PortalPageWorker method getPortalPages.
/**
* Returns a list of PortalPages that have the specified parentPortalPageId as parent.
* If a specific PortalPage exists for the current userLogin it is returned instead of the original one.
*/
public static List<GenericValue> getPortalPages(String parentPortalPageId, Map<String, Object> context) {
List<GenericValue> portalPages = null;
if (UtilValidate.isNotEmpty(parentPortalPageId)) {
Delegator delegator = WidgetWorker.getDelegator(context);
try {
// first get public pages
EntityCondition cond = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("ownerUserLoginId", EntityOperator.EQUALS, "_NA_"), EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("portalPageId", EntityOperator.EQUALS, parentPortalPageId), EntityCondition.makeCondition("parentPortalPageId", EntityOperator.EQUALS, parentPortalPageId)), EntityOperator.OR)), EntityOperator.AND);
portalPages = EntityQuery.use(delegator).from("PortalPage").where(cond).queryList();
List<GenericValue> userPortalPages = new ArrayList<GenericValue>();
if (UtilValidate.isNotEmpty(context.get("userLogin"))) {
// check if a user is logged in
String userLoginId = ((GenericValue) context.get("userLogin")).getString("userLoginId");
// replace with private pages
for (GenericValue portalPage : portalPages) {
List<GenericValue> privatePortalPages = EntityQuery.use(delegator).from("PortalPage").where("ownerUserLoginId", userLoginId, "originalPortalPageId", portalPage.getString("portalPageId")).queryList();
if (UtilValidate.isNotEmpty(privatePortalPages)) {
userPortalPages.add(privatePortalPages.get(0));
} else {
userPortalPages.add(portalPage);
}
}
// add any other created private pages
userPortalPages.addAll(EntityQuery.use(delegator).from("PortalPage").where("ownerUserLoginId", userLoginId, "originalPortalPageId", null, "parentPortalPageId", parentPortalPageId).queryList());
}
portalPages = EntityUtil.orderBy(userPortalPages, UtilMisc.toList("sequenceNum"));
} catch (GenericEntityException e) {
Debug.logError("Could not retrieve portalpages:" + e.getMessage(), module);
}
}
return portalPages;
}
Aggregations