public static ItemBuilder GetById(long id, string itemName, string instanceName)
 {
     return(Read.ById(id, ItemDefinition.Load(itemName, instanceName), instanceName));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Create a list of ItemBuilder objects and export them in a Excel file in order to be reimported
        /// </summary>
        /// <param name="itemName">Item name</param>
        /// <returns>Result of action</returns>
        public ActionResult ExportToImport(string itemName, string instanceName, string connectionString)
        {
            string source = string.Format(CultureInfo.InvariantCulture, @"Excel:List({0})", itemName);

            this.itemBuilder = new ItemBuilder(itemName, instanceName);
            var res = ActionResult.NoAction;

            try
            {
                this.workBook = new XSSFWorkbook();
                this.sheet    = (XSSFSheet)this.workBook.CreateSheet(this.itemBuilder.Definition.Layout.LabelPlural);

                // HEADER
                int countCells = 0;
                this.CreateHeader();

                ReadOnlyCollection <ItemBuilder> list = Read.Active(this.itemBuilder.Definition, instanceName);
                int countRows = 1;
                foreach (ItemBuilder item in list)
                {
                    if (item == null)
                    {
                        continue;
                    }

                    ToolsXlsx.CreateRow(this.sheet, countRows);
                    countCells = 0;
                    foreach (ItemField field in this.itemBuilder.HumanFields)
                    {
                        this.sheet.GetRow(countRows).CreateCell(countCells);

                        // Detectar si es FK
                        if (item.ContainsKey(field.Name) && item[field.Name] != null)
                        {
                            if (this.itemBuilder.Definition.ForeignValues.Any(fv => fv.LocalName.Equals(field.Name, StringComparison.OrdinalIgnoreCase) || fv.LocalName.Equals(field.Name + "Id", StringComparison.OrdinalIgnoreCase)))
                            {
                                ForeignList foreignValue = this.itemBuilder.Definition.ForeignValues.Where(fv => fv.LocalName.Equals(field.Name, StringComparison.OrdinalIgnoreCase) || fv.LocalName.Equals(field.Name + "Id", StringComparison.OrdinalIgnoreCase)).First();

                                long foreignId = 0;
                                if (item[field.Name].GetType().Name.ToUpperInvariant() == "JOBJECT")
                                {
                                    foreignId = (long)item[field.Name + "Id"];
                                }
                                else
                                {
                                    foreignId = (long)item[field.Name];
                                }

                                string      foreignItem      = foreignValue.ItemName;
                                ItemBuilder foreignItemFound = Read.ById(foreignId, this.itemBuilder.Definition, instanceName);

                                if (foreignItemFound != null)
                                {
                                    this.SetCellValue(countRows, countCells, foreignItemFound[foreignValue.ImportReference], FieldDataType.Text);
                                }
                                else
                                {
                                    this.SetCellValue(countRows, countCells, null, FieldDataType.Text);
                                }
                            }
                            else
                            {
                                if (item[field.Name].GetType().Name.ToUpperInvariant() == "JOBJECT")
                                {
                                    string  data = item[field.Name].ToString();
                                    JObject x    = item[field.Name] as JObject;
                                    if (x["Description"] != null)
                                    {
                                        data = x["Description"].ToString();
                                    }

                                    this.sheet.GetRow(countRows).GetCell(countCells).SetCellValue(data);
                                }
                                else
                                {
                                    string fieldName = string.Empty;
                                    if (item.Definition.Fields.Any(f => f.Name == field.Name))
                                    {
                                        fieldName = field.Name;
                                    }
                                    else
                                    {
                                        if (item.Definition.Fields.Any(f => f.Name == field.Name + "Id"))
                                        {
                                            fieldName = string.Format(CultureInfo.InvariantCulture, "{0}Id", field.Name);
                                        }

                                        fieldName = field.Name;
                                    }

                                    if (!string.IsNullOrEmpty(fieldName))
                                    {
                                        ItemField fieldDefinition = item.Definition.Fields.Where(f => f.Name == fieldName).First();
                                        this.SetCellValue(countRows, countCells, item[fieldName], fieldDefinition.DataType);
                                    }
                                }
                            }
                        }

                        countCells++;
                    }

                    countRows++;
                }

                string path = HttpContext.Current.Request.PhysicalApplicationPath;
                if (!path.EndsWith("\\", StringComparison.OrdinalIgnoreCase))
                {
                    path = string.Format(CultureInfo.GetCultureInfo("en-us"), @"{0}", path);
                }

                path = string.Format(CultureInfo.GetCultureInfo("en-us"), @"{0}\Temp\ToImport_{1}.{2}", path, this.itemBuilder.Definition.Layout.LabelPlural, ConstantValue.Excel2007Extension);
                string link = string.Format(CultureInfo.GetCultureInfo("en-us"), @"/Temp/ToImport_{0}.{1}", this.itemBuilder.Definition.Layout.LabelPlural, ConstantValue.Excel2007Extension);

                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    this.workBook.Write(fs);
                }

                res.SetSuccess(link);
            }
            catch (IOException ex)
            {
                res.SetFail(ex);
                ExceptionManager.Trace(ex, source);
            }
            catch (NullReferenceException ex)
            {
                res.SetFail(ex);
                ExceptionManager.Trace(ex, source);
            }
            catch (NotSupportedException ex)
            {
                res.SetFail(ex);
                ExceptionManager.Trace(ex, source);
            }

            return(res);
        }