-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathLayoutFinder.cs
More file actions
908 lines (836 loc) · 29.7 KB
/
Copy pathLayoutFinder.cs
File metadata and controls
908 lines (836 loc) · 29.7 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
// Copyright (c) 2015 SIL International
// This software is licensed under the LGPL, version 2.1 or later
// (http://www.gnu.org/licenses/lgpl-2.1.html)
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using SIL.LCModel.Core.Cellar;
using SIL.LCModel.Core.Text;
using SIL.LCModel.Core.KernelInterfaces;
using SIL.FieldWorks.Common.ViewsInterfaces;
using SIL.FieldWorks.Common.RootSites;
using SIL.LCModel;
using SIL.LCModel.Application;
using SIL.LCModel.DomainServices;
using SIL.FieldWorks.Filters;
using SIL.Utils;
namespace SIL.FieldWorks.Common.Controls
{
/// <summary>
/// LayoutFinder is an implementation of IStringFinder that finds a string based
/// on looking up a layout for a particular HVO.
/// </summary>
public class LayoutFinder : IStringFinder, IPersistAsXml,
IStoresLcmCache, IStoresDataAccess
{
#region Data members
/// <summary/>
protected internal ISilDataAccess m_sda;
internal string m_layoutName;
internal IFwMetaDataCache m_mdc;
internal LcmCache m_cache;
internal LayoutCache m_layouts;
internal XmlNode m_colSpec;
/// <summary/>
protected XmlBrowseViewBaseVc m_vc;
/// <summary/>
protected bool m_fDisposeVc;
private IApp m_app;
#endregion
/// ------------------------------------------------------------------------------------
/// <summary>
/// normal constructor.
/// </summary>
/// <param name="cache">The cache.</param>
/// <param name="layoutName">Name of the layout.</param>
/// <param name="colSpec">The col spec.</param>
/// <param name="app">The application.</param>
/// ------------------------------------------------------------------------------------
public LayoutFinder(LcmCache cache, string layoutName, XmlNode colSpec,
IApp app): this()
{
m_layoutName = layoutName;
m_colSpec = colSpec;
m_app = app;
Cache = cache;
}
/// <summary>
/// Default constructor for persistence.
/// </summary>
public LayoutFinder()
{
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Make a finder appropriate to the given column specification
/// </summary>
/// <param name="cache">LcmCache</param>
/// <param name="colSpec">column specification</param>
/// <param name="vc">The vc.</param>
/// <param name="app">The application.</param>
/// <returns>finder for colSpec</returns>
/// ------------------------------------------------------------------------------------
public static IStringFinder CreateFinder(LcmCache cache, XmlNode colSpec,
XmlBrowseViewBaseVc vc, IApp app)
{
string layoutName = XmlUtils.GetOptionalAttributeValue(colSpec, "layout");
string sSortMethod = XmlUtils.GetOptionalAttributeValue(colSpec, "sortmethod");
string sortType = XmlUtils.GetOptionalAttributeValue(colSpec, "sortType", null);
LayoutFinder result;
if (sSortMethod != null)
{
result = new SortMethodFinder(cache, sSortMethod, layoutName, colSpec, app);
}
else if (sortType != null)
{
switch (sortType)
{
case "integer":
result = new IntCompareFinder(cache, layoutName, colSpec, app);
break;
case "occurrenceInContext":
// LT-8457: Context should be considered only following the occurrence; preceding context should be ignored.
result = new OccurrenceInContextFinder(cache, layoutName, colSpec, app);
break;
case "date":
case "YesNo":
case "stringList":
case "genDate":
// no special action needed here for sorting dates or date that shows as 'yes" or "no";
// Using a SortCollectorEnv triggers special
// action in case "datetime"/"gendate" of XmlVc.ProcessFrag().
result = new LayoutFinder(cache, layoutName, colSpec, app);
break;
default:
throw new ConfigurationException("unexpected sort type: " + sortType, colSpec);
}
}
else
{
result = new LayoutFinder(cache, layoutName, colSpec, app);
}
result.Vc = vc;
return result;
}
/// <summary>
/// Set the SDA when we need to override the one from the cache (using a decorator).
/// </summary>
public ISilDataAccess DataAccess
{
set
{
m_sda = value;
m_mdc = m_sda.MetaDataCache;
}
}
/// <summary>
/// Get/Set the XmlBrowseViewBaseVc.
/// </summary>
public XmlBrowseViewBaseVc Vc
{
get
{
return m_vc;
}
internal set
{
m_vc = value;
m_sda = m_vc.DataAccess;
m_mdc = m_sda.MetaDataCache;
}
}
/// <summary>
/// Stores the reversal ws that should be used for filtering.
/// </summary>
public int ReversalWs { set; get; }
#region StringFinder Members
/// ------------------------------------------------------------------------------------
/// <summary>
/// Stringses the specified hvo.
/// </summary>
/// <param name="hvo">The hvo.</param>
/// <returns></returns>
/// ------------------------------------------------------------------------------------
public string[] Strings(int hvo)
{
try
{
string[] result = null;
if (m_layoutName == null)
result = StringsFor(hvo, m_colSpec, m_vc.WsForce);
if (result == null)
{
XmlNode layout = XmlVc.GetNodeForPart(hvo, m_layoutName, true, m_sda, m_layouts);
if (layout == null)
return new string[0]; // cell will be empty.
result = StringsFor(hvo, layout, m_vc.WsForce);
}
if (result == null)
return new string[0];
else
return result;
}
catch (Exception e)
{
throw new Exception("Failed to get strings for object " + hvo, e);
}
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Figure the node and hvo that correspond to the particular sort item,
/// and generate its key. This figures out the XML needed for the particular
/// key object, and interprets it to make a key.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="sortedFromEnd"></param>
/// <returns></returns>
/// ------------------------------------------------------------------------------------
public string[] Strings(IManyOnePathSortItem item, bool sortedFromEnd)
{
string result = Key(item, true).Text;
if (result == null)
return new string[0];
else
{
if (sortedFromEnd)
result = TsStringUtils.ReverseString(result);
return new string[] { result };
}
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Keys the specified item.
/// </summary>
/// <param name="item">The item.</param>
/// <returns></returns>
/// ------------------------------------------------------------------------------------
public ITsString Key(IManyOnePathSortItem item)
{
return Key(item, false);
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Keys the specified item.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="fForSorting">if set to <c>true</c> [f for sorting].</param>
/// <returns></returns>
/// ------------------------------------------------------------------------------------
public ITsString Key(IManyOnePathSortItem item, bool fForSorting)
{
if (m_cache == null)
throw new ApplicationException("There's no way the browse VC (m_vc) can get a string in its current state.");
int hvo = item.RootObjectHvo;
TsStringCollectorEnv collector;
if (fForSorting)
{
collector = new SortCollectorEnv(null, m_sda, hvo);
}
else
{
collector = new TsStringCollectorEnv(null, m_sda, hvo);
}
// This will check to see if the VC is either null or disposed. The disposed check is neccesary because
// there are several instances where we can have a reference to an instance that was disposed, which will
// cause problems later on.
// Enhance CurtisH/EricP: If this VC gets used in other places, rather than adding more checks like this one,
// it may be better to refactor XWorksViewBase to cause it to reload the sorter and filter from persistence
// every time the tool is changed
if (m_vc == null)
{
m_vc = new XmlBrowseViewBaseVc(m_cache);
m_vc.SuppressPictures = true; // we won't dispose of it, so it mustn't make pictures (which we don't need)
m_vc.DataAccess = m_sda;
m_vc.ReversalWs = ReversalWs;
}
else
{
if (m_vc.Cache == null)
m_vc.Cache = m_cache;
if (m_vc.Cache == null)
throw new ApplicationException("There's no way the browse VC (m_vc) can get a string in its current state.");
}
m_vc.DisplayCell(item, m_colSpec, hvo, collector);
return collector.Result;
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// For most of these we want to return the same thing.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="sortedFromEnd">if set to <c>true</c> [sorted from end].</param>
/// <returns></returns>
/// ------------------------------------------------------------------------------------
public virtual string[] SortStrings(IManyOnePathSortItem item, bool sortedFromEnd)
{
return Strings(item, sortedFromEnd);
}
/// <summary>
/// Add to collector the ManyOnePathSortItems which this sorter derives from
/// the specified object. This implementation follows object and sequence properties,
/// if there is only one in a given structure, and makes an item for each thing
/// found.
/// </summary>
public void CollectItems(int hvo, ArrayList collector)
{
int start = collector.Count;
XmlViewsUtils.CollectBrowseItems(hvo, m_colSpec, collector, m_mdc, m_sda, m_layouts);
}
private string[] StringsFor(int hvo, XmlNode layout, int wsForce)
{
return XmlViewsUtils.StringsFor(m_cache, m_cache.DomainDataByFlid, layout, hvo, m_layouts, null, wsForce);
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Answer true if they are the 'same' finder (will find the same strings).
/// </summary>
/// <param name="other">The other.</param>
/// <returns></returns>
/// ------------------------------------------------------------------------------------
public virtual bool SameFinder(IStringFinder other)
{
var otherLf = other as LayoutFinder;
if (otherLf == null)
return false;
return SameLayoutName(otherLf)
&& SameData(otherLf)
&& SameConfiguration(otherLf);
}
bool SameLayoutName(LayoutFinder otherLf)
{
return otherLf.m_layoutName == m_layoutName ||
(String.IsNullOrEmpty(otherLf.m_layoutName) && String.IsNullOrEmpty(m_layoutName));
}
private bool SameData(LayoutFinder otherLf)
{
if (otherLf.m_sda == m_sda)
return true;
ISilDataAccessManaged first = RootSdaOf(m_sda);
ISilDataAccessManaged second = RootSdaOf(otherLf.m_sda);
return (first == second && first != null);
}
private static ISilDataAccessManaged RootSdaOf(ISilDataAccess sda)
{
if (sda is DomainDataByFlidDecoratorBase)
return RootSdaOf((sda as DomainDataByFlidDecoratorBase).BaseSda);
else
return sda as ISilDataAccessManaged;
}
private bool SameConfiguration(LayoutFinder otherLf)
{
// XmlUtils.NodesMatch() is too strict a comparison for identifying column configurations that will
// display the same value (e.g. we don't care about differences in width), causing us to
// lose the sort arrow when switching between tools sharing common columns (LT-2858).
// For now, just assume that columns with the same label will display the same value.
// If this proves too loose for a particular column, try implementing a sortmethod instead.
string colSpecLabel = XmlUtils.GetMandatoryAttributeValue(m_colSpec, "label");
string otherLfLabel = XmlUtils.GetMandatoryAttributeValue(otherLf.m_colSpec, "label");
string colSpecLabel2 = XmlUtils.GetOptionalAttributeValue(m_colSpec, "headerlabel");
string otherLfLabel2 = XmlUtils.GetOptionalAttributeValue(otherLf.m_colSpec, "headerlabel");
return (colSpecLabel == otherLfLabel ||
colSpecLabel == otherLfLabel2 ||
colSpecLabel2 == otherLfLabel ||
(colSpecLabel2 == otherLfLabel2 && otherLfLabel2 != null));
}
/// <summary>
/// Called in advance of 'finding' strings for many instances, typically all or most
/// of the ones in existence. May preload data to make such a large succession of finds
/// more efficient. This one looks to see whether its ColSpec specifies a preload,
/// and if so, invokes it.
/// </summary>
public void Preload(object rootObj)
{
if (m_vc != null)
m_vc.SetReversalWritingSystemFromRootObject(rootObj);
string preload = XmlUtils.GetOptionalAttributeValue(m_colSpec, "preload", null);
if (String.IsNullOrEmpty(preload))
return;
string[] splits = preload.Split('.');
if (splits.Length != 2)
return; // ignore faulty ones
string className = splits[0];
string methodName = splits[1];
// Get the directory where our DLLs live
string baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
// For now we assume an FDO class.
Assembly fdoAssembly = Assembly.LoadFrom(Path.Combine(baseDir, "FDO.dll"));
Type targetType = fdoAssembly.GetType("SIL.FieldWorks.FDO." + className);
if (targetType == null)
return;
MethodInfo info = targetType.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
if (info == null)
return;
info.Invoke(null, new object[] { m_cache });
}
#endregion
#region IPersistAsXml Members
/// ------------------------------------------------------------------------------------
/// <summary>
/// Persists as XML.
/// </summary>
/// <param name="node">The node.</param>
/// ------------------------------------------------------------------------------------
public virtual void PersistAsXml(XmlNode node)
{
XmlUtils.AppendAttribute(node, "layout", m_layoutName);
node.AppendChild(node.OwnerDocument.ImportNode(m_colSpec, true));
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Inits the XML.
/// </summary>
/// <param name="node">The node.</param>
/// ------------------------------------------------------------------------------------
public virtual void InitXml(XmlNode node)
{
m_layoutName = XmlUtils.GetMandatoryAttributeValue(node, "layout");
m_colSpec = node.SelectSingleNode("column");
}
#endregion
#region IStoresLcmCache Members
/// <summary>
/// This is used to set the cache when one is recreated from XML.
/// </summary>
public LcmCache Cache
{
set
{
if (m_cache == value)
return;
m_sda = value.DomainDataByFlid;
m_cache = value;
m_mdc = value.DomainDataByFlid.MetaDataCache;
m_layouts = new LayoutCache(m_mdc, m_cache.ProjectId.Name, m_app,
m_cache.ProjectId.ProjectFolder);
// The VC is set after the cache when created by the view, but it uses a
// 'real' VC that already has a cache.
// When the VC is created by restoring a persisted layout finder, the cache
// is set later, and needs to be copied to the VC.
if (m_vc != null)
m_vc.Cache = value;
}
}
#endregion
}
/// <summary>
/// SortMethodFinder is an implementation of StringFinder that finds a sort string based
/// on a given sort method defined on the class of the objects being sorted. If the
/// method does not exist, the finder tries the standard method 'SortKey'. If that
/// does not exist, fall back to the string derived from the layout.
/// The string derived from the layout is still used for filtering.
/// </summary>
public class SortMethodFinder : LayoutFinder
{
private string m_sMethodName;
private string m_wsName;
private int m_ws;
/// ------------------------------------------------------------------------------------
/// <summary>
/// normal constructor.
/// </summary>
/// <param name="cache">The cache.</param>
/// <param name="methodName">Name of the method.</param>
/// <param name="layoutName">Name of the layout.</param>
/// <param name="colSpec">The col spec.</param>
/// <param name="app">The application</param>
/// ------------------------------------------------------------------------------------
public SortMethodFinder(LcmCache cache, string methodName, string layoutName,
XmlNode colSpec, IApp app)
: base(cache, layoutName, colSpec, app)
{
SortMethod = methodName;
WritingSystemName = StringServices.GetWsSpecWithoutPrefix(colSpec);
}
/// <summary>
/// Default constructor for persistence.
/// </summary>
public SortMethodFinder()
{
}
string SortMethod
{
set
{
m_sMethodName = value ?? "";
}
}
string WritingSystemName
{
get { return m_wsName; }
set
{
m_wsName = value == "" ? null : value;
}
}
/// <summary>
/// Gets the sort key by traversing the part tree, calling the sort method at the leaves.
/// </summary>
/// <param name="layout">The layout.</param>
/// <param name="cmo">The object.</param>
/// <param name="item">The item.</param>
/// <param name="pathIndex">Index of the path.</param>
/// <param name="sortedFromEnd">if set to <c>true</c> [sorted from end].</param>
/// <returns></returns>
private string[] GetKey(XmlNode layout, ICmObject cmo, IManyOnePathSortItem item, int pathIndex, bool sortedFromEnd)
{
if (layout == null)
return null;
switch (layout.Name)
{
case "obj":
{
int flid = GetFlid(layout, cmo.Hvo);
if (pathIndex != -1 && (pathIndex == item.PathLength || flid != item.PathFlid(pathIndex)))
// we are now off of the path
pathIndex = -1;
int objHvo = m_cache.MainCacheAccessor.get_ObjectProp(cmo.Hvo, flid);
if (objHvo != 0)
{
if (pathIndex != -1
&& (pathIndex < item.PathLength - 1 && objHvo == item.PathObject(pathIndex + 1))
|| (pathIndex == item.PathLength - 1 && objHvo == item.KeyObject))
{
return GetChildObjKey(layout, objHvo, item, pathIndex + 1, sortedFromEnd);
}
// we are off of the path
return GetChildObjKey(layout, objHvo, item, -1, sortedFromEnd);
}
}
break;
case "seq":
{
int flid = GetFlid(layout, cmo.Hvo);
if (pathIndex != -1 && (pathIndex == item.PathLength || flid != item.PathFlid(pathIndex)))
// we are now off of the path
pathIndex = -1;
int size = m_cache.MainCacheAccessor.get_VecSize(cmo.Hvo, flid);
StringBuilder sb = null;
for (int i = 0; i < size; i++)
{
int objHvo = m_cache.MainCacheAccessor.get_VecItem(cmo.Hvo, flid, i);
if (pathIndex != -1
&& (pathIndex < item.PathLength - 1 && objHvo == item.PathObject(pathIndex + 1))
|| (pathIndex == item.PathLength - 1 && objHvo == item.KeyObject))
{
return GetChildObjKey(layout, objHvo, item, pathIndex + 1, sortedFromEnd);
}
// if we are off of the path, we concatenate all vector keys to create an
// aggregate key
var childObjKey = GetChildObjKey(layout, objHvo, item, -1, sortedFromEnd);
if (childObjKey != null)
{
if (sb == null)
sb = new StringBuilder();
foreach (var subKey in childObjKey)
sb.Append(subKey);
}
}
if (sb != null)
return new [] {sb.ToString()};
}
break;
case "layout":
case "part":
{
string partref = XmlUtils.GetOptionalAttributeValue(layout, "ref");
if (partref != null)
{
XmlNode part = XmlVc.GetNodeForPart(cmo.Hvo, partref, true, m_sda, m_layouts);
return GetKey(part, cmo, item, pathIndex, sortedFromEnd);
}
foreach (XmlNode child in layout.ChildNodes)
{
if (child is XmlComment)
continue;
var key = GetKey(child, cmo, item, pathIndex, sortedFromEnd);
if (key != null)
return key;
}
}
break;
}
return null;
}
private string[] GetChildObjKey(XmlNode layout, int hvo, IManyOnePathSortItem item, int pathIndex, bool sortedFromEnd)
{
ICmObject childObj = m_cache.ServiceLocator.ObjectRepository.GetObject(hvo);
string layoutName = XmlUtils.GetOptionalAttributeValue(layout, "layout");
XmlNode part = XmlVc.GetNodeForPart(hvo, layoutName, true, m_sda, m_layouts);
var key = GetKey(part, childObj, item, pathIndex, sortedFromEnd);
if (key != null)
return key;
return CallSortMethod(childObj, sortedFromEnd);
}
/// <summary>
/// This is a simplified version of XmlVc.GetFlid.
/// It does not look for a flid attr, nor try to cache the result.
/// It looks for a "field" property, and optionally a "class" one, and uses them
/// (or the class of hvo, if "class" is missing) to figure the flid.
/// Virtual properties are assumed already created.
/// </summary>
/// <param name="frag">The frag.</param>
/// <param name="hvo">The hvo.</param>
/// <returns></returns>
private int GetFlid(XmlNode frag, int hvo)
{
string stClassName = XmlUtils.GetOptionalAttributeValue(frag, "class");
string stFieldName = XmlUtils.GetMandatoryAttributeValue(frag, "field");
if (string.IsNullOrEmpty(stClassName))
{
int classId = m_sda.get_IntProp(hvo,
(int)CmObjectFields.kflidCmObject_Class);
return m_mdc.GetFieldId2(classId, stFieldName, true);
}
return m_mdc.GetFieldId(stClassName, stFieldName, true);
}
#region StringFinder Members
/// <summary>
/// Get a key from the item.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="sortedFromEnd">if set to <c>true</c> [sorted from end].</param>
/// <returns></returns>
public override string[] SortStrings(IManyOnePathSortItem item, bool sortedFromEnd)
{
if (item.KeyObject == 0)
return new string[0];
// traverse the part tree from the root, the root object and the root layout node should
// be compatible
XmlNode layout = XmlVc.GetNodeForPart(item.RootObjectHvo, m_layoutName, true, m_sda, m_layouts);
var rootObject = item.RootObjectUsing(m_cache);
var key = GetKey(layout, rootObject, item, 0, sortedFromEnd);
if (key == null)
{
// the root object sort method is not tried in GetKey
key = CallSortMethod(rootObject, sortedFromEnd);
if (key == null)
{
// try calling the sort method on the key object
var keyCmObjectUsing = item.KeyObjectUsing(m_cache);
key = CallSortMethod(keyCmObjectUsing, sortedFromEnd);
if (key == null)
{
// Try the default fallback if we can't find the method.
var firstKey = keyCmObjectUsing.SortKey ?? "";
if (sortedFromEnd)
firstKey = TsStringUtils.ReverseString(firstKey);
return new [] {firstKey, keyCmObjectUsing.SortKey2Alpha};
}
}
}
return key;
}
/// <summary>
/// Calls the sort method.
/// </summary>
/// <param name="cmo">The object.</param>
/// <param name="sortedFromEnd">if set to <c>true</c> [sorted from end].</param>
/// <returns></returns>
private string[] CallSortMethod(ICmObject cmo, bool sortedFromEnd)
{
Type typeCmo = cmo.GetType();
try
{
MethodInfo mi = typeCmo.GetMethod(m_sMethodName);
if (mi == null)
return null;
object obj;
if (mi.GetParameters().Length == 2)
{
// Enhance JohnT: possibly we should seek to evaluate this every time, in case it is a magic WS like
// "best vernacular". But interpreting those requires a flid, and we don't have one; indeed, the
// method may retrieve information from several. So we may as well just accept that the fancy ones
// won't work.
if (m_ws == 0 && WritingSystemName != null)
m_ws = WritingSystemServices.InterpretWsLabel(m_cache, WritingSystemName, null, 0, 0, null);
obj = mi.Invoke(cmo, new object[] { sortedFromEnd, m_ws });
}
else
{
obj = mi.Invoke(cmo, new object[] { sortedFromEnd });
}
if (obj is string)
return new [] {(string) obj};
// otherwise assume it already is a string array.
return (string[]) obj;
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// Answer true if they are the 'same' finder (will find the same strings).
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public override bool SameFinder(IStringFinder other)
{
if (other is SortMethodFinder)
{
var smf = other as SortMethodFinder;
return m_sMethodName == smf.m_sMethodName && base.SameFinder(other)
&& m_wsName == smf.m_wsName;
}
return false;
}
#endregion
#region IPersistAsXml Members
/// ------------------------------------------------------------------------------------
/// <summary>
/// Persists as XML.
/// </summary>
/// <param name="node">The node.</param>
/// ------------------------------------------------------------------------------------
public override void PersistAsXml(XmlNode node)
{
base.PersistAsXml(node);
XmlUtils.AppendAttribute(node, "sortmethod", m_sMethodName);
if (!string.IsNullOrEmpty(m_wsName))
XmlUtils.AppendAttribute(node, "ws", m_wsName);
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Inits the XML.
/// </summary>
/// <param name="node">The node.</param>
/// ------------------------------------------------------------------------------------
public override void InitXml(XmlNode node)
{
base.InitXml(node);
SortMethod = XmlUtils.GetMandatoryAttributeValue(node, "sortmethod");
WritingSystemName = XmlUtils.GetOptionalAttributeValue(node, "ws", null);
// Enhance JohnT: if we start using string tables for browse views,
// we will need a better way to provide one to the Vc we make here.
// Note: we don't need a top-level spec because we're only going to process one
// column's worth.
m_vc = new XmlBrowseViewBaseVc();
m_vc.SuppressPictures = true; // we won't dispose of it, so it mustn't make pictures (which we don't need)
}
#endregion
}
/// <summary>
/// IntCompareFinder is an implementation of StringFinder that modifies the sort
/// string by adding leading zeros to pad it to ten digits.
/// </summary>
public class IntCompareFinder : LayoutFinder
{
/// ------------------------------------------------------------------------------------
/// <summary>
/// normal constructor.
/// </summary>
/// <param name="cache">The cache.</param>
/// <param name="layoutName">Name of the layout.</param>
/// <param name="colSpec">The col spec.</param>
/// <param name="app">The application</param>
/// ------------------------------------------------------------------------------------
public IntCompareFinder(LcmCache cache, string layoutName, XmlNode colSpec, IApp app)
: base(cache, layoutName, colSpec, app)
{
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Default constructor for persistence.
/// </summary>
/// ------------------------------------------------------------------------------------
public IntCompareFinder()
{
}
#region StringFinder Members
const int maxDigits = 10; // Int32.MaxValue.ToString().Length;, but that is not 'const'!
/// ------------------------------------------------------------------------------------
/// <summary>
/// Get a key from the item for sorting. Add enough leading zeros so string comparison
/// works.
///
/// Collator sorting generally ignores the minus sign as being a hyphen. So we have
/// to be tricky handling negative numbers. Nine's complement with an inverted sign
/// digit should do the trick...
/// </summary>
/// <param name="item">The item.</param>
/// <param name="sortedFromEnd">if set to <c>true</c> [sorted from end].</param>
/// <returns></returns>
/// ------------------------------------------------------------------------------------
public override string[] SortStrings(IManyOnePathSortItem item, bool sortedFromEnd)
{
string[] baseResult = base.SortStrings(item, sortedFromEnd);
if (sortedFromEnd)
return baseResult; // what on earth would it mean??
if (baseResult.Length != 1)
return baseResult;
string sVal = baseResult[0];
if (sVal.Length == 0)
return new string[] { "9" + new String('0', maxDigits) };
string prefix;
char chFiller;
if (sVal[0] == '-')
{
sVal = NinesComplement(sVal.Substring(1));
prefix = "0"; // negative numbers come first.
chFiller = '9';
}
else
{
prefix = "9"; // positive numbers come later.
chFiller = '0';
}
if (sVal.Length == maxDigits)
return new string[] { prefix + sVal };
else
return new string[] { prefix + new String(chFiller, maxDigits - sVal.Length) + sVal };
}
private string NinesComplement(string sNumber)
{
StringBuilder bldr = new StringBuilder();
while (sNumber.Length > 0)
{
switch (sNumber[0])
{
case '0': bldr.Append('9'); break;
case '1': bldr.Append('8'); break;
case '2': bldr.Append('7'); break;
case '3': bldr.Append('6'); break;
case '4': bldr.Append('5'); break;
case '5': bldr.Append('4'); break;
case '6': bldr.Append('3'); break;
case '7': bldr.Append('2'); break;
case '8': bldr.Append('1'); break;
case '9': bldr.Append('0'); break;
default:
throw new Exception("Invalid character found in supposed integer string!");
}
sNumber = sNumber.Substring(1);
}
return bldr.ToString();
}
/// <summary>
/// Answer true if they are the 'same' finder (will find the same strings).
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public override bool SameFinder(IStringFinder other)
{
if (other is IntCompareFinder)
{
return base.SameFinder(other);
}
else
{
return false;
}
}
#endregion
}
/// <summary>
/// This is a marker class used when building a sort key.
/// </summary>
class SortCollectorEnv : TsStringCollectorEnv
{
public SortCollectorEnv(IVwEnv baseEnv, ISilDataAccess sda, int hvoRoot)
: base(baseEnv, sda, hvoRoot)
{ }
}
}