1
2
3
4
5
6 package org.weda.model.impl;
7
8 import org.weda.converter.ValueTypeConverter;
9 import org.weda.enhance.InjectHivemindObject;
10 import org.weda.model.EditorModelException;
11 import org.weda.model.EditorModelGroupException;
12 import org.weda.property.PropertyDescriptor;
13 import org.weda.store.QueryFilterElement;
14 import org.weda.store.QueryFilterElement.ExpressionType;
15 import org.weda.store.QueryFilterElement.OperatorType;
16 import org.weda.store.QueryFilterElementException;
17
18 /**
19 *
20 * @author Mikhail Titov
21 */
22 public class FilterEditorModel<T extends FilterEditorModelGroup>
23 extends AbstractEditorModel<T>
24 {
25 private String objectAlias;
26 private String propertyPath;
27 @InjectHivemindObject private static ValueTypeConverter converter;
28
29 public FilterEditorModel(){
30 setNeedConversion(false);
31 }
32
33 public void setValue(Object value) throws Exception {
34 try{
35 QueryFilterElement filterElement = getFilterElement();
36 if (!(value instanceof String))
37 if (value == null)
38 filterElement.setExpression(null);
39 else {
40 filterElement.setValue(value);
41 filterElement.setOperator("=");
42 filterElement.setExpressionType(ExpressionType.OPERATOR);
43 filterElement.setOperatorType(OperatorType.SIMPLE);
44 }
45 else
46 filterElement.setExpression((String)value);
47 activateDetailConstraints(filterElement.getValue());
48 }catch(Exception e){
49 if (e instanceof QueryFilterElementException)
50 throw e;
51 else
52 throw new EditorModelException(
53 String.format(
54 "Error setting expression (%s) for " +
55 "filter editor model (%s) in the model group (%s)"
56 , value, getName(), getModelGroup().getName())
57 , e);
58 }
59 }
60
61 public Object getValue() throws EditorModelException {
62 try{
63 QueryFilterElement filterElement = getFilterElement();
64 activateDetailConstraints(filterElement.getValue());
65 if (filterElement.getExpressionType() == ExpressionType.EMPTY)
66 return null;
67 else if ("=".equals(filterElement.getOperator())){
68 String expression = filterElement.getExpression();
69 if (expression==null)
70 return filterElement.getValue();
71 else if (expression.startsWith("="))
72 return expression.substring(1);
73 else
74 return expression;
75 }
76 else
77 return filterElement.getExpression();
78 }catch(Exception e){
79 throw new EditorModelException(
80 String.format(
81 "Error getting current expression from the " +
82 "filter editor model (%s) " +
83 "in the editor model group (%s)"
84 , getName(), getModelGroup().getName())
85 , e);
86 }
87 }
88
89 public PropertyDescriptor getPropertyDescriptor()
90 throws EditorModelException
91 {
92 try {
93 return getFilterElement().getPropertyDescriptor();
94 } catch (EditorModelGroupException ex) {
95 throw new EditorModelException(
96 String.format(
97 "Error getting property descriptor " +
98 "for filter editor model (%s) in editor group (%s)"
99 , getName(), getModelGroup().getName())
100 , ex);
101 }
102 }
103
104 private QueryFilterElement getFilterElement()
105 throws EditorModelGroupException
106 {
107 return getModelGroup().getFilterElement(propertyPath, objectAlias);
108 }
109
110 public String getObjectAlias() {
111 return objectAlias;
112 }
113
114 public void setObjectAlias(String objectAlias) {
115 this.objectAlias = objectAlias;
116 }
117
118 public String getPropertyPath() {
119 return propertyPath;
120 }
121
122 public void setPropertyPath(String propertyPath) {
123 this.propertyPath = propertyPath;
124 }
125
126 }