1
2
3
4
5
6 package org.weda.store.impl;
7
8 import java.util.Iterator;
9 import org.apache.hivemind.util.Defense;
10 import org.weda.enhance.InjectHivemindObject;
11 import org.weda.property.ConstraintException;
12 import org.weda.property.SetConstraint;
13 import org.weda.property.SetValue;
14 import org.weda.property.impl.AbstractConstraint;
15 import org.weda.property.impl.SetValueImpl;
16 import org.weda.store.ObjectSource;
17 import org.weda.store.ObjectSourceException;
18 import org.weda.store.ObjectSourceRegistry;
19
20 /**Ограничение значения свойства множеством значений из источника объектов.
21 *
22 * @author Mikhail Titov
23 */
24 public class ObjectSourceSetConstraint
25 extends AbstractConstraint implements SetConstraint
26 {
27 @InjectHivemindObject ObjectSourceRegistry objectSourceRegistry;
28 private String objectSourceName;
29
30
31 public void check(Object value, Object[] parameters)
32 throws ConstraintException
33 {
34 }
35
36 public Iterator<SetValue> iterator() throws ConstraintException {
37 try {
38 ObjectSource objectSource =
39 objectSourceRegistry.getObjectSource(objectSourceName);
40 objectSource.close();
41 objectSource.refresh();
42 return new ObjectSourceIterator(objectSource);
43 } catch (Exception ex) {
44 throw new ConstraintException(
45 String.format(
46 "Can't create iterator for object source set " +
47 "constraint (%s)"
48 , getId())
49 , ex);
50 }
51 }
52
53 public boolean isValuesAliased() {
54 return false;
55 }
56
57 public void init() throws ConstraintException {
58 try{
59 Defense.notNull(objectSourceName, "objectSourceName");
60 }catch(Exception e){
61 throw new ConstraintException(
62 String.format("Constraint (%s) initializing error", getId())
63 , e);
64 }
65 }
66
67 public String getObjectSourceName() {
68 return objectSourceName;
69 }
70
71 public void setObjectSourceName(String objectSourceName) {
72 this.objectSourceName = objectSourceName;
73 }
74
75 public Class[] getParameterTypes(Class valueType)
76 throws ConstraintException
77 {
78 return null;
79 }
80
81 private static class ObjectSourceIterator implements Iterator<SetValue>
82 {
83 private int row;
84 private ObjectSource objectSource;
85 private int baseClassPosition;
86
87 public ObjectSourceIterator(ObjectSource objectSource){
88 this.objectSource = objectSource;
89 baseClassPosition = objectSource.getBaseClassPosition();
90 row = 0;
91 }
92
93 public void remove() {
94 throw new UnsupportedOperationException(
95 "Remove operation not supported by ObjectSourceIterator");
96 }
97
98 public SetValue next() {
99 Object value = objectSource.getRowAt(row++)[baseClassPosition];
100 return new SetValueImpl(value);
101 }
102
103 public boolean hasNext() {
104 return row < objectSource.getRowCount();
105 }
106
107 }
108 }