1 /*
2  *  "GEDKeeper", the personal genealogical database editor.
3  *  Copyright (C) 2009-2021 by Sergey V. Zhdanovskih.
4  *
5  *  This file is part of "GEDKeeper".
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 using System;
22 using GDModel.Providers.GEDCOM;
23 
24 namespace GDModel
25 {
26     public enum GDMRecordType
27     {
28         rtNone, // may be rename to Unknown?
29 
30         rtIndividual,
31         rtFamily,
32         rtNote,
33         rtMultimedia,
34         rtSource,
35         rtRepository,
36         rtGroup,
37         rtResearch,
38         rtTask,
39         rtCommunication,
40         rtLocation,
41         rtSubmission,
42         rtSubmitter,
43 
44         rtLast/* = rtSubmitter*/
45     }
46 
47 
48     /// <summary>
49     ///
50     /// </summary>
51     public class GDMRecord : GDMTag, IGDMRecord
52     {
53         protected GDMTree fTree;
54         private string fAutomatedRecordID;
55         private GDMChangeDate fChangeDate;
56         private string fUID;
57         private string fXRef;
58 
59         private GDMList<GDMMultimediaLink> fMultimediaLinks;
60         private GDMList<GDMNotes> fNotes;
61         private GDMList<GDMSourceCitation> fSourceCitations;
62         private GDMList<GDMUserReference> fUserReferences;
63 
64 
65         public string AutomatedRecordID
66         {
67             get { return fAutomatedRecordID; }
68             set { fAutomatedRecordID = value; }
69         }
70 
71         public GDMChangeDate ChangeDate
72         {
73             get { return fChangeDate; }
74         }
75 
76         public bool HasMultimediaLinks
77         {
78             get { return fMultimediaLinks != null && fMultimediaLinks.Count != 0; }
79         }
80 
81         public GDMList<GDMMultimediaLink> MultimediaLinks
82         {
83             get {
84                 if (fMultimediaLinks == null) {
85                     fMultimediaLinks = new GDMList<GDMMultimediaLink>();
86                 }
87 
88                 return fMultimediaLinks;
89             }
90         }
91 
92         public bool HasNotes
93         {
94             get { return fNotes != null && fNotes.Count != 0; }
95         }
96 
97         public GDMList<GDMNotes> Notes
98         {
99             get {
100                 if (fNotes == null) {
101                     fNotes = new GDMList<GDMNotes>();
102                 }
103 
104                 return fNotes;
105             }
106         }
107 
108         public GDMRecordType RecordType
109         {
110             get { return (GDMRecordType)base.Id; }
111         }
112 
113         public bool HasSourceCitations
114         {
115             get { return fSourceCitations != null && fSourceCitations.Count != 0; }
116         }
117 
118         public GDMList<GDMSourceCitation> SourceCitations
119         {
120             get {
121                 if (fSourceCitations == null) {
122                     fSourceCitations = new GDMList<GDMSourceCitation>();
123                 }
124 
125                 return fSourceCitations;
126             }
127         }
128 
129         public string UID
130         {
131             get {
132                 if (string.IsNullOrEmpty(fUID)) {
133                     fUID = GEDCOMUtils.CreateUID();
134                 }
135                 return fUID;
136             }
137             set { fUID = value; }
138         }
139 
140         public bool HasUserReferences
141         {
142             get { return fUserReferences != null && fUserReferences.Count != 0; }
143         }
144 
145         public GDMList<GDMUserReference> UserReferences
146         {
147             get {
148                 if (fUserReferences == null) {
149                     fUserReferences = new GDMList<GDMUserReference>();
150                 }
151 
152                 return fUserReferences;
153             }
154         }
155 
156         public string XRef
157         {
158             get { return fXRef; }
159         }
160 
161 
GDMRecord(GDMTree tree)162         public GDMRecord(GDMTree tree)
163         {
164             fTree = tree;
165             fXRef = string.Empty;
166             fAutomatedRecordID = string.Empty;
167             fChangeDate = new GDMChangeDate();
168         }
169 
Dispose(bool disposing)170         protected override void Dispose(bool disposing)
171         {
172             if (disposing) {
173                 if (fNotes != null) fNotes.Dispose();
174                 if (fSourceCitations != null) fSourceCitations.Dispose();
175                 if (fMultimediaLinks != null) fMultimediaLinks.Dispose();
176                 if (fUserReferences != null) fUserReferences.Dispose();
177             }
178             base.Dispose(disposing);
179         }
180 
ResetTree(GDMTree tree)181         public void ResetTree(GDMTree tree)
182         {
183             fTree = tree;
184         }
185 
TrimExcess()186         internal override void TrimExcess()
187         {
188             base.TrimExcess();
189 
190             fChangeDate.TrimExcess();
191             if (fNotes != null) fNotes.TrimExcess();
192             if (fSourceCitations != null) fSourceCitations.TrimExcess();
193             if (fMultimediaLinks != null) fMultimediaLinks.TrimExcess();
194             if (fUserReferences != null) fUserReferences.TrimExcess();
195         }
196 
IndexOfSource(GDMSourceRecord sourceRec)197         public int IndexOfSource(GDMSourceRecord sourceRec)
198         {
199             if (sourceRec != null && fSourceCitations != null) {
200                 int num = fSourceCitations.Count;
201                 for (int i = 0; i < num; i++) {
202                     if (fSourceCitations[i].XRef == sourceRec.XRef) {
203                         return i;
204                     }
205                 }
206             }
207 
208             return -1;
209         }
210 
Assign(GDMTag source)211         public override void Assign(GDMTag source)
212         {
213             GDMRecord sourceRec = source as GDMRecord;
214             if (sourceRec == null)
215                 throw new ArgumentException(@"Argument is null or wrong type", "source");
216 
217             base.Assign(source);
218 
219             if (sourceRec.fNotes != null) AssignList(sourceRec.fNotes, Notes);
220             if (sourceRec.fMultimediaLinks != null) AssignList(sourceRec.fMultimediaLinks, MultimediaLinks);
221             if (sourceRec.fSourceCitations != null) AssignList(sourceRec.fSourceCitations, SourceCitations);
222             if (sourceRec.fUserReferences != null) AssignList(sourceRec.fUserReferences, UserReferences);
223         }
224 
MoveTo(GDMRecord targetRecord)225         public virtual void MoveTo(GDMRecord targetRecord)
226         {
227             var subTags = SubTags;
228             while (subTags.Count > 0) {
229                 GDMTag tag = subTags.Extract(0);
230                 if (tag.GetTagType() == GEDCOMTagType.CHAN) {
231                     tag.Dispose();
232                 } else {
233                     targetRecord.AddTag(tag);
234                 }
235             }
236 
237             while (fNotes != null && fNotes.Count > 0) {
238                 GDMTag tag = fNotes.Extract(0);
239                 targetRecord.Notes.Add((GDMNotes)tag);
240             }
241 
242             while (fMultimediaLinks != null && fMultimediaLinks.Count > 0) {
243                 GDMTag tag = fMultimediaLinks.Extract(0);
244                 targetRecord.MultimediaLinks.Add((GDMMultimediaLink)tag);
245             }
246 
247             while (fSourceCitations != null && fSourceCitations.Count > 0) {
248                 GDMTag tag = fSourceCitations.Extract(0);
249                 targetRecord.SourceCitations.Add((GDMSourceCitation)tag);
250             }
251 
252             while (fUserReferences != null && fUserReferences.Count > 0) {
253                 GDMTag tag = fUserReferences.Extract(0);
254                 targetRecord.UserReferences.Add((GDMUserReference)tag);
255             }
256         }
257 
ReplaceXRefs(GDMXRefReplacer map)258         public override void ReplaceXRefs(GDMXRefReplacer map)
259         {
260             base.ReplaceXRefs(map);
261 
262             if (fNotes != null) fNotes.ReplaceXRefs(map);
263             if (fSourceCitations != null) fSourceCitations.ReplaceXRefs(map);
264             if (fMultimediaLinks != null) fMultimediaLinks.ReplaceXRefs(map);
265             if (fUserReferences != null) fUserReferences.ReplaceXRefs(map);
266         }
267 
Clear()268         public override void Clear()
269         {
270             base.Clear();
271 
272             fAutomatedRecordID = string.Empty;
273             fChangeDate.Clear();
274             if (fNotes != null) fNotes.Clear();
275             if (fSourceCitations != null) fSourceCitations.Clear();
276             if (fMultimediaLinks != null) fMultimediaLinks.Clear();
277             if (fUserReferences != null) fUserReferences.Clear();
278             fUID = string.Empty;
279         }
280 
IsEmpty()281         public override bool IsEmpty()
282         {
283             return base.IsEmpty() && string.IsNullOrEmpty(fAutomatedRecordID) && fChangeDate.IsEmpty() &&
284                 (fNotes == null || fNotes.Count == 0) &&
285                 (fSourceCitations == null || fSourceCitations.Count == 0) &&
286                 (fMultimediaLinks == null || fMultimediaLinks.Count == 0) &&
287                 (fUserReferences == null || fUserReferences.Count == 0);
288         }
289 
SetXRef(GDMTree tree, string newXRef, bool removeOldXRef)290         public void SetXRef(GDMTree tree, string newXRef, bool removeOldXRef)
291         {
292             string oldXRef = fXRef;
293             fXRef = newXRef;
294 
295             if (tree != null) {
296                 tree.SetXRef(oldXRef, this, removeOldXRef);
297             }
298         }
299 
GetXRefNum()300         public string GetXRefNum()
301         {
302             string xref = XRef;
303 
304             int i = 0;
305             int last = xref.Length;
306             while (i < last && (xref[i] < '0' || xref[i] > '9')) i++;
307             xref = ((i < last) ? xref.Substring(i) : string.Empty);
308             return xref;
309         }
310 
GetId()311         public long GetId()
312         {
313             return GEDCOMUtils.GetXRefNumber(XRef);
314         }
315     }
316 }
317