Example #1
0
    def __init__(self, resolution, resolver):
        key = resolution.getResolutionKey()
        self._domainEntity = key.getDomainObject()
        self._isOwnerValueProxy = self._resolver._state.isValueType(TypeUtils.ensureBaseType(key.requestedType))
        self._needsSimpleValues = resolution.needsSimpleValues()
        self._propertyRefs = resolution.takeWork()

        self._resolver = resolver
Example #2
0
    def __init__(self, resolution, resolver):
        key = resolution.getResolutionKey()
        self._domainEntity = key.getDomainObject()
        self._isOwnerValueProxy = self._resolver._state.isValueType(
            TypeUtils.ensureBaseType(key.requestedType))
        self._needsSimpleValues = resolution.needsSimpleValues()
        self._propertyRefs = resolution.takeWork()

        self._resolver = resolver
    def decodeInvocationArguments_(self, source, invocation, contextMethod):
        """Decode the arguments to pass into the domain method. If the domain method
        is not static, the instance object will be in the 0th position.
        """
        isStatic = Request.isAssignableFrom(contextMethod.getReturnType())
        baseLength = contextMethod.getParameterTypes().length
        length = baseLength + (0 if isStatic else 1)
        offset = 0 if isStatic else 1
        contextArgs = [None] * length
        genericArgs = [None] * length

        if not isStatic:
            genericArgs[0] = TypeUtils.getSingleParameterization(InstanceRequest, contextMethod.getGenericReturnType())
            contextArgs[0] = TypeUtils.ensureBaseType(genericArgs[0])

        System.arraycopy(contextMethod.getParameterTypes(), 0, contextArgs, offset, baseLength)
        System.arraycopy(contextMethod.getGenericParameterTypes(), 0, genericArgs, offset, baseLength)

        args = self.decodeInvocationArguments(source, invocation.getParameters(), contextArgs, genericArgs)
        return args
    def decodeInvocationArguments(self, source, parameters, contextArgs,
            genericArgs):
        """Handles instance invocations as the instance at the 0th parameter.
        """
        if parameters is None:
            # Can't return Collections.emptyList() because this must be mutable
            return list()

        assert len(parameters) == contextArgs.length
        args = list()
        for i in range(contextArgs.length):
            type_ = contextArgs[i]
            elementType = None
            if Collection.isAssignableFrom(type_):
                elementType = TypeUtils.ensureBaseType(TypeUtils.getSingleParameterization(Collection, genericArgs[i]))
                split = parameters.get(i)
            else:
                split = parameters.get(i)
            arg = EntityCodex.decode(source, type_, elementType, split)
            arg = source.getResolver().resolveDomainValue(arg, not (EntityProxyId == contextArgs[i]))
            args.add(arg)
        return args
Example #5
0
    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()
Example #6
0
    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()