def resolveDomainValue(self, maybeEntityProxy, detectDeadEntities): """Convert a client-side value into a domain value. @param maybeEntityProxy the client object to resolve @param detectDeadEntities if <code>true</code> this method will throw a ReportableException containing a {@link DeadEntityException} if an EntityProxy cannot be resolved """ if isinstance(maybeEntityProxy, BaseProxy): bean = AutoBeanUtils.getAutoBean(maybeEntityProxy) domain = bean.getTag(Constants.DOMAIN_OBJECT) if domain is None and detectDeadEntities: raise ReportableException(DeadEntityException( 'The requested entity is not available on the server')) return domain elif isinstance(maybeEntityProxy, (list, set)): if isinstance(maybeEntityProxy, list): accumulator = list() elif isinstance(maybeEntityProxy, set): accumulator = set() else: raise ReportableException('Unsupported collection type ' + maybeEntityProxy.getClass().getName()) for o in maybeEntityProxy: accumulator.add(self.resolveDomainValue(o, detectDeadEntities)) return accumulator return maybeEntityProxy
def resolveDomainValue(self, maybeEntityProxy, detectDeadEntities): """Convert a client-side value into a domain value. @param maybeEntityProxy the client object to resolve @param detectDeadEntities if <code>true</code> this method will throw a ReportableException containing a {@link DeadEntityException} if an EntityProxy cannot be resolved """ if isinstance(maybeEntityProxy, BaseProxy): bean = AutoBeanUtils.getAutoBean(maybeEntityProxy) domain = bean.getTag(Constants.DOMAIN_OBJECT) if domain is None and detectDeadEntities: raise ReportableException( DeadEntityException( 'The requested entity is not available on the server')) return domain elif isinstance(maybeEntityProxy, (list, set)): if isinstance(maybeEntityProxy, list): accumulator = list() elif isinstance(maybeEntityProxy, set): accumulator = set() else: raise ReportableException( 'Unsupported collection type ' + maybeEntityProxy.getClass().getName()) for o in maybeEntityProxy: accumulator.add(self.resolveDomainValue(o, detectDeadEntities)) return accumulator return maybeEntityProxy
def resolveClientValue(self, domainValue, clientTypeOrAssignableTo, propertyRefs=None): """Given a domain object, return a value that can be encoded by the client. Creates a Resolution object that holds a client value that represents the given domain value. The resolved client value will be assignable to {@code clientType}. @param domainValue the domain object to be converted into a client-side value @param assignableTo the type in the client to which the resolved value should be assignable. A value of {@code null} indicates that any resolution will suffice. @param propertyRefs the property references requested by the client """ if propertyRefs is None: clientType = clientTypeOrAssignableTo if domainValue is None: return Resolution(None) anyType = clientType is None if anyType: clientType = object() assignableTo = TypeUtils.ensureBaseType(clientType) key = ResolutionKey(domainValue, clientType) previous = self._resolved[key] if (previous is not None and assignableTo.isInstance(previous.getClientObject())): return previous returnClass = self._service.resolveClientType(domainValue.getClass(), assignableTo, True) if anyType: assignableTo = returnClass # Pass simple values through if ValueCodex.canDecode(returnClass): return self.makeResolution(domainValue) # Convert entities to EntityProxies or EntityProxyIds isProxy = BaseProxy.isAssignableFrom(returnClass) isId = issubclass(returnClass, EntityProxyId) if isProxy or isId: proxyClass = returnClass.asSubclass(BaseProxy) return self.resolveClientProxy(domainValue, proxyClass, key) # Convert collections if issubclass(returnClass, Collection): if issubclass(returnClass, list): accumulator = list() elif issubclass(returnClass, set): accumulator = set() else: raise ReportableException('Unsupported collection type' + returnClass.getName()) elementType = TypeUtils.getSingleParameterization(Collection, clientType) for o in domainValue: accumulator.add(self.resolveClientValue(o, elementType).getClientObject()) return self.makeResolution(accumulator) raise ReportableException('Unsupported domain type ' + returnClass.getCanonicalName()) else: assignableTo = clientTypeOrAssignableTo toReturn = self.resolveClientValue(domainValue, assignableTo) if toReturn is None: return None self.addPathsToResolution(toReturn, '', self.expandPropertyRefs(propertyRefs)) while len(self._toProcess) > 0: working = list(self._toProcess) self._toProcess.clear() for resolution in working: if resolution.hasWork(): bean = AutoBeanUtils.getAutoBean(resolution.getClientObject()) bean.accept(self.PropertyResolver(resolution)) return toReturn.getClientObject()
def resolveClientValue(self, domainValue, clientTypeOrAssignableTo, propertyRefs=None): """Given a domain object, return a value that can be encoded by the client. Creates a Resolution object that holds a client value that represents the given domain value. The resolved client value will be assignable to {@code clientType}. @param domainValue the domain object to be converted into a client-side value @param assignableTo the type in the client to which the resolved value should be assignable. A value of {@code null} indicates that any resolution will suffice. @param propertyRefs the property references requested by the client """ if propertyRefs is None: clientType = clientTypeOrAssignableTo if domainValue is None: return Resolution(None) anyType = clientType is None if anyType: clientType = object() assignableTo = TypeUtils.ensureBaseType(clientType) key = ResolutionKey(domainValue, clientType) previous = self._resolved[key] if (previous is not None and assignableTo.isInstance(previous.getClientObject())): return previous returnClass = self._service.resolveClientType( domainValue.getClass(), assignableTo, True) if anyType: assignableTo = returnClass # Pass simple values through if ValueCodex.canDecode(returnClass): return self.makeResolution(domainValue) # Convert entities to EntityProxies or EntityProxyIds isProxy = BaseProxy.isAssignableFrom(returnClass) isId = issubclass(returnClass, EntityProxyId) if isProxy or isId: proxyClass = returnClass.asSubclass(BaseProxy) return self.resolveClientProxy(domainValue, proxyClass, key) # Convert collections if issubclass(returnClass, Collection): if issubclass(returnClass, list): accumulator = list() elif issubclass(returnClass, set): accumulator = set() else: raise ReportableException('Unsupported collection type' + returnClass.getName()) elementType = TypeUtils.getSingleParameterization( Collection, clientType) for o in domainValue: accumulator.add( self.resolveClientValue(o, elementType).getClientObject()) return self.makeResolution(accumulator) raise ReportableException('Unsupported domain type ' + returnClass.getCanonicalName()) else: assignableTo = clientTypeOrAssignableTo toReturn = self.resolveClientValue(domainValue, assignableTo) if toReturn is None: return None self.addPathsToResolution(toReturn, '', self.expandPropertyRefs(propertyRefs)) while len(self._toProcess) > 0: working = list(self._toProcess) self._toProcess.clear() for resolution in working: if resolution.hasWork(): bean = AutoBeanUtils.getAutoBean( resolution.getClientObject()) bean.accept(self.PropertyResolver(resolution)) return toReturn.getClientObject()