/// <summary> /// Will create Duplicate record /// </summary> /// <param name="id">selected record id</param> /// <param name="projectId">Current Project Id</param> /// <param name="type">current tab</param> /// <param name="label">name </param> /// <param name="blobContent">blob</param> /// <param name="userName">name of the logged user</param> /// <param name="newName">new file name</param> /// <param name="resID">resource ID</param> /// <param name="mimetype">extension of the file</param> public int DuplicateResources(int id, int projectId, TypeOfResource type, string label, byte[] blobContent, string userName, string newName, Guid resID, string mimetype) { var project = ProjectManager.Instance.GetProject(projectId, userName); var volume = GetOrAddVolumeFolder(project, type); var parent = String.IsNullOrEmpty(label) ? volume : GetFolders(volume).SingleOrDefault(f => f.Name == label); if (parent == null) { // no folder means new label parent = CreateFolder(volume, label); } try { var resId = Guid.NewGuid(); using (var newBlob = BlobFactory.GetBlobStorage(resId, BlobFactory.Container.Resources)) { newBlob.Content = new byte[blobContent.Length]; blobContent.CopyTo(newBlob.Content, 0); var res = new ResourceFile { Name = newName, Parent = parent, Private = false, OrderNr = parent.Children.Max(r => r.OrderNr) + 1, TypesOfResource = type, ResourceId = resId, Project = project, Owner = Ctx.Users.Single(u => u.UserName == userName), MimeType = mimetype }; newBlob.Save(); return(AddResource(res)); } } catch (Exception) { } return(0); }
public IEnumerable <ResourceFile> GetVolumeFiles(int projectId, TypeOfResource volType) { return (from f in Ctx.Resources.OfType <ResourceFile>() where f.Parent == null && f.TypesOfResource == volType && f.Project.Id == projectId select f); }
public ResourceStat(long value, TypeOfResource typeOfResource) : base(value) { this.RESOURCE_TYPE = (int)typeOfResource; NAME = "ResourceStat"; RESOURCE_CHECK = true; this.TYPE = (int)Type.Resource; }
private void SeedResources(StudentSystemContext context) { string[] resources = File.ReadAllLines( @"C:\Users\Maika\Documents\Programming\Homework\Databases Fundamentals\Databases Advanced\CodeFirstExercises\resources.txt") .Skip(1) .ToArray(); int licenseCount = context.Licenses.Count(); Random rand = new Random(); foreach (var resource in resources) { string[] detailsResource = resource.Split('\t'); string resourceName = detailsResource[0]; TypeOfResource resourceType = (TypeOfResource)Enum.Parse(typeof(TypeOfResource), detailsResource[1]); string resourceUrl = detailsResource[2]; int licenseId = rand.Next(0, licenseCount); License license = context.Licenses.Find(licenseId); Resource newResource = new Resource() { Name = resourceName, ResourceType = resourceType, Url = resourceUrl }; newResource.License.Add(license); context.Resources.Add(newResource); } context.SaveChanges(); }
public IEnumerable <ResourceFolder> GetVolumeFolders(int projectId, TypeOfResource volType) { return (from r in Ctx.Resources.OfType <ResourceFolder>() where r.TypesOfResource == volType && r.Project.Id == projectId && r.Parent == null orderby r.Name select r); }
/// <summary> /// Load all resource to list dictionary. /// Luu toan bo tai nguyen vao list dictionary /// </summary> private void LoadAllResource() { resourceList = new Dictionary <string, ResourceStat>(); foreach (string str in Enum.GetNames(typeof(TypeOfResource.Type))) { TypeOfResource type = new TypeOfResource { type = TypeOfResource.ConvertStringToEnum(str) }; ResourceStat resource = new ResourceStat(PlayerPrefs.GetFloat(str, 0), type); resourceList.Add(str, resource); } }
public bool EmptyVolume(int projectId, TypeOfResource typeOfResource, string userName) { var files = Ctx.Resources.Where(r => r.Project.Id == projectId && r.TypesOfResource == typeOfResource && r.Owner.UserName == userName); foreach (var file in files) { Ctx.Resources.Remove(file); using (var blob = BlobFactory.GetBlobStorage(file.ResourceId, BlobFactory.Container.Resources)) { blob.Remove(); } } return(SaveChanges() > 0); }
public bool RenameResource(int id, TypeOfResource volume, string name, string label, string userName) { var res = Ctx.Resources .Include(r => r.Project) .Include(r => r.Owner) .SingleOrDefault(r => r.Id == id); if (res != null && res.Owner.UserName == userName) { Ctx.Entry(res).Property(r => r.Name).CurrentValue = name; Ctx.Entry(res).Property(r => r.Name).IsModified = true; var result = SaveChanges() >= 1; return(MoveFile(id, volume, label, userName) || result); } return(false); }
public int AddResource(int projectId, TypeOfResource type, string label, string fileName, string fileContentType, byte[] content, string userName) { var project = ProjectManager.Instance.GetProject(projectId, userName); var user = Ctx.Users.Single(u => u.UserName == userName); // get label var parent = Ctx.Resources.OfType <ResourceFolder>().SingleOrDefault(f => f.Name == label && f.TypesOfResource == type && f.Parent == null && f.Project.Id == projectId); // add if not present if (parent == null) { // no folder means new label, if label provided parent = new ResourceFolder { TypesOfResource = type, Name = label ?? String.Empty, Owner = user, OrderNr = 1, Private = false, Project = project }; // add before usage Ctx.Resources.Add(parent); SaveChanges(); } var resId = Guid.NewGuid(); ResourceFile res = null; using (var blob = BlobFactory.GetBlobStorage(resId, BlobFactory.Container.Resources)) { try { blob.Content = content; res = new ResourceFile { Name = fileName, Parent = parent, Private = false, OrderNr = parent.Children.Any() ? parent.Children.Max(r => r.OrderNr) + 1 : 1, TypesOfResource = type, ResourceId = resId, Project = project, Owner = user, MimeType = fileContentType }; blob.Save(); return(AddResource(res)); } catch (Exception) { } } return(0); }
public List <Resource> GetResourceFiles(int opusId, TypeOfResource type, string mimeType, string namePattern) { var res = Ctx.Resources.OfType <Resource>().Where(r => r.Id == opusId && r.TypesOfResource == type).ToList(); if (!String.IsNullOrEmpty(mimeType) && String.IsNullOrEmpty(namePattern)) { return(res.Where(r => r.MimeType.StartsWith(mimeType)).ToList()); } if (!String.IsNullOrEmpty(namePattern) && String.IsNullOrEmpty(mimeType)) { return(res.Where(r => r.Name.EndsWith(namePattern)).ToList()); } if (!String.IsNullOrEmpty(namePattern) && !String.IsNullOrEmpty(mimeType)) { return(res.Where(r => r.Name.EndsWith(namePattern) && r.MimeType.StartsWith(mimeType)).ToList()); } return(res); }
public ResourceFolder AddResourceFolder(Project project, string name, TypeOfResource type, ResourceFolder parent = null) { var owner = ProjectManager.Instance.GetTeamLeader(project.Team.Id); var folder = new ResourceFolder { Owner = owner, Name = name, Parent = parent, // NULL is Volume ResourceId = Guid.NewGuid(), Deleted = false, Private = false, OrderNr = 0, Project = project, TypesOfResource = type }; Ctx.Resources.Add(folder); SaveChanges(); return(folder); }
public void LoadResource() { // TO DO somethin.... string[] s = PlayerPrefsX.GetStringArray(KeySave.ALL_RESOURCE); for (int i = 0; i < s.Length; i++) { string[] temp = s[i].Split(','); Debug.Log(s[i].ToString()); if (temp.Length > 0 && temp.Length <= 2) { TypeOfResource type = new TypeOfResource { type = (TypeOfResource.Type) int.Parse(temp[0]) }; float.TryParse(temp[1], out float value); ResourceStat resource = getResourceNeed(type.type); resource.value = value; } } }
public ResourceFolder AddResourceFolder(int projectId, TypeOfResource volumeType, string name, string userName) { var project = ProjectManager.Instance.GetProject(projectId, userName); var volume = GetOrAddVolumeFolder(project, volumeType); var owner = UserManager.Instance.GetUserByName(userName); var folder = new ResourceFolder { Owner = owner, Name = name, Parent = null, // NULL is Volume ResourceId = Guid.NewGuid(), Deleted = false, Private = false, OrderNr = !volume.Children.Any() ? 1 : volume.Children.Max(p => p.OrderNr) + 1, Project = project, TypesOfResource = volumeType }; Ctx.Resources.Add(folder); SaveChanges(); return(folder); }
public IList <ResourceFile> GetAllFiles(int projectId, TypeOfResource volumeType, string userName, params string[] mime) { var project = ProjectManager.Instance.GetProject(projectId, userName); var volume = GetOrAddVolumeFolder(project, volumeType); var files = volume.Children.OfType <ResourceFile>().ToList().Where(f => mime.Contains(f.MimeType)).ToList(); Action <ResourceFolder> getFiles = null; getFiles = (folder) => { if (!folder.HasChildren()) { return; } files.AddRange(folder.Children.OfType <ResourceFile>().Where(f => mime.Contains(f.MimeType))); foreach (var child in folder.Children.OfType <ResourceFolder>().ToList()) { getFiles(child); } }; volume.Children.OfType <ResourceFolder>().ToList().ForEach(f => getFiles(f)); return(files); }
public ResourceFolder GetOrAddVolumeFolder(Project startProject, TypeOfResource type, bool addVolume = false) { var volume = Ctx.Resources .Where(f => f.Project.Id == startProject.Id) .OfType <ResourceFolder>() .FirstOrDefault(f => f.TypesOfResource == type); // for new projects the volumes may not exist, hence we create once needed if (volume == null && addVolume) { volume = new ResourceFolder { TypesOfResource = type, Project = startProject, Owner = startProject.Team.TeamLead, OrderNr = (int)type, Deleted = false }; volume.Name = volume.GetLocalizedTypeOfResource(); Ctx.Resources.Add(volume); SaveChanges(); } return(volume); }
public ResourceStat(float value, TypeOfResource Type) { this.value = value; this.Type = Type; }
public List <Resource> GetResourceFiles(Document opus, TypeOfResource type, string mimeType, string namePattern) { return(GetResourceFiles(opus.Id, type, mimeType, namePattern)); }
public List <Resource> GetResourceFiles(int projectId, TypeOfResource type, string mimeType) { return(GetResourceFiles(projectId, type, mimeType, "")); }
public IEnumerable <Resource> GetFolderOfVolume(Project project, TypeOfResource typeOfResource) { return(Ctx.Resources .Where(f => f.Project.Id == project.Id && f.TypesOfResource == typeOfResource) .OfType <ResourceFolder>()); }
public int AddResource(int projectId, TypeOfResource type, string label, string fileName, string fileContentType, string content, string userName) { return(AddResource(projectId, type, label, fileName, fileContentType, Encoding.ASCII.GetBytes(content), userName)); }
public int AddResource(int projectId, TypeOfResource type, string label, System.Web.HttpPostedFileBase file, string userName) { return(AddResource(projectId, type, label, file.FileName, file.ContentType, file.InputStream.ReadToEnd(), userName)); }
public bool MoveFile(int id, TypeOfResource volume, string label, string userName) { var res = Ctx.Resources .Include(r => r.Project) .Include(r => r.Owner) .SingleOrDefault(r => r.Id == id && r.Owner.UserName == userName); if (res == null) { return(false); } var prjId = res.Project.Id; ResourceFolder parent = null; // special treatment for moving among volumes if (res.TypesOfResource != volume) { res.TypesOfResource = volume; return(SaveChanges() >= 1); } try { parent = Ctx.Resources.OfType <ResourceFolder>().SingleOrDefault(f => f.Name == label && f.Project.Id == prjId && f.TypesOfResource == volume); } catch (Exception) { // in case Single threw an exception we assume that we have accidentially multiple labels with same name. Hence, we make a distinct list. var labels = Ctx.Resources.OfType <ResourceFolder>() .Where(f => f.Project.Id == prjId && f.TypesOfResource == volume) // only within volume .GroupBy(f => f.Name) // distinct label names .ToList() // assume that EF can't resolve further steps, hence materialize .Select(g => new { // make a new temp object Name = g.Key, Folders = g.ToList(), Count = g.Count() }) .Where(g => g.Count > 1) // handle only those with > 1 label appearances .ToList(); if (labels.Any()) { // the remaining parent parent = Ctx.Resources.OfType <ResourceFolder>().First(f => f.Name == label && f.Project.Id == prjId && f.TypesOfResource == volume); // we now have labels that appear 2 or more times only foreach (var l in labels) { var lName = l.Name; var files = Ctx.Resources.OfType <ResourceFile>().Where(f => f.Parent.Name == lName && f.Project.Id == prjId && f.TypesOfResource == volume); foreach (var resourceFile in files) { resourceFile.Parent = parent; } } // save new parents SaveChanges(); // delete all labels with now entities var emptyLabels = Ctx.Resources.OfType <ResourceFolder>().Where(f => f.Project.Id == prjId && f.TypesOfResource == volume && !f.Children.Any()); foreach (var resourceFolder in emptyLabels) { Ctx.Entry(resourceFolder).State = EntityState.Deleted; } SaveChanges(); } } if (parent == null) { var user = Ctx.Users.Single(u => u.UserName == userName); parent = new ResourceFolder { TypesOfResource = volume, Name = label ?? String.Empty, Owner = user, OrderNr = 1, Private = false, Project = res.Project }; } res.Parent = parent; res.TypesOfResource = volume; return(SaveChanges() >= 1); }