Example #1
0
        /// <summary>
        /// After Save
        /// </summary>
        /// <param name="newRecord">new Record</param>
        /// <param name="success">save success</param>
        /// <returns>success</returns>
        protected override bool AfterSave(bool newRecord, bool success)
        {
            if (!success)
            {
                return(success);
            }
            if (newRecord)
            {
                //	Info
                info = new MOrgInfo(this);
                info.Save();
                //	Access
                MRoleOrgAccess.CreateForOrg(this);
                MRole.GetDefault(GetCtx(), true);       //	reload
            }
            //	Value/Name change
            if (!newRecord && (Is_ValueChanged("Value") || Is_ValueChanged("Name")))
            {
                MAccount.UpdateValueDescription(GetCtx(), "AD_Org_ID=" + GetAD_Org_ID(), Get_Trx());
                if ("Y".Equals(GetCtx().GetContext("$Element_OT")))
                {
                    MAccount.UpdateValueDescription(GetCtx(), "AD_OrgTrx_ID=" + GetAD_Org_ID(), Get_Trx());
                }
            }

            return(true);
        }
        /// <summary>
        /// After Save
        /// </summary>
        /// <param name="newRecord">new Record</param>
        /// <param name="success">save success</param>
        /// <returns>success</returns>
        protected override bool AfterSave(bool newRecord, bool success)
        {
            if (!success)
            {
                return(success);
            }
            if (newRecord)
            {
                //	Info
                info = new MOrgInfo(this);
                info.Save();
                //	Access
                MRoleOrgAccess.CreateForOrg(this);
                MRole.GetDefault(GetCtx(), true);       //	reload
            }
            //	Value/Name change
            if (!newRecord && (Is_ValueChanged("Value") || Is_ValueChanged("Name")))
            {
                MAccount.UpdateValueDescription(GetCtx(), "AD_Org_ID=" + GetAD_Org_ID(), Get_Trx());
                if ("Y".Equals(GetCtx().GetContext("$Element_OT")))
                {
                    MAccount.UpdateValueDescription(GetCtx(), "AD_OrgTrx_ID=" + GetAD_Org_ID(), Get_Trx());
                }
            }

            if (!newRecord)
            {
                if (!IsSummary())
                {
                    int orgTableID = MTable.Get_Table_ID("AD_Org");

                    string sql = "SELECT AD_Tree_ID FROM AD_Tree "
                                 + "WHERE AD_Client_ID=" + GetCtx().GetAD_Client_ID() + " AND AD_Table_ID=" + orgTableID + " AND IsActive='Y' AND IsAllNodes='Y' "
                                 + "ORDER BY IsDefault DESC, AD_Tree_ID";

                    object AD_Tree_ID = DB.ExecuteScalar(sql, null, null);

                    DB.ExecuteQuery("Update AD_TreeNode Set Parent_ID = 0 where Parent_ID=" + GetAD_Org_ID() + " AND AD_Tree_ID=" + Util.GetValueOfInt(AD_Tree_ID));
                }
            }

            return(true);
        }
        /// <summary>
        /// Create Organizational Access for all Automatic Roles
        /// </summary>
        /// <param name="org">org</param>
        /// <returns>true if created</returns>
        public static bool CreateForOrg(MOrg org)
        {
            int counter = 0;

            MRole[] roles = MRole.GetOfClient(org.GetCtx());
            for (int i = 0; i < roles.Length; i++)
            {
                if (!roles[i].IsManual())
                {
                    MRoleOrgAccess orgAccess = new MRoleOrgAccess(org, roles[i].GetAD_Role_ID());
                    if (orgAccess.Save())
                    {
                        counter++;
                    }
                }
            }
            _log.Info(org + " - created #" + counter);
            return(counter != 0);
        }
        /// <summary>
        /// Get Organizational Info
        /// </summary>
        /// <param name="ctx">context</param>
        /// <param name="sql">sql command</param>
        /// <param name="id">id</param>
        /// <returns>array of Role Org Access</returns>
        private static MRoleOrgAccess[] Get(Ctx ctx, String sql, int id)
        {
            List <MRoleOrgAccess> list = new List <MRoleOrgAccess>();
            DataTable             dt   = null;
            IDataReader           idr  = null;

            try
            {
                SqlParameter[] param = new SqlParameter[1];
                param[0] = new SqlParameter("@param1", id);
                idr      = DataBase.DB.ExecuteReader(sql, param, null);
                dt       = new DataTable();
                dt.Load(idr);
                idr.Close();
                foreach (DataRow dr in dt.Rows)
                {
                    list.Add(new MRoleOrgAccess(ctx, dr, null));
                }
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                _log.Log(Level.SEVERE, "get", e);
            }
            finally
            {
                if (idr != null)
                {
                    idr.Close();
                }
                dt = null;
            }

            MRoleOrgAccess[] retValue = new MRoleOrgAccess[list.Count];
            retValue = list.ToArray();
            return(retValue);
        }