From: <tri...@us...> - 2008-03-21 13:15:44
|
Revision: 401 http://equanda.svn.sourceforge.net/equanda/?rev=401&view=rev Author: triathlon98 Date: 2008-03-21 06:15:37 -0700 (Fri, 21 Mar 2008) Log Message: ----------- EQ-47 improved data-filter Modified Paths: -------------- trunk/equanda-client/src/main/java/org/equanda/persistence/ObjectSelector.java trunk/equanda-client/src/main/java/org/equanda/persistence/query/EquandaQuery.java trunk/equanda-client/src/main/java/org/equanda/persistence/query/EquandaQueryImpl.java trunk/equanda-generate/src/main/java/org/equanda/domain/xml/DataFilter.java trunk/equanda-generate/src/main/java/org/equanda/domain/xml/aid/TransformationUtil.java trunk/equanda-generate/src/main/java/org/equanda/domain/xml/transform/RootTable.java trunk/equanda-generate/src/main/java/org/equanda/generate/Generator.java trunk/equanda-generate/src/main/resources/equanda.dtd trunk/equanda-generate/src/main/velocity/ejb3/DMObjectSelector.java.vm trunk/equanda-generate/src/test/java/org/equanda/domain/xml/DataFilterTest.java trunk/equanda-generate/src/test/java/org/equanda/domain/xml/DefaultTest.java trunk/equanda-test/pom.xml trunk/equanda-test/src/main/dm/FootballPlayer.table trunk/equanda-test/test-test/src/test/java/org/equanda/test/xejb/SelectorsFiltersTest.java Added Paths: ----------- trunk/equanda-generate/src/main/java/org/equanda/domain/xml/FilterQuery.java Removed Paths: ------------- trunk/equanda-generate/src/main/java/org/equanda/domain/xml/aid/DataFilterConverter.java Modified: trunk/equanda-client/src/main/java/org/equanda/persistence/ObjectSelector.java =================================================================== --- trunk/equanda-client/src/main/java/org/equanda/persistence/ObjectSelector.java 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-client/src/main/java/org/equanda/persistence/ObjectSelector.java 2008-03-21 13:15:37 UTC (rev 401) @@ -24,13 +24,13 @@ package org.equanda.persistence; -import javolution.lang.TextBuilder; +import org.apache.log4j.Logger; import org.equanda.persistence.query.EquandaQuery; - +import java.util.Collection; +import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.Query; -import java.util.Collection; -import java.util.Map; +import javolution.lang.TextBuilder; /** * Support methods for DMObjectSelector classes @@ -39,14 +39,33 @@ */ public abstract class ObjectSelector { + private static final Logger log = Logger.getLogger( ObjectSelector.class ); + public static final String FILTER_PARAMETER_PREFIX = "equandaFilter"; + protected static Query getEquandaQuery( EntityManager em, EquandaQuery equandaQuery ) { - String queryString = addTypeFilter( equandaQuery.getQueryString(), equandaQuery.getTypeFilter() ); + return getEquandaQuery( em, equandaQuery, null, true ); + } + + protected static Query getEquandaQuery( EntityManager em, EquandaQuery equandaQuery, String queryString ) + { + return getEquandaQuery( em, equandaQuery, queryString, true ); + } + + protected static Query getEquandaQuery( EntityManager em, EquandaQuery equandaQuery, String queryString, + boolean setLimits ) + { + if ( queryString == null ) queryString = equandaQuery.getQueryString(); + queryString = addTypeFilter( queryString, equandaQuery.getTypeFilter() ); + if ( log.isDebugEnabled() ) log.debug( "create Query for " + queryString ); Query query = em.createQuery( queryString ); - Map<String, Object> params = equandaQuery.getParameters(); - for ( String name : params.keySet() ) + + Map<String, Object> parameters = equandaQuery.getParameters(); + for ( String name : parameters.keySet() ) query.setParameter( name, parameters.get( name ) ); + if ( setLimits ) { - query.setParameter( name, params.get( name ) ); + if ( equandaQuery.getMaxResults() > 0 ) query.setMaxResults( equandaQuery.getMaxResults() ); + if ( equandaQuery.getFirstResult() > 0 ) query.setFirstResult( equandaQuery.getFirstResult() ); } return query; } @@ -80,42 +99,60 @@ return obj; } - private static String addFilter( String query, String filter ) + private static String addFilter( String orgQuery, String fromPart, String wherePart ) { - StringBuffer res = new StringBuffer( query ); - if ( filter == null ) return query; + StringBuffer query = new StringBuffer( orgQuery ); - if ( query.indexOf( " ORDER " ) == -1 ) + if ( fromPart != null && fromPart.length() > 0 ) { - if ( query.indexOf( " WHERE " ) == -1 ) + String rest = ""; + int insertPos = query.indexOf( " WHERE " ); + if ( insertPos == -1 ) insertPos = query.indexOf( " ORDER " ); + if ( insertPos >= 0 ) { - res.append( " WHERE " ); - res.append( filter ); + rest = query.substring( insertPos ); + query.setLength( insertPos ); } - else - { - res.append( " AND " ); - res.append( filter ); - } + query.append( ", " ); + query.append( fromPart ); + query.append( rest ); } - else + + if ( wherePart != null && wherePart.length() > 0 ) { + int orderPos = query.indexOf( " ORDER " ); - String order = res.substring( orderPos ); - res.replace( orderPos, res.length(), "" ); - if ( res.indexOf( " WHERE " ) == -1 ) + if ( orderPos == -1 ) { - res.append( " WHERE " ); - res.append( filter ); + if ( query.indexOf( " WHERE " ) == -1 ) + { + query.append( " WHERE " ); + query.append( wherePart ); + } + else + { + query.append( " AND " ); + query.append( wherePart ); + } } else { - res.append( " AND " ); - res.append( filter ); + String order = query.substring( orderPos ); + query.setLength( orderPos ); + if ( query.indexOf( " WHERE " ) == -1 ) + { + query.append( " WHERE " ); + query.append( wherePart ); + } + else + { + query.append( " AND " ); + query.append( wherePart ); + } + query.append( order ); } - res.append( order ); } - return res.toString(); + return query.toString(); } protected static String addTypeFilter( String query, ObjectType type ) @@ -139,7 +176,7 @@ sep = ","; } filter.append( ')' ); - return addFilter( query, filter.toString() ); + return addFilter( query, "", filter.toString() ); } /** @@ -205,6 +242,7 @@ */ protected static String setOrderBy( String queryStr, String fieldName ) { + if ( fieldName == null ) return queryStr; TextBuilder builder = TextBuilder.newInstance(); builder.append( queryStr ); int orderByPos = queryStr.indexOf( " ORDER BY " ); @@ -229,18 +267,31 @@ /** * Adds one extra filter to the query * + * @param equandaQuery original query, filter parameters are added in this * @param queryStr old query * @param name name of the filter - * @param queryPart how filter is to be applied in the query + * @param fromPart stuff which needs to be inserted in the "from" part of the query + * @param wherePart stuff which needs to be inserted in the "where" part of the query * @return the new calculated query */ - protected static String setFilter( String queryStr, String name, String queryPart ) + protected static String setFilter( EquandaQuery equandaQuery, String queryStr, String name, String fromPart, + String wherePart ) { String value = SelectorsState.getFilter( name ); if ( null != value ) { - queryPart = queryPart.replace( "${}", value ); - queryStr = addFilter( queryStr, queryPart ); + if ( null == fromPart ) fromPart = ""; + if ( null == wherePart ) wherePart = ""; + if ( fromPart.length() == 0 && wherePart.length() == 0 ) return queryStr; + + String parname = FILTER_PARAMETER_PREFIX + name; + if ( wherePart.contains( "${}" ) || fromPart.contains( "${}" ) ) equandaQuery.addExtra( parname, value ); + parname = ":" + parname; + queryStr = addFilter( queryStr, fromPart.replace( "${}", parname ), wherePart.replace( "${}", parname ) ); + if ( log.isDebugEnabled() ) + { + log.debug( "setFilter result query " + queryStr + " | inserted " + fromPart + "|" + wherePart ); + } } return queryStr; } Modified: trunk/equanda-client/src/main/java/org/equanda/persistence/query/EquandaQuery.java =================================================================== --- trunk/equanda-client/src/main/java/org/equanda/persistence/query/EquandaQuery.java 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-client/src/main/java/org/equanda/persistence/query/EquandaQuery.java 2008-03-21 13:15:37 UTC (rev 401) @@ -25,7 +25,6 @@ package org.equanda.persistence.query; import org.equanda.persistence.ObjectType; - import java.io.Serializable; import java.util.Map; @@ -49,7 +48,7 @@ String getCountQueryString(); /** - * Gives the maximum number of results. If a value <code>lt;=0</code> then it will be ignored. + * Gives the maximum number of results. If a value <code><=0</code> then it will be ignored. * * @return the maximum number of results. */ @@ -64,14 +63,14 @@ EquandaQuery setMaxResults( int maxResults ); /** - * Gives the starting results index. If a value <code>lt;0</code> then it will be ignored. + * Gives the starting results index. If a value <code><0</code> then it will be ignored. * * @return the starting results index. */ int getFirstResult(); /** - * Sets the starting results index. If a value <code>lt;0</code> then it will be ignored. + * Sets the starting results index. If a value <code><0</code> then it will be ignored. * * @param firstResult number of first record which needs to be fetched * @return the starting results index. @@ -79,19 +78,22 @@ EquandaQuery setFirstResult( int firstResult ); /** - * @return the parametres for this query + * @return the parameters for this query */ Map<String, Object> getParameters(); /** * Add a list of parameters + * * @param parameters parameters nam/value pairs * @return query object itself */ EquandaQuery setParameters( Map<String, Object> parameters ); /** - * Add one parameter + * Add one parameter (would have been more logical to call this "addParameter", but this is more in line with the + * AJPA/Hibernate Query interface). + * * @param name parameter name * @param value parameter value * @return query object itself @@ -106,4 +108,18 @@ ObjectType getTypeFilter(); EquandaQuery setTypeFilter( ObjectType typeFilter ); + + /** + * Allows adding extra parameters which can be cleared from the query. + * This is used internally for adding the parameters to the filters. + * + * @param name extra parameter name + * @param value extra parameter value + */ + void addExtra( String name, Object value ); + + /** + * Clear the extra parameters + */ + void clearExtra(); } Modified: trunk/equanda-client/src/main/java/org/equanda/persistence/query/EquandaQueryImpl.java =================================================================== --- trunk/equanda-client/src/main/java/org/equanda/persistence/query/EquandaQueryImpl.java 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-client/src/main/java/org/equanda/persistence/query/EquandaQueryImpl.java 2008-03-21 13:15:37 UTC (rev 401) @@ -25,7 +25,6 @@ package org.equanda.persistence.query; import org.equanda.persistence.ObjectType; - import java.util.HashMap; import java.util.Map; @@ -42,6 +41,7 @@ private int maxResults; private int firstResult; private Map<String, Object> parameters; + private Map<String, Object> extra; private ObjectType typeFilter; public EquandaQueryImpl() @@ -120,7 +120,11 @@ public Map<String, Object> getParameters() { - return parameters; + if ( null == extra ) return parameters; + Map<String, Object> res = new HashMap<String, Object>(); + if ( parameters != null ) res.putAll( parameters ); + res.putAll( extra ); + return res; } public EquandaQueryImpl setParameters( Map<String, Object> parameters ) @@ -145,4 +149,15 @@ this.typeFilter = typeFilter; return this; } + + public void addExtra( String name, Object value ) + { + if ( null == extra ) extra = new HashMap<String, Object>(); + extra.put( name, value ); + } + + public void clearExtra() + { + if ( null != extra ) extra.clear(); + } } Modified: trunk/equanda-generate/src/main/java/org/equanda/domain/xml/DataFilter.java =================================================================== --- trunk/equanda-generate/src/main/java/org/equanda/domain/xml/DataFilter.java 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-generate/src/main/java/org/equanda/domain/xml/DataFilter.java 2008-03-21 13:15:37 UTC (rev 401) @@ -24,9 +24,10 @@ package org.equanda.domain.xml; -import org.equanda.domain.xml.aid.DataFilterConverter; import com.thoughtworks.xstream.annotations.XStreamAlias; -import com.thoughtworks.xstream.annotations.XStreamConverter; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; +import java.util.ArrayList; +import java.util.List; /** * Implementation for data-filter @@ -34,12 +35,14 @@ * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> */ @XStreamAlias( "data-filter" ) -@XStreamConverter( DataFilterConverter.class ) public class DataFilter { + @XStreamAsAttribute private String name; - private String value; + @XStreamAlias( "filter-query" ) + private FilterQuery filterQuery; + public String getName() { return name; @@ -51,14 +54,9 @@ this.name = name; } - public String getValue() + public FilterQuery getFilterQuery() { - return value; + if ( null == filterQuery ) filterQuery = new FilterQuery(); + return filterQuery; } - - public void setValue( String value ) - { - if ( "".equals( value ) ) value = null; - this.value = value; - } } Added: trunk/equanda-generate/src/main/java/org/equanda/domain/xml/FilterQuery.java =================================================================== --- trunk/equanda-generate/src/main/java/org/equanda/domain/xml/FilterQuery.java (rev 0) +++ trunk/equanda-generate/src/main/java/org/equanda/domain/xml/FilterQuery.java 2008-03-21 13:15:37 UTC (rev 401) @@ -0,0 +1,55 @@ +/** + * This file is part of the equanda project. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + */ + +package org.equanda.domain.xml; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; + +/** + * Implementation for filter-query + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +@XStreamAlias( "filter-query" ) +public class FilterQuery +{ + @XStreamAsAttribute + private String from; + + @XStreamAsAttribute + private String where; + + public String getFrom() + { + if ( null == from ) from = ""; + return from; + } + + public String getWhere() + { + if ( null == where ) where = ""; + return where; + } +} Deleted: trunk/equanda-generate/src/main/java/org/equanda/domain/xml/aid/DataFilterConverter.java =================================================================== --- trunk/equanda-generate/src/main/java/org/equanda/domain/xml/aid/DataFilterConverter.java 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-generate/src/main/java/org/equanda/domain/xml/aid/DataFilterConverter.java 2008-03-21 13:15:37 UTC (rev 401) @@ -1,65 +0,0 @@ -/** - * This file is part of the equanda project. - * - * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF - * ANY KIND, either express or implied. See the License for the specific language governing rights and - * limitations under the License. - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - */ - -package org.equanda.domain.xml.aid; - -import org.equanda.domain.xml.DataFilter; -import com.thoughtworks.xstream.converters.Converter; -import com.thoughtworks.xstream.converters.MarshallingContext; -import com.thoughtworks.xstream.converters.UnmarshallingContext; -import com.thoughtworks.xstream.io.HierarchicalStreamReader; -import com.thoughtworks.xstream.io.HierarchicalStreamWriter; - -/** - * Converter for overwrite-default node. - * - * @author Joachim Van der Auwera <jo...@pr...> - */ -public class DataFilterConverter - implements Converter -{ - private static final String ATTRIBUTE_NAME = "name"; - - public void marshal( Object source, HierarchicalStreamWriter writer, - MarshallingContext context ) - { - DataFilter value = (DataFilter) source; - if ( value.getName() != null ) writer.addAttribute( ATTRIBUTE_NAME, value.getName() ); - if ( value.getValue() != null ) writer.setValue( value.getValue() ); - } - - public Object unmarshal( HierarchicalStreamReader reader, - UnmarshallingContext context ) - { - DataFilter res = new DataFilter(); - res.setName( reader.getAttribute( ATTRIBUTE_NAME ) ); - res.setValue( reader.getValue() ); - return res; - } - - public boolean canConvert( Class type ) - { - return type.equals( DataFilter.class ); - } -} Modified: trunk/equanda-generate/src/main/java/org/equanda/domain/xml/aid/TransformationUtil.java =================================================================== --- trunk/equanda-generate/src/main/java/org/equanda/domain/xml/aid/TransformationUtil.java 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-generate/src/main/java/org/equanda/domain/xml/aid/TransformationUtil.java 2008-03-21 13:15:37 UTC (rev 401) @@ -105,5 +105,16 @@ } return field; } + + public static String toVarName( String str ) + { + if ( str == null ) return null; + if ( str.length() == 0 ) return str; + String res = java.beans.Introspector.decapitalize( str ); + if ( "default".equals( res ) ) return "xdefault"; + if ( "class".equals( res ) ) return "xclass"; + if ( "enum".equals( res ) ) return "xenum"; + return res; + } } Modified: trunk/equanda-generate/src/main/java/org/equanda/domain/xml/transform/RootTable.java =================================================================== --- trunk/equanda-generate/src/main/java/org/equanda/domain/xml/transform/RootTable.java 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-generate/src/main/java/org/equanda/domain/xml/transform/RootTable.java 2008-03-21 13:15:37 UTC (rev 401) @@ -28,6 +28,7 @@ import org.equanda.domain.xml.aid.BooleanTest; import org.equanda.domain.xml.aid.ObjectGetter; import org.equanda.domain.xml.aid.ValidationUtil; +import org.equanda.domain.xml.aid.TransformationUtil; import org.equanda.util.StringUtil; import java.util.*; @@ -230,7 +231,7 @@ public String getVarName() { - return java.beans.Introspector.decapitalize( name ); + return TransformationUtil.toVarName( name ); } public boolean isCloneable() @@ -794,6 +795,11 @@ return getTable().getDataFilters(); } + public boolean hasDataFilters() + { + return getTable().getDataFilters().size() > 0; + } + public long getLastModified() { return lastModified; Modified: trunk/equanda-generate/src/main/java/org/equanda/generate/Generator.java =================================================================== --- trunk/equanda-generate/src/main/java/org/equanda/generate/Generator.java 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-generate/src/main/java/org/equanda/generate/Generator.java 2008-03-21 13:15:37 UTC (rev 401) @@ -56,6 +56,7 @@ import org.apache.xml.serialize.XMLSerializer; import org.equanda.domain.db.Convert; import org.equanda.domain.xml.*; +import org.equanda.domain.xml.aid.TransformationUtil; import org.equanda.domain.xml.transform.RootTable; import org.equanda.util.IniFile; import org.equanda.util.SaveException; @@ -620,11 +621,7 @@ */ public String lowerFirst( String str ) { - if ( str == null ) return null; - if ( str.length() == 0 ) return str; - str = str.substring( 0, 1 ).toLowerCase() + str.substring( 1 ); - if ( "default".equals( str ) ) str = "xdefault"; - return str; + return TransformationUtil.toVarName( str ); } /** Modified: trunk/equanda-generate/src/main/resources/equanda.dtd =================================================================== --- trunk/equanda-generate/src/main/resources/equanda.dtd 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-generate/src/main/resources/equanda.dtd 2008-03-21 13:15:37 UTC (rev 401) @@ -133,13 +133,22 @@ <!-- These are filters which are applied to all the selectors. - The name is the name of the selector, the value is used as "where" clause in the query, where the "${}" is replaced by the filter-value. + The name is the name of the selector, children define how the filter is applied (in this table). --> -<!ELEMENT data-filter (#PCDATA)> +<!ELEMENT data-filter (filter-query?)> <!ATTLIST data-filter name CDATA #REQUIRED> +<!-- + Defines how the filter value can be applied in a ejbql query.The "${}" is replaced by the filter-value. +--> +<!ELEMENT filter-query EMPTY> +<!ATTLIST filter-query +from CDATA #IMPLIED +where CDATA #REQUIRED> + + <!-- Allow grouping of fields. In a GUI this could be represented by a box around the items. --> <!ELEMENT group ((description)?,(field|group|include)*, (hide)?)> <!ATTLIST group Modified: trunk/equanda-generate/src/main/velocity/ejb3/DMObjectSelector.java.vm =================================================================== --- trunk/equanda-generate/src/main/velocity/ejb3/DMObjectSelector.java.vm 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-generate/src/main/velocity/ejb3/DMObjectSelector.java.vm 2008-03-21 13:15:37 UTC (rev 401) @@ -40,9 +40,9 @@ import org.equanda.persistence.SelectorsState; import javax.persistence.EntityManager; import javax.persistence.Query; +import java.util.ArrayList; import java.util.List; -import java.util.Collection; -import java.util.ArrayList; +import java.util.Map; /** * Utility class which combined the selectors for $table.Name @@ -206,6 +206,9 @@ // handle EquandaQuery public static List<DM${table.Name}Bean> equandaRunQuery( EntityManager em, EquandaQuery equandaQuery ) { +#if( $table.hasDataFilters() ) + try { +#end String sorting = SelectorsState.getSorting( "$table.Name" ); Query query; #if( !$table.hasDataFilters() ) @@ -219,23 +222,17 @@ String queryString = equandaQuery.getQueryString(); queryString = setOrderBy( queryString, sorting ); #if( $table.hasDataFilters() ) - queryString = setFilters( queryString ); + queryString = setFilters( equandaQuery, queryString ); #end - EquandaQuery calculatedQuery = new EquandaQueryImpl( queryString ); - query = getEquandaQuery(em, calculatedQuery); + query = getEquandaQuery( em, equandaQuery, queryString ); #if( !$table.hasDataFilters() ) } #end - if ( equandaQuery.getMaxResults() > 0 ) query.setMaxResults( equandaQuery.getMaxResults() ); - - if ( equandaQuery.getFirstResult() > 0 ) query.setFirstResult( equandaQuery.getFirstResult() ); - - for ( String name : equandaQuery.getParameters().keySet() ) - { - query.setParameter( name, equandaQuery.getParameters().get( name ) ); - } return (List<DM${table.Name}Bean>) query.getResultList(); +#if( $table.hasDataFilters() ) + } finally { equandaQuery.clearExtra(); } +#end } // count for uniqueness @@ -253,8 +250,7 @@ public static DM${table.Name}Bean selectId(EntityManager em, Uoid uoid) { - // no flush needed, if the entity is already managed it is used directly - // if it is not managed, then it cannot be dirty :-) + // no flush needed, if entity is already managed it is used directly, if not then it cannot be dirty :-) return em.find( DM${table.Name}Bean.class, uoid ); } @@ -264,13 +260,11 @@ * @param queryString base query string * @return query string with filters applied */ - public static String setFilters( String queryString ) + public static String setFilters( EquandaQuery equandaQuery, String queryString ) { - #if( !$table.hasDataFilters() ) - #foreach( $filter in $table.DataFilters.keySet() ) - queryString = setFilter( queryString, "$filter", "$table.DataFilters.get( $filter )" ); - #end - #end +#foreach( $filter in $table.DataFilters ) + queryString = setFilter( equandaQuery, queryString, "$filter.Name", "$filter.FilterQuery.From", "$filter.FilterQuery.Where" ); +#end return queryString; } @@ -283,20 +277,26 @@ */ public static List runCountQuery( EntityManager em, EquandaQuery equandaQuery ) { +#if( $table.hasDataFilters() ) + try { +#end // make sure the query will return the latest version of the data (including in-transaction changes) //em.flush(); Query query; String queryString = getRunCountQueryString( equandaQuery ); - queryString = setFilters( queryString ); - query = em.createQuery( queryString ); +#if( $table.hasDataFilters() ) + queryString = setFilters( equandaQuery, queryString ); +#end + query = getEquandaQuery( em, equandaQuery, queryString, false ); - for ( String name : equandaQuery.getParameters().keySet() ) - { - query.setParameter( name, equandaQuery.getParameters().get( name ) ); - } + Map<String, Object> parameters = equandaQuery.getParameters(); + for ( String name : parameters.keySet() ) query.setParameter( name, parameters.get( name ) ); return query.getResultList(); +#if( $table.hasDataFilters() ) + } finally { equandaQuery.clearExtra(); } +#end } } Modified: trunk/equanda-generate/src/test/java/org/equanda/domain/xml/DataFilterTest.java =================================================================== --- trunk/equanda-generate/src/test/java/org/equanda/domain/xml/DataFilterTest.java 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-generate/src/test/java/org/equanda/domain/xml/DataFilterTest.java 2008-03-21 13:15:37 UTC (rev 401) @@ -40,13 +40,18 @@ String test = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE equanda SYSTEM \"equanda.dtd\">\n" + "\n" + - "<data-filter name=\"filtername\">filter-value</data-filter>"; + "<data-filter name=\"filtername\">" + + " <filter-query from=\"add-to-from\" where=\"filter-value\"/>" + + "</data-filter>"; XStream stream = getXStreamInstance(); Object res = stream.fromXML( test ); assertTrue( res instanceof DataFilter ); DataFilter datafilter = (DataFilter) res; assertEquals( "filtername", datafilter.getName() ); - assertEquals( "filter-value", datafilter.getValue() ); + FilterQuery fq = datafilter.getFilterQuery(); + assertNotNull( fq ); + assertEquals( "add-to-from", fq.getFrom() ); + assertEquals( "filter-value", fq.getWhere() ); } public void testParse2() @@ -61,6 +66,8 @@ assertTrue( res instanceof DataFilter ); DataFilter datafilter = (DataFilter) res; assertNull( datafilter.getName() ); - assertNull( datafilter.getValue() ); + assertNotNull( datafilter.getFilterQuery() ); + assertEquals( "", datafilter.getFilterQuery().getFrom() ); + assertEquals( "", datafilter.getFilterQuery().getWhere() ); } } Modified: trunk/equanda-generate/src/test/java/org/equanda/domain/xml/DefaultTest.java =================================================================== --- trunk/equanda-generate/src/test/java/org/equanda/domain/xml/DefaultTest.java 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-generate/src/test/java/org/equanda/domain/xml/DefaultTest.java 2008-03-21 13:15:37 UTC (rev 401) @@ -28,7 +28,7 @@ import org.equanda.domain.xml.aid.DefaultType; /** - * test for data-filter class + * test for "Default" class * * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> */ Modified: trunk/equanda-test/pom.xml =================================================================== --- trunk/equanda-test/pom.xml 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-test/pom.xml 2008-03-21 13:15:37 UTC (rev 401) @@ -177,7 +177,7 @@ </dependencyManagement> <properties> - <tapestry-version>5.0.11-SNAPSHOT</tapestry-version> + <tapestry-version>5.0.11</tapestry-version> </properties> </project> Modified: trunk/equanda-test/src/main/dm/FootballPlayer.table =================================================================== --- trunk/equanda-test/src/main/dm/FootballPlayer.table 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-test/src/main/dm/FootballPlayer.table 2008-03-21 13:15:37 UTC (rev 401) @@ -2,23 +2,29 @@ <!DOCTYPE table SYSTEM "equanda.dtd"> <table name="FootballPlayer"> - <data-filter name="GoalkeepersFilter"></data-filter> - <data-filter name="DefendersFilter"></data-filter> - <data-filter name="MidfieldersFilter"></data-filter> - <data-filter name="StrikersFilter"></data-filter> - <data-filter name="GoalkeepersHideFilter"></data-filter> - <data-filter name="DefendersHideFilter"></data-filter> - <data-filter name="MidfieldersHideFilter"></data-filter> - <data-filter name="StrikersHideFilters"></data-filter> + <data-filter name="GoalkeepersFilter"><filter-query where="o.goalkeeper=1"/></data-filter> + <data-filter name="DefendersFilter"><filter-query where="o.defender=1"/></data-filter> + <data-filter name="MidfieldersFilter"><filter-query where="o.midfielder=1"/></data-filter> + <data-filter name="StrikersFilter"><filter-query where="o.striker=1"/></data-filter> + <data-filter name="GoalkeepersHideFilter"><filter-query where="o.goalkeeper=0"/></data-filter> + <data-filter name="DefendersHideFilter"><filter-query where="o.defender=0"/></data-filter> + <data-filter name="MidfieldersHideFilter"><filter-query where="o.midfielder=0"/></data-filter> + <data-filter name="StrikersHideFilters"><filter-query where="o.striker=0"/></data-filter> + <data-filter name="Team"><filter-query where="o.team=${}"/></data-filter> + <data-filter name="Tag"><filter-query from="IN (o.tags) tags" where="tags.tags=${}"/></data-filter> <security-role-remove>LocalUser,AdminUser</security-role-remove> <page> <field name="Name" type="string"> <display/> </field> + <field name="Team"/> <field name="Goalkeeper" type="boolean"/> <field name="Defender" type="boolean"/> <field name="Midfielder" type="boolean"/> <field name="Striker" type="boolean"/> + <field name="Tags" type="string"> + <multiple/> + </field> </page> <select name="All" order="EQUANDA_MODIFIED"/> <select name="FieldPlayers"> Modified: trunk/equanda-test/test-test/src/test/java/org/equanda/test/xejb/SelectorsFiltersTest.java =================================================================== --- trunk/equanda-test/test-test/src/test/java/org/equanda/test/xejb/SelectorsFiltersTest.java 2008-03-19 23:26:39 UTC (rev 400) +++ trunk/equanda-test/test-test/src/test/java/org/equanda/test/xejb/SelectorsFiltersTest.java 2008-03-21 13:15:37 UTC (rev 401) @@ -27,8 +27,8 @@ import org.equanda.test.dm.client.EquandaGlobal; import org.equanda.test.dm.client.FootballPlayer; import org.equanda.test.dm.client.FootballPlayerSelectorEJB; - import java.util.List; +import java.util.ArrayList; /** * Tests for the selectors filters (setting/unsetting the variable should affect the order of the results) @@ -38,8 +38,7 @@ public class SelectorsFiltersTest extends TestAdapter { - /* @todo temporarily disabled as the filters behaviour/configuation needs to change, see EQ-47 - public void testFilters() + public void testNoParameterFilter() throws Exception { FootballPlayer playerG = new FootballPlayer(); @@ -66,7 +65,7 @@ List<FootballPlayer> results = sel.selectAll(); assertEquals( 4, results.size() ); - SelectorsState.setFilter( "GoalkeepersFilter", "o.goalkeeper=1" ); + SelectorsState.setFilter( "GoalkeepersFilter", "true" ); results = sel.selectAll(); assertEquals( 1, results.size() ); @@ -84,5 +83,116 @@ playerM.removeEntityBean(); playerS.removeEntityBean(); } - */ + + public void testParameterFilter() + throws Exception + { + FootballPlayer playerG = new FootballPlayer(); + playerG.setGoalkeeper( true ); + playerG.setName( "G" ); + playerG.setTeam( "Anderlecht" ); + playerG.equandaUpdate(); + + FootballPlayer playerD = new FootballPlayer(); + playerD.setDefender( true ); + playerD.setName( "D" ); + playerD.setTeam( "Anderlecht" ); + playerD.equandaUpdate(); + + FootballPlayer playerM = new FootballPlayer(); + playerM.setMidfielder( true ); + playerM.setName( "M" ); + playerM.setTeam( "Standard" ); + playerM.equandaUpdate(); + + FootballPlayer playerS = new FootballPlayer(); + playerS.setMidfielder( true ); + playerS.setName( "S" ); + playerS.setTeam( "Standard" ); + playerS.equandaUpdate(); + + FootballPlayerSelectorEJB sel = EquandaGlobal.getFootballPlayerSelector(); + List<FootballPlayer> results = sel.selectAll(); + assertEquals( 4, results.size() ); + + SelectorsState.setFilter( "Team", "Standard" ); + results = sel.selectAll(); + assertEquals( 2, results.size() ); + assertEquals( "M", results.get( 0 ).getName() ); + assertEquals( "S", results.get( 1 ).getName() ); + + SelectorsState.setFilter( "Team", null ); + results = sel.selectAll(); + assertEquals( 4, results.size() ); + + sel.remove(); + + playerG.removeEntityBean(); + playerD.removeEntityBean(); + playerM.removeEntityBean(); + playerS.removeEntityBean(); + } + + public void testFromFilter() + throws Exception + { + List<String> tags; + + FootballPlayer playerG = new FootballPlayer(); + playerG.setGoalkeeper( true ); + playerG.setName( "G" ); + playerG.equandaUpdate(); + + FootballPlayer playerD = new FootballPlayer(); + playerD.setDefender( true ); + playerD.setName( "D" ); + tags=new ArrayList<String>(); + tags.add("A"); + tags.add("B"); + playerD.setTags( tags ); + playerD.equandaUpdate(); + + FootballPlayer playerM = new FootballPlayer(); + playerM.setMidfielder( true ); + playerM.setName( "M" ); + tags=new ArrayList<String>(); + tags.add("B"); + tags.add("C"); + playerM.setTags( tags ); + playerM.equandaUpdate(); + + FootballPlayer playerS = new FootballPlayer(); + playerS.setMidfielder( true ); + playerS.setName( "S" ); + tags=new ArrayList<String>(); + tags.add("C"); + playerS.setTags( tags ); + playerS.equandaUpdate(); + + FootballPlayerSelectorEJB sel = EquandaGlobal.getFootballPlayerSelector(); + List<FootballPlayer> results = sel.selectAll(); + assertEquals( 4, results.size() ); + + SelectorsState.setFilter( "Tag", "A" ); + results = sel.selectAll(); + assertEquals( 1, results.size() ); + assertEquals( "D", results.get( 0 ).getName() ); + + SelectorsState.setFilter( "Tag", "C" ); + results = sel.selectAll(); + assertEquals( 2, results.size() ); + assertEquals( "M", results.get( 0 ).getName() ); + assertEquals( "S", results.get( 1 ).getName() ); + + SelectorsState.setFilter( "Tag", null ); + results = sel.selectAll(); + assertEquals( 4, results.size() ); + + sel.remove(); + + playerG.removeEntityBean(); + playerD.removeEntityBean(); + playerM.removeEntityBean(); + playerS.removeEntityBean(); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |