예제 #1
0
        protected override int CreateSystemRow(FillDataMode mode, int RowIndex, params object[] item)
        {
            int    i = 0;
            object objSysRowAction = item[i++];
            //Entry
            object objCode          = item[i++];
            object objName          = item[i++];
            object objClassTypeId   = item[i++];
            object objStartDate     = item[i++];
            object objEndDate       = item[i++];
            object objTemplateName  = item[i++];
            object objIsActive      = item[i++];
            object objCategoryCodes = item[i++];
            object objSortOrder     = item[i++];
            //SEO
            object objSeoTitle       = item[i++];
            object objSeoUrl         = item[i++];
            object objSeoDescription = item[i++];
            object objSeoKeywords    = item[i++];

            CatalogEntryDto.CatalogEntryRow entryRow = null;

            try
            {
                RowAction sysRowAction = RowAction.Default;

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

                string code;
                if (objCode != null)
                {
                    code = (string)objCode;
                }
                else
                {
                    throw new AbsentValue("Code");
                }

                bool            bIsNew          = false;
                CatalogEntryDto catalogEntryDto = CatalogEntryManager.GetCatalogEntryDto(code, new CatalogEntryResponseGroup(CatalogEntryResponseGroup.ResponseGroup.CatalogEntryFull));
                if (catalogEntryDto.CatalogEntry.Count > 0)
                {
                    if (sysRowAction == RowAction.Insert)
                    {
                        throw new MDPImportException(String.Format("The Entry with code '{0}' already exists.", code));
                    }

                    entryRow = catalogEntryDto.CatalogEntry[0];

                    if (sysRowAction == RowAction.Delete)
                    {
                        CatalogContext.Current.DeleteCatalogEntry(entryRow.CatalogEntryId, true);
                        return(0);
                    }
                }
                else
                {
                    if (sysRowAction == RowAction.Update)
                    {
                        throw new MDPImportException(String.Format("The Entry with code '{0}' does not exists.", code));
                    }

                    if (sysRowAction == RowAction.Delete)
                    {
                        throw new MDPImportException(String.Format("The Entry with code '{0}' does not exists.", code));
                    }

                    entryRow = catalogEntryDto.CatalogEntry.NewCatalogEntryRow();
                    entryRow.ApplicationId = CatalogConfiguration.Instance.ApplicationId;
                    entryRow.CatalogId     = _CatalogId;
                    entryRow.Code          = code;
                    bIsNew = true;
                }

                //Entry
                if (objName != null)
                {
                    string Name = (string)objName;
                    entryRow.Name = Name;
                }
                else if (bIsNew)
                {
                    throw new AbsentValue("Name");
                }

                if (objClassTypeId != null)
                {
                    string classTypeId = (string)objClassTypeId;
                    entryRow.ClassTypeId = classTypeId;
                }
                else if (bIsNew)
                {
                    entryRow.ClassTypeId = EntryType.Product;
                }

                if (objStartDate != null)
                {
                    DateTime startDate = (DateTime)objStartDate;
                    entryRow.StartDate = startDate.ToUniversalTime();
                }
                else if (bIsNew)
                {
                    entryRow.StartDate = DateTime.UtcNow;
                }

                if (objEndDate != null)
                {
                    DateTime endDate = (DateTime)objEndDate;
                    entryRow.EndDate = endDate.ToUniversalTime();
                }
                else if (bIsNew)
                {
                    entryRow.EndDate = DateTime.UtcNow.AddYears(1);
                }

                if (objTemplateName != null)
                {
                    string templateName = (string)objTemplateName;
                    entryRow.TemplateName = templateName;
                }
                else if (bIsNew)
                {
                    entryRow.TemplateName = String.Empty;
                }

                if (objIsActive != null)
                {
                    bool IsActive = (bool)objIsActive;
                    entryRow.IsActive = IsActive;
                }
                else if (bIsNew)
                {
                    entryRow.IsActive = false;
                }

                int oldMetaClassId = 0;
                if (!_isSystemClass && _metaClassId > 0)
                {
                    if (!bIsNew)
                    {
                        oldMetaClassId = entryRow.MetaClassId;
                    }

                    entryRow.MetaClassId = _metaClassId;
                }
                else if (bIsNew)
                {
                    throw new MDPImportException("The new entry cannot be created without metaclass definition.");
                }

                if (bIsNew)
                {
                    catalogEntryDto.CatalogEntry.AddCatalogEntryRow(entryRow);
                }
                else
                {
                    entryRow.SerializedData = null;
                }

                //SEO
                CatalogEntryDto.CatalogItemSeoRow catalogItemSeoRow = null;
                bool bSeoIsNew = false;
                if (!String.IsNullOrEmpty(this.Context.Language))
                {
                    if (catalogEntryDto.CatalogItemSeo.Count > 0)
                    {
                        DataRow[] drs = catalogEntryDto.CatalogItemSeo.Select(String.Format("LanguageCode LIKE '{0}' AND CatalogEntryId = {1}", this.Context.Language, entryRow.CatalogEntryId));
                        if (drs.Length > 0)
                        {
                            catalogItemSeoRow = (CatalogEntryDto.CatalogItemSeoRow)drs[0];
                        }
                    }

                    if (catalogItemSeoRow == null)
                    {
                        catalogItemSeoRow = catalogEntryDto.CatalogItemSeo.NewCatalogItemSeoRow();
                        catalogItemSeoRow.ApplicationId  = CatalogConfiguration.Instance.ApplicationId;
                        catalogItemSeoRow.LanguageCode   = this.Context.Language.ToLower();
                        catalogItemSeoRow.CatalogEntryId = entryRow.CatalogEntryId;
                        bSeoIsNew = true;
                    }

                    if (objSeoTitle != null)
                    {
                        catalogItemSeoRow.Title = (string)objSeoTitle;
                    }

                    if (objSeoUrl != null)
                    {
                        catalogItemSeoRow.Uri = (string)objSeoUrl;
                    }
                    else if (bSeoIsNew)
                    {
                        // Auto generate the URL if empty
                        string name = catalogEntryDto.CatalogEntry.Count > 0 ? catalogEntryDto.CatalogEntry[0].Name : "";
                        string url  = String.Format("{0}.aspx", CommerceHelper.CleanUrlField(name));

                        int index = 1;
                        while (CatalogContext.Current.GetCatalogEntryByUriDto(url, this.Context.Language).CatalogEntry.Count != 0 || CatalogContext.Current.GetCatalogNodeDto(url, this.Context.Language).CatalogNode.Count != 0)
                        {
                            url = String.Format("{0}-{1}.aspx", CommerceHelper.CleanUrlField(name), index.ToString());
                            index++;
                        }

                        catalogItemSeoRow.Uri = url;
                    }

                    if (objSeoDescription != null)
                    {
                        catalogItemSeoRow.Description = (string)objSeoDescription;
                    }

                    if (objSeoKeywords != null)
                    {
                        catalogItemSeoRow.Keywords = (string)objSeoKeywords;
                    }

                    if (bSeoIsNew)
                    {
                        catalogEntryDto.CatalogItemSeo.AddCatalogItemSeoRow(catalogItemSeoRow);
                    }
                }

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

                    int sortOrder = -1;
                    if (objSortOrder != null)
                    {
                        sortOrder = (int)objSortOrder;
                    }

                    if (objCategoryCodes != null)
                    {
                        //NodeEntryRelation

                        string[] categoryCodes = ((string)objCategoryCodes).Split(',');

                        Catalog.Dto.CatalogRelationDto catalogRelationDto = FrameworkContext.Current.CatalogSystem.GetCatalogRelationDto(this._CatalogId, 0, entryRow.CatalogEntryId, String.Empty, new CatalogRelationResponseGroup(CatalogRelationResponseGroup.ResponseGroup.NodeEntry));

                        Catalog.Dto.CatalogNodeDto catalogNodeDto = FrameworkContext.Current.CatalogSystem.GetCatalogNodesDto(this._CatalogId);
                        if (catalogNodeDto.CatalogNode.Count > 0)
                        {
                            //remove product from category
                            if (catalogRelationDto.NodeEntryRelation.Count > 0)
                            {
                                foreach (CatalogRelationDto.NodeEntryRelationRow nodeEntryRelationRow in catalogRelationDto.NodeEntryRelation)
                                {
                                    DataRow[] catalogNodeDataRows = catalogNodeDto.CatalogNode.Select(String.Format("CatalogNodeId = {0}", nodeEntryRelationRow.CatalogNodeId));
                                    if (catalogNodeDataRows.Length > 0)
                                    {
                                        Catalog.Dto.CatalogNodeDto.CatalogNodeRow catalogNode = (Catalog.Dto.CatalogNodeDto.CatalogNodeRow)catalogNodeDataRows[0];

                                        bool bExist = false;
                                        foreach (string categoryCode in categoryCodes)
                                        {
                                            if (catalogNode.Code.Equals(categoryCode))
                                            {
                                                if (sortOrder >= 0)
                                                {
                                                    nodeEntryRelationRow.SortOrder = sortOrder;
                                                }

                                                bExist = true;
                                                break;
                                            }
                                        }
                                        if (!bExist)
                                        {
                                            nodeEntryRelationRow.Delete();
                                        }
                                    }
                                }
                            }

                            //add entry to category
                            foreach (string categoryCode in categoryCodes)
                            {
                                DataRow[] catalogNodeDataRows = catalogNodeDto.CatalogNode.Select(String.Format("Code = '{0}'", categoryCode.Replace("'", "''")));
                                if (catalogNodeDataRows.Length > 0)
                                {
                                    Catalog.Dto.CatalogNodeDto.CatalogNodeRow catalogNode = (Catalog.Dto.CatalogNodeDto.CatalogNodeRow)catalogNodeDataRows[0];

                                    DataRow[] nodeEntryRelationDataRows = catalogRelationDto.NodeEntryRelation.Select(String.Format("CatalogNodeId = {0}", catalogNode.CatalogNodeId));
                                    if (nodeEntryRelationDataRows.Length == 0)
                                    {
                                        Catalog.Dto.CatalogRelationDto.NodeEntryRelationRow row = catalogRelationDto.NodeEntryRelation.NewNodeEntryRelationRow();
                                        row.CatalogId      = this._CatalogId;
                                        row.CatalogEntryId = entryRow.CatalogEntryId;
                                        row.CatalogNodeId  = catalogNode.CatalogNodeId;

                                        if (sortOrder >= 0)
                                        {
                                            row.SortOrder = sortOrder;
                                        }
                                        else
                                        {
                                            row.SortOrder = 0;
                                        }

                                        catalogRelationDto.NodeEntryRelation.AddNodeEntryRelationRow(row);
                                    }
                                }
                            }
                        }

                        if (catalogRelationDto.HasChanges())
                        {
                            CatalogContext.Current.SaveCatalogRelationDto(catalogRelationDto);
                        }
                    }

                    if (!bIsNew && !_isSystemClass && oldMetaClassId != entryRow.MetaClassId)
                    {
                        MetaObject.Delete(this.Context, entryRow.CatalogEntryId, oldMetaClassId);
                        MetaObject obj = MetaObject.NewObject(this.Context, entryRow.CatalogEntryId, entryRow.MetaClassId);
                        obj.AcceptChanges(this.Context);
                    }

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

            return(entryRow.CatalogEntryId);
        }
예제 #2
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 = new CatalogEntryDto();
             * */

            foreach (UserControl ctrl in SeoCtrl.Controls)
            {
                IDictionary newContext = new ListDictionary();
                CatalogEntryDto.CatalogItemSeoRow seoRow = null;

                if (dto.CatalogItemSeo.Count > 0)
                {
                    // find appropriate row
                    DataRow[] rows = dto.CatalogItemSeo.Select(String.Format("LanguageCode = '{0}'", ((SeoTab)ctrl).LanguageCode));

                    if (rows.Length > 0)
                    {
                        seoRow = (CatalogEntryDto.CatalogItemSeoRow)rows[0];
                    }
                    else
                    {
                        seoRow = dto.CatalogItemSeo.NewCatalogItemSeoRow();
                        seoRow.ApplicationId  = CatalogConfiguration.Instance.ApplicationId;
                        seoRow.CatalogEntryId = CatalogEntryId == 0 ? -1 : CatalogEntryId;
                        seoRow.Uri            = String.Empty;
                    }
                }
                else
                {
                    seoRow = dto.CatalogItemSeo.NewCatalogItemSeoRow();
                    seoRow.ApplicationId  = CatalogConfiguration.Instance.ApplicationId;
                    seoRow.CatalogEntryId = CatalogEntryId == 0 ? -1 : CatalogEntryId;
                    seoRow.Uri            = String.Empty;
                }

                // Auto generate the URL if empty
                if (String.IsNullOrEmpty(seoRow.Uri))
                {
                    string name     = dto.CatalogEntry.Count > 0 ? dto.CatalogEntry[0].Name : "";
                    string langCode = ((SeoTab)ctrl).LanguageCode;
                    string url      = String.Format("{0}.aspx", CommerceHelper.CleanUrlField(name));

                    int index = 1;
                    while (CatalogContext.Current.GetCatalogEntryByUriDto(url, langCode).CatalogEntry.Count != 0 || CatalogContext.Current.GetCatalogNodeDto(url, langCode).CatalogNode.Count != 0)
                    {
                        url = String.Format("{0}-{1}.aspx", CommerceHelper.CleanUrlField(name), index.ToString());
                        index++;
                    }

                    // Check
                    seoRow.Uri = url;
                }

                newContext.Add("CatalogItemSeoRow", seoRow);

                ((IAdminTabControl)ctrl).SaveChanges(newContext);

                if (seoRow.RowState == DataRowState.Detached)
                {
                    dto.CatalogItemSeo.Rows.Add(seoRow);
                }
            }
        }