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

namespace Coscine.Api.Project.Models
{
    public class ResourceModel : DatabaseModel<Resource>
    {
16 17
        private readonly IConfiguration _configuration;

18
        public ResourceModel() : base(Program.Configuration)
19
        {
20
            _configuration = Program.Configuration;
21 22
        }

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

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

61 62 63
            return resource;
        }

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
        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);
                    rdsResourceTypeModel.Update(rdsResourceType);
                }
                else
                {
                    RDSResourceType rdsResourceType = new RDSResourceType()
                    {
98 99
                        BucketName = GetRDSBucketName(),
                        Size = rdsResourceTypeObject.Size,
100 101 102 103 104 105
                    };
                    rdsResourceTypeModel.Insert(rdsResourceType);
                    resource.ResourceTypeOptionId = rdsResourceType.Id;
                    Update(resource);
                }
            }
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
            else if (resource.Type.DisplayName == "s3")
            {
                S3ResourceTypeObject s3ResourceTypeObject = resourceTypeOption.ToObject<S3ResourceTypeObject>();
                S3ResourceTypeModel s3ResourceTypeModel = new S3ResourceTypeModel();
                if (resource.ResourceTypeOptionId != null)
                {
                    S3ResourceType s3ResourceType = s3ResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value);

                    s3ResourceType.BucketName = s3ResourceTypeObject.BucketName;
                    s3ResourceType.AccessKey = s3ResourceTypeObject.AccessKey ?? s3ResourceType.AccessKey;
                    s3ResourceType.SecretKey = s3ResourceTypeObject.SecretKey ?? s3ResourceType.SecretKey;

                    s3ResourceTypeModel.Update(s3ResourceType);
                }
                else
                {
                    S3ResourceType s3ResourceType = new S3ResourceType()
                    {
                        BucketName = s3ResourceTypeObject.BucketName,
                        AccessKey = s3ResourceTypeObject.AccessKey,
                        SecretKey = s3ResourceTypeObject.SecretKey
                    };
                    s3ResourceTypeModel.Insert(s3ResourceType);
                    resource.ResourceTypeOptionId = s3ResourceType.Id;
                    Update(resource);
                }
            }
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
            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!");
            }
        }

166 167 168 169 170 171 172 173 174 175 176
        private string GetRDSBucketName()
        {
            var prefix = _configuration.GetStringAndWait("coscine/global/buckets/prefix");
            var number = Int32.Parse(_configuration.GetStringAndWait("coscine/global/buckets/currentid"));
            var maxNumber = Int32.Parse(_configuration.GetStringAndWait("coscine/global/buckets/highestid"));
            var newNumber = number % maxNumber == 0 ? 1 : number + 1;
            _configuration.Put("coscine/global/buckets/currentid", "" + newNumber);

            return String.Format("{0}{1,3:000}", prefix, number);
        }

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        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);
            }
        }

195
        public bool HasAccess(User user, Database.Model.Resource resource, params string[] allowedAccess)
196
        {
197 198 199 200 201 202 203
            IEnumerable<string> allowedAccessLabels = allowedAccess.Select(x => x.ToLower().Trim()).ToList();
            return DatabaseConnection.ConnectToDatabase((db) => (from relation in db.ProjectRoles
                                                                 where relation.Project.ProjectResourceProjectIdIds != null && relation.Project.ProjectResourceProjectIdIds
                                                                    .Any((projectResource) => projectResource.Resource.Id == resource.Id)
                                                                     && relation.User.Id == user.Id
                                                                     && allowedAccessLabels.Contains(relation.Role.DisplayName.ToLower())
                                                                 select relation).Any());
204
        }
205

206 207 208 209 210 211 212 213 214 215 216 217 218 219
        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;
220
            resource.Description = resourceObject.Description;
221 222 223 224 225
            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;
226 227 228 229
            if(resourceObject.License != null)
            {
                resource.LicenseId = resourceObject.License.Id;
            }
230

231 232
            // the application profile can not be altered after creation
            // resource.ApplicationProfile = resourceObject.ApplicationProfile;
233

234
            resource.FixedValues = resourceObject.FixedValues != null ? resourceObject.FixedValues.ToString() : "{}";
235

236 237 238
            // the resource creator can not be altered after creation
            // resource.Creator = resourceObject.Creator;

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
            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));
            }
263 264 265 266 267
            else if (resource.Type.DisplayName == "s3" && resource.ResourceTypeOptionId != null)
            {
                S3ResourceTypeModel s3ResourceTypeModel = new S3ResourceTypeModel();
                s3ResourceTypeModel.Delete(s3ResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value));
            }
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
            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())
284
                            .Select((discipline) => new DisciplineObject(discipline.Id, discipline.Url, discipline.DisplayNameDe, discipline.DisplayNameEn));
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

            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);
303 304 305 306 307 308 309
                resourceTypeOptionObject = new RDSResourceTypeObject(rdsResourceType.Id, rdsResourceType.BucketName, (int)rdsResourceType.Size);
            }
            else if (resource.Type.DisplayName == "s3" && resource.ResourceTypeOptionId != null)
            {
                S3ResourceTypeModel s3ResourceTypeModel = new S3ResourceTypeModel();
                var s3ResourceType = s3ResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value);
                resourceTypeOptionObject = new S3ResourceTypeObject(s3ResourceType.Id, s3ResourceType.BucketName, null, null);
310 311 312 313 314 315 316 317 318 319 320 321
            }
            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,
322
                resource.Description,
323 324 325 326 327 328
                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,
329 330
                JObject.FromObject(resourceTypeOptionObject),
                resource.ApplicationProfile,
331 332
                JToken.Parse(resource.FixedValues == null ? "{}": resource.FixedValues ),
                (resource.Creator != null) ? resource.Creator : null
333 334
            );
        }
335

336
        public override Expression<Func<Resource, Guid>> GetIdFromObject()
337
        {
338
            return databaseObject => databaseObject.Id;
339 340 341 342 343 344
        }

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

L. Ellenbeck's avatar
L. Ellenbeck committed
346
        public override void SetObjectId(Resource databaseObject, Guid id)
347
        {
L. Ellenbeck's avatar
L. Ellenbeck committed
348
            databaseObject.Id = id;
349
        }
350 351
    }
}