ResourceModel.cs 15.8 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 41
                ApplicationProfile = resourceObject.ApplicationProfile,
                FixedValues = resourceObject.FixedValues != null ? resourceObject.FixedValues.ToString() :"{}"                
42
            };
43 44 45 46
            if(resourceObject.License != null)
            {
                resource.LicenseId = resourceObject.License.Id;
            }
47
            Insert(resource);
48 49 50 51 52 53 54 55 56 57 58 59
            
            try
            {
                SetDisciplines(resource, resourceObject.Disciplines);
                SetResourceTypeObject(resource, resourceObject.ResourceTypeOption);
            }
            catch (Exception e)
            {
                Delete(resource);
                throw e;
            }

60 61 62
            return resource;
        }

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
        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()
                    {
97 98
                        BucketName = GetRDSBucketName(),
                        Size = rdsResourceTypeObject.Size,
99 100 101 102 103 104
                    };
                    rdsResourceTypeModel.Insert(rdsResourceType);
                    resource.ResourceTypeOptionId = rdsResourceType.Id;
                    Update(resource);
                }
            }
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
            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);
                }
            }
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 157 158 159 160 161 162 163 164
            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!");
            }
        }

165 166 167 168 169 170 171 172 173 174 175
        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);
        }

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

194 195 196 197 198 199 200
        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"
201 202
                            && (relation.Project.ProjectResourceProjectIdIds != null && relation.Project.ProjectResourceProjectIdIds.
                                Any((projectResource) => projectResource.Resource == resource))
203 204 205
                        select relation).Any();
            });
        }
206

207 208 209 210 211 212 213 214 215 216 217 218 219 220
        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;
221
            resource.Description = resourceObject.Description;
222 223 224 225 226
            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;
227 228 229 230
            if(resourceObject.License != null)
            {
                resource.LicenseId = resourceObject.License.Id;
            }
231 232 233
            // the application profile can not be altered after creation
            // resource.ApplicationProfile = resourceObject.ApplicationProfile;
            resource.FixedValues = resourceObject.FixedValues != null ? resourceObject.FixedValues.ToString() : "{}";
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

            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));
            }
259 260 261 262 263
            else if (resource.Type.DisplayName == "s3" && resource.ResourceTypeOptionId != null)
            {
                S3ResourceTypeModel s3ResourceTypeModel = new S3ResourceTypeModel();
                s3ResourceTypeModel.Delete(s3ResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value));
            }
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
            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())
280
                            .Select((discipline) => new DisciplineObject(discipline.Id, discipline.Url, discipline.DisplayNameDe, discipline.DisplayNameEn));
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

            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);
299 300 301 302 303 304 305
                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);
306 307 308 309 310 311 312 313 314 315 316 317
            }
            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,
318
                resource.Description,
319 320 321 322 323 324
                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,
325 326 327
                JObject.FromObject(resourceTypeOptionObject),
                resource.ApplicationProfile,
                JToken.Parse(resource.FixedValues == null ? "{}": resource.FixedValues )
328 329
            );
        }
330

331
        public override Expression<Func<Resource, Guid>> GetIdFromObject()
332
        {
333
            return databaseObject => databaseObject.Id;
334 335 336 337 338 339
        }

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

L. Ellenbeck's avatar
L. Ellenbeck committed
341
        public override void SetObjectId(Resource databaseObject, Guid id)
342
        {
L. Ellenbeck's avatar
L. Ellenbeck committed
343
            databaseObject.Id = id;
344
        }
345 346
    }
}