Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Enhancement: Many serverside AdvancedCriteria convenience methods missing

    Hello Isomorphic,

    for com.isomorphic.criteria.AdvancedCriteria (http://www.smartclient.com/smartgwte...dCriteria.html) there are no convenience methods to add com.isomorphic.criteria.SimpleCriteria, com.isomorphic.criteria.criterion.* or com.isomorphic.criteria.AdvancedCriteria to an existing object.

    All I can see are "constructors", but no "add" methods. Methods like DSRequest.addToCriteria(...) (http://www.smartclient.com/smartgwte...lang.Object%29) or like on the client side would really help a lot.

    Thank you & Best regards,
    Blama

    #2
    Hi Isomorphic,

    these would be really helpful, at least some add that works like DSRequest.addToCriteria(...) ("add to existing list if outermost operator is AND, create new AND otherwise"), but takes AdvancedCriteria (or Criteria or Criterion), not (fieldName, operator, value).

    I have this code inside a loop:
    Code:
    for (Object fn : dsRequest.getDataSource().getFieldNames()) {
    	String fieldName = fn.toString();
    	if (fieldName.endsWith("_TENANT_ID")) {
    		if (!isOuterJoined(fieldName, dsRequest.getDataSource()))
    			dsRequest.addToCriteria(fieldName, DefaultOperators.Equals.getID(),
    					User.getUserTenantId(dsRequest.getHttpServletRequest()));
    		else {
    			// xyz_TENANT_ID = x OR xyz_TENANT_ID IS NULL
    			AdvancedCriteria xyzTenantID = new AdvancedCriteria(DefaultOperators.Or, new Criterion[] {
    					new SimpleCriterion(fieldName, DefaultOperators.Equals, User.getUserTenantId(dsRequest
    							.getHttpServletRequest())), new IsNullCriterion(fieldName) });
    [B]			// (old == null) ? new : (old)+(new) or even ((old))+(new)
    			AdvancedCriteria newAc = (dsRequest.getAdvancedCriteria() == null) ? xyzTenantID : new AdvancedCriteria(
    					DefaultOperators.And, new Criterion[] { dsRequest.getAdvancedCriteria().asCriterion(),
    							xyzTenantID.asCriterion() });
    [/B]			dsRequest.setAdvancedCriteria(newAc);
    		}
    	}
    }
    It results in a parenthesis-monster like this (formatted):
    Code:
    SELECT COUNT (*)
      FROM T_RESELLER
           LEFT OUTER JOIN T_CATEGORY1 ON T_RESELLER.CATEGORY1_ID = T_CATEGORY1.ID
           LEFT OUTER JOIN T_CATEGORY2 ON T_RESELLER.CATEGORY2_ID = T_CATEGORY2.ID
           LEFT OUTER JOIN T_CATEGORY3 ON T_RESELLER.CATEGORY3_ID = T_CATEGORY3.ID
           LEFT OUTER JOIN T_CATEGORY4 ON T_RESELLER.CATEGORY4_ID = T_CATEGORY4.ID
           LEFT OUTER JOIN T_CATEGORY5 ON T_RESELLER.CATEGORY5_ID = T_CATEGORY5.ID
           LEFT OUTER JOIN T_CATEGORY6 ON T_RESELLER.CATEGORY6_ID = T_CATEGORY6.ID
           LEFT OUTER JOIN T_CATEGORY7 ON T_RESELLER.CATEGORY7_ID = T_CATEGORY7.ID
           LEFT OUTER JOIN T_CATEGORY8 ON T_RESELLER.CATEGORY8_ID = T_CATEGORY8.ID
           LEFT OUTER JOIN T_CATEGORY9 ON T_RESELLER.CATEGORY9_ID = T_CATEGORY9.ID
           LEFT OUTER JOIN V_CURRENT_ACCOUNT_COUNT ON T_RESELLER.ID = V_CURRENT_ACCOUNT_COUNT.RESELLER_ID
           JOIN V_USER_CREATED_BY ON T_RESELLER.CREATED_BY = V_USER_CREATED_BY.ID
           JOIN V_USER_MODIFIED_BY ON T_RESELLER.MODIFIED_BY = V_USER_MODIFIED_BY.ID
     WHERE (    [B](    (    (    (    (    (    (    (    (    ([/B] (T_RESELLER.TENANT_ID = 0 AND T_RESELLER.TENANT_ID IS NOT NULL))
                                                         AND (   (V_CURRENT_ACCOUNT_COUNT.TENANT_ID = 0 AND V_CURRENT_ACCOUNT_COUNT.TENANT_ID IS NOT NULL)
                                                              OR (V_CURRENT_ACCOUNT_COUNT.TENANT_ID IS NULL))
                                                         AND (V_USER_CREATED_BY.TENANT_ID = 0 AND V_USER_CREATED_BY.TENANT_ID IS NOT NULL)
                                                         AND (V_USER_MODIFIED_BY.TENANT_ID = 0 AND V_USER_MODIFIED_BY.TENANT_ID IS NOT NULL))
                                                    AND (   (T_CATEGORY1.TENANT_ID = 0 AND T_CATEGORY1.TENANT_ID IS NOT NULL)
                                                         OR (T_CATEGORY1.TENANT_ID IS NULL)))
                                               AND ( (T_CATEGORY2.TENANT_ID = 0 AND T_CATEGORY2.TENANT_ID IS NOT NULL) OR (T_CATEGORY2.TENANT_ID IS NULL)))
                                          AND ( (T_CATEGORY3.TENANT_ID = 0 AND T_CATEGORY3.TENANT_ID IS NOT NULL) OR (T_CATEGORY3.TENANT_ID IS NULL)))
                                     AND ( (T_CATEGORY4.TENANT_ID = 0 AND T_CATEGORY4.TENANT_ID IS NOT NULL) OR (T_CATEGORY4.TENANT_ID IS NULL)))
                                AND ( (T_CATEGORY5.TENANT_ID = 0 AND T_CATEGORY5.TENANT_ID IS NOT NULL) OR (T_CATEGORY5.TENANT_ID IS NULL)))
                           AND ( (T_CATEGORY6.TENANT_ID = 0 AND T_CATEGORY6.TENANT_ID IS NOT NULL) OR (T_CATEGORY6.TENANT_ID IS NULL)))
                      AND ( (T_CATEGORY7.TENANT_ID = 0 AND T_CATEGORY7.TENANT_ID IS NOT NULL) OR (T_CATEGORY7.TENANT_ID IS NULL)))
                 AND ( (T_CATEGORY8.TENANT_ID = 0 AND T_CATEGORY8.TENANT_ID IS NOT NULL) OR (T_CATEGORY8.TENANT_ID IS NULL)))
            AND ( (T_CATEGORY9.TENANT_ID = 0 AND T_CATEGORY9.TENANT_ID IS NOT NULL) OR (T_CATEGORY9.TENANT_ID IS NULL)))
    The following expresses the same, but is better readable:
    Code:
    SELECT COUNT (*)
      FROM T_RESELLER
           LEFT OUTER JOIN T_CATEGORY1 ON T_RESELLER.CATEGORY1_ID = T_CATEGORY1.ID
           LEFT OUTER JOIN T_CATEGORY2 ON T_RESELLER.CATEGORY2_ID = T_CATEGORY2.ID
           LEFT OUTER JOIN T_CATEGORY3 ON T_RESELLER.CATEGORY3_ID = T_CATEGORY3.ID
           LEFT OUTER JOIN T_CATEGORY4 ON T_RESELLER.CATEGORY4_ID = T_CATEGORY4.ID
           LEFT OUTER JOIN T_CATEGORY5 ON T_RESELLER.CATEGORY5_ID = T_CATEGORY5.ID
           LEFT OUTER JOIN T_CATEGORY6 ON T_RESELLER.CATEGORY6_ID = T_CATEGORY6.ID
           LEFT OUTER JOIN T_CATEGORY7 ON T_RESELLER.CATEGORY7_ID = T_CATEGORY7.ID
           LEFT OUTER JOIN T_CATEGORY8 ON T_RESELLER.CATEGORY8_ID = T_CATEGORY8.ID
           LEFT OUTER JOIN T_CATEGORY9 ON T_RESELLER.CATEGORY9_ID = T_CATEGORY9.ID
           LEFT OUTER JOIN V_CURRENT_ACCOUNT_COUNT ON T_RESELLER.ID = V_CURRENT_ACCOUNT_COUNT.RESELLER_ID
           JOIN V_USER_CREATED_BY ON T_RESELLER.CREATED_BY = V_USER_CREATED_BY.ID
           JOIN V_USER_MODIFIED_BY ON T_RESELLER.MODIFIED_BY = V_USER_MODIFIED_BY.ID
     WHERE     ( (T_RESELLER.TENANT_ID = 0 AND T_RESELLER.TENANT_ID IS NOT NULL))
           AND ( (V_CURRENT_ACCOUNT_COUNT.TENANT_ID = 0 AND V_CURRENT_ACCOUNT_COUNT.TENANT_ID IS NOT NULL) OR (V_CURRENT_ACCOUNT_COUNT.TENANT_ID IS NULL))
           AND (V_USER_CREATED_BY.TENANT_ID = 0 AND V_USER_CREATED_BY.TENANT_ID IS NOT NULL)
           AND (V_USER_MODIFIED_BY.TENANT_ID = 0 AND V_USER_MODIFIED_BY.TENANT_ID IS NOT NULL)
           AND ( (T_CATEGORY1.TENANT_ID = 0 AND T_CATEGORY1.TENANT_ID IS NOT NULL) OR (T_CATEGORY1.TENANT_ID IS NULL))
           AND ( (T_CATEGORY2.TENANT_ID = 0 AND T_CATEGORY2.TENANT_ID IS NOT NULL) OR (T_CATEGORY2.TENANT_ID IS NULL))
           AND ( (T_CATEGORY3.TENANT_ID = 0 AND T_CATEGORY3.TENANT_ID IS NOT NULL) OR (T_CATEGORY3.TENANT_ID IS NULL))
           AND ( (T_CATEGORY4.TENANT_ID = 0 AND T_CATEGORY4.TENANT_ID IS NOT NULL) OR (T_CATEGORY4.TENANT_ID IS NULL))
           AND ( (T_CATEGORY5.TENANT_ID = 0 AND T_CATEGORY5.TENANT_ID IS NOT NULL) OR (T_CATEGORY5.TENANT_ID IS NULL))
           AND ( (T_CATEGORY6.TENANT_ID = 0 AND T_CATEGORY6.TENANT_ID IS NOT NULL) OR (T_CATEGORY6.TENANT_ID IS NULL))
           AND ( (T_CATEGORY7.TENANT_ID = 0 AND T_CATEGORY7.TENANT_ID IS NOT NULL) OR (T_CATEGORY7.TENANT_ID IS NULL))
           AND ( (T_CATEGORY8.TENANT_ID = 0 AND T_CATEGORY8.TENANT_ID IS NOT NULL) OR (T_CATEGORY8.TENANT_ID IS NULL))
           AND ( (T_CATEGORY9.TENANT_ID = 0 AND T_CATEGORY9.TENANT_ID IS NOT NULL) OR (T_CATEGORY9.TENANT_ID IS NULL))
    Would the latter already be possible, meaning I'm overlooking some API?

    Best regards,
    Blama

    Comment


      #3
      Hi Isomorphic,

      I played around a bit and came up with this:
      Code:
      private static AdvancedCriteria addToCriteria(AdvancedCriteria oldAC, AdvancedCriteria newAC) {
      	if (oldAC == null && newAC == null)
      		return null;
      	else if (oldAC != null && newAC == null)
      		return oldAC;
      	else if (oldAC == null && newAC != null)
      		return newAC;
      	else {
      		if (oldAC.asCriterion().getOperatorId().equals(DefaultOperators.And.getID())) {
      			@SuppressWarnings("unchecked")
      			Map<String, Object> oldCritList = oldAC.getCriteriaAsMap();
      			@SuppressWarnings("unchecked")
      			ArrayList<Object> s = (ArrayList<Object>) oldCritList.get("criteria");
      			s.add(newAC.asCriterion());
      			AdvancedCriteria returnAC = AdvancedCriteria.fromCollections(s, DefaultOperators.And.getID());
      			return returnAC;
      		} else
      			return new AdvancedCriteria(DefaultOperators.And, new Criterion[] { oldAC.asCriterion(), newAC.asCriterion() });
      	}
      }
      In my case, it returned the correct result, but I wouldn't bet on it :)

      Is this safe to use?

      Best regards,
      Blama

      Comment

      Working...
      X