public IQueryable <Document> Build(ObjectContext context) { var defRepo = new DocDefRepository(UserId); if (DocDefId == Guid.Empty) { DocDefId = defRepo.DocDefByName(DocDefName).Id; } var descIds = defRepo.GetDocDefDescendant(DocDefId).ToList(); return(from doc in ((cissaEntities)context).Documents where descIds.Contains(doc.Def_Id ?? Guid.Empty) && (doc.Deleted == null || doc.Deleted == false) select doc); }
public DocDef GetDocDef() { var defRepo = new DocDefRepository(UserId); DocDef docDef; if (DocDefId == Guid.Empty) { docDef = defRepo.DocDefByName(DocDefName); DocDefId = docDef.Id; } else { docDef = defRepo.DocDefById(DocDefId); } return(docDef); }
public DocDef GetDocDef(QueryDef def) { using (var defRepo = new DocDefRepository(DataContext, def.UserId)) { DocDef docDef; if (def.Source != null && def.DocDefId == Guid.Empty) { docDef = defRepo.DocDefByName(def.DocDefName); def.Source.DocDefId = docDef.Id; } else { docDef = defRepo.DocDefById(def.DocDefId); } return(docDef); } }
public override IQueryable <Document> Build(DocDef docDef, ObjectContext context, Guid userId) { var em = (cissaEntities)context; var source = BuildSource(docDef, context, userId); if (docDef.Attributes == null) { var defRepo = new DocDefRepository(userId); docDef = defRepo.DocDefById(docDef.Id); } var attr = docDef.Attributes.First(a => String.Compare(a.Name, AttributeName, true) == 0); if (Conditions.Count == 0) { if (attr.Type.Id == (short)CissaDataType.Text) { var text = Value != null?Value.ToString() : ""; switch (Condition) { case ConditionOperation.Equal: return(source.Intersect( em.Text_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value == text && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.NotEqual: return(source.Intersect( em.Text_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value != text && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.Contains: return(source.Intersect( em.Text_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value.Contains(text) && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.IsNull: return(source.Intersect( em.Text_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value == null && a.Expired >= MaxDate) .Select(a => a.Document))); default: throw new ApplicationException("Cannot apply operator for Text attribute"); } } if (attr.Type.Id == (short)CissaDataType.Int) { var val = Value != null?Convert.ToInt32(Value) : 0; switch (Condition) { case ConditionOperation.Equal: return(source.Intersect( em.Int_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value == val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.NotEqual: return(source.Intersect( em.Int_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value != val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.GreatEqual: return(source.Intersect( em.Int_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value >= val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.GreatThen: return(source.Intersect( em.Int_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value > val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.LessEqual: return(source.Intersect( em.Int_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value <= val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.LessThen: return(source.Intersect( em.Int_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value < val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.IsNotNull: return(source.Intersect( em.Int_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Expired >= MaxDate) .Select(a => a.Document))); default: throw new ApplicationException("Cannot apply operator for Int attribute"); } } if (attr.Type.Id == (short)CissaDataType.Float) { var val = Value != null?Convert.ToDouble(Value) : 0; switch (Condition) { case ConditionOperation.Equal: return(source.Intersect( em.Float_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value == val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.NotEqual: return(source.Intersect( em.Float_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value != val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.GreatEqual: return(source.Intersect( em.Float_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value >= val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.GreatThen: return(source.Intersect( em.Float_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value > val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.LessEqual: return(source.Intersect( em.Float_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value <= val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.LessThen: return(source.Intersect( em.Float_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value < val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.IsNotNull: return(source.Intersect( em.Float_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Expired >= MaxDate) .Select(a => a.Document))); default: throw new ApplicationException("Cannot apply operator for Float attribute"); } } if (attr.Type.Id == (short)CissaDataType.Currency) { var val = Value != null?Convert.ToDecimal(Value) : 0; switch (Condition) { case ConditionOperation.Equal: return(source.Intersect( em.Currency_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value == val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.NotEqual: return(source.Intersect( em.Currency_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value != val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.GreatEqual: return(source.Intersect( em.Currency_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value >= val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.GreatThen: return(source.Intersect( em.Currency_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value > val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.LessEqual: return(source.Intersect( em.Currency_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value <= val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.LessThen: return(source.Intersect( em.Currency_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value < val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.IsNotNull: return(source.Intersect( em.Currency_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Expired >= MaxDate) .Select(a => a.Document))); default: throw new ApplicationException("Cannot apply operator for Currency attribute"); } } if (attr.Type.Id == (short)CissaDataType.DateTime) { var val = Value != null?Convert.ToDateTime(Value) : DateTime.MinValue; switch (Condition) { case ConditionOperation.Equal: return(source.Intersect( em.Date_Time_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value == val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.NotEqual: return(source.Intersect( em.Date_Time_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value != val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.GreatEqual: return(source.Intersect( em.Date_Time_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value >= val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.GreatThen: return(source.Intersect( em.Date_Time_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value > val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.LessEqual: return(source.Intersect( em.Date_Time_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value <= val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.LessThen: return(source.Intersect( em.Date_Time_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value < val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.IsNotNull: return(source.Intersect( em.Date_Time_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Expired >= MaxDate) .Select(a => a.Document))); default: throw new ApplicationException("Cannot apply operator for DateTime attribute"); } } if (attr.Type.Id == (short)CissaDataType.Bool) { var val = Value != null?Convert.ToBoolean(Value) : false; switch (Condition) { case ConditionOperation.Equal: return(source.Intersect( em.Boolean_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value == val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.NotEqual: return(source.Intersect( em.Boolean_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value != val && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.IsNotNull: return(source.Intersect( em.Boolean_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Expired >= MaxDate) .Select(a => a.Document))); default: throw new ApplicationException("Cannot apply operator for Bool attribute"); } } if (attr.Type.Id == (short)CissaDataType.Enum) { var val = Value != null?Value.ToString() : ""; Guid enumId; if (!Guid.TryParse(val, out enumId)) { var enumRepo = new EnumRepository(); enumId = enumRepo.GetEnumValueId(attr.EnumDefType.Id, val); } switch (Condition) { case ConditionOperation.Equal: return(source.Intersect( em.Enum_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value == enumId && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.NotEqual: return(source.Intersect( em.Enum_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value != enumId && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.IsNotNull: return(source.Intersect( em.Enum_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Expired >= MaxDate) .Select(a => a.Document))); default: throw new ApplicationException("Cannot apply operator for Enum attribute"); } } if (attr.Type.Id == (short)CissaDataType.Doc) { var val = Value != null?Value.ToString() : ""; var docId = Guid.Empty; var hasDocId = Guid.TryParse(val, out docId); if (Condition == ConditionOperation.Is) { DocDef isDocDef = null; var defRepo = new DocDefRepository(); isDocDef = hasDocId ? defRepo.DocDefById(docId) : defRepo.DocDefByName(val); var descendants = defRepo.GetDocDefDescendant(isDocDef.Id); var docs = em.Documents.Where( d => d.Def_Id != null && descendants.Contains(d.Def_Id ?? Guid.Empty) && (d.Deleted == null || d.Deleted == false)).Select(d => d.Id); return(source.Intersect( em.Document_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && docs.Contains(a.Value) && a.Expired >= MaxDate) .Select(a => a.Document))); } if (!hasDocId) { throw new ApplicationException("Invalid argument type for Document attribute"); } switch (Condition) { case ConditionOperation.Equal: return(source.Intersect( em.Document_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value == docId && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.NotEqual: return(source.Intersect( em.Document_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Value != docId && a.Expired >= MaxDate) .Select(a => a.Document))); case ConditionOperation.IsNotNull: return(source.Intersect( em.Document_Attributes.Include("Documents") .Where(a => a.Def_Id == attr.Id && a.Expired >= MaxDate) .Select(a => a.Document))); default: throw new ApplicationException("Cannot apply operator for Document attribute"); } } throw new ApplicationException("Не могу сформировать запрос. Данный тип не поддерживается!"); } if (attr.Type.Id != (short)CissaDataType.Doc && attr.Type.Id != (short)CissaDataType.DocList) { throw new ApplicationException("Ошибка в запросе! Атрибут не является документным."); } foreach (var condition in Conditions) { var sq = condition.Build(attr.DocDefType, context, userId); var sub = attr.Type.Id == (short)CissaDataType.DocList ? em.DocumentList_Attributes.Include("Documents") .Where(a => sq.Select(d => d.Id).Contains(a.Value) && a.Expired >= MaxDate) .Select(a => a.Document) : em.Document_Attributes.Include("Documents") .Where(a => sq.Select(d => d.Id).Contains(a.Value) && a.Expired >= MaxDate) .Select(a => a.Document); source = source.Intersect(sub); } return(source); }