Exemplo n.º 1
0
        private static object Create(Type objectType, object criteria)
        {
            Server.DataPortalResult  result    = null;
            Server.DataPortalContext dpContext = null;
            try
            {
                OnDataPortalInitInvoke(null);

                if (!YYT.Security.AuthorizationRules.CanCreateObject(objectType))
                {
                    throw new System.Security.SecurityException(string.Format(
                                                                    Resources.UserNotAuthorizedException,
                                                                    "create",
                                                                    objectType.Name));
                }

                var method = Server.DataPortalMethodCache.GetCreateMethod(objectType, criteria);

                DataPortalClient.IDataPortalProxy proxy;
                proxy = GetDataPortalProxy(method.RunLocal);

                dpContext =
                    new YYT.Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote);

                OnDataPortalInvoke(new DataPortalEventArgs(dpContext, objectType, DataPortalOperations.Create));

                try
                {
                    result = proxy.Create(objectType, criteria, dpContext);
                }
                catch (Server.DataPortalException ex)
                {
                    result = ex.Result;
                    if (proxy.IsServerRemote)
                    {
                        ApplicationContext.SetGlobalContext(result.GlobalContext);
                    }
                    throw new DataPortalException(
                              string.Format("DataPortal.Create {0} ({1})", Resources.Failed, ex.InnerException.InnerException),
                              ex.InnerException, result.ReturnObject);
                }

                if (proxy.IsServerRemote)
                {
                    ApplicationContext.SetGlobalContext(result.GlobalContext);
                }

                OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, DataPortalOperations.Create));
            }
            catch (Exception ex)
            {
                OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, DataPortalOperations.Create, ex));
                throw;
            }
            return(result.ReturnObject);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Called by the business object's Save() method to
        /// insert, update or delete an object in the database.
        /// </summary>
        /// <remarks>
        /// Note that this method returns a reference to the updated business object.
        /// If the server-side DataPortal is running remotely, this will be a new and
        /// different object from the original, and all object references MUST be updated
        /// to use this new object.
        /// </remarks>
        /// <param name="obj">A reference to the business object to be updated.</param>
        /// <returns>A reference to the updated business object.</returns>
        public static object Update(object obj)
        {
            Server.DataPortalResult  result    = null;
            Server.DataPortalContext dpContext = null;
            DataPortalOperations     operation = DataPortalOperations.Update;
            Type objectType = obj.GetType();

            try
            {
                OnDataPortalInitInvoke(null);
                DataPortalMethodInfo method;
                var factoryInfo = ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType);
                if (factoryInfo != null)
                {
                    var factoryType = FactoryDataPortal.FactoryLoader.GetFactoryType(factoryInfo.FactoryTypeName);
                    var bbase       = obj as Core.BusinessBase;
                    if (bbase != null && bbase.IsDeleted)
                    {
                        if (!YYT.Security.AuthorizationRules.CanDeleteObject(objectType))
                        {
                            throw new System.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException,
                                                                                      "delete",
                                                                                      objectType.Name));
                        }
                        method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.DeleteMethodName, new object[] { obj });
                    }
                    else
                    {
                        if (!YYT.Security.AuthorizationRules.CanEditObject(objectType))
                        {
                            throw new System.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException,
                                                                                      "save",
                                                                                      objectType.Name));
                        }
                        method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.UpdateMethodName, new object[] { obj });
                    }
                }
                else
                {
                    string methodName;
                    if (obj is CommandBase)
                    {
                        methodName = "DataPortal_Execute";
                        operation  = DataPortalOperations.Execute;
                        if (!YYT.Security.AuthorizationRules.CanEditObject(objectType))
                        {
                            throw new System.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException,
                                                                                      "execute",
                                                                                      objectType.Name));
                        }
                    }
                    else
                    {
                        var bbase = obj as Core.BusinessBase;
                        if (bbase != null)
                        {
                            if (bbase.IsDeleted)
                            {
                                methodName = "DataPortal_DeleteSelf";
                                if (!YYT.Security.AuthorizationRules.CanDeleteObject(objectType))
                                {
                                    throw new System.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException,
                                                                                              "delete",
                                                                                              objectType.Name));
                                }
                            }
                            else
                            if (bbase.IsNew)
                            {
                                methodName = "DataPortal_Insert";
                                if (!YYT.Security.AuthorizationRules.CanCreateObject(objectType))
                                {
                                    throw new System.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException,
                                                                                              "create",
                                                                                              objectType.Name));
                                }
                            }
                            else
                            {
                                methodName = "DataPortal_Update";
                                if (!YYT.Security.AuthorizationRules.CanEditObject(objectType))
                                {
                                    throw new System.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException,
                                                                                              "save",
                                                                                              objectType.Name));
                                }
                            }
                        }
                        else
                        {
                            methodName = "DataPortal_Update";
                            if (!YYT.Security.AuthorizationRules.CanEditObject(objectType))
                            {
                                throw new System.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException,
                                                                                          "save",
                                                                                          objectType.Name));
                            }
                        }
                    }
                    method = Server.DataPortalMethodCache.GetMethodInfo(obj.GetType(), methodName);
                }

                DataPortalClient.IDataPortalProxy proxy;
                proxy = GetDataPortalProxy(method.RunLocal);

                dpContext =
                    new Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote);

                OnDataPortalInvoke(new DataPortalEventArgs(dpContext, objectType, operation));

                try
                {
                    if (!proxy.IsServerRemote && ApplicationContext.AutoCloneOnUpdate)
                    {
                        // when using local data portal, automatically
                        // clone original object before saving
                        ICloneable cloneable = obj as ICloneable;
                        if (cloneable != null)
                        {
                            obj = cloneable.Clone();
                        }
                    }
                    result = proxy.Update(obj, dpContext);
                }
                catch (Server.DataPortalException ex)
                {
                    result = ex.Result;
                    if (proxy.IsServerRemote)
                    {
                        ApplicationContext.SetGlobalContext(result.GlobalContext);
                    }
                    throw new DataPortalException(
                              String.Format("DataPortal.Update {0} ({1})", Resources.Failed, ex.InnerException.InnerException),
                              ex.InnerException, result.ReturnObject);
                }

                if (proxy.IsServerRemote)
                {
                    ApplicationContext.SetGlobalContext(result.GlobalContext);
                }

                OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, operation));
            }
            catch (Exception ex)
            {
                OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, operation, ex));
                throw;
            }
            return(result.ReturnObject);
        }
Exemplo n.º 3
0
        internal static object Fetch(Type objectType, object criteria)
        {
            Server.DataPortalResult  result    = null;
            Server.DataPortalContext dpContext = null;
            try
            {
                OnDataPortalInitInvoke(null);

                if (!YYT.Security.AuthorizationRules.CanGetObject(objectType))
                {
                    throw new System.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException,
                                                                              "get",
                                                                              objectType.Name));
                }

                var method = Server.DataPortalMethodCache.GetFetchMethod(objectType, criteria);

                DataPortalClient.IDataPortalProxy proxy;
                proxy = GetDataPortalProxy(method.RunLocal);

                dpContext =
                    new Server.DataPortalContext(GetPrincipal(),
                                                 proxy.IsServerRemote);

                OnDataPortalInvoke(new DataPortalEventArgs(dpContext, objectType, DataPortalOperations.Fetch));

                try
                {
                    result = proxy.Fetch(objectType, criteria, dpContext);
                }
                catch (Server.DataPortalException ex)
                {
                    result = ex.Result;
                    if (proxy.IsServerRemote)
                    {
                        ApplicationContext.SetGlobalContext(result.GlobalContext);
                    }
                    string innerMessage = string.Empty;
                    if (ex.InnerException is YYT.Reflection.CallMethodException)
                    {
                        if (ex.InnerException.InnerException != null)
                        {
                            innerMessage = ex.InnerException.InnerException.Message;
                        }
                    }
                    else
                    {
                        innerMessage = ex.InnerException.Message;
                    }
                    throw new DataPortalException(
                              String.Format("DataPortal.Fetch {0} ({1})", Resources.Failed, innerMessage),
                              ex.InnerException, result.ReturnObject);
                }

                if (proxy.IsServerRemote)
                {
                    ApplicationContext.SetGlobalContext(result.GlobalContext);
                }

                OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, DataPortalOperations.Fetch));
            }
            catch (Exception ex)
            {
                OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, DataPortalOperations.Fetch, ex));
                throw;
            }
            return(result.ReturnObject);
        }