svn-gvsig-desktop / trunk / extensions / extExpressionField / src / com / iver / cit / gvsig / project / documents / table / gui / EvalExpression.java @ 28289
History | View | Annotate | Download (8.22 KB)
1 |
package com.iver.cit.gvsig.project.documents.table.gui; |
---|---|
2 |
|
3 |
import java.sql.Types; |
4 |
import java.util.BitSet; |
5 |
import java.util.Date; |
6 |
import java.util.prefs.Preferences; |
7 |
|
8 |
import org.apache.bsf.BSFException; |
9 |
|
10 |
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException; |
11 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
12 |
import com.hardcode.gdbms.engine.values.Value; |
13 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
14 |
import com.iver.andami.PluginServices; |
15 |
import com.iver.andami.messages.NotificationManager; |
16 |
import com.iver.cit.gvsig.EditionUtilities; |
17 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
18 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException; |
19 |
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException; |
20 |
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException; |
21 |
import com.iver.cit.gvsig.fmap.core.IRow; |
22 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
23 |
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
24 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
25 |
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
26 |
import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
27 |
import com.iver.cit.gvsig.fmap.edition.IEditableSource; |
28 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
29 |
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter; |
30 |
import com.iver.cit.gvsig.fmap.edition.IWriteable; |
31 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
32 |
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
33 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
34 |
/**
|
35 |
* @author Vicente Caballero Navarro
|
36 |
*/
|
37 |
public class EvalExpression { |
38 |
private FieldDescription[] fieldDescriptors; |
39 |
private int selectedIndex; |
40 |
private FieldDescription fieldDescriptor;
|
41 |
private FLyrVect lv;
|
42 |
private IEditableSource ies =null; |
43 |
private Table table=null; |
44 |
private static Preferences prefs = Preferences.userRoot().node( "fieldExpressionOptions" ); |
45 |
private int limit; |
46 |
public EvalExpression() {
|
47 |
limit=prefs.getInt("limit_rows_in_memory",-1); |
48 |
} |
49 |
public void setTable(Table table) { |
50 |
BitSet columnSelected = table.getSelectedFieldIndices();
|
51 |
fieldDescriptors = table.getModel().getModelo().getFieldsDescription(); |
52 |
selectedIndex = columnSelected.nextSetBit(0);
|
53 |
fieldDescriptor = fieldDescriptors[selectedIndex]; |
54 |
this.table=table;
|
55 |
lv=(FLyrVect)table.getModel().getAssociatedTable(); |
56 |
if (lv ==null) |
57 |
ies=table.getModel().getModelo(); |
58 |
else
|
59 |
ies = (VectorialEditableAdapter) lv.getSource(); |
60 |
} |
61 |
public void setValue(Object obj,int i) { |
62 |
//VectorialEditableAdapter vea = (VectorialEditableAdapter) lv.getSource();
|
63 |
Value value = getValue(obj); |
64 |
//System.out.println("num = "+i);
|
65 |
IRow feat=null;
|
66 |
try {
|
67 |
feat = ies.getRow(i).getLinkedRow().cloneRow(); |
68 |
} catch (ExpansionFileReadException e) {
|
69 |
NotificationManager.addError(e); |
70 |
} catch (ReadDriverException e) {
|
71 |
NotificationManager.addError(e); |
72 |
} |
73 |
Value[] values = feat.getAttributes();
|
74 |
values[selectedIndex] = value; |
75 |
feat.setAttributes(values); |
76 |
|
77 |
IRowEdited edRow = new DefaultRowEdited(feat,
|
78 |
IRowEdited.STATUS_MODIFIED, i); |
79 |
try {
|
80 |
ies.modifyRow(edRow.getIndex(), edRow.getLinkedRow(), "",
|
81 |
EditionEvent.ALPHANUMERIC); |
82 |
} catch (ExpansionFileWriteException e) {
|
83 |
NotificationManager.addError(e); |
84 |
} catch (ExpansionFileReadException e) {
|
85 |
NotificationManager.addError(e); |
86 |
} catch (ValidateRowException e) {
|
87 |
NotificationManager.addError(e); |
88 |
} catch (ReadDriverException e) {
|
89 |
NotificationManager.addError(e); |
90 |
} |
91 |
|
92 |
} |
93 |
|
94 |
public void isCorrectValue(Object obj) throws BSFException { |
95 |
if (obj instanceof Number || obj instanceof Date || obj instanceof Boolean || obj instanceof String) { |
96 |
|
97 |
}else{
|
98 |
throw new BSFException("incorrect"); |
99 |
} |
100 |
} |
101 |
|
102 |
|
103 |
/**
|
104 |
* Returns the value created from object.
|
105 |
*
|
106 |
* @param obj value.
|
107 |
*
|
108 |
* @return Value.
|
109 |
*/
|
110 |
private Value getValue(Object obj) { |
111 |
int typeField = fieldDescriptor.getFieldType();
|
112 |
Value value = null;//ValueFactory.createNullValue(); |
113 |
|
114 |
if (obj instanceof Number) { |
115 |
if (typeField == Types.DOUBLE || typeField == Types.NUMERIC) { |
116 |
double dv = ((Number) obj).doubleValue(); |
117 |
value = ValueFactory.createValue(dv); |
118 |
} else if (typeField == Types.FLOAT) { |
119 |
float df = ((Number) obj).floatValue(); |
120 |
value = ValueFactory.createValue(df); |
121 |
} else if (typeField == Types.INTEGER) { |
122 |
int di = ((Number) obj).intValue(); |
123 |
value = ValueFactory.createValue(di); |
124 |
} else if (typeField == Types.BIGINT) { |
125 |
long di = ((Number) obj).longValue(); |
126 |
value = ValueFactory.createValue(di); |
127 |
} else if (typeField == Types.VARCHAR) { |
128 |
String s = ((Number) obj).toString(); |
129 |
value = ValueFactory.createValue(s); |
130 |
} else if (typeField == Types.BOOLEAN) { |
131 |
if (((Number) obj).intValue()==0){ |
132 |
value=ValueFactory.createValue(false);
|
133 |
}else{
|
134 |
value=ValueFactory.createValue(true);
|
135 |
} |
136 |
} |
137 |
} else if (obj instanceof Date) { |
138 |
if (typeField == Types.DATE) { |
139 |
Date date = (Date) obj; |
140 |
value = ValueFactory.createValue(date); |
141 |
} else if (typeField == Types.VARCHAR) { |
142 |
String s = ((Date) obj).toString(); |
143 |
value = ValueFactory.createValue(s); |
144 |
} |
145 |
} else if (obj instanceof Boolean) { |
146 |
if (typeField == Types.BOOLEAN) { |
147 |
boolean b = ((Boolean) obj).booleanValue(); |
148 |
value = ValueFactory.createValue(b); |
149 |
} else if (typeField == Types.VARCHAR) { |
150 |
String s = ((Boolean) obj).toString(); |
151 |
value = ValueFactory.createValue(s); |
152 |
} |
153 |
} else if (obj instanceof String) { |
154 |
if (typeField == Types.VARCHAR) { |
155 |
String s = obj.toString();
|
156 |
value = ValueFactory.createValue(s); |
157 |
} |
158 |
}else{
|
159 |
value=ValueFactory.createNullValue(); |
160 |
} |
161 |
|
162 |
return value;
|
163 |
} |
164 |
public FieldDescription getFieldDescriptorSelected() {
|
165 |
return fieldDescriptor;
|
166 |
} |
167 |
public FieldDescription[] getFieldDescriptors() { |
168 |
return fieldDescriptors;
|
169 |
} |
170 |
// public void setFieldValue(Object obj,int i) {
|
171 |
// try {
|
172 |
// ((DBFDriver)table.getModel().getModelo().getOriginalDriver()).setFieldValue(i,selectedIndex,obj);
|
173 |
// } catch (DriverLoadException e) {
|
174 |
// e.printStackTrace();
|
175 |
// } catch (IOException e) {
|
176 |
// e.printStackTrace();
|
177 |
// }
|
178 |
// }
|
179 |
public void saveEdits(int numRows) throws ReadDriverException, InitializeWriterException, StopWriterVisitorException { |
180 |
if (limit==-1 || numRows == 0 || (numRows % limit)!=0) { |
181 |
return;
|
182 |
} |
183 |
ies.endComplexRow(PluginServices.getText(this, "expression")); |
184 |
if ((lv != null) && |
185 |
lv.getSource() instanceof VectorialEditableAdapter) {
|
186 |
VectorialEditableAdapter vea = (VectorialEditableAdapter) lv.getSource(); |
187 |
ISpatialWriter spatialWriter = (ISpatialWriter) vea.getDriver(); |
188 |
vea.cleanSelectableDatasource(); |
189 |
lv.setRecordset(vea.getRecordset()); // Queremos que el recordset del layer
|
190 |
// refleje los cambios en los campos.
|
191 |
ILayerDefinition lyrDef = EditionUtilities.createLayerDefinition(lv); |
192 |
spatialWriter.initialize(lyrDef); |
193 |
vea.saveEdits(spatialWriter,EditionEvent.ALPHANUMERIC); |
194 |
vea.getCommandRecord().clearAll(); |
195 |
} else {
|
196 |
if (ies instanceof IWriteable){ |
197 |
IWriteable w = (IWriteable) ies; |
198 |
IWriter writer = w.getWriter(); |
199 |
if (writer == null){ |
200 |
}else{
|
201 |
ITableDefinition tableDef = ies.getTableDefinition(); |
202 |
writer.initialize(tableDef); |
203 |
|
204 |
ies.saveEdits(writer,EditionEvent.ALPHANUMERIC); |
205 |
ies.getSelection().clear(); |
206 |
} |
207 |
} |
208 |
ies.getCommandRecord().clearAll(); |
209 |
} |
210 |
ies.startComplexRow(); |
211 |
table.refresh(); |
212 |
} |
213 |
} |