示例#1
0
        /// <summary>
        /// Saves the changes.
        /// </summary>
        /// <param name="context">The context.</param>
        public void SaveChanges(IDictionary context)
        {
            CatalogEntryDto dto = (CatalogEntryDto)context["CatalogEntryDto"];

            /*if (CatalogEntryId > 0 && dto == null)
             *      dto = CatalogContext.Current.GetCatalogEntryDto(CatalogEntryId);
             * else if (CatalogEntryId == 0 && dto == null)
             *      dto = new CatalogEntryDto();*/

            CatalogRelationDto relations = (CatalogRelationDto)context["CatalogRelationDto"];

            /*if (CatalogEntryId > 0 && relations == null)
             *      relations = CatalogContext.Current.GetCatalogRelationDto(ParentCatalogId, ParentCatalogNodeId, CatalogEntryId, String.Empty, new CatalogRelationResponseGroup(CatalogRelationResponseGroup.ResponseGroup.CatalogEntry));
             * else if (CatalogEntryId == 0 && relations == null)
             *      relations = new CatalogRelationDto();*/

            CatalogRelationDto.CatalogEntryRelationDataTable tbl = relations.CatalogEntryRelation;

            foreach (GridItem item in _addedItems)
            {
                int     id  = Int32.Parse(item["ID"].ToString());
                decimal qty = 0;
                Decimal.TryParse(item["Quantity"].ToString(), out qty);
                string groupName = item["GroupName"].ToString();
                int    sortOrder = 0;
                Int32.TryParse(item["SortOrder"].ToString(), out sortOrder);

                CatalogRelationDto.CatalogEntryRelationRow row = null;
                if (tbl.Count > 0)
                {
                    // find the existing one
                    foreach (CatalogRelationDto.CatalogEntryRelationRow nrow in tbl.Rows)
                    {
                        if (nrow.ChildEntryId == id)
                        {
                            row = nrow;
                            break;
                        }
                    }
                }

                if (row == null)
                {
                    row = tbl.NewCatalogEntryRelationRow();
                }

                row.ParentEntryId = dto.CatalogEntry[0].CatalogEntryId;

                //dto.CatalogEntry[0].ClassTypeId
                row.RelationTypeId = RelationTypeId;
                row.ChildEntryId   = id;
                row.Quantity       = qty;
                row.GroupName      = groupName;
                row.SortOrder      = sortOrder;

                if (row.RowState == DataRowState.Detached)
                {
                    tbl.Rows.Add(row);
                }
            }

            // delete removed items from the table
            foreach (GridItem item in _removedItems)
            {
                if (tbl.Count > 0)
                {
                    int id = Int32.Parse(item["ID"].ToString());
                    // find the existing one
                    foreach (CatalogRelationDto.CatalogEntryRelationRow row in tbl.Rows)
                    {
                        // if row has been deleted during previous cycle step(s), skip it
                        if (row.RowState == DataRowState.Deleted)
                        {
                            continue;
                        }

                        // delete the row
                        if (row.ChildEntryId == id)
                        {
                            row.Delete();
                            break;
                        }
                    }
                }
            }
        }
        protected override int CreateSystemRow(FillDataMode Mode, int RowIndex, params object[] Item)
        {
            int    i = 0;
            object objSysRowAction = Item[i++];
            object objParentCode   = Item[i++];
            object objChildCode    = Item[i++];
            object objQuantity     = Item[i++];
            object objGroupName    = Item[i++];
            object objSortOrder    = Item[i++];

            CatalogRelationDto.CatalogEntryRelationRow catalogEntryRelationRow = null;
            CatalogRelationDto catalogRelationDto = new CatalogRelationDto();

            try
            {
                RowAction sysRowAction = RowAction.Default;

                if (objSysRowAction != null)
                {
                    sysRowAction = GetRowActionEnum((string)objSysRowAction);
                }

                string parentCode;
                if (!String.IsNullOrEmpty((string)objParentCode))
                {
                    parentCode = (string)objParentCode;
                }
                else
                {
                    throw new AbsentValue("Parent Entry Code");
                }

                string childCode;
                if (!String.IsNullOrEmpty((string)objChildCode))
                {
                    childCode = (string)objChildCode;
                }
                else
                {
                    throw new AbsentValue("Child Entry Code");
                }

                bool            bIsNew          = false;
                CatalogEntryDto catalogEntryDto = CatalogEntryManager.GetCatalogEntryDto(parentCode, new CatalogEntryResponseGroup(CatalogEntryResponseGroup.ResponseGroup.CatalogEntryInfo));
                if (catalogEntryDto.CatalogEntry.Count > 0)
                {
                    CatalogEntryDto.CatalogEntryRow entry = catalogEntryDto.CatalogEntry[0];
                    if (entry.ClassTypeId.Equals(EntryType.Product, StringComparison.OrdinalIgnoreCase) ||
                        entry.ClassTypeId.Equals(EntryType.Bundle, StringComparison.OrdinalIgnoreCase) ||
                        entry.ClassTypeId.Equals(EntryType.Package, StringComparison.OrdinalIgnoreCase) ||
                        entry.ClassTypeId.Equals(EntryType.DynamicPackage, StringComparison.OrdinalIgnoreCase))
                    {
                        CatalogEntryDto childEntryDto = CatalogEntryManager.GetCatalogEntryDto(childCode, new CatalogEntryResponseGroup(CatalogEntryResponseGroup.ResponseGroup.CatalogEntryInfo));
                        if (childEntryDto.CatalogEntry.Count > 0)
                        {
                            CatalogEntryDto.CatalogEntryRow childEntry = childEntryDto.CatalogEntry[0];

                            catalogRelationDto = CatalogRelationManager.GetCatalogRelationDto(this._CatalogId, 0, entry.CatalogEntryId, String.Empty, new CatalogRelationResponseGroup(CatalogRelationResponseGroup.ResponseGroup.CatalogEntry));
                            if (catalogRelationDto.CatalogEntryRelation.Count > 0)
                            {
                                DataRow[] drs = catalogRelationDto.CatalogEntryRelation.Select(String.Format("ParentEntryId = {0} AND ChildEntryId = {1}", entry.CatalogEntryId, childEntry.CatalogEntryId));
                                if (drs.Length > 0)
                                {
                                    catalogEntryRelationRow = (CatalogRelationDto.CatalogEntryRelationRow)drs[0];

                                    if (sysRowAction == RowAction.Insert)
                                    {
                                        throw new MDPImportException(String.Format("The Relation with Parent Entry code '{0}' and Child Entry code '{1}' already exists.", parentCode, childCode));
                                    }

                                    if (sysRowAction == RowAction.Delete)
                                    {
                                        catalogEntryRelationRow.Delete();
                                        CatalogRelationManager.SaveCatalogRelation(catalogRelationDto);
                                        return(0);
                                    }
                                }
                            }

                            if (catalogEntryRelationRow == null)
                            {
                                if (sysRowAction == RowAction.Update)
                                {
                                    throw new MDPImportException(String.Format("The Relation with Parent Entry code '{0}' and Child Entry code '{1}' does not exists.", parentCode, childCode));
                                }

                                if (sysRowAction == RowAction.Delete)
                                {
                                    throw new MDPImportException(String.Format("The Relation with Parent Entry code '{0}' and Child Entry code '{1}' does not exists.", parentCode, childCode));
                                }

                                catalogEntryRelationRow = catalogRelationDto.CatalogEntryRelation.NewCatalogEntryRelationRow();
                                catalogEntryRelationRow.ParentEntryId = entry.CatalogEntryId;
                                catalogEntryRelationRow.ChildEntryId  = childEntry.CatalogEntryId;
                                catalogEntryRelationRow.Quantity      = 1;
                                catalogEntryRelationRow.GroupName     = String.Empty;
                                catalogEntryRelationRow.SortOrder     = 0;

                                switch (entry.ClassTypeId)
                                {
                                case EntryType.Product:
                                    catalogEntryRelationRow.RelationTypeId = EntryRelationType.ProductVariation;
                                    break;

                                case EntryType.Package:
                                    catalogEntryRelationRow.RelationTypeId = EntryRelationType.PackageEntry;
                                    break;

                                case EntryType.Bundle:
                                case EntryType.DynamicPackage:
                                    catalogEntryRelationRow.RelationTypeId = EntryRelationType.BundleEntry;
                                    break;
                                }

                                bIsNew = true;
                            }
                        }
                        else
                        {
                            throw new MDPImportException(String.Format("The Child Entry with code '{0}' does not exists.", childCode));
                        }
                    }
                    else
                    {
                        throw new MDPImportException(String.Format("The Parent Entry with code '{0}' has wrong type ('{1}').", parentCode, entry.ClassTypeId));
                    }
                }
                else
                {
                    throw new MDPImportException(String.Format("The Parent Entry with code '{0}' does not exists.", parentCode));
                }

                //SalePrice
                if (objQuantity != null)
                {
                    catalogEntryRelationRow.Quantity = (decimal)objQuantity;
                }

                if (objGroupName != null)
                {
                    catalogEntryRelationRow.GroupName = (string)objGroupName;
                }

                if (objSortOrder != null)
                {
                    catalogEntryRelationRow.SortOrder = (int)objSortOrder;
                }

                if (bIsNew)
                {
                    catalogRelationDto.CatalogEntryRelation.AddCatalogEntryRelationRow(catalogEntryRelationRow);
                }

                using (TransactionScope tx = new TransactionScope())
                {
                    // Save modifications
                    if (catalogRelationDto.HasChanges())
                    {
                        CatalogContext.Current.SaveCatalogRelationDto(catalogRelationDto);
                    }

                    tx.Complete();
                }
            }
            catch (Exception ex)
            {
                throw new MDPImportException(ex.Message, null, RowIndex, null, null, Item);
            }

            return(catalogEntryRelationRow.ParentEntryId);
        }