gvsig-scripting / org.gvsig.scripting.app / trunk / org.gvsig.scripting.app / org.gvsig.scripting.app.extension / src / main / resources-plugin / scripting / lib / gvsig21 / store.py @ 442
History | View | Annotate | Download (3.64 KB)
1 |
|
---|---|
2 |
from org.gvsig.fmap.dal.feature.impl import DefaultFeatureStore |
3 |
|
4 |
class FeatureStore(DefaultFeatureStore): |
5 |
"""Represents gvsig store. It's used as Table/Layer objects store
|
6 |
(org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore)
|
7 |
"""
|
8 |
def __init__(self,store): |
9 |
WrapperToJava.__init__(self, store)
|
10 |
#self.data = None
|
11 |
self.fset = None |
12 |
|
13 |
def features(self, expresion = None, sortBy="", asc=True): |
14 |
"""Returns layer features set (FeatureSet class), with all layer features,
|
15 |
or selected featured if there are (could be empty feature set) or None if
|
16 |
gets an Exception.
|
17 |
:param expresion: filter to apply to the feature set to select
|
18 |
determinates features that match with expression
|
19 |
:type expresion: string
|
20 |
:param sortBy: name of attribute to sort
|
21 |
:type sortby: string
|
22 |
:param: asc: order
|
23 |
:type asc: boolean
|
24 |
:return: FeatureSet
|
25 |
"""
|
26 |
if expresion == None and sortBy =="": |
27 |
self.fset = self.getFeatureSet() |
28 |
else:
|
29 |
try:
|
30 |
application = ApplicationLocator.getManager() |
31 |
datamanager = application.getDataManager() |
32 |
query = self.createFeatureQuery()
|
33 |
if sortBy != "": |
34 |
order = FeatureQueryOrder() |
35 |
order.add(sortBy, asc) |
36 |
query.setOrder(order) |
37 |
if expresion != None: |
38 |
query.setFilter(datamanager.createExpresion(expresion)) |
39 |
self.fset = self.getFeatureSet(query) |
40 |
except Exception, e: |
41 |
return None |
42 |
|
43 |
return FeatureSet(self.fset) |
44 |
|
45 |
def edit(self): |
46 |
"""Set store in edition mode"""
|
47 |
if not self.isEditing(): |
48 |
self().edit()
|
49 |
|
50 |
def append(self, *valuesList, **values): |
51 |
"""
|
52 |
Creates a new feature from given values and insert it in the feature
|
53 |
set. If an error occurs raises RuntimeException.
|
54 |
:param values: dictionary with name property value or list named params
|
55 |
:type values: dict
|
56 |
"""
|
57 |
try:
|
58 |
if len(valuesList) ==1: |
59 |
values.update(valuesList[0])
|
60 |
|
61 |
if not self.isEditing(): |
62 |
self.edit()
|
63 |
f = self.createNewFeature()
|
64 |
|
65 |
if f == None: |
66 |
raise RuntimeError("Failed to create a new Feature") |
67 |
for k,v in values.iteritems(): |
68 |
f.set(k,v) |
69 |
self.insert(f)
|
70 |
except Throwable, ex:
|
71 |
raise RuntimeException("Can't append values %s to layer %s (%s)" % ( |
72 |
repr(values),
|
73 |
self.getName(),
|
74 |
str(ex)
|
75 |
) |
76 |
) |
77 |
|
78 |
def updateSchema(self, schema): |
79 |
"""
|
80 |
Updates store FeatureType. If an error occurs raises
|
81 |
RuntimeException.
|
82 |
"""
|
83 |
try:
|
84 |
self().update(schema._javaobj)
|
85 |
except Throwable, ex:
|
86 |
raise RuntimeException(repr(ex)) |
87 |
|
88 |
def update(self, feature): |
89 |
"""
|
90 |
Updates exist feature in the layer featureSet
|
91 |
:param editableFeature: editableFeature
|
92 |
:type editableFeature: Java editableFeature
|
93 |
"""
|
94 |
if not self.isEditing(): |
95 |
self.edit()
|
96 |
|
97 |
if not isinstance(feature, EditableFeature): |
98 |
feature = feature._javaobj |
99 |
self.fset.update(feature)
|
100 |
|
101 |
def getSchema(self): |
102 |
"""Returns store data attributtes"""
|
103 |
return Schema(self.getDefaultFeatureType()) |
104 |
|
105 |
def commit(self): |
106 |
"""
|
107 |
Finish store edition saving changes. If an error occurs raises Throwable
|
108 |
Exception.
|
109 |
"""
|
110 |
try:
|
111 |
self.finishEditing()
|
112 |
except Throwable, ex:
|
113 |
self.abort()
|
114 |
raise Throwable("Can't finish layer edition, cancelling changes. %s" % repr(ex)) |
115 |
|
116 |
def abort(self): |
117 |
"""Finish store edition withoout saving changes and disposes itself"""
|
118 |
self.cancelEditing()
|
119 |
self.dispose()
|
120 |
|
121 |
def getSelection(self): |
122 |
"""Returns store features selected set"""
|
123 |
return FeatureSet(self().getSelection()) |
124 |
|
125 |
|