示例#1
0
        /// <summary>
        /// Create New Role
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="userLevel"></param>
        /// <param name="OrgID"></param>
        /// <returns></returns>
        public bool AddNewRole(string Name, string userLevel, List <int> OrgID)
        {
            var retValue         = false;
            int AD_Role_Table_ID = Convert.ToInt32(DB.ExecuteScalar("SELECT AD_Table_ID FROM AD_Table WHERE TableName='AD_Role'", null, null));

            try
            {
                string sql = @"SELECT AD_Column_ID,ColumnName,
                                  defaultvalue
                                FROM AD_Column
                                WHERE AD_Table_ID =" + AD_Role_Table_ID + @"
                                AND isActive      ='Y'
                                AND defaultvalue IS NOT NULL";

                DataSet ds = DB.ExecuteDataset(sql);                    // Get Default Values
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return(false);
                }

                MRole role = new MRole(ctx, 0, null);

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)           // Setting Default Values
                {
                    string value = ds.Tables[0].Rows[i]["DefaultValue"].ToString();

                    if (value.StartsWith("@"))
                    {
                        value = value.Substring(0, value.Length - 1);
                        string columnName = value.Substring(value.IndexOf("@") + 1);
                        value = ctx.GetContext(columnName);     // get global context
                    }
                    role.Set_Value(ds.Tables[0].Rows[i]["ColumnName"].ToString(), value);
                }
                role.SetIsManual(true);
                role.SetName(Name);
                role.SetUserLevel(userLevel);
                if (role.Save())
                {
                    if (OrgID != null)
                    {
                        for (int i = 0; i < OrgID.Count; i++)           // Assigning org access to role
                        {
                            MOrg           org   = new MOrg(ctx, OrgID[i], null);
                            MRoleOrgAccess roles = new MRoleOrgAccess(org, role.GetAD_Role_ID());
                            roles.SetAD_Client_ID(ctx.GetAD_Client_ID());
                            roles.SetAD_Org_ID(OrgID[i]);
                            roles.SetIsReadOnly(false);
                            roles.Save();
                        }
                        retValue = true;
                    }
                    else
                    {
                        retValue = true;
                    }
                }
            }
            catch
            {
                retValue = false;
            }


            //rr.Set_Value(


            return(retValue);
        }
示例#2
0
        /// <summary>
        /// Create New Role
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="userLevel"></param>
        /// <param name="OrgID"></param>
        /// <returns></returns>
        public String AddNewRole(string Name, string userLevel, List <int> OrgID)
        {
            string info = "";
            string msg;
            int    AD_Role_Table_ID = Convert.ToInt32(DB.ExecuteScalar("SELECT AD_Table_ID FROM AD_Table WHERE TableName='AD_Role'", null, null));

            try
            {
                string sql = @"SELECT AD_Column_ID,ColumnName,
                                  defaultvalue
                                FROM AD_Column
                                WHERE AD_Table_ID =" + AD_Role_Table_ID + @"
                                AND isActive      ='Y'
                                AND defaultvalue IS NOT NULL";

                DataSet ds = DB.ExecuteDataset(sql);                    // Get Default Values
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return(VAdvantage.Utility.Msg.GetMsg(ctx, "DefaultValueNotFound"));
                }

                MRole role = new MRole(ctx, 0, null);

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)           // Setting Default Values
                {
                    string value = ds.Tables[0].Rows[i]["DefaultValue"].ToString();

                    if (value.StartsWith("@"))
                    {
                        value = value.Substring(0, value.Length - 1);
                        string columnName = value.Substring(value.IndexOf("@") + 1);
                        value = ctx.GetContext(columnName);     // get global context
                    }
                    role.Set_Value(ds.Tables[0].Rows[i]["ColumnName"].ToString(), value);
                }
                role.SetIsManual(true);
                role.SetName(Name);
                role.SetUserLevel(userLevel);
                if (role.Save())
                {
                    if (OrgID != null)
                    {
                        for (int i = 0; i < OrgID.Count; i++)           // Assigning org access to role
                        {
                            MOrg           org   = new MOrg(ctx, OrgID[i], null);
                            MRoleOrgAccess roles = new MRoleOrgAccess(org, role.GetAD_Role_ID());
                            roles.SetAD_Client_ID(ctx.GetAD_Client_ID());
                            roles.SetAD_Org_ID(OrgID[i]);
                            roles.SetIsReadOnly(false);
                            roles.Save();
                        }
                    }
                }
                else
                {
                    ValueNamePair ppE = VAdvantage.Logging.VLogger.RetrieveError();

                    if (ppE != null)
                    {
                        msg  = ppE.GetValue();
                        info = ppE.GetName();
                    }
                }



                return(info);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }


            //rr.Set_Value(
        }
        }       //	prepare

        /// <summary>
        /// Perform Process.
        /// </summary>
        /// <returns>Message (text with variables)</returns>
        protected override String DoIt()
        {
            log.Info("C_BPartner_ID=" + _C_BPartner_ID
                     + ", AD_Org_ID=" + _AD_Org_ID
                     + ", AD_OrgType_ID=" + _AD_OrgType_ID
                     + ", AD_Role_ID=" + _AD_Role_ID);
            if (_C_BPartner_ID == 0)
            {
                throw new Exception("No Business Partner ID");
            }
            MBPartner bp = new MBPartner(GetCtx(), _C_BPartner_ID, Get_Trx());

            if (bp.Get_ID() == 0)
            {
                throw new Exception("Business Partner not found - C_BPartner_ID=" + _C_BPartner_ID);
            }
            //	BP Location
            MBPartnerLocation[] locs = bp.GetLocations(false);
            if (locs == null || locs.Length == 0)
            {
                throw new ArgumentException("Business Partner has no Location");
            }
            //	Location
            int C_Location_ID = locs[0].GetC_Location_ID();

            if (C_Location_ID == 0)
            {
                throw new ArgumentException("Business Partner Location has no Address");
            }

            //	Create Org
            Boolean newOrg = _AD_Org_ID == 0;
            MOrg    org    = new MOrg(GetCtx(), _AD_Org_ID, Get_Trx());

            if (newOrg)
            {
                org.SetValue(bp.GetValue());
                org.SetName(bp.GetName());
                org.SetDescription(bp.GetDescription());
                if (!org.Save())
                {
                    return(GetRetrievedError(org, "Organization not saved"));
                    //throw new Exception("Organization not saved");
                }
            }
            else        //	check if linked to already
            {
                int C_BPartner_ID = org.GetLinkedC_BPartner_ID();
                if (C_BPartner_ID > 0)
                {
                    throw new ArgumentException("Organization '" + org.GetName()
                                                + "' already linked (to C_BPartner_ID=" + C_BPartner_ID + ")");
                }
            }
            _AD_Org_ID = org.GetAD_Org_ID();

            //	Update Org Info
            MOrgInfo oInfo = org.GetInfo();

            oInfo.SetAD_OrgType_ID(_AD_OrgType_ID);
            if (newOrg)
            {
                oInfo.SetC_Location_ID(C_Location_ID);
            }

            //	Create Warehouse
            MWarehouse wh = null;

            if (!newOrg)
            {
                MWarehouse[] whs = MWarehouse.GetForOrg(GetCtx(), _AD_Org_ID);
                if (whs != null && whs.Length > 0)
                {
                    wh = whs[0]; //	pick first
                }
            }
            //	New Warehouse
            if (wh == null)
            {
                wh = new MWarehouse(org);
                if (!wh.Save())
                {
                    return(GetRetrievedError(wh, "Warehouse not saved"));
                    //throw new Exception("Warehouse not saved");
                }
            }
            //	Create Locator
            MLocator mLoc = wh.GetDefaultLocator();

            if (mLoc == null)
            {
                mLoc = new MLocator(wh, "Standard");
                mLoc.SetIsDefault(true);
                mLoc.Save();
            }

            //	Update/Save Org Info
            oInfo.SetM_Warehouse_ID(wh.GetM_Warehouse_ID());
            if (!oInfo.Save(Get_Trx()))
            {
                return(GetRetrievedError(oInfo, "Organization Info not saved"));
                //throw new Exception("Organization Info not saved");
            }

            //	Update BPartner
            bp.SetAD_OrgBP_ID(_AD_Org_ID);
            if (bp.GetAD_Org_ID() != 0)
            {
                bp.SetClientOrg(bp.GetAD_Client_ID(), 0); //	Shared BPartner
            }

            //	Save BP
            if (!bp.Save())
            {
                return(GetRetrievedError(bp, "Business Partner not updated"));
                //throw new Exception("Business Partner not updated");
            }

            //	Limit to specific Role
            if (_AD_Role_ID != 0)
            {
                Boolean          found       = false;
                MRoleOrgAccess[] orgAccesses = MRoleOrgAccess.GetOfOrg(GetCtx(), _AD_Org_ID);
                //	delete all accesses except the specific
                for (int i = 0; i < orgAccesses.Length; i++)
                {
                    if (orgAccesses[i].GetAD_Role_ID() == _AD_Role_ID)
                    {
                        found = true;
                    }
                    else
                    {
                        orgAccesses[i].Delete(true);
                    }
                }
                //	create access
                if (!found)
                {
                    MRoleOrgAccess orgAccess = new MRoleOrgAccess(org, _AD_Role_ID);
                    orgAccess.Save();
                }
            }

            //	Reset Client Role
            MRole.GetDefault(GetCtx(), true);

            return("Business Partner - Organization Link created");
        } //	doIt