/// <summary> /// Gets all the profiles as a JsonResult /// </summary> /// <returns></returns> public JsonResult GetProfilesTable() { if (Request.IsAjaxRequest()) { string raw_profiles = _employeeProfileTable.getJoinRows(); JArray profiles = JsonConvert.DeserializeObject <JArray>(raw_profiles); foreach (JObject profile in profiles) { profile.Remove("customFields"); //Count the number of users with the specified profileId profile["numEmployees"] = JsonConvert.DeserializeObject <JArray>(_employeeTable.Get("profileId", profile["_id"].ToString())).Count; } return(Json(JsonConvert.SerializeObject(profiles))); } return(Json("[]")); }
/// <summary> /// This method save all the users imported data /// </summary> /// <param name="file">Excel File</param> /// <returns></returns> /// <author>Edwin (Origin) - Abigail Rodriguez(Edit)</author> public String saveImport(String data, IEnumerable <HttpPostedFileBase> files) { try { String dataimport = data.ToString(); JArray dataimportarray = JsonConvert.DeserializeObject <JArray>(dataimport); int count = 0; int totalAdd = 0; int totalFail = 0; int totalAddIMG = 0; int totalFailIMG = 0; JArray result = new JArray(); JObject bossNotFound = new JObject(); foreach (JObject items in dataimportarray) { count++; bool error = false; /*The selected employee name is already in use and is not the employee who has it*/ if (employeeExists(items["employee"].ToString()) == "true") { result.Add("{\"error\":\"El ID del Empleado ya está siendo utilizado\", \"registro\":\"" + count + "\"}"); error = true; } /* Format validations */ else if (!Regex.IsMatch(items["employee"].ToString(), "([a-zA-Z0-9-_.]){4,}") || items["employee"].ToString() == "") { result.Add("{\"error\":\"Formato incorrecto para ID del Empleado\", \"registro\":\"" + count + "\"}"); error = true; } else if (!Regex.IsMatch(items["name"].ToString(), "[A-ZÁÉÍÓÚÑa-záéíóúñ]+( [A-ZÁÉÍÓÚÑa-záéíóúñ]+){0,2}")) { result.Add("\"error\":\"Formato incorrecto para: name\", \"registro\":\"" + count + "\"}"); error = true; } else if (!Regex.IsMatch(items["lastname"].ToString(), "[A-ZÁÉÍÓÚÑa-záéíóúñ]+( [A-ZÁÉÍÓÚÑa-záéíóúñ]+){0,1}")) { result.Add("{\"error\":\"Formato incorrecto para apellido\",\"registro\":\"" + count + "\"}"); error = true; } else if (!Regex.IsMatch(items["motherlastname"].ToString(), "[A-ZÁÉÍÓÚÑa-záéíóúñ]+( [A-ZÁÉÍÓÚÑa-záéíóúñ]+){0,1}")) { result.Add("{\"error\":\"Formato incorrecto para apellido\",\"registro\":\"" + count + "\"}"); error = true; } if (error) { totalFail++; continue; } //Section to get profile ID string profileID = ""; //JArray profileOtro = JsonConvert.DeserializeObject<JArray>(employeeprofileTable.Get("name", "Null")); //JObject profileOt = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(profileOtro[0])); //profileID = profileOt["_id"].ToString(); if (items["profileId"] != null && items["profileId"].ToString() != "") { string profileResult = employeeprofileTable.Get("name", items["profileId"].ToString()); if (profileResult != "[]" && profileResult != "" && profileResult != null) { JArray conjuntja = JsonConvert.DeserializeObject <JArray>(profileResult); profileID = (from mov in conjuntja select(string) mov["_id"]).First().ToString(); } } //Change name representation items["name"] = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(items["name"].ToString().ToLower().Trim()); items["lastname"] = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(items["lastname"].ToString().ToLower().Trim()); //there are fields that we know that exists so we set them into the json String jsonData = "{'employee':'" + items["employee"] + "','name':'" + items["name"].ToString().Replace("+", " ") + "','imgext':'" + "','lastname':'" + items["lastname"].ToString().Replace("+", " ") + "','motherlastname':'" + items["motherlastname"].ToString().Replace("+", " ") + "','type':'" + items["type"] + "','profileId':'" + profileID + "','area':'" + items["area"]; try //trying to set the creator's id { jsonData += "','creatorId':'"; jsonData += this.Session["_id"]; jsonData += "'}"; } catch (Exception e) { /*Ignored*/ } string id = employeetable.saveRow(jsonData, null); //Save new user _logTable.SaveLog(Session["_id"].ToString(), "Empleados", "Insert: " + items["employee"].ToString(), "Employees", DateTime.Now.ToString()); result.Add("{\"success\":\"Empleado guardado en registo: " + count + "\"}"); totalAdd++; } //Save imagen files string ext = null; string relativepath = @"\Uploads\Images\TempEmployee"; string absolutepath = Server.MapPath(relativepath); ////Delete all files if (System.IO.Directory.Exists(absolutepath + "\\")) { System.IO.Directory.Delete(absolutepath + "\\", true); } ////Create de upload directory if (!System.IO.Directory.Exists(absolutepath + "\\")) { System.IO.Directory.CreateDirectory(absolutepath + "\\"); } for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; //Uploaded file string fileName = file.FileName; ext = file.FileName.Split('.').Last(); //getting the extension if (ext == "rar" || ext == "zip") { try { //Saves de compress file file.SaveAs(absolutepath + "\\" + fileName); //Extract file ZipFile.ExtractToDirectory(absolutepath + "\\" + fileName, absolutepath + "\\"); string[] filesArray = System.IO.Directory.GetFiles(absolutepath + "\\" + fileName.Split('.').First() + "\\"); foreach (string s in filesArray) { string namefileS = Path.GetFileName(s); JArray employeesInfo = JsonConvert.DeserializeObject <JArray>(employeetable.Get("employee", namefileS.Split('.').First())); if (employeesInfo.Count() > 0 && employeesInfo != null) { JObject employeeInfo = JsonConvert.DeserializeObject <JObject>(JsonConvert.SerializeObject(employeesInfo[0])); employeeInfo["imgext"] = s.Split('.').Last(); employeetable.SaveRow(JsonConvert.SerializeObject(employeeInfo), employeeInfo["_id"].ToString()); _logTable.SaveLog(Session["_id"].ToString(), "Empleados", "Insert: " + employeeInfo["employee"].ToString(), "Employees", DateTime.Now.ToString()); System.IO.File.Move(s, Server.MapPath(@"\Uploads\Images\" + employeeInfo["_id"].ToString() + "." + s.Split('.').Last())); totalAddIMG++; } } } catch (Exception e) { totalFailIMG++; result.Add("{\"error\":\"Error al gaurdar imagen\"}"); } } } JObject finalResult = new JObject(); finalResult.Add("employeeSuccess", totalAdd.ToString()); finalResult.Add("employeeError", totalFail.ToString()); finalResult.Add("imgSuccess", totalAddIMG.ToString()); finalResult.Add("imgError", totalFailIMG.ToString()); finalResult.Add("details", JsonConvert.SerializeObject(result)); return(JsonConvert.SerializeObject(finalResult)); } catch (Exception ex) { return(null); } }