

from org.gvsig.fmap.dal.feature.impl import DefaultFeature as JFeature
from java.lang import RuntimeException

def _geometry(self):
  return self.getDefaultGeometry()
  
def _getValues(self):
  items = dict()
  for name,value in self.iteritems():
    items[name] = value
  return items 

def _edit(self):
  raise Exception("Deprecated method, use import gvSIG_2_0_0 for compatibilitty with gvSIG 2.0.0 or use getEditable method")

def _getitem(self,attributeName):
  return self.get(attributeName)

def _getattr(self,attributeName):
  # FIX console error when try to introspect feature object
  if name in ('__methods__'):
      return dict()
  elif name in ('__members__'):
      return self.getValues().keys()
  elif name == '__dict__':
      return self.getValues()      
      
  try:
    v = getattr(self, attributeName, None)
    if v == None:
      v = self.get(attributeName)        
    return v
  except Throwable, ex:
    raise RuntimeException("Can't access to attribute %s of feature (%s)" % (attributeName, str(ex)))    

def _call(self):
  return self

def _len(self):
  return self.getType().size()

def _iteritems(self):
  descriptor = self.getType()
  for attr in descriptor.getAttributeDescriptors():
    name = attr.getName()
    value = self.get(attr.getName())
    yield name,value
  
def _itervalues(self):
  descriptor = self.getType()
  for attr in descriptor.getAttributeDescriptors():
    name = attr.getName()
    value = self.get(attr.getName())
    yield value
  
def _iterkeys(self):
  descriptor = self.getType()
  for attr in descriptor.getAttributeDescriptors():
    name = attr.getName()
    yield name
  

#
# Inject new methods in the class JFeature
#
JFeature.getValues = _getValues
JFeature.geometry = _geometry
JFeature.edit = _edit
JFeature.__getitem__ = _getitem
JFeature.__getattr__ = _getattr
JFeature.__call__ = _call
JFeature.__len__ = _len
JFeature.__iter__ = _itervalues
JFeature.itervalues = _itervalues
JFeature.iteritems = _iteritems
JFeature.iterkeys = _iterkeys


