Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting.app / trunk / org.gvsig.scripting.app / org.gvsig.scripting.app.extension / src / main / resources-plugin / scripting / lib / gvsig2_1_0 / vectorial / featurestore.py @ 447

History | View | Annotate | Download (4.74 KB)

1

    
2

    
3
__docformat__ = "restructuredtext en"
4

    
5
from org.gvsig.fmap.dal.feature.impl import DefaultFeatureStore as JFeatureStore
6

    
7
class FeatureStore(JFeatureStore):
8
  """
9
  Represents feature store.
10
  It's used as Table/Layer objects store
11
  
12
  Extends the `java FeatureStore`_.
13
  
14
  .. _`java FeatureStore` : FIXME_URL
15
  """
16

    
17
  @staticmethod
18
  def getSchema(self):
19
    """
20
    Returns the schema of the store.
21
    Its configuration of attributes.
22
    """
23
    return self.getDefaultFeatureType()
24

    
25
  @staticmethod
26
  def features(self, expresion = None, sortby="", asc=True):
27
    """
28
    Returns layer features set, with all layer features, 
29
    or selected featured if there are (could be empty feature set).
30
    
31
    :parameters:
32
      expresion : string
33
        filter to apply to the feature set to select
34
      sortby : string
35
        coma separated names of attribute to sort
36
      asc : boolean
37
        True for order ascending, False for orden descending
38
    :return:
39
      The feature set request
40
    :returntype:
41
      FeatureSet
42
    """   
43
    if expresion == None and sortby =="":
44
      return self.getFeatureSet()         
45
    
46
    try:
47
      application = ApplicationLocator.getManager()
48
      datamanager =  application.getDataManager()
49
      query = self.createFeatureQuery()
50
      if sortby != "":
51
          order = FeatureQueryOrder()
52
          order.add(sortby, asc)
53
          query.setOrder(order)
54
      if expresion != None:
55
          query.setFilter(datamanager.createExpresion(expresion))
56
      fset = self.getFeatureSet(query)
57
    except Exception, e:
58
      return None
59
    return fset
60

    
61
  @staticmethod
62
  def __iter__(self):
63
    return self.getFeatureSet()
64
  
65
  @staticmethod
66
  def __len__(self):
67
    return self.getFeatureSet().getSize()
68
  
69
  @staticmethod
70
  def append(self, *args, **kwargs):
71
    """
72
    Append a new feature to the store.
73
    If the store is not in editing mode, it is set before to append the feature.
74
    
75
    If one args is provided, it soul be the feature to append.
76
    If pairs of name,value arguments are specified, a feature is build with its,
77
    and is appened to the store.
78
    """
79
    try:
80
      if not self.isEditing():
81
        self.super_edit() 
82

    
83
      if len(args) ==1:
84
        values.insert(args[0])
85
        return
86
      
87
      f = self.createNewFeature()
88
      
89
      if f == None:
90
        raise RuntimeError("Failed to create a new Feature")
91
        
92
      for k,v in kwargs.iteritems():
93
        f.set(k,v)
94
        self.insert(f)
95
        
96
    except Throwable, ex:
97
      raise RuntimeException("Can't append values %s to layer %s (%s)" % (
98
        repr(kwargs), 
99
        self.getName(), 
100
        str(ex)
101
        )
102
      )
103

    
104
  @staticmethod
105
  def update(self, feature):
106
    """
107
    Updates exist feature in the store or the schema of store.
108
    
109
    If the store is not in editing mode, it is set before to append the feature.
110

111
    If as parameter pass a Schema, update the schema of the store. If is a feature
112
    update this feature.
113
    
114
    :parameters:
115
      feature : Feature 
116
        feature to update
117
    """
118
    if not self.isEditing():
119
        self.super_edit(self) 
120
    self.super_update(self,feature) 
121
  
122
  @staticmethod
123
  def edit(self):
124
    """
125
    Set store in edition mode.
126
    
127
    If the store is already in editing mode do thing.
128
    """     
129
    if not self.isEditing():
130
      self.super_edit()
131
     
132
  @staticmethod
133
  def commit(self):
134
    """
135
    Finish store edition saving changes. 
136
    If an error occurs abort transaction and raises Exception.
137
    """
138
    try:
139
      self.finishEditing()          
140
    except Throwable, ex:
141
      self.abort()
142
      raise RuntimeException("Can't finish layer edition, cancelling changes. %s" % repr(ex))
143
  
144
  @staticmethod
145
  def abort(self):
146
    """
147
    Finish store edition withoout saving changes.
148
    
149
    All changes are undoing.
150
    """
151
    self.cancelEditing() 
152
 
153
  def __call__(self):
154
    """
155
    Return the java object represented by this object.
156
    
157
    This method is for compatibility with scripting API of gvSIG 2.0
158
    
159
    :ReturnType:
160
      LayerVectorial
161
    :deprecated: 
162
      With gvSIG 2.1 the call this method isn't necesary
163
      
164
    """
165
    return self    
166

    
167

    
168
#
169
# Save methods edit and update that are rewriten in python
170
#
171
JFeatureStore.super_edit = JFeatureStore.edit
172
JFeatureStore.super_update = JFeatureStore.update
173
#
174
# Inject new methods in the class JFeatureStore
175
#
176
JFeatureStore.getSchema = FeatureStore.getSchema
177
JFeatureStore.features = FeatureStore.features
178
JFeatureStore.append = FeatureStore.append
179
JFeatureStore.update = FeatureStore.update
180
JFeatureStore.edit = FeatureStore.edit
181
JFeatureStore.commit = FeatureStore.commit
182
JFeatureStore.abort = FeatureStore.abort
183
JFeatureStore.__call__ = FeatureStore.__call__
184
JFeatureStore.__iter__ = FeatureStore.__iter__
185
JFeatureStore.__len__ = FeatureStore.__len__
186

    
187

    
188