protected override MultiLineTextDto InsertImpl(MultiLineTextDto dto)
        {
            var results = from item in SeedData.Ton.MultiLineTexts
                    where item.Id == dto.Id
                    select item;

              if (results.Count() == 0)
              {
            CheckValidity(dto);
            dto.Id = Guid.NewGuid();
            SeedData.Ton.MultiLineTexts.Add(dto);
            return dto;
              }
              else
              {
            if (results.Count() == 1) //ID ALREADY EXISTS
              throw new Exceptions.IdAlreadyExistsException(dto.Id);
            else                      //MULTIPLE IDS ALREADY EXIST??
              throw new Exceptions.VeryBadException();
              }
        }
 private void CheckValidity(MultiLineTextDto dto)
 {
     //VALIDITY
       if (dto == null)
     throw new ArgumentNullException("dto");
       if (dto.LineIds.Count < int.Parse(DalResources.MinLinesPerMultiLineText))
     throw new ArgumentOutOfRangeException("dto.LineIds.Count");
 }
        protected override MultiLineTextDto UpdateImpl(MultiLineTextDto dto)
        {
            var results = from item in SeedData.Ton.MultiLineTexts
                    where item.Id == dto.Id
                    select item;

              if (results.Count() == 1)
              {
            CheckValidity(dto);
            CheckReferentialIntegrity(dto);
            var MultiLineTextToUpdate = results.First();
            SeedData.Ton.MultiLineTexts.Remove(MultiLineTextToUpdate);
            dto.Id = Guid.NewGuid();
            SeedData.Ton.MultiLineTexts.Add(dto);
            return dto;
              }
              else
              {
            if (results.Count() == 0)
              throw new Exceptions.IdNotFoundException(dto.Id);
            else
              throw new Exceptions.VeryBadException();
              }
        }
        private static void CheckReferentialIntegrity(MultiLineTextDto dto)
        {
            //LINE IDS ARE VALID
              foreach (var id in dto.LineIds)
              {
            var count = (from p in SeedData.Ton.Lines
                     where p.Id == id
                     select p).Count();

            if (count == 1)
              continue;
            else if (count == 0)
              throw new Exceptions.IdNotFoundException(id);
            else
              throw new Exceptions.VeryBadException();
              }

              //USER IS VALID
              var userCount = (from user in SeedData.Ton.Users
                       where user.Id == dto.UserId &&
                             user.Username == dto.Username
                       select user).Count();
              if (userCount == 0)
            throw new Exceptions.IdNotFoundException(dto.UserId);
              else if (userCount != 1)
            throw new Exceptions.VeryBadException();
        }
 protected override MultiLineTextDto InsertImpl(MultiLineTextDto dto)
 {
   using (var ctx = LearnLanguagesContextManager.Instance.GetManager())
   {
     MultiLineTextData newMultiLineTextData = EfHelper.AddToContext(dto, ctx.ObjectContext);
     ctx.ObjectContext.SaveChanges();
     dto.Id = newMultiLineTextData.Id;
     return dto;
   }
 }
 //public Result<MultiLineTextDto> New(object criteria)
 //{
 //  Result<MultiLineTextDto> retResult = Result<MultiLineTextDto>.Undefined(null);
 //  try
 //  {
 //    var dto = new MultiLineTextDto()
 //    {
 //      Id = Guid.NewGuid(),
 //      LanguageId = SeedData.Ton.DefaultLanguageId
 //    };
 //    retResult = Result<MultiLineTextDto>.Success(dto);
 //  }
 //  catch (Exception ex)
 //  {
 //    retResult = Result<MultiLineTextDto>.FailureWithInfo(null, ex);
 //  }
 //  return retResult;
 //}
 //public Result<MultiLineTextDto> Fetch(Guid id)
 //{
 //  Result<MultiLineTextDto> retResult = Result<MultiLineTextDto>.Undefined(null);
 //  try
 //  {
 //    var results = from item in SeedData.Ton.MultiLineTexts
 //                  where item.Id == id
 //                  select item;
 //    if (results.Count() == 1)
 //      retResult = Result<MultiLineTextDto>.Success(results.First());
 //    else
 //    {
 //      if (results.Count() == 0)
 //        retResult = Result<MultiLineTextDto>.FailureWithInfo(null,
 //          new Exceptions.FetchFailedException(DalResources.ErrorMsgIdNotFoundException));
 //      else
 //        retResult = Result<MultiLineTextDto>.FailureWithInfo(null, new Exceptions.FetchFailedException());
 //    }
 //  }
 //  catch (Exception ex)
 //  {
 //    retResult = Result<MultiLineTextDto>.FailureWithInfo(null, ex);
 //  }
 //  return retResult;
 //}
 //public Result<MultiLineTextDto> Update(MultiLineTextDto dto)
 //{
 //  Result<MultiLineTextDto> retResult = Result<MultiLineTextDto>.Undefined(null);
 //  try
 //  {
 //    var results = from item in SeedData.Ton.MultiLineTexts
 //                  where item.Id == dto.Id
 //                  select item;
 //    if (results.Count() == 1)
 //    {
 //      var MultiLineTextToUpdate = results.First();
 //      SeedData.Ton.MultiLineTexts.Remove(MultiLineTextToUpdate);
 //      dto.Id = Guid.NewGuid();
 //      SeedData.Ton.MultiLineTexts.Add(dto);
 //      retResult = Result<MultiLineTextDto>.Success(dto);
 //    }
 //    else
 //    {
 //      if (results.Count() == 0)
 //        retResult = Result<MultiLineTextDto>.FailureWithInfo(null,
 //          new Exceptions.UpdateFailedException(DalResources.ErrorMsgIdNotFoundException));
 //      else
 //        retResult = Result<MultiLineTextDto>.FailureWithInfo(null, new Exceptions.FetchFailedException());
 //    }
 //  }
 //  catch (Exception ex)
 //  {
 //    retResult = Result<MultiLineTextDto>.FailureWithInfo(null, ex);
 //  }
 //  return retResult;
 //}
 //public Result<MultiLineTextDto> Insert(MultiLineTextDto dto)
 //{
 //  Result<MultiLineTextDto> retResult = Result<MultiLineTextDto>.Undefined(null);
 //  try
 //  {
 //    var results = from item in SeedData.Ton.MultiLineTexts
 //                  where item.Id == dto.Id
 //                  select item;
 //    if (results.Count() == 0)
 //    {
 //      dto.Id = Guid.NewGuid();
 //      //MIMIC LANGUAGEID REQUIRED CONSTRAINT IN DB
 //      if (dto.LanguageId == Guid.Empty || !SeedData.Ton.ContainsLanguageId(dto.LanguageId))
 //      {
 //        //I'VE RESTRUCTURED HOW TO DO EXCEPTIONHANDLING, SO THIS IS NOT QUITE HOW IT SHOULD BE DONE.
 //        //THIS SHOULD BE AN INSERTIMPL METHOD, AND IT SHOULD THROW ITS OWN EXCEPTION THAT IS WRAPPED IN THE
 //        //MultiLineTextDALBASE CLASS IN AN INSERTFAILEDEXCEPTION.
 //        throw new Exceptions.InsertFailedException(string.Format(DalResources.ErrorMsgIdNotFoundException, dto.LanguageId));
 //      }
 //      SeedData.Ton.MultiLineTexts.Add(dto);
 //      retResult = Result<MultiLineTextDto>.Success(dto);
 //    }
 //    else
 //    {
 //      if (results.Count() == 1) //ID ALREADY EXISTS
 //        retResult = Result<MultiLineTextDto>.FailureWithInfo(dto,
 //          new Exceptions.UpdateFailedException(DalResources.ErrorMsgIdNotFoundException));
 //      else                      //MULTIPLE IDS ALREADY EXIST??
 //        retResult = Result<MultiLineTextDto>.FailureWithInfo(null, new Exceptions.FetchFailedException());
 //    }
 //  }
 //  catch (Exception ex)
 //  {
 //    retResult = Result<MultiLineTextDto>.FailureWithInfo(null, ex);
 //  }
 //  return retResult;
 //}
 //public Result<MultiLineTextDto> Delete(Guid id)
 //{
 //  Result<MultiLineTextDto> retResult = Result<MultiLineTextDto>.Undefined(null);
 //  try
 //  {
 //    var results = from item in SeedData.Ton.MultiLineTexts
 //                  where item.Id == id
 //                  select item;
 //    if (results.Count() == 1)
 //    {
 //      var MultiLineTextToRemove = results.First();
 //      SeedData.Ton.MultiLineTexts.Remove(MultiLineTextToRemove);
 //      retResult = Result<MultiLineTextDto>.Success(MultiLineTextToRemove);
 //    }
 //    else
 //    {
 //      if (results.Count() == 0)
 //        retResult = Result<MultiLineTextDto>.FailureWithInfo(null,
 //          new Exceptions.DeleteFailedException(DalResources.ErrorMsgIdNotFoundException));
 //      else
 //        retResult = Result<MultiLineTextDto>.FailureWithInfo(null, new Exceptions.DeleteFailedException());
 //    }
 //  }
 //  catch (Exception ex)
 //  {
 //    retResult = Result<MultiLineTextDto>.FailureWithInfo(null, ex);
 //  }
 //  return retResult;
 //}
 //public LearnLanguages.Result<ICollection<MultiLineTextDto>> GetAll()
 //{
 //  Result<ICollection<MultiLineTextDto>> retResult = Result<ICollection<MultiLineTextDto>>.Undefined(null);
 //  try
 //  {
 //    var allDtos = new List<MultiLineTextDto>(SeedData.Ton.MultiLineTexts);
 //    retResult = Result<ICollection<MultiLineTextDto>>.Success(allDtos);
 //  }
 //  catch (Exception ex)
 //  {
 //    retResult = Result<ICollection<MultiLineTextDto>>.FailureWithInfo(null, ex);
 //  }
 //  return retResult;
 //}
 protected override MultiLineTextDto NewImpl(object criteria)
 {
     var dto = new MultiLineTextDto()
       {
     Id = Guid.NewGuid(),
     UserId = SeedData.Ton.GetTestValidUserDto().Id,
     Username = SeedData.Ton.TestValidUsername
       };
       return dto;
 }
    protected override MultiLineTextDto UpdateImpl(MultiLineTextDto dto)
    {
      var currentUserId = Business.BusinessHelper.GetCurrentUserId();

      using (var ctx = LearnLanguagesContextManager.Instance.GetManager())
      {
        var results = from multiLineTextData in ctx.ObjectContext.MultiLineTextDatas
                      where multiLineTextData.Id == dto.Id &&
                            multiLineTextData.UserDataId == currentUserId
                      select multiLineTextData;

        if (results.Count() == 1)
        {
          var multiLineTextData = results.First();
          EfHelper.LoadDataFromDto(ref multiLineTextData, dto, ctx.ObjectContext);
          
          ctx.ObjectContext.SaveChanges(); 

          var updatedDto = EfHelper.ToDto(multiLineTextData);
          return updatedDto;
        }
        else
        {
          if (results.Count() == 0)
            throw new Exceptions.IdNotFoundException(dto.Id);
          else
          {
            //results.count is not one or zero.  either it's negative, which would be framework absurd, or its more than one,
            //which means that we have multiple multiLineTexts with the same id.  this is very bad.
            var errorMsg = string.Format(DalResources.ErrorMsgVeryBadException,
                                         DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero);
            throw new Exceptions.VeryBadException(errorMsg);
          }
        }
      }
    }
    //public Result<MultiLineTextDto> New(object criteria)
    //{
    //  //throw new NotImplementedException("Ef.MultiLineTextDal.New(object)");
    //  Result<MultiLineTextDto> retResult = Result<MultiLineTextDto>.Undefined(null);
    //  try
    //  {
        
    //  }
    //  catch (Exception ex)
    //  {
    //    retResult = Result<MultiLineTextDto>.FailureWithInfo(null, new Exceptions.CreateFailedException(ex));
    //  }
    //  return retResult;
    //}
    //public Result<MultiLineTextDto> Fetch(Guid id)
    //{
    //  Result<MultiLineTextDto> retResult = Result<MultiLineTextDto>.Undefined(null);
    //  try
    //  {
    //    using (var ctx = LearnLanguagesContextManager.Instance.GetManager())
    //    {
    //      var results = from multiLineTextData in ctx.ObjectContext.MultiLineTextDatas
    //                    where multiLineTextData.Id == id
    //                    select multiLineTextData;

    //      if (results.Count() == 1)
    //        retResult = Result<MultiLineTextDto>.Success(EfHelper.ToDto(results.First()));
    //      else
    //      {
    //        if (results.Count() == 0)
    //          retResult = Result<MultiLineTextDto>.FailureWithInfo(null,
    //            new Exceptions.FetchFailedException(DalResources.ErrorMsgIdNotFound));
    //        else
    //          retResult = Result<MultiLineTextDto>.FailureWithInfo(null, new Exceptions.FetchFailedException());
    //      }
    //    }
    //  }
    //  catch (Exception ex)
    //  {
    //    retResult = Result<MultiLineTextDto>.FailureWithInfo(null, new Exceptions.FetchFailedException(ex));
    //  }
    //  return retResult;
    //}
    //public Result<MultiLineTextDto> Update(MultiLineTextDto dto)
    //{
    //  Result<MultiLineTextDto> retResult = Result<MultiLineTextDto>.Undefined(null);
    //  try
    //  {
    //    using (var ctx = LearnLanguagesContextManager.Instance.GetManager())
    //    {
    //      var results = from multiLineTextData in ctx.ObjectContext.MultiLineTextDatas
    //                    where multiLineTextData.Id == dto.Id
    //                    select multiLineTextData;

    //      if (results.Count() == 1)
    //      {
    //        var multiLineTextDataToUpdate = results.First();
    //        ctx.ObjectContext.MultiLineTextDatas.DeleteObject(multiLineTextDataToUpdate);
    //        var newMultiLineTextData = EfHelper.ToData(dto);
    //        ctx.ObjectContext.MultiLineTextDatas.AddObject(newMultiLineTextData);
    //        ctx.ObjectContext.SaveChanges();
    //        dto.Id = newMultiLineTextData.Id;
    //        retResult = Result<MultiLineTextDto>.Success(dto);
    //      }
    //      else
    //      {
    //        if (results.Count() == 0)
    //          retResult = Result<MultiLineTextDto>.FailureWithInfo(null,
    //            new Exceptions.UpdateFailedException(DalResources.ErrorMsgIdNotFound));
    //        else
    //          retResult = Result<MultiLineTextDto>.FailureWithInfo(null, new Exceptions.UpdateFailedException());
    //      }
    //    }
    //  }
    //  catch (Exception ex)
    //  {
    //    retResult = Result<MultiLineTextDto>.FailureWithInfo(null, new Exceptions.UpdateFailedException(ex));
    //  }
    //  return retResult;
    //}
    //public Result<MultiLineTextDto> Insert(MultiLineTextDto dto)
    //{
    //  Result<MultiLineTextDto> retResult = Result<MultiLineTextDto>.Undefined(null);
    //  try
    //  {
    //    using (var ctx = LearnLanguagesContextManager.Instance.GetManager())
    //    {
    //      var results = from multiLineTextData in ctx.ObjectContext.MultiLineTextDatas
    //                    where multiLineTextData.Id == dto.Id
    //                    select multiLineTextData;

    //      //SHOULD FIND ZERO LANGUAGEDTOS (NO DUPLICATE IDS, NO DUPLICATE DTOS)
    //      if (results.Count() == 0)
    //      {
    //        var data = EfHelper.ToData(dto);
    //        ctx.ObjectContext.MultiLineTextDatas.AddObject(data);
    //        ctx.ObjectContext.SaveChanges();
    //        dto.Id = data.Id;
    //        retResult = Result<MultiLineTextDto>.Success(dto);
    //      }
    //      else
    //      {
    //        if (results.Count() == 1) //ID ALREADY EXISTS
    //          retResult = Result<MultiLineTextDto>.FailureWithInfo(dto,
    //            new Exceptions.InsertFailedException(DalResources.ErrorMsgIdNotFound));
    //        else                      //MULTIPLE IDS ALREADY EXIST?? SHOULD NOT BE POSSIBLE
    //          retResult = Result<MultiLineTextDto>.FailureWithInfo(null, new Exceptions.InsertFailedException());
    //      }
    //    }
    //  }
    //  catch (Exception ex)
    //  {
    //    retResult = Result<MultiLineTextDto>.FailureWithInfo(null, new Exceptions.InsertFailedException(ex));
    //  }
    //  return retResult;
    //}
    //public Result<MultiLineTextDto> Delete(Guid id)
    //{
    //  Result<MultiLineTextDto> retResult = Result<MultiLineTextDto>.Undefined(null);
    //  try
    //  {
    //    using (var ctx = LearnLanguagesContextManager.Instance.GetManager())
    //    {
    //      var results = from multiLineTextData in ctx.ObjectContext.MultiLineTextDatas
    //                    where multiLineTextData.Id == id
    //                    select multiLineTextData;

    //      if (results.Count() == 1)
    //      {
    //        var multiLineTextDataToRemove = results.First();
    //        ctx.ObjectContext.MultiLineTextDatas.DeleteObject((multiLineTextDataToRemove));
    //        ctx.ObjectContext.SaveChanges();
    //        retResult = Result<MultiLineTextDto>.Success(EfHelper.ToDto(multiLineTextDataToRemove));
    //      }
    //      else
    //      {
    //        if (results.Count() == 0)
    //          retResult = Result<MultiLineTextDto>.FailureWithInfo(null,
    //            new Exceptions.DeleteFailedException(DalResources.ErrorMsgIdNotFound));
    //        else
    //          retResult = Result<MultiLineTextDto>.FailureWithInfo(null, new Exceptions.DeleteFailedException());
    //      }
    //    }
    //  }
    //  catch (Exception ex)
    //  {
    //    retResult = Result<MultiLineTextDto>.FailureWithInfo(null, new Exceptions.DeleteFailedException(ex));
    //  }
    //  return retResult;
    //}
    //public Result<ICollection<MultiLineTextDto>> GetAll()
    //{
    //  var retAllMultiLineTextDtos = new List<MultiLineTextDto>();
    //  using (var ctx = LearnLanguagesContextManager.Instance.GetManager())
    //  {
    //    var allMultiLineTextDatas = from multiLineTextData in ctx.ObjectContext.MultiLineTextDatas
    //                         select multiLineTextData;
    //    foreach (var multiLineTextData in allMultiLineTextDatas)
    //    {
    //      var multiLineTextDto = EfHelper.ToDto(multiLineTextData);
    //      retAllMultiLineTextDtos.Add(multiLineTextDto);
    //    }
    //    //var allDtos = new List<MultiLineTextDto>(ctx.ObjectContext.MultiLineTextDatas);
    //    return retAllMultiLineTextDtos;
    //  }
    //}
    protected override MultiLineTextDto NewImpl(object criteria)
    {
      var identity = (UserIdentity)Csla.ApplicationContext.User.Identity;
      string currentUsername = identity.Name;
      Guid currentUserId = Guid.Empty;
      using (var ctx = LearnLanguagesContextManager.Instance.GetManager())
      {
        currentUserId = (from user in ctx.ObjectContext.UserDatas
                         where user.Username == currentUsername
                         select user.Id).First();
      }
      //if ((criteria != null) && !(criteria is UserDto))
      //  throw new Exceptions.BadCriteriaException(DalResources.ErrorMsgBadCriteriaExceptionDetail_ExpectedTypeIsUserDto);

      MultiLineTextDto newMultiLineTextDto = new MultiLineTextDto()
      {
        Id = Guid.NewGuid(),
        Title = DalResources.DefaultNewMultiLineTextTitle,
        AdditionalMetadata = DalResources.DefaultNewMultiLineTextAdditionalMetadata,
        UserId = currentUserId,
        Username = currentUsername
      };

      return newMultiLineTextDto;
    }
Beispiel #9
0
    public static void LoadDataFromDto(ref MultiLineTextData data,
                                       MultiLineTextDto dto, 
                                       LearnLanguagesContext context)
    {
      //USER INFO
      data.UserDataId = dto.UserId;
      data.UserData = EfHelper.GetUserData(dto.UserId, context);

      //LINE IDS
      foreach (var id in dto.LineIds)
      {
        LineData lineData = EfHelper.GetLineData(id, context);
        data.LineDatas.Add(lineData);
      }

      //SCALAR
      data.Title = dto.Title;
      data.AdditionalMetadata = dto.AdditionalMetadata;
    }
Beispiel #10
0
    /// <summary>
    /// Adds the dto to the context, loading UserData and LineDatas into the newly
    /// created PhraseBeliefData.  Does NOT save changes to the context.
    /// </summary>
    public static MultiLineTextData AddToContext(MultiLineTextDto dto, LearnLanguagesContext context)
    {
      //CREATE NEW TRANSLATIONDATA
      var data = context.MultiLineTextDatas.CreateObject();

      //SCALARS
      data.Title = dto.Title;
      data.AdditionalMetadata = dto.AdditionalMetadata;

      //ASSIGN USER INFO
      data.UserDataId = dto.UserId;

      //ADD LINE DATAS FROM DTO.LINEIDS
      if (dto.LineIds != null)
      {
        foreach (var id in dto.LineIds)
        {
          var results = (from line in context.LineDatas
                         where line.Id == id
                         select line);

          if (results.Count() == 1)
          {
            var lineData = results.First();
            data.LineDatas.Add(lineData);
          }
          else if (results.Count() == 0)
            throw new Exceptions.IdNotFoundException(id);
          else
          {
            var errorMsg = string.Format(DalResources.ErrorMsgVeryBadException,
                                         DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero);
            throw new Exceptions.VeryBadException(errorMsg);
          }
        }
      }

      //ADD DATA TO CONTEXT
      context.MultiLineTextDatas.AddObject(data);

      return data;
    }
Beispiel #11
0
    public static MultiLineTextDto ToDto(MultiLineTextData data)
    {
      var dto = new MultiLineTextDto()
      {
        Id = data.Id,
        AdditionalMetadata = data.AdditionalMetadata,
        LineIds = (from line in data.LineDatas
                   select line.Id).ToList(),
        Title = data.Title,
        UserId = data.UserDataId,
        Username = data.UserData.Username,
      };

      return dto;
    }