Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.lib / org.gvsig.exportto.lib.api / src / main / java / org / gvsig / export / spi / AbstractExportParameters.java @ 44411

History | View | Annotate | Download (6.92 KB)

1
package org.gvsig.export.spi;
2

    
3
import java.text.DateFormat;
4
import java.text.SimpleDateFormat;
5
import java.util.Date;
6
import org.gvsig.export.ExportAttributes;
7
import org.gvsig.export.ExportLocator;
8
import org.gvsig.fmap.dal.exception.DataException;
9
import org.gvsig.fmap.dal.feature.FeatureStore;
10
import org.gvsig.fmap.dal.feature.FeatureType;
11
import org.gvsig.export.ExportParameters;
12
import org.gvsig.expressionevaluator.Expression;
13
import org.gvsig.tools.ToolsLocator;
14
import org.gvsig.tools.dynobject.DynStruct;
15
import org.gvsig.tools.persistence.PersistenceManager;
16
import org.gvsig.tools.persistence.PersistentState;
17
import org.gvsig.tools.persistence.exception.PersistenceException;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20

    
21
/**
22
 *
23
 * @author jjdelcerro
24
 */
25
public abstract class AbstractExportParameters implements ExportParameters {
26

    
27
    private FeatureStore sourceFeatureStore;
28
    private Expression filterExpression;
29
    private int featuresToUse;
30
    private Object context;
31
    protected ExportAttributes exportAttributes = null;
32
    private Date date = new Date();
33
    public ExportServiceFactory factory;
34

    
35
    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractExportParameters.class);
36

    
37
    public AbstractExportParameters(ExportServiceFactory factory) {
38
        this.factory = factory;
39
        this.exportAttributes = ExportLocator.getServiceManager().createExportAttributes();
40
    }
41

    
42
    @Override
43
    public boolean needsSelectTargetProjection() {
44
        return false;
45
    }
46

    
47
    @Override
48
    public FeatureType getSourceFeatureType() {
49
        if (!(this.exportAttributes == null)) {
50
            return this.exportAttributes.getSourceFeatureType();
51
        } else {
52
            return null;
53
        }
54
    }
55

    
56
    @Override
57
    public void setSourceFeatureType(FeatureType sourceFeatureType) {
58
        this.exportAttributes.setSourceFeatureType(sourceFeatureType);
59
    }
60

    
61
    @Override
62
    public void setSourceFeatureStore(FeatureStore sourceFeatureStore) {
63
        this.sourceFeatureStore = sourceFeatureStore;
64
        try {
65
            FeatureType sourceFeatureType = sourceFeatureStore.getDefaultFeatureType();
66
            this.exportAttributes.setSourceFeatureType(sourceFeatureType);
67
        } catch (DataException ex) {
68
            throw new RuntimeException("Can't set feature type", ex);
69
        }
70
    }
71

    
72
    @Override
73
    public FeatureStore getSourceFeatureStore() {
74
        return this.sourceFeatureStore;
75
    }
76

    
77
    @Override
78
    public Expression getFilterExpresion() {
79
        return this.filterExpression;
80
    }
81

    
82
    @Override
83
    public void setFilterExpresion(Expression expression) {
84
        this.filterExpression = expression;
85
    }
86

    
87
    @Override
88
    public int getFeaturesToUse() {
89
        return featuresToUse;
90
    }
91

    
92
    @Override
93
    public void setFeaturesToUse(int featuresToUse) {
94
        this.featuresToUse = featuresToUse;
95
    }
96

    
97
    @Override
98
    public Object getContext() {
99
        return context;
100
    }
101

    
102
    @Override
103
    public void setContext(Object context) {
104
        this.context = context;
105
    }
106

    
107
    @Override
108
    public ExportAttributes getExportAttributes() {
109
        return this.exportAttributes;
110
    }
111

    
112
    @Override
113
    public void setExportAttributes(ExportAttributes export) {
114
        this.exportAttributes = export;
115
    }
116

    
117
    @Override
118
    public ExportParameters clone() throws CloneNotSupportedException {
119
        ExportParameters clone = (ExportParameters) super.clone();
120

    
121
        if (this.filterExpression != null) {
122
            clone.setFilterExpresion(this.filterExpression.clone());
123
        }
124
        if (this.exportAttributes != null) {
125
            clone.setExportAttributes(this.exportAttributes.clone());
126
        }
127

    
128
        return clone;
129
    }
130

    
131
    @Override
132
    public Date getCreationDate() {
133
        return this.date;
134
    }
135

    
136
    public void setCreationDate(Date date) {
137
        this.date = date;
138
    }
139

    
140
    @Override
141
    public String getLabel() {
142
        StringBuilder builder = new StringBuilder();
143
        DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
144
        String dateName;
145
        dateName = df.format(this.getCreationDate());
146
        builder.append(dateName);
147
        builder.append(": ");
148

    
149
        String serviceName;
150
        if (this.getServiceName() != null) {
151
            serviceName = this.getServiceName();
152
        } else {
153
            serviceName = "Null service";
154
        }
155
        builder.append(serviceName);
156

    
157
        if (this.getSourceFeatureStore() != null) {
158
            String storeName = this.getSourceFeatureStore().getName();
159
            builder.append(" - ");
160
            builder.append(storeName);
161
        }
162

    
163
        return builder.toString();
164
    }
165

    
166
    @Override
167
    public Object getValue() {
168
        return this;
169
    }
170

    
171
    @Override
172
    public String toString() {
173
        return this.getLabel();
174
    }
175

    
176
    @Override
177
    public ExportServiceFactory getFactory() {
178
        return this.factory;
179
    }
180

    
181
    @Override
182
    public FeatureType getTargetFeatureType() {
183
        if (this.getExportAttributes() == null) {
184
            LOGGER.warn("Not been able to get target feature type from export attributes because it's null");
185
            return null;
186
        }
187
        return this.getExportAttributes().getTargetFeatureType();
188
    }
189

    
190
    public static void registerPersistence() {
191
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
192
        if (manager.getDefinition("AbstractExportParameters") == null) {
193
            DynStruct definition = manager.addDefinition(AbstractExportParameters.class,
194
                    "AbstractExportParameters", "AbstractExportParameters persistence definition", null, null);
195
            definition.addDynFieldInt("featuresToUse").setMandatory(false);
196
            definition.addDynFieldObject("filterExpression").setClassOfValue(Expression.class).setMandatory(false);
197
            definition.addDynFieldObject("exportAttributes").setClassOfValue(ExportAttributes.class).setMandatory(false);
198
            definition.addDynFieldDate("dateCreation").setMandatory(false);
199
            definition.addDynFieldString("factory");
200
        }
201
    }
202

    
203
    public void saveToState(PersistentState state) throws PersistenceException {
204
        state.set("featuresToUse", this.featuresToUse);
205
        state.set("filterExpression", this.filterExpression);
206
        state.set("exportAttributes", this.exportAttributes);
207
        state.set("dateCreation", this.date);
208
        state.set("factory", this.factory.getName());
209
    }
210

    
211
    public void loadFromState(PersistentState state) throws PersistenceException {
212
        this.featuresToUse = state.getInt("featuresToUse");
213
        this.filterExpression = (Expression) state.get("filterExpression");
214
        this.exportAttributes = (ExportAttributes) state.get("exportAttributes");
215
        this.date = state.getDate("dateCreation");
216
        String nameFactory = state.getString("factory");
217
        ExportServiceFactory statefactory = ExportLocator.getServiceManager().getServiceFactory(nameFactory);
218
        this.factory = statefactory;
219
    }
220

    
221
}