summaryrefslogtreecommitdiff
path: root/src/traffic-incidents-service/org.genivi.trafficinfo/src/org/genivi/trafficincidentsservice/refimpl/TrafficIncidentConvertor.java
blob: 020b8beddd18e26d4641b61718ac37edb85b5d10 (plain)
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
/**
 * Copyright (C) 2013 TomTom International B.V.
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.genivi.trafficincidentsservice.refimpl;

import java.util.Date;
import java.util.List;

import org.genivi.common.LocalizedString;
import org.genivi.trafficincidentsservice.trafficincidents.trafficincident.Advice;
import org.genivi.trafficincidentsservice.trafficincidents.trafficincident.Applicability;
import org.genivi.trafficincidentsservice.trafficincidents.trafficincident.Cause;
import org.genivi.trafficincidentsservice.trafficincidents.trafficincident.DirectCause;
import org.genivi.trafficincidentsservice.trafficincidents.trafficincident.LaneRestrictions;
import org.genivi.trafficincidentsservice.trafficincidents.trafficincident.LinkedCause;
import org.genivi.trafficincidentsservice.trafficincidents.trafficincident.TrafficIncident;
import org.genivi.trafficincidentsservice.trafficincidents.trafficincident.trafficincidentFactory;
import org.genivi.trafficincidentsservice.trafficincidents.trafficincident.VehicleRelatedRestriction;

public class TrafficIncidentConvertor {
  private static final trafficincidentFactory TI_FACTORY = trafficincidentFactory.eINSTANCE;

  public static TrafficIncident xTrafficIncident2TrafficIncident(
      org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.TrafficIncident xTrafficIncident,
      String languageCode, String languageCountryCode) {
    TrafficIncident trafficIncident = TI_FACTORY.createTrafficIncident();
    
    // trafficIncidentId: not available in xTrafficIncident.
    
    // effectCode
    if (xTrafficIncident.isSetEffectCode()) {
      trafficIncident.setEffectCode(xTrafficIncident.getEffectCode());
    }
    
    // effectText: not available in xTrafficIncident.
    
    // startTime
    if (xTrafficIncident.isSetStartTime()) {
      Date startDate = new Date(xTrafficIncident.getStartTime().getTime());
      trafficIncident.setStartTime(startDate);
    }
    
    // stopTime
    if (xTrafficIncident.isSetStopTime()) {
      Date stopDate = new Date(xTrafficIncident.getStopTime().getTime());
      trafficIncident.setStopTime(stopDate);
    }
    
    // tendency
    if (xTrafficIncident.isSetTendency()) {
      trafficIncident.setTendency(xTrafficIncident.getTendency());
    }
    
    // tendencyText:; not available in xTrafficIncident.
    
    // lengthAffected
    if (xTrafficIncident.isSetLengthAffected()) {
      trafficIncident.setLengthAffected(xTrafficIncident.getLengthAffected());
    }
    
    // averageSpeedAbsolute
    if (xTrafficIncident.isSetAverageSpeedAbsolute()) {
      trafficIncident.setAverageSpeedAbsolute(xTrafficIncident.getAverageSpeedAbsolute());
    }
    
    // delay
    if (xTrafficIncident.isSetDelay()) {
      trafficIncident.setDelay(xTrafficIncident.getDelay());
    }
    
    // segmentSpeedLimit
    if (xTrafficIncident.isSetSegmentSpeedLimit()) {
      trafficIncident.setSegmentSpeedLimit(xTrafficIncident.getSegmentSpeedLimit());
    }
    
    // expectedSpeedAbsolute
    if (xTrafficIncident.isSetExpectedSpeedAbsolute()) {
      trafficIncident.setExpectedSpeedAbsolute(xTrafficIncident.getExpectedSpeedAbsolute());
    }
    
    // locationInfo: not available in xTrafficIncident.
    
    // causeTexts: not available in xTrafficIncident.
    
    // applicabilityTexts: not available in xTrafficIncident.
    
    // adviceTexts: not available in xTrafficIncident.
    
    // categoryIds: not available in xTrafficIncident.
    
    // locationInfo: not available in xTrafficIncident.
    
    // causes
    for (org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.Cause xCause: xTrafficIncident.getCauses()) {
      trafficIncident.getCauses().add(xCause2Cause(xCause, languageCode, languageCountryCode));
    }
    
    // advices
    for (org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.Advice xAdvice: xTrafficIncident.getAdvices()) {
      trafficIncident.getAdvices().add(xAdvice2Advice(xAdvice, languageCode, languageCountryCode));
    }
    
    // applicability
    for (org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.Applicability xApplicability: xTrafficIncident.getApplicability()) {
      trafficIncident.getApplicability().add(xApplicability2Applicability(xApplicability));
    }
    
    // location: not available in xTrafficIncident.
    
    // locationBoundingBox: not available in xTrafficIncident.
    
    return trafficIncident;
  }

  private static Cause xCause2Cause(org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.Cause xCause, String languageCode, String languageCountryCode) {
    Cause cause;
    
    if (xCause instanceof org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.DirectCause) {
      cause = xDirectCause2DirectCause((org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.DirectCause) xCause, languageCode, languageCountryCode);
    } else {
      cause = xLinkedCause2LinkedCause((org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.LinkedCause) xCause);
    }
    cause.setMainCause(xCause.getMainCause());
    
    return cause;
  }

  private static DirectCause xDirectCause2DirectCause(org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.DirectCause xDirectCause,
      String languageCode, String languageCountryCode) {
    DirectCause directCause = TI_FACTORY.createDirectCause();
    
    // warningLevel
    if (xDirectCause.isSetWarningLevel()) {
      directCause.setWarningLevel(xDirectCause.getWarningLevel());
    }
    
    // unverifiedInformation
    directCause.setUnverifiedInformation(xDirectCause.isUnverifiedInformation());
    
    // subCause
    if (xDirectCause.isSetSubCause()) {
      directCause.setSubCause(xDirectCause.getSubCause());
    }
    
    // lengthAffected
    if (xDirectCause.isSetLengthAffected()) {
      directCause.setLengthAffected(xDirectCause.getLengthAffected());
    }
    
    // freeText
    if (xDirectCause.getFreeText().size() != 0) {
      directCause.setFreeText(getTextForSelectedLanguage(xDirectCause.getFreeText(), languageCode, languageCountryCode));
    }
    
    // laneRestrictions
    if (xDirectCause.isSetLaneRestrictions()) {
      directCause.setLaneRestrictions(xLaneRestrictions2LaneRestrictions(xDirectCause.getLaneRestrictions()));
    }
    
    // causeOffset
    if (xDirectCause.isSetCauseOffset()) {
      directCause.setCauseOffset(xDirectCause.getCauseOffset());
    }
    
    return directCause;
  }

  private static LaneRestrictions xLaneRestrictions2LaneRestrictions(
      org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.LaneRestrictions xLaneRestrictions) {
    LaneRestrictions laneRestrictions = TI_FACTORY.createLaneRestrictions();
    
    // laneRestrictionType
    laneRestrictions.setLaneRestrictionType(xLaneRestrictions.getLaneRestrictionType());
    
    // numberOfLanes
    if (xLaneRestrictions.isSetNumberOfLanes()) {
      laneRestrictions.setNumberOfLanes(xLaneRestrictions.getNumberOfLanes());
    }
    
    return laneRestrictions;
  }

  private static LinkedCause xLinkedCause2LinkedCause(org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.LinkedCause xLinkedCause) {
    LinkedCause linkedCause = TI_FACTORY.createLinkedCause();
    
    // causeTrafficIncidentId: not available in xTrafficIncident.
    
    return linkedCause;
  }

  private static Advice xAdvice2Advice(org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.Advice xAdvice, String languageCode, String languageCountryCode) {
    Advice advice = TI_FACTORY.createAdvice();
    
    // mainAdvice
    advice.setMainAdvice(xAdvice.getMainAdvice());
    
    // subAdvice
    if (xAdvice.isSetSubAdvice()) {
      advice.setSubAdvice(xAdvice.getSubAdvice());
    }
    
    // freeText
    if (xAdvice.getFreeText().size() != 0) {
      advice.setFreeText(getTextForSelectedLanguage(xAdvice.getFreeText(), languageCode, languageCountryCode));
    }
    
    // applicability
    for (org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.Applicability xApplicability: xAdvice.getApplicability()) {
      advice.getApplicability().add(xApplicability2Applicability(xApplicability));
    }
    
    return advice;
  }

  private static Applicability xApplicability2Applicability(org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.Applicability xApplicability) {
    Applicability applicability = TI_FACTORY.createApplicability();
    
    // vehicleType
    applicability.setVehicleType(xApplicability.getVehicleType());
    
    // vehicleRelatedRestrictions
    for (org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.VehicleRelatedRestriction xVehicleRelatedRestriction: xApplicability.getVehicleRelatedRestrictions()) {
      applicability.getVehicleRelatedRestrictions().add(xVehicleRelatedRestriction2VehicleRelatedRestriction(xVehicleRelatedRestriction));
    }
    
    return applicability;
  }

  private static VehicleRelatedRestriction xVehicleRelatedRestriction2VehicleRelatedRestriction(
      org.genivi.trafficincidentsservice.trafficincidentfeed.trafficincident.VehicleRelatedRestriction xVehicleRelatedRestriction) {
    VehicleRelatedRestriction vehicleRelatedRestriction = TI_FACTORY.createVehicleRelatedRestriction();
    
    // restrictionType
    vehicleRelatedRestriction.setRestrictionType(xVehicleRelatedRestriction.getRestrictionType());
    
    // restrictionValue
    if (xVehicleRelatedRestriction.isSetRestrictionValue()) {
      vehicleRelatedRestriction.setRestrictionValue(xVehicleRelatedRestriction.getRestrictionValue());
    }
    
    // restrictionLocation: not available in xTrafficIncident.
    
    return vehicleRelatedRestriction;
  }

  public static String getTextForSelectedLanguage(List<LocalizedString> localizedStrings, String languageCode, String languageCountryCode) {
    // if languageCountryCode is set, try to find a match on language and country
    if (languageCountryCode != null) {
      for (LocalizedString localizedString: localizedStrings) {
        if (localizedString.getLanguageCode().equals(languageCode)  &&
            localizedString.isSetCountryCode()  &&
            localizedString.getCountryCode().equals(languageCountryCode)) {
          return localizedString.getString();
        }
      }
    }
    
    // try to find a match language only
    for (LocalizedString localizedString: localizedStrings) {
      if (localizedString.getLanguageCode().equals(languageCode)) {
        return localizedString.getString();
      }
    }
    
    // just take the first one
    return localizedStrings.get(0).getString();
  }
}