Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * This program is free software; you can redistribute it and/or modify it under the
   * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
   * Foundation.
   *
   * You should have received a copy of the GNU Lesser General Public License along with this
   * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
   * or from the Free Software Foundation, Inc.,
   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Lesser General Public License for more details.
  *
  * Copyright (c) 2009 Pentaho Corporation.  All rights reserved.
  */
 package org.pentaho.pms.example;
 
 import java.util.List;
 
 import  org.pentaho.di.core.database.DatabaseMeta;
 import  org.pentaho.di.core.xml.XMLHandler;
This class shows an example of extending MQLQuery's functionality. While the implementation isn't 100% complete, it demonstrates extending various parts of the MQL XML Serialization / Deserialization, along with algorithm extensions in the AdvancedSQLGenerator class.

Author(s):
Will Gorman (wgorman@pentaho.org)
 
 @SuppressWarnings("deprecation")
 public class AdvancedMQLQuery extends MQLQueryImpl
 {
 
 	private static final Log logger = LogFactory.getLog(AdvancedMQLQuery.class);
 
an example that extends Selection with alias capabilities
 
 	public static class AliasedSelection extends Selection {
     String formula;
     Boolean hasAgg;
 
     public String toString() {
       return "[bc=" + getBusinessColumn() + "; alias=" + "; formula="+"]";
     }
     
 		public AliasedSelection(BusinessColumn columnString alias) {
 			super(column);
 			this. = alias;
 		}
     
     public AliasedSelection(String formulathrows PentahoMetadataException {
       super(null);
       this. = formula;
     }
 
 		public String getAlias() {
 			return ;
 		}
     
     public String getFormula() {
       return ;
     }
 
     public void initPMSFormula(BusinessModel model, DatabaseMeta databaseMetaList<Selectionselectionsthrows PentahoMetadataException{
        = new AliasAwarePMSFormula(modeldatabaseMetaselections.); // formula;
     }
    
    
traverse the field list and see if any of the fields are aggregate fields. we cache hasAgg for future calls

Returns:
true if aggregate
    public boolean hasAggregate() {
      if ( == null) {
         = false;
        if (getBusinessColumn() != null) {
           = getBusinessColumn().hasAggregate();
          return ;
        } else {
        
          for (Selection sel : .getBusinessColumns()) {
            if (sel.hasAggregate()) {
               = true;
              return ;
            }
          }
          
          // the formula may also define an aggregate function... 
          // we need to see if an agg function is defined
          if (.hasAggregateFunction()) {
             = true;
            return ;
          }
        }
      }
      return ;
    }
    
    public PMSFormula getPMSFormula() {
      return ;
    }
    
    public boolean hasFormula() {
      return  != null;
    }
    
		public boolean equals(Object obj) {
             equals(abc.getAlias()) &&
             equals(abc.getFormula());
		}
		public boolean equals(Object aObject b) {
			return (a == null && b == null) || ((a != null && b != null) && a.equals(b));
		}
	}
  public AdvancedMQLQuery(String XML, DatabaseMeta databaseMetaString localeCwmSchemaFactoryInterface factory)
  {
    super(XMLdatabaseMetalocalefactory);
  }
  
	public AdvancedMQLQuery(String XMLString localeCwmSchemaFactoryInterface factory)
	{
		super(XMLnulllocalefactory);
	}
	public AdvancedMQLQuery(SchemaMeta schemaMetaBusinessModel model, DatabaseMeta databaseMeta,
			String locale)
	{
		super(schemaMetamodeldatabaseMetalocale);
	}
  public void addSelection(AliasedSelection selection) {
    if (!selection.hasFormula() && selection.getBusinessColumn() == null) {
      throw new RuntimeException("Error adding selection, no formula or business column specified");
    }
    super.addSelection(selection);
  }
  
overridden method allowing more advanced selection functionality
	protected void addSelectionFromXmlNode(Element selectionElement) {
    String column = null;
    String alias = null;
    String formula = null;
    
		NodeList nodes = selectionElement.getElementsByTagName("column"); //$NON-NLS-1$
		if (nodes.getLength() > 0) {
			column = XMLHandler.getNodeValue(nodes.item(0));
      if ((column != null) && (column.trim().length() == 0)) {
        column = null;
      }
    }
    
    nodes = selectionElement.getElementsByTagName("alias"); //$NON-NLS-1$
    if (nodes.getLength() > 0) {
      alias = XMLHandler.getNodeValue(nodes.item(0));
      if ((alias != null) && (alias.trim().length() == 0)) {
        alias = null;
      }
    }
    
    nodes = selectionElement.getElementsByTagName("formula"); //$NON-NLS-1$
    if (nodes.getLength() > 0) {
      formula = XMLHandler.getNodeValue(nodes.item(0));
      if ((formula != null) && (formula.trim().length() == 0)) {
        formula = null;
      }
    }
    	
    if (column != null) {
  		BusinessColumn businessColumn = getModel().findBusinessColumn(column);
			if (businessColumn != null) {
				addSelection(new AliasedSelection(businessColumnalias));
else {
        throw new RuntimeException("Failed to find business column '" + column + "' in model.");
      }
else if (formula != null) {
      try {
        addSelection(new AliasedSelection(formula));
      } catch (PentahoMetadataException e) {
        throw new RuntimeException(e);
      }
    } else {
      throw new RuntimeException("Failed to parse selection, no column or formula provided");      
    }
	}
  protected void addSelectionToDocument(Document docSelection selectionElement selectionElement) {
    AliasedSelection aliasedSelection = (AliasedSelection)selection;
    BusinessColumn column = selection.getBusinessColumn();
    Element element = doc.createElement("view"); //$NON-NLS-1$
    // element.appendChild( doc.createTextNode( column.getBusinessTable().getId() ) );
    //
    // Work-around for PMD-93 - not using BusinessView in the MQL.
    if (column != null) {
      BusinessCategory rootCat = getModel().getRootCategory();
      BusinessCategory businessCategory = rootCat.findBusinessCategoryForBusinessColumn(column);
      element.appendChild(doc.createTextNode(businessCategory.getId()));
  
      selectionElement.appendChild(element);
  
      element = doc.createElement("column"); //$NON-NLS-1$
      element.appendChild(doc.createTextNode(column.getId()));
      selectionElement.appendChild(element);
      if (aliasedSelection.getAlias() != null) {
        element = doc.createElement("alias"); //$NON-NLS-1$
        element.appendChild(doc.createTextNode(aliasedSelection.getAlias()));
        selectionElement.appendChild(element);
      }
    } else if (aliasedSelection.getFormula() != null) {
      element = doc.createElement("formula"); //$NON-NLS-1$
      element.appendChild(doc.createTextNode(aliasedSelection.getFormula()));
      selectionElement.appendChild(element);
    }
  }
  protected void addOrderByFromXmlNode(Element orderElementthrows PentahoMetadataException {
    boolean ascending = true;
    String view = null;
    String column = null;
    String formula = null;
    String alias = null;
    NodeList nodes = orderElement.getElementsByTagName("direction"); //$NON-NLS-1$
    if (nodes.getLength() > 0) {
      ascending = XMLHandler.getNodeValue(nodes.item(0)).equals("asc"); //$NON-NLS-1$
    }
    nodes = orderElement.getElementsByTagName("view"); //$NON-NLS-1$
    if (nodes.getLength() > 0) {
      view = XMLHandler.getNodeValue(nodes.item(0));
    }
    nodes = orderElement.getElementsByTagName("column"); //$NON-NLS-1$
    if (nodes.getLength() > 0) {
      column = XMLHandler.getNodeValue(nodes.item(0));
    }
    nodes = orderElement.getElementsByTagName("alias"); //$NON-NLS-1$
    if (nodes.getLength() > 0) {
      alias = XMLHandler.getNodeValue(nodes.item(0));
    }
    nodes = orderElement.getElementsByTagName("formula"); //$NON-NLS-1$
    if (nodes.getLength() > 0) {
      formula = XMLHandler.getNodeValue(nodes.item(0));
    }
    if (view != null && column != null) {
      BusinessCategory rootCat = getModel().getRootCategory();
      BusinessCategory businessCategory = rootCat.findBusinessCategory(view);
      if (businessCategory == null) {
        throw new PentahoMetadataException(Messages.getErrorString(
            "MQLQuery.ERROR_0014_BUSINESS_CATEGORY_NOT_FOUND"view)); //$NON-NLS-1$ 
      }
      BusinessColumn businessColumn = businessCategory.findBusinessColumn(column);
      if (businessColumn == null) {
        throw new PentahoMetadataException(Messages.getErrorString(
            "MQLQuery.ERROR_0016_BUSINESS_COLUMN_NOT_FOUND"businessCategory.getId(), column)); //$NON-NLS-1$ 
      }
      getOrder().add(new OrderBy(new AliasedSelection(businessColumnalias), ascending));
    } else if (formula != null) {
      getOrder().add(new OrderBy(new AliasedSelection(formula), ascending));
    } else {
      throw new PentahoMetadataException("no column or formula specified"); //$NON-NLS-1$ 
    }
  }
  
  protected void addOrderByToDocument(Document docOrderBy orderByElement orderElement) {
    Element element = doc.createElement("direction"); //$NON-NLS-1$
    element.appendChild(doc.createTextNode(orderBy.isAscending() ? "asc" : "desc")); //$NON-NLS-1$ //$NON-NLS-2$
    orderElement.appendChild(element);
    // Work-around for PMD-93 - Need this to be better into the future...
    AliasedSelection selection = (AliasedSelection)orderBy.getSelection();
    if (!selection.hasFormula()) {
      element = doc.createElement("view"); //$NON-NLS-1$
      BusinessCategory rootCat = getModel().getRootCategory();
      BusinessCategory businessView = rootCat.findBusinessCategoryForBusinessColumn(selection.getBusinessColumn());
      element.appendChild(doc.createTextNode(businessView.getId()));
  
      orderElement.appendChild(element);
      element = doc.createElement("column"); //$NON-NLS-1$
      element.appendChild(doc.createTextNode(selection.getBusinessColumn().getId()));
      orderElement.appendChild(element);
      
      if (selection.getAlias() != null) {
        orderElement.appendChild(element);
        element = doc.createElement("alias"); //$NON-NLS-1$
        element.appendChild(doc.createTextNode(selection.getAlias()));
        orderElement.appendChild(element);
      }
    } else {
      orderElement.appendChild(element);
      element = doc.createElement("formula"); //$NON-NLS-1$
      element.appendChild(doc.createTextNode(selection.getFormula()));
      orderElement.appendChild(element);
    }
  }
  
  
override addConstraint to use own where condition

Parameters:
operator operator to separate constraints with
condition MQL Function which defines the constraint
 
	public void addConstraint(String operatorString conditionthrows PentahoMetadataException
	{
		List<Selectionaliases = super.getSelections();
		WhereCondition where = new WhereCondition(formulaoperatorcondition);
	}
  public void addOrderBy(AliasedSelection selection,  boolean ascending) {
    getOrder().add(new OrderBy(selectionascending));
  }
	{
	}
New to GrepCode? Check out our FAQ X