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

namespace Coscine.Api.Project.Models
{
    public class ResourceModel : DatabaseModel<Resource>
    {
15
        public ResourceModel() : base(Program.Configuration)
16 17 18
        {
        }

19 20
        public Resource StoreFromObject(ResourceObject resourceObject)
        {
21
            if (resourceObject.Disciplines.Count() == 0 || resourceObject.ResourceTypeOption == null)
22
            {
23 24 25 26 27
                throw new ArgumentException("Discipline and ResourceTypeOption are necessary!");
            }

            Resource resource = new Resource()
            {                
28
                DisplayName = resourceObject.DisplayName,
29
                ResourceName = resourceObject.ResourceName,
30
                Description = resourceObject.Description,
31 32
                Keywords = resourceObject.Keywords,
                UsageRights = resourceObject.UsageRights,
L. Ellenbeck's avatar
L. Ellenbeck committed
33
                TypeId = resourceObject.Type.Id,
34 35
                Type = new ResourceTypeModel().GetById(resourceObject.Type.Id),
                VisibilityId = resourceObject.Visibility.Id,
36
                
37
            };
38 39 40 41
            if(resourceObject.License != null)
            {
                resource.LicenseId = resourceObject.License.Id;
            }
42
            Insert(resource);
43 44 45 46 47 48 49 50 51 52 53 54
            
            try
            {
                SetDisciplines(resource, resourceObject.Disciplines);
                SetResourceTypeObject(resource, resourceObject.ResourceTypeOption);
            }
            catch (Exception e)
            {
                Delete(resource);
                throw e;
            }

55 56 57
            return resource;
        }

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        public int DeleteResource(Resource resource)
        {
            ProjectResourceModel projectResourceModel = new ProjectResourceModel();
            foreach (var projectResource in projectResourceModel.GetAllWhere((projectResource) => projectResource.ResourceId == resource.Id))
            {
                projectResourceModel.Delete(projectResource);
            }

            ResourceDisciplineModel resourceDisciplineModel = new ResourceDisciplineModel();
            foreach (var resourceDiscipline in resourceDisciplineModel.GetAllWhere((resourceDicipline) => resourceDicipline.ResourceId == resource.Id))
            {
                resourceDisciplineModel.Delete(resourceDiscipline);
            }

            DeleteResourceTypeObject(resource);

            return Delete(resource);
        }

        private void SetResourceTypeObject(Resource resource, JObject resourceTypeOption)
        {
            if (resource.Type.DisplayName == "rds")
            {
                RDSResourceTypeObject rdsResourceTypeObject = resourceTypeOption.ToObject<RDSResourceTypeObject>();
                RDSResourceTypeModel rdsResourceTypeModel = new RDSResourceTypeModel();
                if (resource.ResourceTypeOptionId != null)
                {
                    RDSResourceType rdsResourceType = rdsResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value);

                    rdsResourceType.BucketName = rdsResourceTypeObject.BucketName;
                    rdsResourceType.AccessKey = rdsResourceTypeObject.AccessKey ?? rdsResourceType.AccessKey;
                    rdsResourceType.SecretKey = rdsResourceTypeObject.SecretKey ?? rdsResourceType.SecretKey;

                    rdsResourceTypeModel.Update(rdsResourceType);
                }
                else
                {
                    RDSResourceType rdsResourceType = new RDSResourceType()
                    {
                        BucketName = rdsResourceTypeObject.BucketName,
                        AccessKey = rdsResourceTypeObject.AccessKey,
                        SecretKey = rdsResourceTypeObject.SecretKey
                    };
                    rdsResourceTypeModel.Insert(rdsResourceType);
                    resource.ResourceTypeOptionId = rdsResourceType.Id;
                    Update(resource);
                }
            }
            else if (resource.Type.DisplayName == "gitlab")
            {
                GitlabResourceTypeObject gitlabResourceTypeObject = resourceTypeOption.ToObject<GitlabResourceTypeObject>();
                GitlabResourceTypeModel gitlabResourceTypeModel = new GitlabResourceTypeModel();
                if (resource.ResourceTypeOptionId != null)
                {
                    GitlabResourceType gitlabResourceType = gitlabResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value);

                    gitlabResourceType.RepositoryNumber = gitlabResourceTypeObject.RepositoryNumber;
                    gitlabResourceType.RepositoryUrl = gitlabResourceTypeObject.RepositoryUrl;
                    gitlabResourceType.Token = gitlabResourceTypeObject.Token ?? gitlabResourceType.Token;

                    gitlabResourceTypeModel.Update(gitlabResourceType);
                }
                else
                {
                    GitlabResourceType gitlabResourceType = new GitlabResourceType()
                    {
                        RepositoryNumber = gitlabResourceTypeObject.RepositoryNumber,
                        RepositoryUrl = gitlabResourceTypeObject.RepositoryUrl,
                        Token = gitlabResourceTypeObject.Token
                    };
                    gitlabResourceTypeModel.Insert(gitlabResourceType);
                    resource.ResourceTypeOptionId = gitlabResourceType.Id;
                    Update(resource);
                }
            }
            else
            {
                throw new ArgumentException("Not supported resource type!");
            }
        }

        private void SetDisciplines(Resource resource, IEnumerable<DisciplineObject> disciplines)
        {
            ResourceDisciplineModel resourceDisciplineModel = new ResourceDisciplineModel();
            foreach (var oldDiscipline in resourceDisciplineModel.GetAllWhere((resourceDiscipline) => resourceDiscipline.ResourceId == resource.Id))
            {
                resourceDisciplineModel.Delete(oldDiscipline);
            }
            foreach (var discipline in disciplines)
            {
                ResourceDiscipline resourceDiscipline = new ResourceDiscipline()
                {
                    DisciplineId = discipline.Id,
                    ResourceId = resource.Id
                };
                resourceDisciplineModel.Insert(resourceDiscipline);
            }
        }

157 158 159 160 161 162 163
        public bool OwnsResource(User user, Resource resource)
        {
            return DatabaseConnection.ConnectToDatabase((db) =>
            {
                return (from relation in db.ProjectRoles
                        where relation.User == user
                            && relation.Role.DisplayName == "Owner"
164 165
                            && (relation.Project.ProjectResourceProjectIdIds != null && relation.Project.ProjectResourceProjectIdIds.
                                Any((projectResource) => projectResource.Resource == resource))
166 167 168
                        select relation).Any();
            });
        }
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183
        public int UpdateByObject(Resource resource, ResourceObject resourceObject)
        {
            if (resourceObject.Disciplines.Count() == 0 || resourceObject.ResourceTypeOption == null)
            {
                throw new ArgumentException("Discipline and ResourceTypeOption are necessary!");
            }

            if (resource.TypeId != resourceObject.Type.Id)
            {
                DeleteResourceTypeObject(resource);
            }

            resource.DisplayName = resourceObject.DisplayName;
            resource.ResourceName = resourceObject.ResourceName;
184
            resource.Description = resourceObject.Description;
185 186 187 188 189
            resource.Keywords = resourceObject.Keywords;
            resource.UsageRights = resourceObject.UsageRights;
            resource.TypeId = resourceObject.Type.Id;
            resource.Type = new ResourceTypeModel().GetById(resourceObject.Type.Id);
            resource.VisibilityId = resourceObject.Visibility.Id;
190 191 192 193 194
            if(resourceObject.License != null)
            {
                resource.LicenseId = resourceObject.License.Id;
            }
            
195 196
            resource.ApplicationProfile = resourceObject.ApplicationProfile;
            resource.FixedValues = resourceObject.FixedValues.ToString();
197 198 199 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 226 227 228 229 230 231 232 233 234 235 236 237

            SetDisciplines(resource, resourceObject.Disciplines);
            SetResourceTypeObject(resource, resourceObject.ResourceTypeOption);

            return Update(resource);
        }

        // TODO: Find out why resource.Type is not set
        public void SetType(Resource resource)
        {
            if (resource.Type == null)
            {
                ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
                resource.Type = resourceTypeModel.GetById(resource.TypeId);
            }
        }

        public void DeleteResourceTypeObject(Resource resource)
        {
            SetType(resource);
            if (resource.Type.DisplayName == "rds" && resource.ResourceTypeOptionId != null)
            {
                RDSResourceTypeModel rdsResourceTypeModel = new RDSResourceTypeModel();
                rdsResourceTypeModel.Delete(rdsResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value));
            }
            else if (resource.Type.DisplayName == "gitlab" && resource.ResourceTypeOptionId != null)
            {
                GitlabResourceTypeModel gitlabResourceTypeModel = new GitlabResourceTypeModel();
                gitlabResourceTypeModel.Delete(gitlabResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value));
            }
        }

        public ResourceObject CreateReturnObjectFromDatabaseObject(Resource resource)
        {
            SetType(resource);

            DisciplineModel disciplineModel = new DisciplineModel();
            var disciplines = disciplineModel.GetAllWhere((discipline) =>
                            (from relation in discipline.ResourceDisciplineDisciplineIdIds
                             where relation.ResourceId == resource.Id
                             select relation).Any())
238
                            .Select((discipline) => new DisciplineObject(discipline.Id, discipline.Url, discipline.DisplayNameDe, discipline.DisplayNameEn));
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

            if (resource.Visibility == null && resource.VisibilityId != null)
            {
                VisibilityModel visibilityModel = new VisibilityModel();
                resource.Visibility = visibilityModel.GetById(resource.VisibilityId.Value);
            }

            if (resource.License == null && resource.LicenseId != null)
            {
                LicenseModel licenseModel = new LicenseModel();
                resource.License = licenseModel.GetById(resource.LicenseId.Value);
            }

            ResourceTypeOptionObject resourceTypeOptionObject = null;
            if(resource.Type.DisplayName == "rds" && resource.ResourceTypeOptionId != null)
            {
                RDSResourceTypeModel rdsResourceTypeModel = new RDSResourceTypeModel();
                var rdsResourceType = rdsResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value);
                resourceTypeOptionObject = new RDSResourceTypeObject(rdsResourceType.Id, rdsResourceType.BucketName, null, null);
            }
            else if(resource.Type.DisplayName == "gitlab" && resource.ResourceTypeOptionId != null)
            {
                GitlabResourceTypeModel gitlabResourceTypeModel = new GitlabResourceTypeModel();
                var gitlabResourceType = gitlabResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value);
                resourceTypeOptionObject = new GitlabResourceTypeObject(gitlabResourceType.Id, gitlabResourceType.RepositoryNumber, gitlabResourceType.RepositoryUrl, null);
            }

            return new ResourceObject(
                resource.Id,
                resource.DisplayName,
                resource.ResourceName,
270
                resource.Description,
271 272 273 274 275 276
                resource.Keywords,
                resource.UsageRights,
                new ResourceTypeObject(resource.Type.Id, resource.Type.DisplayName),
                disciplines,
                (resource.Visibility != null) ? new VisibilityObject(resource.Visibility.Id, resource.Visibility.DisplayName) : null,
                (resource.License != null) ? new LicenseObject(resource.License.Id, resource.License.DisplayName) : null,
277 278 279
                JObject.FromObject(resourceTypeOptionObject),
                resource.ApplicationProfile,
                JToken.Parse(resource.FixedValues)
280 281
            );
        }
282

283
        public override Expression<Func<Resource, Guid>> GetIdFromObject()
284
        {
285
            return databaseObject => databaseObject.Id;
286 287 288 289 290 291
        }

        public override ITable<Resource> GetITableFromDatabase(CoscineDB db)
        {
            return db.Resources;
        }
292

L. Ellenbeck's avatar
L. Ellenbeck committed
293
        public override void SetObjectId(Resource databaseObject, Guid id)
294
        {
L. Ellenbeck's avatar
L. Ellenbeck committed
295
            databaseObject.Id = id;
296
        }
297 298
    }
}