ProjectModel.cs 11.6 KB
Newer Older
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Linq.Expressions;
5
using Coscine.Api.Project.ReturnObjects;
6
using Coscine.ApiCommons.Models;
7 8 9 10 11 12 13 14
using Coscine.Database.Model;
using LinqToDB;

namespace Coscine.Api.Project.Models
{
    public class ProjectModel : DatabaseModel<Coscine.Database.Model.Project>
    {

15
        public ProjectModel() : base(Program.Configuration) 
16 17 18 19 20
        {
        }

        public Coscine.Database.Model.Project StoreFromObject(ProjectObject projectObject, User user)
        {
21 22 23 24 25
            if(projectObject.Disciplines.Count() == 0 || projectObject.Institutes.Count() == 0)
            {
                throw new ArgumentException("Discipline and Institute are necessary!");
            }

26 27 28 29
            Coscine.Database.Model.Project project = new Coscine.Database.Model.Project()
            {
                Description = projectObject.Description,
                DisplayName = projectObject.DisplayName,
30 31 32
                StartDate = projectObject.StartDate,
                EndDate = projectObject.EndDate,
                Keywords = projectObject.Keywords,
33 34 35 36 37 38

                ProjectName = projectObject.ProjectName,
                PrincipleInvestigators = projectObject.PrincipleInvestigators,
                GrantId = projectObject.GrantId,

                VisibilityId = projectObject.Visibility.Id,
39 40
            };
            Insert(project);
41 42 43 44 45 46 47 48 49 50
            try
            {
                SetDisciplines(project, projectObject.Disciplines);
                SetInstitutes(project, projectObject.Institutes);
            }
            catch (Exception e)
            {
                Delete(project);
                throw e;
            }
51 52 53 54
            SetOwner(project, user);
            return project;
        }

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        private void SetDisciplines(Database.Model.Project project, IEnumerable<DisciplineObject> disciplines)
        {
            ProjectDisciplineModel projectDisciplineModel = new ProjectDisciplineModel();
            foreach(var oldDiscipline in projectDisciplineModel.GetAllWhere((projectDiscipline) => projectDiscipline.ProjectId == project.Id))
            {
                projectDisciplineModel.Delete(oldDiscipline);
            }
            foreach (var discipline in disciplines)
            {
                projectDisciplineModel.Insert(new ProjectDiscipline()
                {
                    ProjectId = project.Id,
                    DisciplineId = discipline.Id
                });
            }
        }

        private void SetInstitutes(Database.Model.Project project, IEnumerable<InstituteObject> institutes)
        {
            ProjectInstituteModel projectInstituteModel = new ProjectInstituteModel();
            foreach (var oldInstitute in projectInstituteModel.GetAllWhere((projectInstitute) => projectInstitute.ProjectId == project.Id))
            {
                projectInstituteModel.Delete(oldInstitute);
            }
            foreach (var institute in institutes)
            {
                projectInstituteModel.Insert(new ProjectInstitute()
                {
                    ProjectId = project.Id,
                    InstituteId = institute.Id
                });
            }
        }

Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
89
        public ProjectRole SetOwner(Coscine.Database.Model.Project project, User user)
90
        {
91 92 93
            ProjectRoleModel projectRoleModel = new ProjectRoleModel();

            ProjectRole projectRole = new ProjectRole()
94
            {
95 96 97 98 99 100
                RelationId = Guid.NewGuid(),
                ProjectId = project.Id,
                UserId = user.Id,
                RoleId = new RoleModel().GetWhere((x) => x.DisplayName == "Owner").Id
            };
            projectRoleModel.Insert(projectRole);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
101 102

            return projectRole;
103 104
        }

105 106 107 108 109
        public bool HasAccess(User user, Guid projectId, params string[] allowedAccess)
        {
            return HasAccess(user, GetById(projectId), allowedAccess);
        }

110
        public bool HasAccess(User user, Database.Model.Project project, params string[] allowedAccess)
111
        {
112 113
            ProjectRoleModel projectRoleModel = new ProjectRoleModel();
            allowedAccess = allowedAccess.Select(x => x.ToLower().Trim()).ToArray();
114

115 116 117 118 119
            IEnumerable<Coscine.Database.Model.ProjectRole> projectRoles = projectRoleModel.GetAllWhere(
                (projectRoleRelation) => projectRoleRelation.ProjectId == project.Id && 
                                         projectRoleRelation.UserId == user.Id &&
                                         allowedAccess.Contains(projectRoleRelation.Role.DisplayName.ToLower()));
            return projectRoles.Count() > 0;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
120 121
        }

122
        public IEnumerable<Database.Model.Project> GetWithAccess(User user, params string[] allowedAccess)
123
        {
124 125 126 127 128 129 130
            ProjectRoleModel projectRoleModel = new ProjectRoleModel();
            ProjectModel projectModel = new ProjectModel();

            allowedAccess = allowedAccess.Select(x => x.ToLower().Trim()).ToArray();
            var allUserProjectRoles = projectRoleModel.GetAllWhere((projectRoleRelation) => projectRoleRelation.UserId == user.Id &&
                                                                                            allowedAccess.Contains(projectRoleRelation.Role.DisplayName.ToLower()));
            var allowedProjectIds = allUserProjectRoles.Select((projectRole) => projectRole.ProjectId);
131 132 133 134 135 136 137 138 139 140
            var allowedProjects = projectModel.GetAllWhere((project) => allowedProjectIds.Contains(project.Id)
                                                                        && ((!project.SubProjectsSubProjectIdIds.Any()) // get top level projects not having any parent projects
                                                                            || !(from subProject in project.SubProjectsSubProjectIdIds // check if the direct parent project is accessible to the current user
                                                                                where (from parentProjectRole in subProject.Project.ProjectRolesProjectIdIds
                                                                                        where parentProjectRole.UserId == user.Id
                                                                                        && allowedAccess.Contains(parentProjectRole.Role.DisplayName.ToLower())
                                                                                        select parentProjectRole).Any()
                                                                                select subProject).Any())
                                                            );

141
            return allowedProjects.ToList();
142 143 144 145
        }

        public void AddResource(Coscine.Database.Model.Project project, Resource resource)
        {
146 147
            ProjectResourceModel projectResourceModel = new ProjectResourceModel();
            if (projectResourceModel.GetAllWhere((projectResource) => projectResource.ResourceId == resource.Id && projectResource.ProjectId == project.Id).Any())
148 149 150
            {
                throw new InvalidOperationException("Resource is already assigned to project!");
            }
151 152 153 154 155 156 157 158 159 160
            ProjectResource newProjectResource = new ProjectResource
            {
                ProjectId = project.Id,
                ResourceId = resource.Id
            };
            projectResourceModel.Insert(newProjectResource);
        }

        public int UpdateByObject(Database.Model.Project project, ProjectObject projectObject)
        {
161 162 163 164 165
            if (projectObject.Disciplines.Count() == 0 || projectObject.Institutes.Count() == 0)
            {
                throw new ArgumentException("Discipline and Institute are necessary!");
            }

166 167
            project.Description = projectObject.Description;
            project.DisplayName = projectObject.DisplayName;
168 169 170
            project.StartDate = projectObject.StartDate;
            project.EndDate = projectObject.EndDate;
            project.Keywords = projectObject.Keywords;
171

172 173 174 175 176 177 178 179
            project.ProjectName = projectObject.ProjectName;
            project.PrincipleInvestigators = projectObject.PrincipleInvestigators;
            project.GrantId = projectObject.GrantId;

            SetDisciplines(project, projectObject.Disciplines);
            SetInstitutes(project, projectObject.Institutes);
            project.VisibilityId = projectObject.Visibility.Id;

180
            return Update(project);
181 182
        }

183 184 185 186 187 188
        public ProjectObject CreateReturnObjectFromDatabaseObject(Database.Model.Project project)
        {
            return CreateReturnObjectFromDatabaseObject(project, new Guid());
        }

        public ProjectObject CreateReturnObjectFromDatabaseObject(Database.Model.Project project, Guid parentId)
189 190 191 192 193 194
        {
            IEnumerable<DisciplineObject> disciplines = new List<DisciplineObject>();
            if(project.ProjectDisciplineProjectIdIds == null)
            {
                DisciplineModel disciplineModel = new DisciplineModel();
                disciplines = disciplineModel.GetAllWhere((discipline) => (from relation in discipline.ProjectDisciplineDisciplineIdIds where relation.ProjectId == project.Id select relation).Any())
195
                                                .Select((discipline) => new DisciplineObject(discipline.Id, discipline.Url, discipline.DisplayNameDe, discipline.DisplayNameEn));
196 197 198
            }
            else
            {
199
                disciplines = project.ProjectDisciplineProjectIdIds.Select((discipline) => new DisciplineObject(discipline.Discipline.Id, discipline.Discipline.Url, discipline.Discipline.DisplayNameDe, discipline.Discipline.DisplayNameEn));
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
            }
            IEnumerable<InstituteObject> institutes = new List<InstituteObject>();
            if (project.ProjectInstituteProjectIdIds == null)
            {
                InstituteModel instituteModel = new InstituteModel();
                institutes = instituteModel.GetAllWhere((institute) => (from relation in institute.ProjectInstituteInstituteIdIds where relation.ProjectId == project.Id select relation).Any())
                                                .Select((institute) => new InstituteObject(institute.Id, institute.IKZ, institute.DisplayName));
            }
            else
            {
                institutes = project.ProjectInstituteProjectIdIds.Select((institute) => new InstituteObject(institute.Institute.Id, institute.Institute.IKZ, institute.Institute.DisplayName));
            }
            if (project.Visibility == null && project.VisibilityId.HasValue)
            {
                VisibilityModel visibilityModel = new VisibilityModel();
                project.Visibility = visibilityModel.GetById(project.VisibilityId.Value);
            }

            return new ProjectObject(project.Id, project.Description, project.DisplayName, project.StartDate, project.EndDate, project.Keywords, 
                                                    project.ProjectName, project.PrincipleInvestigators, project.GrantId,
                                                    disciplines,
                                                    institutes,
                                                    project.Visibility == null ? null : new VisibilityObject(project.Visibility.Id, project.Visibility.DisplayName),
                                                    parentId);
        }

226
        public override Expression<Func<Database.Model.Project, Guid>> GetIdFromObject()
227
        {
228
            return databaseObject => databaseObject.Id;
229 230 231 232 233 234
        }

        public override ITable<Database.Model.Project> GetITableFromDatabase(CoscineDB db)
        {
            return db.Projects;
        }
235

L. Ellenbeck's avatar
L. Ellenbeck committed
236
        public override void SetObjectId(Database.Model.Project databaseObject, Guid id)
237
        {
L. Ellenbeck's avatar
L. Ellenbeck committed
238
            databaseObject.Id = id;
239
        }
240 241
    }
}