public static List <Field> GetUserRichTextFields(this FieldCollection fields)
 {
     if (fields == null)
     {
         throw new ArgumentNullException("fields");
     }
     return(FieldUtil.GetUserRichTextFields(fields));
 }
        /// <summary>
        ///     Determines whether a Field is a Rich Text field
        /// </summary>
        /// <param name = "field">The field.</param>
        /// <returns>
        ///     <c>true</c> if the field is a rich text field; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsRichText(this Field field)
        {
            if (field == null)
            {
                throw new ArgumentNullException("field");
            }

            FieldUtil.FieldType fieldType = FieldUtil.GetFieldType(field.Type);
            return(fieldType == FieldUtil.FieldType.RichText);
        }
예제 #3
0
        private static void ApplyAggregation(Facet facet, FacetValue value, ArraySegment <int> docsInQuery, IndexReader indexReader, int docBase, IState state)
        {
            var name      = facet.AggregationField;
            var rangeType = FieldUtil.GetRangeTypeFromFieldName(name);

            if (rangeType == RangeType.None)
            {
                name      = FieldUtil.ApplyRangeSuffixIfNecessary(facet.AggregationField, RangeType.Double);
                rangeType = RangeType.Double;
            }

            long[]   longs   = null;
            double[] doubles = null;
            switch (rangeType)
            {
            case RangeType.Long:
                longs = FieldCache_Fields.DEFAULT.GetLongs(indexReader, name, state);
                break;

            case RangeType.Double:
                doubles = FieldCache_Fields.DEFAULT.GetDoubles(indexReader, name, state);
                break;

            default:
                throw new InvalidOperationException("Invalid range type for " + facet.Name + ", don't know how to handle " + rangeType);
            }

            for (int index = 0; index < docsInQuery.Count; index++)
            {
                var doc = docsInQuery.Array[index];

                var currentVal = rangeType == RangeType.Long ? longs[doc - docBase] : doubles[doc - docBase];
                if ((facet.Aggregation & FacetAggregation.Max) == FacetAggregation.Max)
                {
                    value.Max = Math.Max(value.Max ?? double.MinValue, currentVal);
                }

                if ((facet.Aggregation & FacetAggregation.Min) == FacetAggregation.Min)
                {
                    value.Min = Math.Min(value.Min ?? double.MaxValue, currentVal);
                }

                if ((facet.Aggregation & FacetAggregation.Sum) == FacetAggregation.Sum)
                {
                    value.Sum = currentVal + (value.Sum ?? 0d);
                }

                if ((facet.Aggregation & FacetAggregation.Average) == FacetAggregation.Average)
                {
                    value.Average = currentVal + (value.Average ?? 0d);
                }
            }
        }
예제 #4
0
        public static Dictionary <string, FacetResult> Parse(IReadOnlyList <Facet> facets, out Dictionary <string, Facet> defaultFacets, out Dictionary <string, List <ParsedRange> > rangeFacets)
        {
            var results = new Dictionary <string, FacetResult>();

            defaultFacets = new Dictionary <string, Facet>();
            rangeFacets   = new Dictionary <string, List <ParsedRange> >();
            foreach (var facet in facets)
            {
                var key = string.IsNullOrWhiteSpace(facet.DisplayName) ? facet.Name : facet.DisplayName;

                defaultFacets[key] = facet;
                if (facet.Aggregation != FacetAggregation.Count && facet.Aggregation != FacetAggregation.None)
                {
                    if (string.IsNullOrEmpty(facet.AggregationField))
                    {
                        throw new InvalidOperationException($"Facet {facet.Name} cannot have aggregation set to {facet.Aggregation} without having a value in AggregationField");
                    }

                    if (facet.AggregationField.EndsWith(Constants.Documents.Indexing.Fields.RangeFieldSuffix) == false)
                    {
                        var rangeType = FacetedQueryHelper.GetRangeTypeForAggregationType(facet.AggregationType);
                        facet.AggregationField = FieldUtil.ApplyRangeSuffixIfNecessary(facet.AggregationField, rangeType);
                    }
                }

                switch (facet.Mode)
                {
                case FacetMode.Default:
                    results[key] = new FacetResult();
                    break;

                case FacetMode.Ranges:
                    rangeFacets[key] = facet.Ranges
                                       .Select(range => ParseRange(facet.Name, range))
                                       .ToList();

                    results[key] = new FacetResult
                    {
                        Values = facet.Ranges
                                 .Select(range => new FacetValue {
                            Range = range
                        })
                                 .ToList()
                    };
                    break;

                default:
                    throw new ArgumentException(string.Format("Could not understand '{0}'", facet.Mode));
                }
            }

            return(results);
        }
예제 #5
0
        public ExportTable Export()
        {
            // 取得匯出規則描述
            XmlElement        descElement      = TeacherBulkProcess.GetExportDescription();
            IFieldFormater    fieldFormater    = new BaseFieldFormater();
            IResponseFormater responseFormater = new ResponseFormater();

            FieldCollection       fieldCollection = fieldFormater.Format(descElement);
            ExportFieldCollection exportFields    = responseFormater.Format(descElement);

            fieldCollection = FieldUtil.Match(fieldCollection, _selectFields);
            exportFields    = FieldUtil.Match(exportFields, _selectFields);

            IRequestGenerator reqGenerator = new ExportStudentRequestGenerator();

            reqGenerator.SetSelectedFields(_selectFields);

            ICondition condition = new BaseCondition("ID", "-1");

            reqGenerator.AddCondition(condition);
            foreach (string id in _conditions)
            {
                ICondition condition2 = new BaseCondition("ID", id);
                reqGenerator.AddCondition(condition2);
            }

            DSRequest  request  = reqGenerator.Generate();
            DSResponse response = TeacherBulkProcess.GetExportList(request);

            ExportTable table = new ExportTable();

            foreach (ExportField field in exportFields)
            {
                table.AddColumn(field);
            }

            foreach (XmlElement record in response.GetContent().GetElements("Teacher"))
            {
                ExportRow row = table.AddRow();
                foreach (ExportField column in table.Columns)
                {
                    int        columnIndex = column.ColumnIndex;
                    ExportCell cell        = row.Cells[columnIndex];
                    XmlNode    cellNode    = record.SelectSingleNode(column.XPath);
                    if (cellNode != null)
                    {
                        cell.Value = cellNode.InnerText;
                    }
                }
            }
            return(table);
        }
예제 #6
0
        private static void ApplyAggregation(Dictionary <FacetAggregationField, FacetedQueryParser.FacetResult.Aggregation> aggregations, FacetValues values, ArraySegment <int> docsInQuery, IndexReader indexReader, int docBase, IState state)
        {
            foreach (var kvp in aggregations)
            {
                if (string.IsNullOrEmpty(kvp.Key.Name)) // Count
                {
                    continue;
                }

                var value = values.Get(kvp.Key);

                var name    = FieldUtil.ApplyRangeSuffixIfNecessary(kvp.Key.Name, RangeType.Double);
                var doubles = FieldCache_Fields.DEFAULT.GetDoubles(indexReader, name, state);

                var    val = kvp.Value;
                double min = value.Min ?? double.MaxValue, max = value.Max ?? double.MinValue, sum = value.Sum ?? 0, avg = value.Average ?? 0;
                int[]  array = docsInQuery.Array;
                for (var index = 0; index < docsInQuery.Count; index++)
                {
                    var doc        = array[index];
                    var currentVal = doubles[doc - docBase];
                    sum += currentVal;
                    avg += currentVal;
                    min  = Math.Min(min, currentVal);
                    max  = Math.Max(max, currentVal);
                }

                if (val.Min)
                {
                    value.Min = min;
                }

                if (val.Average)
                {
                    value.Average = avg;
                }

                if (val.Max)
                {
                    value.Max = max;
                }

                if (val.Sum)
                {
                    value.Sum = sum;
                }
            }
        }
예제 #7
0
        public static string GetRangeName(string field, string text)
        {
            var rangeType = FieldUtil.GetRangeTypeFromFieldName(field);

            switch (rangeType)
            {
            case RangeType.Long:
                return(NumericUtils.PrefixCodedToLong(text).ToInvariantString());

            case RangeType.Double:
                return(NumericUtils.PrefixCodedToDouble(text).ToInvariantString());

            default:
                return(text);
            }
        }
예제 #8
0
        private void HandleRangeFacets(KeyValuePair <string, FacetedQueryParser.FacetResult> result, List <ReaderFacetInfo> returnedReaders, CancellationToken token)
        {
            var needToApplyAggregation = result.Value.Aggregations.Count > 0;

            foreach (var readerFacetInfo in returnedReaders)
            {
                var name          = FieldUtil.ApplyRangeSuffixIfNecessary(result.Value.AggregateBy, result.Value.RangeType);
                var termsForField = IndexedTerms.GetTermsAndDocumentsFor(readerFacetInfo.Reader, readerFacetInfo.DocBase, name, _indexName, _state);

                var ranges = result.Value.Ranges;
                foreach (var kvp in termsForField)
                {
                    token.ThrowIfCancellationRequested();

                    for (int i = 0; i < ranges.Count; i++)
                    {
                        var parsedRange = ranges[i];
                        if (parsedRange.IsMatch(kvp.Key))
                        {
                            var facetValue = result.Value.Result.Values[i];

                            var intersectedDocuments = GetIntersectedDocuments(new ArraySegment <int>(kvp.Value), readerFacetInfo.Results, needToApplyAggregation);
                            var intersectCount       = intersectedDocuments.Count;
                            if (intersectCount == 0)
                            {
                                continue;
                            }

                            facetValue.Count += intersectCount;

                            if (needToApplyAggregation)
                            {
                                var docsInQuery = new ArraySegment <int>(intersectedDocuments.Documents, 0, intersectedDocuments.Count);
                                ApplyAggregation(result.Value.Aggregations, facetValue, docsInQuery, readerFacetInfo.Reader, readerFacetInfo.DocBase, _state);
                                IntArraysPool.Instance.FreeArray(intersectedDocuments.Documents);
                                intersectedDocuments.Documents = null;
                            }
                        }
                    }
                }
            }
        }
예제 #9
0
        private static string ConvertFieldValue(string field, string value)
        {
            if (NumberUtil.IsNull(value))
            {
                return(null);
            }

            var rangeType = FieldUtil.GetRangeTypeFromFieldName(field);

            switch (rangeType)
            {
            case RangeType.Long:
                var longValue = NumberUtil.StringToLong(value);
                return(NumericUtils.LongToPrefixCoded(longValue.Value));

            case RangeType.Double:
                var doubleValue = NumberUtil.StringToDouble(value);
                return(NumericUtils.DoubleToPrefixCoded(doubleValue.Value));

            default:
                return(value);
            }
        }
예제 #10
0
 public LocalizadoValidationAttribute(string pattern, Type ResourceType)
     : base(pattern)
 {
     pattern = FieldUtil.GetResource(pattern, ResourceType);
 }
예제 #11
0
        protected Document GetProjection(Lucene.Net.Documents.Document input, float score, string lowerId, IState state)
        {
            Document doc = null;

            if (FieldsToFetch.AnyExtractableFromIndex == false)
            {
                doc = DirectGet(input, lowerId, state);

                if (doc == null)
                {
                    return(null);
                }

                return(GetProjectionFromDocument(doc, input, score, FieldsToFetch, _context, state));
            }

            var documentLoaded = false;

            var result = new DynamicJsonValue();

            Dictionary <string, FieldsToFetch.FieldToFetch> fields = null;

            if (FieldsToFetch.ExtractAllFromIndex)
            {
                fields = input.GetFields()
                         .Where(x => x.Name != Constants.Documents.Indexing.Fields.DocumentIdFieldName &&
                                x.Name != Constants.Documents.Indexing.Fields.ReduceKeyHashFieldName &&
                                x.Name != Constants.Documents.Indexing.Fields.ReduceKeyValueFieldName &&
                                FieldUtil.GetRangeTypeFromFieldName(x.Name) == RangeType.None)
                         .Distinct(UniqueFieldNames.Instance)
                         .ToDictionary(x => x.Name, x => new FieldsToFetch.FieldToFetch(x.Name, null, null, x.IsStored, isDocumentId: false));
            }

            if (fields == null)
            {
                fields = FieldsToFetch.Fields;
            }
            else if (FieldsToFetch.Fields != null && FieldsToFetch.Fields.Count > 0)
            {
                foreach (var kvp in FieldsToFetch.Fields)
                {
                    if (fields.ContainsKey(kvp.Key))
                    {
                        continue;
                    }

                    fields[kvp.Key] = kvp.Value;
                }
            }

            foreach (var fieldToFetch in fields.Values)
            {
                if (TryExtractValueFromIndex(fieldToFetch, input, result, state))
                {
                    continue;
                }

                if (documentLoaded == false)
                {
                    doc = DirectGet(input, lowerId, state);

                    documentLoaded = true;
                }

                if (doc == null)
                {
                    continue;
                }

                if (TryGetValue(fieldToFetch, doc, input, state, out var fieldVal))
                {
                    if (FieldsToFetch.SingleBodyOrMethodWithNoAlias)
                    {
                        if (fieldVal is BlittableJsonReaderObject nested)
                        {
                            doc.Data = nested;
                        }
                        else if (fieldVal is Document d)
                        {
                            doc = d;
                        }
                        else
                        {
                            ThrowInvalidQueryBodyResponse(fieldVal);
                        }
                        doc.IndexScore = score;
                        return(doc);
                    }
                    if (fieldVal is List <object> list)
                    {
                        fieldVal = new DynamicJsonArray(list);
                    }
                    result[fieldToFetch.ProjectedName ?? fieldToFetch.Name.Value] = fieldVal;
                }
            }

            if (doc == null)
            {
                doc = new Document
                {
                    Id = _context.GetLazyString(lowerId)
                };
            }

            return(ReturnProjection(result, doc, score, _context));
        }
예제 #12
0
        private void HandleRangeFacets(
            List <ReaderFacetInfo> returnedReaders,
            KeyValuePair <string, FacetedQueryParser.FacetResult> result,
            bool legacy, CancellationToken token)
        {
            var needToApplyAggregation = result.Value.Aggregations.Count > 0;
            var facetValues            = new Dictionary <string, FacetValues>();

            var ranges = result.Value.Ranges;

            foreach (var range in ranges)
            {
                var key = range.RangeText;
                if (facetValues.TryGetValue(key, out var collectionOfFacetValues))
                {
                    continue;
                }

                collectionOfFacetValues = new FacetValues(legacy);
                if (needToApplyAggregation == false)
                {
                    collectionOfFacetValues.AddDefault(key);
                }
                else
                {
                    foreach (var aggregation in result.Value.Aggregations)
                    {
                        collectionOfFacetValues.Add(aggregation.Key, key);
                    }
                }

                facetValues.Add(key, collectionOfFacetValues);
            }

            foreach (var readerFacetInfo in returnedReaders)
            {
                var name          = FieldUtil.ApplyRangeSuffixIfNecessary(result.Value.AggregateBy, result.Value.RangeType);
                var termsForField = IndexedTerms.GetTermsAndDocumentsFor(readerFacetInfo.Reader, readerFacetInfo.DocBase, name, _indexName, _state);

                foreach (var kvp in termsForField)
                {
                    foreach (var range in ranges)
                    {
                        token.ThrowIfCancellationRequested();

                        if (range.IsMatch(kvp.Key) == false)
                        {
                            continue;
                        }

                        var intersectedDocuments = GetIntersectedDocuments(new ArraySegment <int>(kvp.Value), readerFacetInfo.Results, needToApplyAggregation);
                        var intersectCount       = intersectedDocuments.Count;
                        if (intersectCount == 0)
                        {
                            continue;
                        }

                        var collectionOfFacetValues = facetValues[range.RangeText];
                        collectionOfFacetValues.IncrementCount(intersectCount);

                        if (needToApplyAggregation)
                        {
                            var docsInQuery = new ArraySegment <int>(intersectedDocuments.Documents, 0, intersectedDocuments.Count);
                            ApplyAggregation(result.Value.Aggregations, collectionOfFacetValues, docsInQuery, readerFacetInfo.Reader, readerFacetInfo.DocBase, _state);
                            IntArraysPool.Instance.FreeArray(intersectedDocuments.Documents);
                            intersectedDocuments.Documents = null;
                        }
                    }
                }
            }

            foreach (var kvp in facetValues)
            {
                if (kvp.Value.Any == false)
                {
                    continue;
                }

                result.Value.Result.Values.AddRange(kvp.Value.GetAll());
            }
        }
예제 #13
0
        public ExportTable Export()
        {
            // 取得縣市對照表
            XmlElement schoolLocationList = Config.GetSchoolLocationList().GetContent().BaseElement;

            // 取得匯出規則描述
            XmlElement        descElement      = StudentBulkProcess.GetExportDescription();
            IFieldFormater    fieldFormater    = new BaseFieldFormater();
            IResponseFormater responseFormater = new ResponseFormater();

            FieldCollection       fieldCollection = fieldFormater.Format(descElement);
            ExportFieldCollection exportFields    = responseFormater.Format(descElement);



            fieldCollection = FieldUtil.Match(fieldCollection, _selectFields);
            exportFields    = FieldUtil.Match(exportFields, _selectFields);

            //// 有選狀態時加入
            //if (_selectFields.FindByDisplayText("狀態") != null)
            //{
            //    fieldCollection.Add(_selectFields.FindByDisplayText("狀態"));
            //    ExportField ex = new ExportField();
            //    ex.DisplayText = "狀態";
            //    ex.RequestName = "StudentStatus";
            //    ex.ColumnIndex = exportFields.Length;
            //    ex.DataType = "";
            //    ex.XPath = "";
            //    exportFields.Add(ex);

            //}


            IRequestGenerator reqGenerator = new ExportStudentRequestGenerator();

            _selectFieldsID = new FieldCollection();
            foreach (Field fd in _selectFields)
            {
                _selectFieldsID.Add(fd);
            }

            if (_selectFieldsID.Find("StudentID") == null)
            {
                Field fd1 = new Field();
                fd1.FieldName   = "StudentID";
                fd1.DisplayText = "學生系統編號";
                _selectFieldsID.Add(fd1);
            }
            reqGenerator.SetSelectedFields(_selectFieldsID);

            // 預設找-1, 不然會傳回所有學生
            ICondition condition = new BaseCondition("ID", "-1");

            reqGenerator.AddCondition(condition);
            foreach (string id in _conditions)
            {
                ICondition condition2 = new BaseCondition("ID", id);
                reqGenerator.AddCondition(condition2);
            }

            reqGenerator.AddOrder(new Order("GradeYear"));
            reqGenerator.AddOrder(new Order("Department"));
            reqGenerator.AddOrder(new Order("RefClassID"));
            reqGenerator.AddOrder(new Order("SeatNo"));

            DSRequest  request  = reqGenerator.Generate();
            DSResponse response = QueryStudent.GetExportList(request);

            ExportTable table = new ExportTable();



            foreach (ExportField field in exportFields)
            {
                table.AddColumn(field);
            }

            //// 取得學生狀態
            //Dictionary<string, string> StudStatusDic = new Dictionary<string, string>();
            //foreach (JHSchool.Data.JHStudentRecord stud in JHSchool.Data.JHStudent.SelectByIDs(K12.Presentation.NLDPanels.Student.SelectedSource ))
            //    StudStatusDic.Add(stud.ID, stud.Status.ToString());

            foreach (XmlElement record in response.GetContent().GetElements("Student"))
            {
                ExportRow row = table.AddRow();
                foreach (ExportField column in table.Columns)
                {
                    int        columnIndex = column.ColumnIndex;
                    ExportCell cell        = row.Cells[columnIndex];

                    XmlNode cellNode = record.SelectSingleNode(column.XPath);

                    //if(column.DisplayText !="狀態")
                    //    cellNode = record.SelectSingleNode(column.XPath);
                    // CustodianOtherInfo/CustodianOtherInfo[1]/EducationDegree[1]

                    #region 這段程式是處理匯入/匯出程式不一致問題
                    if (column.XPath.StartsWith("CustodianOtherInfo/Custodian"))
                    {
                        if (cellNode == null)
                        {
                            string x = column.XPath.Replace("CustodianOtherInfo/Custodian", "CustodianOtherInfo/CustodianOtherInfo");
                            cellNode = record.SelectSingleNode(x);
                            if (cellNode == null)
                            {
                                x        = column.XPath.Replace("CustodianOtherInfo/CustodianOtherInfo", "CustodianOtherInfo/Custodian");
                                cellNode = record.SelectSingleNode(x);
                            }
                        }
                    }
                    if (column.XPath.StartsWith("FatherOtherInfo/Father"))
                    {
                        if (cellNode == null)
                        {
                            string x = column.XPath.Replace("FatherOtherInfo/Father", "FatherOtherInfo/FatherOtherInfo");
                            cellNode = record.SelectSingleNode(x);
                            if (cellNode == null)
                            {
                                x        = column.XPath.Replace("FatherOtherInfo/FatherOtherInfo", "FatherOtherInfo/Father");
                                cellNode = record.SelectSingleNode(x);
                            }
                        }
                    }
                    if (column.XPath.StartsWith("MotherOtherInfo/Mother"))
                    {
                        if (cellNode == null)
                        {
                            string x = column.XPath.Replace("MotherOtherInfo/Mother", "MotherOtherInfo/MotherOtherInfo");
                            cellNode = record.SelectSingleNode(x);
                            if (cellNode == null)
                            {
                                x        = column.XPath.Replace("MotherOtherInfo/MotherOtherInfo", "MotherOtherInfo/Mother");
                                cellNode = record.SelectSingleNode(x);
                            }
                        }
                    }
                    #endregion

                    if (cellNode != null)
                    {
                        if (column.FieldName == "GraduateSchoolLocationCode")
                        {
                            cell.Value = GetCounty(schoolLocationList, cellNode.InnerText);
                        }
                        else if (column.FieldName == "DeptName") //處理科別繼承問題。
                        {
                            //這個欄位的資料一定會被回傳,因為設定了 Mandatory 屬性。
                            XmlNode selfDept = record.SelectSingleNode("SelfDeptName");
                            if (string.IsNullOrEmpty(selfDept.InnerText))
                            {
                                cell.Value = cellNode.InnerText;
                            }
                            else
                            {
                                cell.Value = selfDept.InnerText;
                            }
                        }
                        else if (column.FieldName == "Status")
                        {
                            cell.Value = GetStudStatusStr(cellNode.InnerText);
                        }
                        else
                        {
                            cell.Value = cellNode.InnerText;
                        }
                    }

                    //if (column.DisplayText == "狀態")//record.SelectSingleNode("StudentID")!=null )
                    //{
                    //    // 學生狀態
                    //    if (StudStatusDic.ContainsKey(record.SelectSingleNode("StudentID").InnerText))
                    //        cell.Value = StudStatusDic[record.SelectSingleNode("StudentID").InnerText];
                    //}
                }
            }
            return(table);
        }