Fixes #212 Generic Filter: add ability to parameterize and set view to query on parameter values

This commit is contained in:
Konstantin Krivopustov 2010-01-15 11:05:50 +00:00
parent 701d19911b
commit 33071e60fa
13 changed files with 140 additions and 29 deletions

View File

@ -279,9 +279,11 @@ public abstract class AbstractCollectionDatasource<T extends Entity<K>, K>
templateParams.put(customPerfix + entry.getKey(), entry.getValue());
}
WindowContext windowContext = dsContext.getWindowContext();
if (windowContext != null) {
templateParams.putAll(windowContext.getParams());
if (dsContext != null) {
WindowContext windowContext = dsContext.getWindowContext();
if (windowContext != null) {
templateParams.putAll(windowContext.getParams());
}
}
UserSession userSession = UserSessionClient.getUserSession();

View File

@ -517,6 +517,8 @@
<xs:attribute name="caption" type="xs:string" use="optional"/>
<xs:attribute name="paramClass" type="xs:string" use="optional"/>
<xs:attribute name="join" type="xs:string" use="optional"/>
<xs:attribute name="paramWhere" type="xs:string" use="optional"/>
<xs:attribute name="paramView" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>

View File

@ -627,7 +627,6 @@ public class WebFilter
saveAsFolder();
}
System.out.println(event.getProperty().getValue());
actions.setValue(nullActionId);
}
}

View File

@ -38,6 +38,8 @@ public abstract class Condition {
protected Param param;
protected String entityAlias;
protected boolean hidden;
protected String entityParamWhere;
protected String entityParamView;
protected List<Listener> listeners = new ArrayList<Listener>();
@ -52,6 +54,8 @@ public abstract class Condition {
caption = element.attributeValue("caption");
unary = Boolean.valueOf(element.attributeValue("unary"));
hidden = Boolean.valueOf(element.attributeValue("hidden"));
entityParamWhere = element.attributeValue("paramWhere");
entityParamView = element.attributeValue("paramView");
String aclass = element.attributeValue("class");
if (!isBlank(aclass))
@ -63,15 +67,9 @@ public abstract class Condition {
String paramName = paramElem.attributeValue("name");
if (unary) {
param = new Param(paramName, null);
param = new Param(paramName, null, null, null);
} else {
aclass = paramElem.attributeValue("class");
if (isBlank(aclass)) {
param = new Param(paramName, javaClass);
} else {
param = new Param(paramName,
ReflectionHelper.getClass(paramElem.attributeValue("class")));
}
param = new Param(paramName, javaClass, entityParamWhere, entityParamView);
}
param.parseValue(paramElem.getText());
@ -86,6 +84,8 @@ public abstract class Condition {
javaClass = descriptor.getJavaClass();
unary = javaClass == null;
param = descriptor.createParam(this);
entityParamWhere = descriptor.getEntityParamWhere();
entityParamView = descriptor.getEntityParamView();
}
public void addListener(Listener listener) {
@ -174,6 +174,11 @@ public abstract class Condition {
paramElem.addAttribute("name", param.getName());
paramElem.setText(param.formatValue());
if (entityParamWhere != null)
element.addAttribute("paramWhere", entityParamWhere);
if (entityParamView != null)
element.addAttribute("paramView", entityParamView);
}
}
@ -201,6 +206,14 @@ public abstract class Condition {
return "";
}
public String getEntityParamView() {
return entityParamView;
}
public String getEntityParamWhere() {
return entityParamWhere;
}
public String createParamName() {
return "component$" + getFilterComponentName() + "." +
getName().replace('.', '_') + RandomStringUtils.randomNumeric(5);

View File

@ -20,15 +20,28 @@ public class ConditionCreator extends ConditionDescriptor {
locCaption = MessageProvider.getMessage(getClass(), "conditionCreator");
}
@Override
public Condition createCondition() {
return new NewCustomCondition(this, "", null, entityAlias);
}
@Override
public Param createParam(Condition condition) {
return null;
}
@Override
public Class getJavaClass() {
return null;
}
@Override
protected String getEntityParamWhere() {
return null;
}
@Override
protected String getEntityParamView() {
return null;
}
}

View File

@ -59,11 +59,15 @@ public abstract class ConditionDescriptor {
}
public Param createParam(Condition condition) {
Param param = new Param(condition.createParamName(), getJavaClass());
Param param = new Param(condition.createParamName(), getJavaClass(), getEntityParamWhere(), getEntityParamView());
return param;
}
public abstract Condition createCondition();
public abstract Class getJavaClass();
public abstract String getEntityParamWhere();
public abstract String getEntityParamView();
}

View File

@ -48,4 +48,14 @@ public class CustomConditionDescriptor extends ConditionDescriptor {
return paramClass;
}
}
@Override
protected String getEntityParamWhere() {
return element.attributeValue("paramWhere");
}
@Override
protected String getEntityParamView() {
return element.attributeValue("paramView");
}
}

View File

@ -25,6 +25,8 @@ import java.util.*;
import java.util.regex.Matcher;
public class CustomConditionEditDlg extends Window {
private Label entityParamWhereLab;
private Label entityParamViewLab;
public enum ParamType {
STRING,
@ -44,6 +46,9 @@ public class CustomConditionEditDlg extends Window {
private TextField whereText;
private TextField joinText;
private AbstractSelect typeSelect;
private TextField entityParamWhereText;
private TextField entityParamViewText;
private static final String FIELD_WIDTH = "250px";
private String messagesPack;
@ -53,7 +58,7 @@ public class CustomConditionEditDlg extends Window {
public CustomConditionEditDlg(final CustomCondition condition) {
super(condition.getLocCaption());
setWidth("420px");
setWidth("450px");
this.condition = condition;
this.messagesPack = AppConfig.getInstance().getMessagesPack();
@ -75,9 +80,9 @@ public class CustomConditionEditDlg extends Window {
grid.setMargin(true, false, true, false);
int i = 0;
// allow to change caption if it doesn't set in descriptor
// allow to change caption if it isn't set in descriptor
if (StringUtils.isBlank(condition.getCaption())) {
grid.setRows(5);
grid.setRows(7);
Label nameLab = new Label(MessageProvider.getMessage(getClass(), "CustomConditionEditDlg.nameLabel"));
grid.addComponent(nameLab, 0, i);
@ -90,7 +95,7 @@ public class CustomConditionEditDlg extends Window {
grid.addComponent(nameText, 1, i++);
} else {
grid.setRows(4);
grid.setRows(6);
}
Label joinLab = new Label(MessageProvider.getMessage(getClass(), "CustomConditionEditDlg.joinLabel"));
@ -125,10 +130,14 @@ public class CustomConditionEditDlg extends Window {
fillTypeSelect(typeSelect, condition.getParam());
typeSelect.addListener(new Property.ValueChangeListener() {
public void valueChange(Property.ValueChangeEvent event) {
boolean entity = ParamType.ENTITY.equals(typeSelect.getValue())
|| ParamType.ENUM.equals(typeSelect.getValue());
entityLab.setEnabled(entity);
entitySelect.setEnabled(entity);
boolean isEntity = ParamType.ENTITY.equals(typeSelect.getValue());
boolean isEnum = ParamType.ENUM.equals(typeSelect.getValue());
entityLab.setEnabled(isEntity || isEnum);
entitySelect.setEnabled(isEntity || isEnum);
entityParamWhereLab.setEnabled(isEntity);
entityParamWhereText.setEnabled(isEntity);
entityParamViewLab.setEnabled(isEntity);
entityParamViewText.setEnabled(isEntity);
fillEntitySelect(entitySelect, condition.getParam());
}
});
@ -148,6 +157,31 @@ public class CustomConditionEditDlg extends Window {
grid.addComponent(entitySelect, 1, i++);
grid.setComponentAlignment(entitySelect, Alignment.MIDDLE_RIGHT);
entityParamWhereLab = new Label(MessageProvider.getMessage(getClass(), "CustomConditionEditDlg.entityParamWhereLab"));
entityParamWhereLab.setEnabled(ParamType.ENTITY.equals(typeSelect.getValue()));
grid.addComponent(entityParamWhereLab, 0, i);
grid.setComponentAlignment(entityParamWhereLab, Alignment.MIDDLE_RIGHT);
entityParamWhereText = new TextField();
entityParamWhereText.setWidth(FIELD_WIDTH);
entityParamWhereText.setRows(2);
entityParamWhereText.setNullRepresentation("");
entityParamWhereText.setValue(condition.getEntityParamWhere());
entityParamWhereText.setEnabled(ParamType.ENTITY.equals(typeSelect.getValue()));
grid.addComponent(entityParamWhereText, 1, i++);
entityParamViewLab = new Label(MessageProvider.getMessage(getClass(), "CustomConditionEditDlg.entityParamViewLab"));
entityParamViewLab.setEnabled(ParamType.ENTITY.equals(typeSelect.getValue()));
grid.addComponent(entityParamViewLab, 0, i);
grid.setComponentAlignment(entityParamViewLab, Alignment.MIDDLE_RIGHT);
entityParamViewText = new TextField();
entityParamViewText.setWidth(FIELD_WIDTH);
entityParamViewText.setNullRepresentation("");
entityParamViewText.setValue(condition.getEntityParamView());
entityParamViewText.setEnabled(ParamType.ENTITY.equals(typeSelect.getValue()));
grid.addComponent(entityParamViewText, 1, i++);
layout.addComponent(grid);
HorizontalLayout btnLayout = new HorizontalLayout();
@ -203,7 +237,10 @@ public class CustomConditionEditDlg extends Window {
Class javaClass = getParamJavaClass(type);
condition.setJavaClass(javaClass);
Param param = new Param(paramName, javaClass);
String entityParamWhere = (String) entityParamWhereText.getValue();
String entityParamView = (String) entityParamViewText.getValue();
Param param = new Param(paramName, javaClass, entityParamWhere, entityParamView);
condition.setParam(param);
}

View File

@ -16,9 +16,7 @@ import com.haulmont.chile.core.datatypes.impl.DateDatatype;
import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.cuba.core.app.DataService;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.LoadContext;
import com.haulmont.cuba.core.global.MessageProvider;
import com.haulmont.cuba.core.global.MetadataProvider;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.gui.ServiceLocator;
import com.haulmont.cuba.gui.data.ValueListener;
import com.haulmont.cuba.gui.data.impl.CollectionDatasourceImpl;
@ -37,6 +35,9 @@ import java.util.UUID;
public class Param {
private String entityWhere;
private String entityView;
public enum Type {
ENTITY,
ENUM,
@ -51,7 +52,7 @@ public class Param {
private Class javaClass;
private Object value;
public Param(String name, Class javaClass) {
public Param(String name, Class javaClass, String entityWhere, String entityView) {
this.name = name;
if (javaClass != null) {
this.javaClass = javaClass;
@ -67,6 +68,8 @@ public class Param {
type = Type.UNARY;
this.javaClass = Boolean.class;
}
this.entityWhere = entityWhere;
this.entityView = entityView;
}
public String getName() {
@ -318,7 +321,16 @@ public class Param {
private AbstractField createEntityLookup() {
MetaClass metaClass = MetadataProvider.getSession().getClass(javaClass);
CollectionDatasourceImpl ds =
new CollectionDatasourceImpl(null, new GenericDataService(false), "ds", metaClass, null);
new CollectionDatasourceImpl(null, new GenericDataService(false), "ds", metaClass, entityView);
if (entityWhere != null) {
QueryTransformer transformer = QueryTransformerFactory.createTransformer(
"select e from " + metaClass.getName() + " e",
metaClass.getName());
transformer.addWhere(entityWhere);
String q = transformer.getResult();
ds.setQuery(q);
}
WebLookupField lookup = new WebLookupField();
lookup.setOptionsDatasource(ds);

View File

@ -146,10 +146,10 @@ public class PropertyCondition extends Condition {
if (operator.isUnary()) {
unary = true;
setParam(new Param(paramName, null));
setParam(new Param(paramName, null, null, null));
} else {
unary = false;
setParam(new Param(paramName, javaClass));
setParam(new Param(paramName, javaClass, entityParamWhere, entityParamView));
}
}
}

View File

@ -18,6 +18,8 @@ import com.haulmont.cuba.core.global.MessageUtils;
public class PropertyConditionDescriptor extends ConditionDescriptor {
protected String entityParamWhere;
protected String entityParamView;
public PropertyConditionDescriptor(String name,
String caption,
@ -46,6 +48,8 @@ public class PropertyConditionDescriptor extends ConditionDescriptor {
messagesPack,
filterComponentName,
datasource);
entityParamWhere = element.attributeValue("paramWhere");
entityParamView = element.attributeValue("paramView");
}
@Override
@ -53,6 +57,7 @@ public class PropertyConditionDescriptor extends ConditionDescriptor {
return new PropertyCondition(this, entityAlias);
}
@Override
public Class getJavaClass() {
MetaProperty metaProperty = metaClass.getPropertyEx(name).getMetaProperty();
Class paramClass;
@ -63,5 +68,13 @@ public class PropertyConditionDescriptor extends ConditionDescriptor {
return paramClass;
}
@Override
protected String getEntityParamWhere() {
return entityParamWhere;
}
@Override
protected String getEntityParamView() {
return entityParamView;
}
}

View File

@ -52,6 +52,8 @@ CustomConditionEditDlg.joinLabel=Join
CustomConditionEditDlg.whereLabel=Where
CustomConditionEditDlg.paramTypeLabel=Parameter Type
CustomConditionEditDlg.entityLabel=Entity/Enum
CustomConditionEditDlg.entityParamWhereLab=Parameter Where
CustomConditionEditDlg.entityParamViewLab=Parameter View
CustomConditionEditDlg$ParamType.STRING=String
CustomConditionEditDlg$ParamType.DATE=Date

View File

@ -42,11 +42,15 @@ PropertyCondition$Op.GREATER=>
PropertyCondition$Op.GREATER_OR_EQUAL=>=
PropertyCondition$Op.LESSER=<
PropertyCondition$Op.LESSER_OR_EQUAL=<=
CustomConditionEditDlg.hintLabel=Алиас сущности: <b>%s</b>. Используйте <b>?</b> для параметра.
CustomConditionEditDlg.joinLabel=Join
CustomConditionEditDlg.whereLabel=Where
CustomConditionEditDlg.paramTypeLabel=Тип параметра
CustomConditionEditDlg.entityLabel=Сущность/Перечисление
CustomConditionEditDlg.entityParamWhereLab=Параметр Where
CustomConditionEditDlg.entityParamViewLab=Параметр View
CustomConditionEditDlg$ParamType.STRING=Строка
CustomConditionEditDlg$ParamType.DATE=Дата
CustomConditionEditDlg$ParamType.BOOLEAN=Да/Нет