1 package org.weda.model.impl;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8 import org.weda.NamedObject;
9 import org.weda.enhance.Cache;
10 import org.weda.enhance.InjectHivemindObject;
11 import org.weda.model.EditorModel;
12 import org.weda.model.EditorModelException;
13 import org.weda.model.EditorModelGroup;
14 import org.weda.property.Constraint;
15 import org.weda.property.ConstraintException;
16 import org.weda.property.SetConstraint;
17 import org.weda.property.SetValue;
18 import org.weda.property.impl.SetValueImpl;
19 import org.weda.store.DetailObjectSourceInfo;
20 import org.weda.store.ObjectSource;
21 import org.weda.store.ObjectSourceConstraint;
22 import org.weda.store.ObjectSourceException;
23 import org.weda.store.ObjectSourceRegistry;
24 import org.weda.store.impl.ObjectSourceConstraintImpl;
25
26 /**
27 *
28 * @author Mikhail Titov
29 */
30 public abstract class AbstractEditorModel<T extends EditorModelGroup>
31 implements EditorModel<T>, NamedObject
32 {
33 private final static String VALUES_LIST_SUFIX = "#valuesList";
34 private String name;
35 private String pattern;
36 private boolean editable = true;
37 private T modelGroup;
38 private boolean needConversion = true;
39 private boolean hasValuesList;
40 private SetConstraint setConstraint;
41 private String valuesListCacheName;
42 private boolean isValuesListInitialized = false;
43 private String disableIfNull;
44 private boolean levelsValuesUniq = true;
45
46 private List<DetailObjectSourceInfo> detailInfos;
47 @Cache()
48 private Map<ObjectSource, ObjectSourceConstraint> detailConstraints;
49
50 @InjectHivemindObject()
51 private ObjectSourceRegistry objectSourceRegistry;
52
53 public String getObjectName() {
54 return getModelGroup().getName()+"#"+getName();
55 }
56
57 public void addDetailInfo(DetailObjectSourceInfo info){
58 if (detailInfos==null)
59 detailInfos = new ArrayList<DetailObjectSourceInfo>(2);
60 detailInfos.add(info);
61 }
62
63 protected void activateDetailConstraints(Object value)
64 throws EditorModelException
65 {
66 if (detailInfos==null)
67 return;
68 try{
69 Object prevValue = null;
70 if (detailConstraints!=null){
71 prevValue =
72 detailConstraints.values().iterator().next().getValue();
73 }
74 if (prevValue!=null && prevValue.equals(value))
75 return;
76 deactivateDetailConstraints();
77 if (value==null)
78 return;
79 for (DetailObjectSourceInfo detailInfo: detailInfos){
80 try{
81 ObjectSource detailObjectSource =
82 objectSourceRegistry.getObjectSource(
83 detailInfo.getName());
84 ObjectSourceConstraintImpl c =
85 new ObjectSourceConstraintImpl();
86 c.setPropertyName(detailInfo.getPropertyName());
87 c.setValue(value);
88 detailObjectSource.close();
89 detailObjectSource.addConstraint(c);
90 if (detailConstraints==null)
91 detailConstraints =
92 new HashMap<ObjectSource, ObjectSourceConstraint>();
93 detailConstraints.put(detailObjectSource, c);
94 if (detailInfo.isRefreshOnActivate())
95 detailObjectSource.refresh();
96 }catch(ObjectSourceException e){
97 deactivateDetailConstraints();
98 throw new EditorModelException(
99 String.format(
100 "Can't add constraint for property (%s) " +
101 "in detail objectSource (%s)"
102 , detailInfo.getPropertyName()
103 , detailInfo.getName())
104 , e);
105 }
106 }
107 }catch(Exception e){
108 throw new EditorModelException(
109 String.format(
110 "Can't activate detail constaints from editor model (%s) " +
111 "in editor model group (%s)"
112 , getName(), getModelGroup().getName())
113 , e);
114 }
115
116 }
117
118 protected void deactivateDetailConstraints() throws EditorModelException {
119 try {
120 if (detailInfos!=null && detailConstraints!=null){
121 for (Map.Entry<ObjectSource, ObjectSourceConstraint> entry
122 : detailConstraints.entrySet())
123 {
124 entry.getKey().close();
125 entry.getKey().removeConstraint(entry.getValue());
126 }
127 detailConstraints = null;
128 }
129 } catch (ObjectSourceException ex) {
130 throw new EditorModelException(
131 String.format(
132 "Error while deactivating detail constraints " +
133 "in the editor model (%s) in the editor group (%s)"
134 , getName(), getModelGroup().getName())
135 , ex);
136 }
137 }
138
139 public String getName() {
140 return name;
141 }
142
143 public void setName(String name) {
144 this.name = name;
145 }
146
147 public String getPattern() {
148 return pattern;
149 }
150
151 public void setPattern(String pattern) {
152 this.pattern = pattern;
153 }
154
155 public boolean isEditable() throws EditorModelException {
156 return isLeadingModelValueNull() ? false : editable;
157 }
158
159 public void setEditable(boolean editable) {
160 this.editable = editable;
161 }
162
163 public Object getValuePretender(){
164 return modelGroup.getCachedModelValue(getName());
165 }
166
167 public void setValuePretender(Object valuePretender) {
168 modelGroup.cacheModelValue(getName(), valuePretender);
169 }
170
171 public T getModelGroup() {
172 return modelGroup;
173 }
174
175 public void setModelGroup(T modelGroup) {
176 this.modelGroup = modelGroup;
177 }
178
179 public boolean isValuePretenderSetted() {
180 return getModelGroup().isModelValueCached(getName());
181 }
182
183 public boolean isNeedConversion() {
184 return needConversion;
185 }
186
187 public void setNeedConversion(boolean needConversion) {
188 this.needConversion = needConversion;
189 }
190
191 public void resetValuesList() {
192 getModelGroup().clearModelValue(valuesListCacheName);
193 }
194
195 public void refreshValuesList() throws EditorModelException {
196 initValuesList();
197 try {
198 List<SetValue> setValues = new ArrayList<SetValue>();
199 SetValueImpl nullSetValue = new SetValueImpl(null);
200 nullSetValue.setConstraint(setConstraint);
201 nullSetValue.setAlias("");
202 setValues.add(nullSetValue);
203 for (Iterator<SetValue> it=setConstraint.iterator(); it.hasNext();)
204 setValues.add(it.next());
205 SetValue[] arr = new SetValue[setValues.size()];
206 for (int i=0; i<setValues.size(); ++i)
207 arr[i] = setValues.get(i);
208 getModelGroup().cacheModelValue(valuesListCacheName, arr);
209 } catch (ConstraintException ex) {
210 throw new EditorModelException(
211 String.format(
212 "Can't refresh values list for editor model (%s) " +
213 "in editor model group (%s)"
214 , getName(), getModelGroup().getName())
215 , ex);
216 }
217 }
218
219 public boolean hasValuesList() throws EditorModelException {
220 if (isLeadingModelValueNull())
221 return false;
222 else{
223 initValuesList();
224 return hasValuesList;
225 }
226 }
227
228 public SetValue[] getValuesList() throws EditorModelException {
229 if (!getModelGroup().isModelValueCached(valuesListCacheName))
230 refreshValuesList();
231 return (SetValue[])getModelGroup()
232 .getCachedModelValue(valuesListCacheName);
233 }
234
235 public boolean isValuesListAliased() throws EditorModelException {
236 initValuesList();
237 return setConstraint.isValuesAliased();
238 }
239
240 private void initValuesList() throws EditorModelException {
241 if (isValuesListInitialized) return;
242 try{
243 hasValuesList = false;
244 List<Constraint> constraints =
245 getPropertyDescriptor().getConstraints();
246 if (constraints!=null)
247 for (Constraint constraint: constraints)
248 if (constraint instanceof SetConstraint){
249 setConstraint = (SetConstraint)constraint;
250 hasValuesList = true;
251 valuesListCacheName = getName()+VALUES_LIST_SUFIX;
252 break;
253 }
254 isValuesListInitialized = true;
255 }catch(Exception e){
256 throw new EditorModelException(
257 String.format(
258 "Editor model (%s) in editor group (%s) " +
259 "values list initalizing error"
260 , getName(), getModelGroup().getName())
261 , e);
262 }
263 }
264
265 private boolean isLeadingModelValueNull() throws EditorModelException {
266 boolean res;
267 try {
268 res = false;
269 if (disableIfNull!=null){
270 EditorModel leadingModel =
271 getModelGroup().getEditorModel(disableIfNull);
272 res = leadingModel.getValue()==null;
273 }
274 }catch (Exception e) {
275 throw new EditorModelException(
276 String.format(
277 "Error while detecting leading model (%s) " +
278 "value for null"
279 , disableIfNull)
280 , e);
281 }
282 return res;
283 }
284
285 public String getDisableIfNull() {
286 return disableIfNull;
287 }
288
289 public void setDisableIfNull(String disableIfNull) {
290 this.disableIfNull = disableIfNull;
291 }
292
293 public void resetModel() throws EditorModelException {
294 deactivateDetailConstraints();
295 }
296
297 public boolean isLevelsValuesUniq() {
298 return levelsValuesUniq;
299 }
300
301 protected void setLevelsValuesUniq(boolean levelsValuesUniq) {
302 this.levelsValuesUniq = levelsValuesUniq;
303 }
304
305 }