bvcms /CmsData/QueryFunctions.cs

Language C# Lines 316
MD5 Hash a200bba4335cd09974b847694b17aa68 Estimated Cost $6,540 (why?)
Repository https://bitbucket.org/mahalowe/bvcms View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 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
 97
 98
 99
100
101
102
103
104
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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UtilityExtensions;
using System.Text.RegularExpressions;
using System.Data.Linq;
using System.Xml.Linq;
using System.Data.Linq.SqlClient;
using IronPython.Hosting;
using System.IO;
using CmsData.Codes;
using System.Web;
using CmsData.API;

namespace CmsData
{
    public class QueryFunctions
    {
        private CMSDataContext Db;

        public QueryFunctions()
        {
            Db = new CMSDataContext("Data Source=.;Initial Catalog=CMS_bellevue;Integrated Security=True");
        }
        public QueryFunctions(CMSDataContext Db)
        {
            this.Db = Db;
        }
        public static string VitalStats(CMSDataContext Db)
        {
            var qf = new QueryFunctions(Db);
            var script = Db.Content("VitalStats");
            if (script == null)
                return "no VitalStats script";
#if DEBUG2
            var options = new Dictionary<string, object>();
            options["Debug"] = true;
            var engine = Python.CreateEngine(options);
            var paths = engine.GetSearchPaths();
            paths.Add(path);
            engine.SetSearchPaths(paths);
            var sc = engine.CreateScriptSourceFromFile(HttpContext.Current.Server.MapPath("/MembershipAutomation2.py"));
#else
            var engine = Python.CreateEngine();
            var sc = engine.CreateScriptSourceFromString(script.Body);
#endif

            try
            {
                var code = sc.Compile();
                var scope = engine.CreateScope();
                code.Execute(scope);

                dynamic VitalStats = scope.GetVariable("VitalStats");
                dynamic m = VitalStats();
                return m.Run(qf);
            }
            catch (Exception ex)
            {
                return "VitalStats script error: " + ex.Message;
            }
        }
        public int MeetingCount(int days, int progid, int divid, int orgid)
        {
            var dt = DateTime.Now.AddDays(-days);
            var q = from m in Db.Meetings
                    where m.MeetingDate >= dt
                    where orgid == 0 || m.OrganizationId == orgid
                    where divid == 0 || m.Organization.DivOrgs.Any(t => t.DivId == divid)
                    where progid == 0 || m.Organization.DivOrgs.Any(t => t.Division.ProgDivs.Any(d => d.ProgId == progid))
                    select m;
            return q.Count();
        }
        public int NumPresent(int days, int progid, int divid, int orgid)
        {
            var dt = DateTime.Now.AddDays(-days);
            var q = from m in Db.Meetings
                    where m.MeetingDate >= dt
                    where orgid == 0 || m.OrganizationId == orgid
                    where divid == 0 || m.Organization.DivOrgs.Any(t => t.DivId == divid)
                    where progid == 0 || m.Organization.DivOrgs.Any(t => t.Division.ProgDivs.Any(d => d.ProgId == progid))
                    select m;
			if (!q.Any())
				return 0;
			return q.Sum(mm => mm.NumPresent);
        }

        public int RegistrationCount(int days, int progid, int divid, int orgid)
        {
            var dt = DateTime.Now.AddDays(-days);
            var q = from m in Db.OrganizationMembers
                    where m.EnrollmentDate >= dt
                    where m.Organization.RegistrationTypeId > 0
                    where orgid == 0 || m.OrganizationId == orgid
                    where divid == 0 || m.Organization.DivOrgs.Any(t => t.DivId == divid)
                    where progid == 0 || m.Organization.DivOrgs.Any(t => t.Division.ProgDivs.Any(d => d.ProgId == progid))
                    select m;
            return q.Count();
        }
        public decimal ContributionTotals(int days1, int days2, int fundid)
        {
            return ContributionTotals(days1, days2, fundid.ToString());
        }

        public int ContributionCount(int days1, int days2, int fundid)
        {
            return ContributionCount(days1, days2, fundid.ToString());
        }

        public int ContributionCount(int days, int fundid)
        {
            return ContributionCount(days, fundid.ToString());
        }
        public int QueryCount(string s)
        {
            var qB = Db.QueryBuilderClauses.FirstOrDefault(c => c.Description == s);
            if (qB == null)
                return 0;
            var q = Db.People.Where(qB.Predicate(Db));
            return q.Count();
        }
        public decimal ContributionTotals(int days1, int days2, string funds)
        {
            var fundids = (from f in funds.Split(',')
                           let i = f.ToInt()
                           where i > 0
                           select i).ToArray();
            var exfundids = (from f in funds.Split(',')
                             let i = f.ToInt()
                             where i < 0
                             select -i).ToArray();

            var dt1 = DateTime.Now.AddDays(-days1);
            var dt2 = DateTime.Now.AddDays(-days2);
            var typs = new int[] { 6, 7 };
            var q = from c in Db.Contributions
                    where c.ContributionDate >= dt1
                    where days2 == 0 || c.ContributionDate <= dt2
                    where c.ContributionTypeId != ContributionTypeCode.Pledge
                    where fundids.Length == 0 || fundids.Contains(c.FundId)
                    where exfundids.Length == 0 || !exfundids.Contains(c.FundId)
                    where !typs.Contains(c.ContributionTypeId)
                    select c;
            return q.Sum(c => c.ContributionAmount) ?? 0;
        }

        public int ContributionCount(int days1, int days2, string funds)
        {
            var fundids = (from f in funds.Split(',')
                           let i = f.ToInt()
                           where i > 0
                           select i).ToArray();
            var exfundids = (from f in funds.Split(',')
                             let i = f.ToInt()
                             where i < 0
                             select -i).ToArray();

            var dt1 = DateTime.Now.AddDays(-days1);
            var dt2 = DateTime.Now.AddDays(-days2);
            var typs = new int[] { 6, 7 };
            var q = from c in Db.Contributions
                    where c.ContributionDate >= dt1
                    where days2 == 0 || c.ContributionDate <= dt2
                    where c.ContributionTypeId != ContributionTypeCode.Pledge
                    where c.ContributionAmount > 0
                    where fundids.Length == 0 || fundids.Contains(c.FundId)
                    where exfundids.Length == 0 || !exfundids.Contains(c.FundId)
                    where !typs.Contains(c.ContributionTypeId)
                    select c;
            return q.Count();
        }

        public int ContributionCount(int days, string funds)
        {
            var fundids = (from f in funds.Split(',')
                           let i = f.ToInt()
                           where i > 0
                           select i).ToArray();
            var exfundids = (from f in funds.Split(',')
                             let i = f.ToInt()
                             where i < 0
                             select -i).ToArray();

            var dt = DateTime.Now.AddDays(-days);
            var typs = new int[] { 6, 7 };
            var q = from c in Db.Contributions
                    where c.ContributionDate >= dt
                    where c.ContributionTypeId != ContributionTypeCode.Pledge
                    where c.ContributionAmount > 0
                    where fundids.Length == 0 || fundids.Contains(c.FundId)
                    where exfundids.Length == 0 || !exfundids.Contains(c.FundId)
                    where !typs.Contains(c.ContributionTypeId)
                    select c;
            return q.Count();
        }
        public static int Import(CMSDataContext Db, string text, string name)
		{
			var x = XDocument.Parse(text);
			QueryBuilderClause c = null;
			foreach (var xc in x.Root.Elements())
			{
				if (name.HasValue())
					c = InsertClause(Db, xc, null, name);
				else
					c = InsertClause(Db, xc, null, null);
			}
			return c.QueryId;
		}
        public string Export(int id, string name)
        {
            var clause = Db.LoadQueryById(id);
            var w = new APIWriter();
            w.Start("Search");
			w.Attr("Description", name);

            //var settings = new XmlWriterSettings();
            //settings.Encoding = new System.Text.UTF8Encoding(false);
            //using (w = XmlWriter.Create(context.HttpContext.Response.OutputStream, settings))
            //{
            //    w.WriteStartElement("Search");
            //    WriteClause(clause);
            //    w.WriteEndElement();
            //}
			return "";
        }
        private void WriteClause(QueryBuilderClause clause, API.APIWriter w)
        {
            //w.WriteStartElement("Condition");
            //w.WriteAttributeString("ClauseOrder", clause.ClauseOrder.ToString());
            //w.WriteAttributeString("Field", clause.Field);
            //if (clause.Description.HasValue())
            //    w.WriteAttributeString("Description", clause.Description);
            //w.WriteAttributeString("Comparison", clause.Comparison);
            //if (clause.TextValue.HasValue())
            //    w.WriteAttributeString("TextValue", clause.TextValue);
            //if (clause.DateValue.HasValue)
            //    w.WriteAttributeString("DateValue", clause.DateValue.ToString());
            //if (clause.CodeIdValue.HasValue())
            //    w.WriteAttributeString("CodeIdValue", clause.CodeIdValue);
            //if (clause.StartDate.HasValue)
            //    w.WriteAttributeString("StartDate", clause.StartDate.ToString());
            //if (clause.EndDate.HasValue)
            //    w.WriteAttributeString("EndDate", clause.EndDate.ToString());
            //if (clause.Program > 0)
            //    w.WriteAttributeString("Program", clause.Program.ToString());
            //if (clause.Division > 0)
            //    w.WriteAttributeString("Division", clause.Division.ToString());
            //if (clause.Organization > 0)
            //    w.WriteAttributeString("Organization", clause.Organization.ToString());
            //if (clause.Days > 0)
            //    w.WriteAttributeString("Days", clause.Days.ToString());
            //if (clause.Quarters.HasValue())
            //    w.WriteAttributeString("Quarters", clause.Quarters);
            //if (clause.Tags.HasValue())
            //    w.WriteAttributeString("Tags", clause.Tags);
            //if (clause.Schedule > 0)
            //    w.WriteAttributeString("Schedule", clause.Schedule.ToString());
            //if (clause.Age.HasValue)
            //    w.WriteAttributeString("Age", clause.Age.ToString());
            //foreach (var qc in clause.Clauses)
            //    WriteClause(qc);
            //w.WriteEndElement();
        }
        private static QueryBuilderClause InsertClause(CMSDataContext Db, XElement r, int? parent, string name=null)
        {
            var c = new QueryBuilderClause
            {
                Field = Attribute(r, "Field"),
                GroupId = parent,
                ClauseOrder = Attribute(r, "ClauseOrder").ToInt(),
                Comparison = Attribute(r, "Comparison"),
                TextValue = Attribute(r, "TextValue"),
                DateValue = AttributeDate(r, "DateValue"),
                CodeIdValue = Attribute(r, "CodeIdValue"),
                StartDate = AttributeDate(r, "StartDate"),
                EndDate = AttributeDate(r, "EndDate"),
                Program = Attribute(r, "Program").ToInt(),
                Division = Attribute(r, "Division").ToInt(),
                Organization = Attribute(r, "Organization").ToInt(),
                Days = Attribute(r, "Days").ToInt(),
                Quarters = Attribute(r, "Quarters"),
                Tags = Attribute(r, "Tags"),
                Schedule = Attribute(r, "Schedule").ToInt(),
                Age = Attribute(r, "Age").ToInt(),
                Description = name,
                SavedBy = Util.UserName
            };
            Db.QueryBuilderClauses.InsertOnSubmit(c);
            Db.SubmitChanges();
            if(c.Field == "Group")
                foreach (var rr in r.Elements())
                    InsertClause(Db, rr, c.QueryId);
            return c;
        }
        private static string Attribute(XElement r, string attr)
        {
            return Attribute(r, attr, null);
        }
        private static string Attribute(XElement r, string attr, string def)
        {
            var a = r.Attributes(attr).FirstOrDefault();
            if (a == null)
                return def;
            return a.Value;
        }
        private static DateTime? AttributeDate(XElement r, string attr)
        {
            var a = r.Attributes(attr).FirstOrDefault();
            if (a == null)
                return null;
            return a.Value.ToDate();
        }
    }
}
Back to Top