Ejemplo n.º 1
0
        private ImportResult DoImport(Node parent, Stream inputStream, bool update, bool import)
        {
            var successful   = new List <ImportData>();
            var unsuccessful = new List <ImportData>();

            var lines      = new List <Dictionary <string, string> >();
            var headers    = new List <string>();
            var parentNode = parent;
            var splitChar  = ';';

            using (var reader = OpenStream(inputStream))
            {
                // extracting header from stream
                if (!reader.EndOfStream)
                {
                    var header = reader.ReadLine();
                    splitChar = DetectSplitChar(header);
                    headers   = SplitLine(header, splitChar);
                }

                // rewrite strem reading
                string lineString;
                var    lineIndex = 1;
                while ((lineString = reader.ReadLine()) != null)
                {
                    var lineData = SplitLine(lineString, splitChar);
                    var lineDict = new Dictionary <string, string>();

                    if (lineData.Count != headers.Count)
                    {
                        throw new InvalidOperationException(string.Format(SenseNetResourceManager.Current.GetString("ListImporterPortlet", "ErrorInvalidColumns"), lineIndex));
                    }

                    for (var i = 0; i < lineData.Count; i++)
                    {
                        lineDict.Add(headers[i].Trim('"'), lineData[i].Replace("\"\"", "\"").Trim('"'));
                    }

                    lines.Add(lineDict);
                    lineIndex++;
                }
            }

            // creating content
            foreach (var line in lines)
            {
                if (!line.ContainsKey("Type"))
                {
                    throw new Exception(String.Format(SenseNetResourceManager.Current.GetString("ListImporterPortlet", "MissingRequiredColumnFromCSV"), "Type"));
                }

                var name        = line.ContainsKey("Name") ? line["Name"] : string.Empty;
                var contentType = line["Type"];

                // excluding restricted or null valued fields (this excludes fields not related to the current type of content as well)
                var excludedFields = (from item in line
                                      where FieldsNotToImport.Contains(item.Key) || string.IsNullOrEmpty(item.Value)
                                      select item.Key).ToList();

                // removing excluded fields from the current line
                foreach (var field in excludedFields)
                {
                    line.Remove(field);
                }

                // creating, parsing and saving imported content
                var existingChildren = from c in parentNode.PhysicalChildArray select c.Name;
                var existingName     = existingChildren.Contains(name);

                if (!existingName && import)
                {
                    // if new content get the content type
                    if (string.IsNullOrEmpty(contentType))
                    {
                        throw new Exception(String.Format(SenseNetResourceManager.Current.GetString("ListImporterPortlet", "MissingRequiredColumnFromCSV"), "ContentType (type)"));
                    }

                    // create new content)
                    try
                    {
                        // content type non existent fields add to list custom fields
                        var nonExistentListFields = CreateNonExistentListFields(contentType, parentNode, line);

                        // if non existent fields doesn't has # mark in name add # mark to name
                        foreach (KeyValuePair <string, string> nonExistentListField in nonExistentListFields)
                        {
                            if (!nonExistentListField.Key.Contains('#'))
                            {
                                line.Remove(nonExistentListField.Key);
                                line.Add(string.Format("#{0}", nonExistentListField.Key), nonExistentListField.Value);
                            }
                        }

                        var newContent = Content.CreateNewAndParse(contentType, parentNode, name, line);

                        // because of the empty name we need to ensure
                        // that we do not try to use existing name
                        newContent.ContentHandler.AllowIncrementalNaming = true;

                        newContent.Save();
                        successful.Add(new ImportData(newContent.Name, ""));
                    }
                    catch (Exception e)
                    {
                        unsuccessful.Add(new ImportData(name, e.Message));
                    }
                }
                else if (existingName && update)
                {
                    // updating existing content
                    try
                    {
                        var oldNode =
                            (from c in parentNode.PhysicalChildArray where c.Name == name select c).FirstOrDefault();

                        if (oldNode == null)
                        {
                            throw new Exception(String.Format(SenseNetResourceManager.Current.GetString("ListImporterPortlet", "UpdatableNodeIsMissing"), name));
                        }

                        var nonExistentListFields = CreateNonExistentListFields(oldNode.NodeType.Name, parentNode, line);

                        // if non existent fields doesn't has # mark in name add # mark to name
                        foreach (KeyValuePair <string, string> nonExistentListField in nonExistentListFields)
                        {
                            if (!nonExistentListField.Key.Contains('#'))
                            {
                                line.Remove(nonExistentListField.Key);
                                line.Add(string.Format("#{0}", nonExistentListField.Key), nonExistentListField.Value);
                            }
                        }

                        if (Update(oldNode, line))
                        {
                            successful.Add(new ImportData(name, SenseNetResourceManager.Current.GetString("ListImporterPortlet", "UpdateSuccessfulMessage")));
                        }
                        else
                        {
                            unsuccessful.Add(new ImportData(name, SenseNetResourceManager.Current.GetString("ListImporterPortlet", "UpdateFailedMessage")));
                        }
                    }
                    catch (Exception e)
                    {
                        unsuccessful.Add(new ImportData(name, e.Message));
                    }
                }
                else
                {
                    // doing nothing
                    unsuccessful.Add(new ImportData(name, SenseNetResourceManager.Current.GetString("ListImporterPortlet", "NotImportedMessage")));
                }
            }

            return(new ImportResult(unsuccessful.Count == 0, successful, unsuccessful));
        }