Beispiel #1
0
        /// <summary>
        /// Before Save
        /// </summary>
        /// <param name="newRecord"></param>
        /// <returns></returns>
        protected override bool BeforeSave(bool newRecord)
        {
            if (!CheckVersions(false))
            {
                return(false);
            }

            int displayType = GetAD_Reference_ID();

            //	Length
            if (DisplayType.IsLOB(displayType)) //	LOBs are 0
            {
                if (GetFieldLength() != 0)
                {
                    SetFieldLength(0);
                }
            }
            else if (GetFieldLength() == 0)
            {
                if (DisplayType.IsID(displayType))
                {
                    SetFieldLength(10);
                }
                else if (DisplayType.IsNumeric(displayType))
                {
                    SetFieldLength(14);
                }
                else if (DisplayType.IsDate(displayType))
                {
                    SetFieldLength(7);
                }
                else if (DisplayType.YesNo == displayType)
                {
                    SetFieldLength(1);
                }
                else
                {
                    log.SaveError("FillMandatory", Utility.Msg.GetElement(GetCtx(), "FieldLength"));
                    return(false);
                }
            }

            /** Views are not updateable
             * UPDATE AD_Column c
             * SET IsUpdateable='N', IsAlwaysUpdateable='N'
             * WHERE AD_Table_ID IN (SELECT AD_Table_ID FROM AD_Table WHERE IsView='Y')
             **/

            //	Virtual Column
            if (IsVirtualColumn())
            {
                if (IsMandatory())
                {
                    SetIsMandatory(false);
                }
                if (IsMandatoryUI())
                {
                    SetIsMandatoryUI(false);
                }
                if (IsUpdateable())
                {
                    SetIsUpdateable(false);
                }
            }
            //	Updateable/Mandatory
            if (IsParent() || IsKey())
            {
                SetIsUpdateable(false);
                SetIsMandatory(true);
            }
            if (IsAlwaysUpdateable() && !IsUpdateable())
            {
                SetIsAlwaysUpdateable(false);
            }
            //	Encrypted
            if (IsEncrypted())
            {
                int dt = GetAD_Reference_ID();
                if (IsKey() || IsParent() || IsStandardColumn() ||
                    IsVirtualColumn() || IsIdentifier() || IsTranslated() ||
                    DisplayType.IsLookup(dt) || DisplayType.IsLOB(dt) ||
                    "DocumentNo".ToLower().Equals(GetColumnName().ToLower()) ||
                    "Value".ToLower().Equals(GetColumnName().ToLower()) ||
                    "Name".ToLower().Equals(GetColumnName().ToLower()))
                {
                    log.Warning("Encryption not sensible - " + GetColumnName());
                    SetIsEncrypted(false);
                }
            }

            //	Sync Terminology
            if ((newRecord || Is_ValueChanged("AD_Element_ID")) &&
                GetAD_Element_ID() != 0)
            {
                _element = new M_Element(GetCtx(), GetAD_Element_ID(), Get_TrxName());
                SetColumnName(_element.GetColumnName());
                SetName(_element.GetName());
                SetDescription(_element.GetDescription());
                SetHelp(_element.GetHelp());
            }

            if (IsKey() && (newRecord || Is_ValueChanged("IsKey")))
            {
                SetConstraintType(null);
            }

            return(true);
        }
        }       //	prepare

        /**
         *  Process
         *	@return info
         *	@throws Exception
         */
        protected override String DoIt()// throws Exception
        {
            log.Info("AD_Column_ID=" + p_AD_Column_ID
                     + ", IsEncrypted=" + p_IsEncrypted
                     + ", ChangeSetting=" + p_ChangeSetting
                     + ", MaxLength=" + p_MaxLength);
            MColumn column = new MColumn(GetCtx(), p_AD_Column_ID, null);

            if (column.Get_ID() == 0 || column.Get_ID() != p_AD_Column_ID)
            {
                throw new Exception("@NotFound@ @AD_Column_ID@ - " + p_AD_Column_ID);
            }
            //
            String columnName = column.GetColumnName();
            int    dt         = column.GetAD_Reference_ID();

            //	Can it be enabled?
            if (column.IsKey() ||
                column.IsParent() ||
                column.IsStandardColumn() ||
                column.IsVirtualColumn() ||
                column.IsIdentifier() ||
                column.IsTranslated() ||
                DisplayType.IsLookup(dt) ||
                DisplayType.IsLOB(dt) ||
                "DocumentNo".Equals(column.GetColumnName(), StringComparison.OrdinalIgnoreCase) ||
                "Value".Equals(column.GetColumnName(), StringComparison.OrdinalIgnoreCase) ||
                "Name".Equals(column.GetColumnName(), StringComparison.OrdinalIgnoreCase))
            {
                if (column.IsEncrypted())
                {
                    column.SetIsEncrypted(false);
                    column.Save();
                }
                return(columnName + ": cannot be encrypted");
            }

            //	Start
            AddLog(0, null, null, "Encryption Class = " + SecureEngineUtility.SecureEngine.GetClassName());
            bool error = false;

            //	Test Value
            if (p_TestValue != null && p_TestValue.Length > 0)
            {
                String encString = SecureEngineUtility.SecureEngine.Encrypt(p_TestValue);
                AddLog(0, null, null, "Encrypted Test Value=" + encString);
                String clearString = SecureEngineUtility.SecureEngine.Decrypt(encString);
                if (p_TestValue.Equals(clearString))
                {
                    AddLog(0, null, null, "Decrypted=" + clearString
                           + " (same as test value)");
                }
                else
                {
                    AddLog(0, null, null, "Decrypted=" + clearString
                           + " (NOT the same as test value - check algorithm)");
                    error = true;
                }
                int encLength = encString.Length;
                AddLog(0, null, null, "Test Length=" + p_TestValue.Length + " -> " + encLength);
                if (encLength <= column.GetFieldLength())
                {
                    AddLog(0, null, null, "Encrypted Length (" + encLength
                           + ") fits into field (" + column.GetFieldLength() + ")");
                }
                else
                {
                    AddLog(0, null, null, "Encrypted Length (" + encLength
                           + ") does NOT fit into field (" + column.GetFieldLength() + ") - resize field");
                    error = true;
                }
            }

            //	Length Test
            if (p_MaxLength != 0)
            {
                String testClear = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                while (testClear.Length < p_MaxLength)
                {
                    testClear += testClear;
                }
                testClear = testClear.Substring(0, p_MaxLength);
                log.Config("Test=" + testClear + " (" + p_MaxLength + ")");
                //
                String encString = SecureEngineUtility.SecureEngine.Encrypt(testClear);
                int    encLength = encString.Length;
                AddLog(0, null, null, "Test Max Length=" + testClear.Length + " -> " + encLength);
                if (encLength <= column.GetFieldLength())
                {
                    AddLog(0, null, null, "Encrypted Max Length (" + encLength
                           + ") fits into field (" + column.GetFieldLength() + ")");
                }
                else
                {
                    AddLog(0, null, null, "Encrypted Max Length (" + encLength
                           + ") does NOT fit into field (" + column.GetFieldLength() + ") - resize field");
                    error = true;
                }
            }

            if (p_IsEncrypted != column.IsEncrypted())
            {
                if (error || !p_ChangeSetting)
                {
                    AddLog(0, null, null, "Encryption NOT changed - Encryption=" + column.IsEncrypted());
                }
                else
                {
                    column.SetIsEncrypted(p_IsEncrypted);
                    if (column.Save())
                    {
                        AddLog(0, null, null, "Encryption CHANGED - Encryption=" + column.IsEncrypted());
                    }
                    else
                    {
                        AddLog(0, null, null, "Save Error");
                    }
                }
            }
            return("Encryption=" + column.IsEncrypted());
        }
        }       //	prepare

        /**
         *  Process
         *	@return info
         *	@throws Exception
         */
        protected override String DoIt()// throws Exception
        {
            log.Info("AD_Column_ID=" + p_AD_Column_ID
                     + ", IsEncrypted=" + p_IsEncrypted
                     + ", ChangeSetting=" + p_ChangeSetting
                     + ", MaxLength=" + p_MaxLength);
            MColumn column = new MColumn(GetCtx(), p_AD_Column_ID, Get_Trx());

            if (column.Get_ID() == 0 || column.Get_ID() != p_AD_Column_ID)
            {
                throw new Exception("@NotFound@ @AD_Column_ID@ - " + p_AD_Column_ID);
            }
            //
            String columnName = column.GetColumnName();
            int    dt         = column.GetAD_Reference_ID();

            //	Can it be enabled?
            if (column.IsKey() ||
                column.IsParent() ||
                column.IsStandardColumn() ||
                column.IsVirtualColumn() ||
                column.IsIdentifier() ||
                column.IsTranslated() ||
                DisplayType.IsLookup(dt) ||
                DisplayType.IsLOB(dt) ||
                "DocumentNo".Equals(column.GetColumnName(), StringComparison.OrdinalIgnoreCase) ||
                "Value".Equals(column.GetColumnName(), StringComparison.OrdinalIgnoreCase) ||
                "Name".Equals(column.GetColumnName(), StringComparison.OrdinalIgnoreCase))
            {
                if (column.IsEncrypted())
                {
                    column.SetIsEncrypted(false);
                    column.Save(Get_Trx());
                }
                return(columnName + ": cannot be encrypted");
            }

            //	Start
            AddLog(0, null, null, "Encryption Class = " + SecureEngine.GetClassName());
            bool error = false;

            //	Test Value
            if (p_TestValue != null && p_TestValue.Length > 0)
            {
                String encString = SecureEngine.Encrypt(p_TestValue);
                AddLog(0, null, null, "Encrypted Test Value=" + encString);
                String clearString = SecureEngine.Decrypt(encString);
                if (p_TestValue.Equals(clearString))
                {
                    AddLog(0, null, null, "Decrypted=" + clearString
                           + " (same as test value)");
                }
                else
                {
                    AddLog(0, null, null, "Decrypted=" + clearString
                           + " (NOT the same as test value - check algorithm)");
                    error = true;
                }
                int encLength = encString.Length;
                AddLog(0, null, null, "Test Length=" + p_TestValue.Length + " -> " + encLength);
                if (encLength <= column.GetFieldLength())
                {
                    AddLog(0, null, null, "Encrypted Length (" + encLength
                           + ") fits into field (" + column.GetFieldLength() + ")");
                }
                else
                {
                    AddLog(0, null, null, "Encrypted Length (" + encLength
                           + ") does NOT fit into field (" + column.GetFieldLength() + ") - resize field");
                    error = true;
                }
            }

            //	Length Test
            if (p_MaxLength != 0)
            {
                String testClear = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                while (testClear.Length < p_MaxLength)
                {
                    testClear += testClear;
                }
                testClear = testClear.Substring(0, p_MaxLength);
                log.Config("Test=" + testClear + " (" + p_MaxLength + ")");
                //
                String encString = SecureEngine.Encrypt(testClear);
                int    encLength = encString.Length;
                AddLog(0, null, null, "Test Max Length=" + testClear.Length + " -> " + encLength);
                if (encLength <= column.GetFieldLength())
                {
                    AddLog(0, null, null, "Encrypted Max Length (" + encLength
                           + ") fits into field (" + column.GetFieldLength() + ")");
                }
                else
                {
                    AddLog(0, null, null, "Encrypted Max Length (" + encLength
                           + ") does NOT fit into field (" + column.GetFieldLength() + ") - resize field");
                    error = true;
                }
            }

            if (p_IsEncrypted != column.IsEncrypted())
            {
                if (error || !p_ChangeSetting)
                {
                    AddLog(0, null, null, "Encryption NOT changed - Encryption=" + column.IsEncrypted());
                }
                else
                {
                    column.SetIsEncrypted(p_IsEncrypted);
                    if (column.Save(Get_Trx()))
                    {
                        AddLog(0, null, null, "Encryption CHANGED - Encryption=" + column.IsEncrypted());
                    }
                    else
                    {
                        AddLog(0, null, null, "Save Error");
                    }
                }
            }


            if (p_IsEncrypted == column.IsEncrypted() && !error)      // Done By Karan on 10-nov-2016, to encrypt/decrypt passwords according to settings.
            {
                //object colID = DB.ExecuteScalar("SELECT AD_Column_ID FROM AD_Column WHERE AD_Table_ID =(SELECT AD_Table_ID From AD_Table WHERE TableName='AD_User') AND ColumnName='Password'", null, Get_Trx());



                // if (colID != null && colID != DBNull.Value && Convert.ToInt32(colID) == column.GetAD_Column_ID())
                //{

                string tableName = MTable.GetTableName(GetCtx(), column.GetAD_Table_ID());

                DataSet ds = DB.ExecuteDataset("SELECT " + column.GetColumnName() + "," + tableName
                                               + "_ID FROM " + tableName, null, Get_Trx());
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    if (p_IsEncrypted)
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            if (ds.Tables[0].Rows[i][column.GetColumnName()] != null && ds.Tables[0].Rows[i][column.GetColumnName()] != DBNull.Value &&
                                !SecureEngine.IsEncrypted(ds.Tables[0].Rows[i][column.GetColumnName()].ToString()))
                            {
                                //MUser user = new MUser(GetCtx(), Util.GetValueOfInt(ds.Tables[0].Rows[i][MTable.GetTableName(GetCtx(), column.GetAD_Table_ID()) + "_ID"]), Get_Trx());
                                //user.SetPassword(SecureEngine.Encrypt(ds.Tables[0].Rows[i][column.GetColumnName()].ToString()));

                                int encLength = SecureEngine.Encrypt(ds.Tables[0].Rows[i][column.GetColumnName()].ToString()).Length;

                                if (encLength <= column.GetFieldLength())
                                {
                                    //PO tab = MTable.GetPO(GetCtx(), tableName,
                                    //    Util.GetValueOfInt(ds.Tables[0].Rows[i][tableName + "_ID"]), Get_Trx());

                                    //tab.Set_Value(column.GetColumnName(), (SecureEngine.Encrypt(ds.Tables[0].Rows[i][column.GetColumnName()].ToString())));
                                    //if (!tab.Save(Get_Trx()))
                                    //{
                                    //    Rollback();
                                    //    return "Encryption=" + false;
                                    //}
                                    string p_NewPassword = SecureEngine.Encrypt(ds.Tables[0].Rows[i][column.GetColumnName()].ToString());
                                    String sql           = "UPDATE " + tableName + " SET Updated=SYSDATE, UpdatedBy=" + GetAD_User_ID();
                                    if (!string.IsNullOrEmpty(p_NewPassword))
                                    {
                                        sql += ", " + column.GetColumnName() + "=" + GlobalVariable.TO_STRING(p_NewPassword);
                                    }
                                    sql += " WHERE " + tableName + "_ID=" + Util.GetValueOfInt(ds.Tables[0].Rows[i][tableName + "_ID"]);
                                    int iRes = DB.ExecuteQuery(sql, null, Get_Trx());
                                    if (iRes <= 0)
                                    {
                                        Rollback();
                                        return("Encryption=" + false);
                                    }
                                }
                                else
                                {
                                    Rollback();
                                    return("After Encryption some values may exceed the value of column length. Please exceed column Length.");
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            if (ds.Tables[0].Rows[i][column.GetColumnName()] != null && ds.Tables[0].Rows[i][column.GetColumnName()] != DBNull.Value &&
                                SecureEngine.IsEncrypted(ds.Tables[0].Rows[i][column.GetColumnName()].ToString()))
                            {
                                // MUser user = new MUser(GetCtx(), Util.GetValueOfInt(ds.Tables[0].Rows[i][MTable.GetTableName(GetCtx(), column.GetAD_Table_ID())+"_ID"]), Get_Trx());

                                //PO tab = MTable.GetPO(GetCtx(), tableName,
                                //   Util.GetValueOfInt(ds.Tables[0].Rows[i][tableName + "_ID"]), Get_Trx());

                                //tab.Set_Value(column.GetColumnName(), (SecureEngine.Decrypt(ds.Tables[0].Rows[i][column.GetColumnName()].ToString())));
                                //if (!tab.Save(Get_Trx()))
                                //{
                                //    Rollback();
                                //    return "Encryption=" + false;
                                //}

                                string p_NewPassword = SecureEngine.Decrypt(ds.Tables[0].Rows[i][column.GetColumnName()].ToString());
                                String sql           = "UPDATE " + tableName + "  SET Updated=SYSDATE, UpdatedBy=" + GetAD_User_ID();
                                if (!string.IsNullOrEmpty(p_NewPassword))
                                {
                                    sql += ", " + column.GetColumnName() + "=" + GlobalVariable.TO_STRING(p_NewPassword);
                                }
                                sql += " WHERE " + tableName + "_ID  =" + Util.GetValueOfInt(ds.Tables[0].Rows[i][tableName + "_ID"]);
                                int iRes = DB.ExecuteQuery(sql, null, Get_Trx());
                                if (iRes <= 0)
                                {
                                    Rollback();
                                    return("Encryption=" + false);
                                }
                            }
                        }
                    }
                }
                //}
            }
            return("Encryption=" + column.IsEncrypted());
        }
        }       //	getTargetColumn

        /// <summary>
        /// Execute Auto Assignment
        /// </summary>
        /// <param name="po">PO to be modified</param>
        /// <returns>true if modified</returns>
        public bool ExecuteIt(PO po)
        {
            //	Check Column
            MColumn column     = GetTargetColumn();
            String  columnName = column.GetColumnName();
            int     index      = po.Get_ColumnIndex(columnName);

            if (index == -1)
            {
                throw new Exception(ToString() + ": AD_Column_ID not found");
            }
            //	Check Value
            Object value      = po.Get_Value(index);
            String assignRule = GetAssignRule();

            if (value == null && assignRule.Equals(ASSIGNRULE_OnlyIfNOTNULL))
            {
                return(false);
            }
            else if (value != null && assignRule.Equals(ASSIGNRULE_OnlyIfNULL))
            {
                return(false);
            }

            //	Check Criteria
            if (m_criteria == null)
            {
                GetCriteria(false);
            }
            bool modified = false;

            for (int i = 0; i < m_criteria.Length; i++)
            {
                MAssignCriteria criteria = m_criteria[i];
                if (criteria.IsMet(po))
                {
                    modified = true;
                    break;
                }
            }
            if (!modified)
            {
                return(false);
            }

            //	Assignment
            String methodName    = "set" + columnName;
            Type   parameterType = null;
            Object parameter     = null;
            int    displayType   = column.GetAD_Reference_ID();
            String valueString   = GetValueString();

            if (DisplayType.IsText(displayType) || displayType == DisplayType.List)
            {
                parameterType = typeof(string);
                parameter     = valueString;
            }
            else if (DisplayType.IsID(displayType) || displayType == DisplayType.Integer)
            {
                parameterType = typeof(int);
                if (GetRecord_ID() != 0)
                {
                    parameter = GetRecord_ID();
                }
                else if (valueString != null && valueString.Length > 0)
                {
                    try
                    {
                        parameter = int.Parse(valueString);
                    }
                    catch (Exception e)
                    {
                        log.Warning(ToString() + " " + e);
                        return(false);
                    }
                }
            }
            else if (DisplayType.IsNumeric(displayType))
            {
                parameterType = typeof(Decimal);
                if (valueString != null && valueString.Length > 0)
                {
                    try
                    {
                        parameter = Decimal.Parse(valueString);
                    }
                    catch (Exception e)
                    {
                        log.Warning(ToString() + " " + e);
                        return(false);
                    }
                }
            }
            else if (DisplayType.IsDate(displayType))
            {
                parameterType = typeof(DateTime);
                if (valueString != null && valueString.Length > 0)
                {
                    try
                    {
                        parameter = DateTime.Parse(valueString);
                    }
                    catch (Exception e)
                    {
                        log.Warning(ToString() + " " + e);
                        return(false);
                    }
                }
            }
            else if (displayType == DisplayType.YesNo)
            {
                parameterType = typeof(bool);
                parameter     = "Y".Equals(valueString);
            }
            else if (displayType == DisplayType.Button)
            {
                parameterType = typeof(string);
                parameter     = GetValueString();
            }
            else if (DisplayType.IsLOB(displayType))    //	CLOB is String
            {
                parameterType = typeof(byte[]);
                //	parameter = getValueString();
            }

            //	Assignment
            try
            {
                Type clazz = po.GetType();
                System.Reflection.MethodInfo method = clazz.GetMethod(methodName, new Type[] { parameterType });
                method.Invoke(po, new Object[] { parameter });
            }
            catch (Exception e)
            {
                log.Log(Level.WARNING, ToString(), e);
                //	fallback
                if (parameter is Boolean)
                {
                    po.Set_Value(index, valueString);
                }
                else
                {
                    po.Set_Value(index, parameter);
                }
                //	modified = false;
            }
            return(modified);
        }       //	executeIt