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