File : data/Itinerary.js

1
/*
2
Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/
3
4
This  program is free software;
5
you can redistribute it and/or modify it under the terms of the
6
GNU General Public License as published by the Free Software Foundation;
7
either version 3 of the License, or any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License
14
along with this program; if not, write to the Free Software
15
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16
*/
17
/*
18
Changes:
19
    - v4.0.0:
20
        - created from v3.6.0
21
Doc reviewed 202208
22
 */
23
24
import ObjId from '../data/ObjId.js';
25
import ObjType from '../data/ObjType.js';
26
import Collection from '../data/Collection.js';
27
import ItineraryPoint from '../data/ItineraryPoint.js';
28
import Maneuver from '../data/Maneuver.js';
29
import theHTMLSanitizer from '../core/htmlSanitizer/HTMLSanitizer.js';
30
import TravelObject from '../data/TravelObject.js';
31
import { ZERO, INVALID_OBJ_ID } from '../main/Constants.js';
32
33
/* ------------------------------------------------------------------------------------------------------------------------- */
34
/**
35
This class represent an itinerary
36
*/
37
/* ------------------------------------------------------------------------------------------------------------------------- */
38
39
class Itinerary    extends TravelObject {
40
41
    /**
42
    The object type for itineraries
43
    @type {ObjType}
44
    */
45
46
    static #objType = new ObjType (
47
        'Itinerary',
48
        [ 'hasProfile', 'ascent', 'descent', 'itineraryPoints', 'maneuvers', 'provider', 'transitMode', 'objId' ]
49
    );
50
51
    /**
52
    the objId of the Itinerary
53
    @type {Number}
54
    */
55
56
    #objId = INVALID_OBJ_ID;
57
58
    /**
59
    a boolean set to true when the itinerary have a profile
60
    @type {Boolean}
61
    */
62
63
    #hasProfile = false;
64
65
    /**
66
    the ascent of the Itinerary when a profile exists, otherwise ZERO
67
    @type {Number}
68
    */
69
70
    #ascent = ZERO;
71
72
    /**
73
    the descent of the Itinerary when a profile exists, otherwise ZERO
74
    @type {Number}
75
    */
76
77
    #descent = ZERO;
78
79
    /**
80
    the provider name used for this Itinerary
81
    @type {String}
82
    */
83
84
    #provider = '';
85
86
    /**
87
    the transit mode used for this Itinerary
88
    @type {String}
89
    */
90
91
    #transitMode = '';
92
93
    /**
94
    a Collection of ItineraryPoints
95
    @type {Collection.<ItineraryPoint>}
96
    */
97
98
    #itineraryPoints = new Collection ( ItineraryPoint );
99
100
    /**
101
    a Collection of Maneuvers
102
    @type {Collection.<Maneuver>}
103
    */
104
105
    #maneuvers = new Collection ( Maneuver );
106
107
    /**
108
    The constructor
109
    */
110
111
    constructor ( ) {
112
        super ( );
113
        this.#objId = ObjId.nextObjId;
114
    }
115
116
    /**
117
    a boolean set to true when the itinerary have a profile
118
    @type {Boolean}
119
    */
120
121
    get hasProfile ( ) { return this.#hasProfile; }
122
123
    set hasProfile ( hasProfile ) {
124
        this.#hasProfile = 'boolean' === typeof ( hasProfile ) ? hasProfile : false;
125
    }
126
127
    /**
128
    the ascent of the Itinerary when a profile exists, otherwise ZERO
129
    @type {Number}
130
    */
131
132
    get ascent ( ) { return this.#ascent; }
133
134
    set ascent ( ascent ) {
135
        this.#ascent = 'number' === typeof ( ascent ) ? Number.parseInt ( ascent ) : ZERO;
136
    }
137
138
    /**
139
    the descent of the Itinerary when a profile exists, otherwise ZERO
140
    @type {Number}
141
    */
142
143
    get descent ( ) { return this.#descent; }
144
145
    set descent ( descent ) {
146
        this.#descent = 'number' === typeof ( descent ) ? Number.parseInt ( descent ) : ZERO;
147
    }
148
149
    /**
150
    the provider name used for this Itinerary
151
    @type {String}
152
    */
153
154
    get provider ( ) { return this.#provider; }
155
156
    set provider ( provider ) {
157
        this.#provider = 'string' === typeof ( provider ) ? theHTMLSanitizer.sanitizeToJsString ( provider ) : '';
158
    }
159
160
    /**
161
    the transit mode used for this Itinerary
162
    @type {String}
163
    */
164
165
    get transitMode ( ) { return this.#transitMode; }
166
167
    set transitMode ( transitMode ) {
168
        this.#transitMode = 'string' === typeof ( transitMode ) ? theHTMLSanitizer.sanitizeToJsString ( transitMode ) : '';
169
    }
170
171
    /**
172
    a Collection of ItineraryPoints
173
    @type {Collection.<ItineraryPoint>}
174
    */
175
176
    get itineraryPoints ( ) { return this.#itineraryPoints; }
177
178
    /**
179
    a Collection of Maneuvers
180
    @type {Collection.<Maneuver>}
181
    */
182
183
    get maneuvers ( ) { return this.#maneuvers; }
184
185
    /**
186
    the ObjType of the Itinerary.
187
    @type {ObjType}
188
    */
189
190
    get objType ( ) { return Itinerary.#objType; }
191
192
    /**
193
    the objId of the Itinerary. objId are unique identifier given by the code
194
    @type {Number}
195
    */
196
197
    get objId ( ) { return this.#objId; }
198
199
    /**
200
    An object literal with the Itinerary properties and without any methods.
201
    This object can be used with the JSON object
202
    @type {JsonObject}
203
    */
204
205
    get jsonObject ( ) {
206
        return {
207
            hasProfile : this.hasProfile,
208
            ascent : this.ascent,
209
            descent : this.descent,
210
            itineraryPoints : this.itineraryPoints.jsonObject,
211
            maneuvers : this.maneuvers.jsonObject,
212
            provider : this.provider,
213
            transitMode : this.transitMode,
214
            objId : this.#objId,
215
            objType : this.objType.jsonObject
216
        };
217
    }
218
    set jsonObject ( something ) {
219
        const otherthing = this.validateObject ( something );
220
        this.hasProfile = otherthing.hasProfile;
221
        this.ascent = otherthing.ascent;
222
        this.descent = otherthing.descent;
223
        this.itineraryPoints.jsonObject = otherthing.itineraryPoints;
224
        this.maneuvers.jsonObject = otherthing.maneuvers;
225
        this.provider = otherthing.provider;
226
        this.transitMode = otherthing.transitMode;
227
        this.#objId = ObjId.nextObjId;
228
229
        // rebuilding links between maneuvers and itineraryPoints
230
        const itineraryPointObjIdMap = new Map ( );
231
        let sourceCounter = ZERO;
232
        const itineraryPointsIterator = this.itineraryPoints.iterator;
233
        while ( ! itineraryPointsIterator.done ) {
234
            itineraryPointObjIdMap.set (
235
                otherthing.itineraryPoints [ sourceCounter ].objId,
236
                itineraryPointsIterator.value.objId
237
            );
238
            sourceCounter ++;
239
        }
240
        const maneuverIterator = this.maneuvers.iterator;
241
        while ( ! maneuverIterator.done ) {
242
            maneuverIterator.value.itineraryPointObjId =
243
                itineraryPointObjIdMap.get ( maneuverIterator.value.itineraryPointObjId );
244
        }
245
    }
246
}
247
248
export default Itinerary;
249
250
/* --- End of file --------------------------------------------------------------------------------------------------------- */
251