summaryrefslogtreecommitdiff
path: root/src/traffic-incidents-service/org.genivi.trafficinfo/src/org/genivi/locref/refimpl/LocationReferenceDecodingHandlerRefImpl.java
blob: 9faa59a840013b27c64cf8eb14cda43af1a36ef9 (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
/**
 * 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.locref.refimpl;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.genivi.locref.LocationReference;
import org.genivi.locref.LocationReferenceDecodingService;
import org.genivi.locref.LocationReferenceDecodingServiceListener;
import org.genivi.locref.decoder.LocationData;
import org.genivi.locref.decoder.LocationReferenceDecoder;


public class LocationReferenceDecodingHandlerRefImpl extends EObjectImpl implements LocationReferenceDecodingService {
    private final static Logger LOGGER = Logger.getLogger(LocationReferenceDecodingHandlerRefImpl.class.getName()); 
 
    private static final String        DEFAULT_LANGUAGE = "nld";
    private static final String        DEFAULT_COUNTRY = null;
 
    private String languageCode = DEFAULT_LANGUAGE;
    private String countryCode = DEFAULT_COUNTRY;
    Map<Byte, LocationReferenceDecoder> decoders = new HashMap<>();
    

    public LocationReferenceDecodingHandlerRefImpl() {
      LOGGER.setLevel(Level.SEVERE);
    }
    
    @Override
    public void setLanguage(String languageCode, String countryCode) {
      this.languageCode = languageCode;
      this.countryCode = countryCode;
      for (LocationReferenceDecoder decoder: decoders.values()) {
        decoder.setLanguage(languageCode, countryCode);
      }
    }

    @Override
    public void getLanguage(LocationReferenceDecodingServiceListener listener) {
      listener.getLanguageReply(languageCode, countryCode);
    }
    
    public void addDecoder(Byte type, LocationReferenceDecoder decoder) {
      decoder.setLanguage(languageCode, countryCode);
      decoders.put(type, decoder);
    }

    public void removeDecoder(String type) {
      decoders.remove(type);
    }

    @Override
    public void decode(LocationReferenceDecodingServiceListener listener,
        LocationReference locationReference) {
      if (locationReference == null) {
        throw new RuntimeException("LocationReferenceDecodingHandlerRefImpl: locationReference is null");
      }
      Byte locationReferenceType = (Byte) (byte) locationReference.getLocationReferenceType();
      LocationReferenceDecoder decoder = decoders.get(locationReferenceType);
      if (decoder == null) {
          throw new RuntimeException("Location reference type \'" +
                  locationReferenceType.toString() + "\' not supported");
      }
      Object locationReferenceData = locationReference.getData();
      LOGGER.info("Decoding location of type: " + locationReferenceType.toString());
      LocationData locationData = decoder.decode(locationReferenceData);
      
      listener.decodeReply(locationData.getLocation(), locationData.getLocationInfo());
    }
}