示例#1
0
        public ActivityDTO SaveActivity(ActivityDTO activity)
        {
            Log.WriteWarning("SaveActivity:Username={0},GlobalId={1}", SecurityInfo.SessionData.Profile.UserName, activity.GlobalId);
            var dbActivity = activity.Map <Activity>();

            using (var trans = Session.BeginSaveTransaction())
            {
                //Profile dbProfile = Session.Load<Profile>(SecurityInfo.SessionData.Profile.Id);
                //Activity db = null;
                //if(activity.GlobalId!=Constants.UnsavedGlobalId)
                //{
                //    db = Session.Get<Activity>(activity.GlobalId);
                //    Mapper.Map(activity, db);
                //}
                //else
                //{
                //    db = activity.Map<Activity>();
                //    db.CreationDate = Configuration.TimerService.UtcNow;
                //    db.Profile = dbProfile;
                //}

                //if (SecurityInfo.SessionData.Profile.Id != db.Profile.Id)
                //{
                //    throw new CrossProfileOperationException("Cannot modify activity for another user");
                //}
                Profile dbProfile = Session.Load <Profile>(SecurityInfo.SessionData.Profile.GlobalId);

                if (dbActivity.IsNew)
                {
                    dbActivity.CreationDate = Configuration.TimerService.UtcNow;
                }
                else
                {
                    var db = Session.Get <Activity>(activity.GlobalId);
                    if (db != null)
                    {
                        if (SecurityInfo.SessionData.Profile.GlobalId != db.Profile.GlobalId)
                        {
                            throw new CrossProfileOperationException("Cannot modify Activity for another user");
                        }
                    }
                }


                dbActivity.Profile = dbProfile;
                if (string.IsNullOrEmpty(dbActivity.Color))
                {
                    dbActivity.Color = Constants.DefaultColor;
                }

                int res = Session.QueryOver <Activity>().Where(x => x.Name == dbActivity.Name && x.GlobalId != dbActivity.GlobalId && x.Profile == dbProfile).RowCount();
                if (res > 0)
                {
                    throw new UniqueException("Activity with the same name is already exist");
                }

                dbActivity = Session.Merge(dbActivity);
                dbProfile.DataInfo.ActivityHash = Guid.NewGuid();
                trans.Commit();
                return(dbActivity.Map <ActivityDTO>());
            }
        }