Example #1
0
        public bool Replace(CUSTOMRP.Model.REPORT model)
        {
            bool result = false;
            bool isNew  = false;

            CUSTOMRP.Model.REPORT original = null;
            if (model.ID == 0)
            {
                model.ID = Add(model);
                result   = model.ID != 0;
                isNew    = true;
            }
            else
            {
                original = GetModel(model.ADDUSER, model.ID);

                result = Update(model);
            }
            if (!result)
            {
                return(false);
            }                               // error occurred

            // prepare AuditLog object
            Model.AUDITLOG auditobj = model.GetAuditLogObject(original);
            auditobj.UserID     = model.ADDUSER;
            auditobj.CreateDate = DateTime.Now;

            #region Report Columns handling

            try
            {
                CUSTOMRP.DAL.REPORTCOLUMN dalRC = new REPORTCOLUMN();

                //v1.0.0 - Cheong - 2015/06/30 - delete removed columns
                if (!isNew)
                {
                    Model.REPORTCOLUMN[] dbRC = (from oldrc in dalRC.GetModelListForReport(model.ADDUSER, model.ID)
                                                 join newrc in model.ReportColumns on new { oldrc.COLUMNNAME, oldrc.COLUMNFUNC, oldrc.COLUMNTYPE } equals new { newrc.COLUMNNAME, newrc.COLUMNFUNC, newrc.COLUMNTYPE } into g
                                                 from gjoin in g.DefaultIfEmpty()
                                                 where gjoin == null
                                                 select oldrc).ToArray();

                    foreach (Model.REPORTCOLUMN rc in dbRC)
                    {
                        dalRC.Delete(model.ADDUSER, rc.ID);
                    }
                }

                //v1.0.0 - Cheong - 2016/03/23 - Preserve order on criteria and group columns
                int contentSEQ  = 1;
                int criteriaSEQ = 1;
                int sortonSEQ   = 1;
                int groupSEQ    = 1;
                foreach (CUSTOMRP.Model.REPORTCOLUMN rc in model.ReportColumns)
                {
                    rc.RPID = model.ID;
                    switch (rc.COLUMNFUNC)
                    {
                    case 1:
                    {
                        rc.SEQ = contentSEQ;
                        contentSEQ++;
                    }
                    break;

                    case 2:
                    {
                        rc.SEQ = criteriaSEQ;
                        criteriaSEQ++;
                    }
                    break;

                    case 3:
                    {
                        rc.SEQ = sortonSEQ;
                        sortonSEQ++;
                    }
                    break;

                    case 6:
                    {
                        rc.SEQ = groupSEQ;
                        groupSEQ++;
                    }
                    break;

                    default:
                    {
                        rc.SEQ = -1;
                    }
                    break;
                    }
                    //if (rc.COLUMNFUNC == 1)
                    //{
                    //    rc.SEQ = contentSEQ;
                    //    contentSEQ++;
                    //}
                    //else if (rc.COLUMNFUNC == 3)
                    //{
                    //    rc.SEQ = sortonSEQ;
                    //    sortonSEQ++;
                    //}
                    //else
                    //{
                    //    rc.SEQ = -1;
                    //}
                    result = dalRC.Replace(model.ADDUSER, rc);
                    if (!result)
                    {
                        return(false);
                    }
                }
            }
            catch
            {
                result = false;
            }

            #endregion Report Columns handling

            #region WordFile handling
            try
            {
                if (model.WordFile != null)
                {
                    DAL.WORDFILE   dalWF = new WORDFILE();
                    Model.WORDFILE wf    = dalWF.GetModelByReportID(model.ADDUSER, model.ID);
                    if (wf == null)
                    {
                        if (model.WordFile.RPID == 0)
                        {
                            model.WordFile.RPID = model.ID;
                        }
                        dalWF.AddFile(model.WordFile);
                    }
                    else
                    {
                        wf.Description  = model.WordFile.Description;
                        wf.WordFileName = model.WordFile.WordFileName;
                        wf.OrigFileName = model.WordFile.OrigFileName;
                        wf.ModifyUser   = model.WordFile.ModifyUser;
                        wf.ModifyDate   = model.WordFile.ModifyDate;
                        dalWF.UpdateFile(wf);
                    }
                }
            }
            catch
            {
                result = false;
            }

            #endregion WordFile handling

            auditobj.MessageType = Model.AUDITLOG.Severity.Audit;
            auditobj.ModuleName  = "DAL.REPORT.REPLACE";
            auditobj.Message     = String.Format(isNew ? AppNum.AuditMessage.ReportInsertSuccess : AppNum.AuditMessage.ReportUpdateSuccess, model.ID);

            AUDITLOG.Add(auditobj);

            return(result);
        }