View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.service.component.agent.impl.worldbank.workgenerator;
20  
21  import java.io.Serializable;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.jms.Destination;
27  import javax.jms.JMSException;
28  
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.beans.factory.annotation.Autowired;
32  import org.springframework.beans.factory.annotation.Qualifier;
33  import org.springframework.stereotype.Service;
34  
35  import com.hack23.cia.model.external.worldbank.indicators.impl.IndicatorElement;
36  import com.hack23.cia.model.internal.application.data.impl.WorldBankDataSources;
37  import com.hack23.cia.model.internal.application.system.impl.ApplicationConfiguration;
38  import com.hack23.cia.model.internal.application.system.impl.ConfigurationGroup;
39  import com.hack23.cia.service.data.api.ApplicationConfigurationService;
40  
41  /**
42   * The Class WorldBankDataWorkGeneratorImpl.
43   */
44  @Service("WorldBankDataWorkGeneratorImpl")
45  final class WorldBankDataWorkGeneratorImpl extends AbstractWorldBankDataSourcesWorkGenerator {
46  
47  	/** The Constant LOGGER. */
48  	private static final Logger LOGGER = LoggerFactory.getLogger(WorldBankDataWorkGeneratorImpl.class);
49  
50  	/** The data workdestination. */
51  	@Autowired
52  	@Qualifier("com.hack23.cia.model.external.worldbank.data.impl.Data")
53  	private Destination dataWorkdestination;
54  
55  	/** The application configuration service. */
56  	@Autowired
57  	private ApplicationConfigurationService applicationConfigurationService;
58  
59  	/**
60  	 * Instantiates a new world bank data work generator impl.
61  	 */
62  	public WorldBankDataWorkGeneratorImpl() {
63  		super(WorldBankDataSources.DATA);
64  	}
65  
66  	@Override
67  	public void generateWorkOrders() {
68  
69  		try {
70  			final ApplicationConfiguration importDataForCountries = applicationConfigurationService.checkValueOrLoadDefault("Countries to import data from worldbank (isocode) alt comma separated list", "Load worldbank data for countries", ConfigurationGroup.AGENT, WorldBankCountryWorkGeneratorImpl.class.getSimpleName(), "Worldbank country data loading", "Responsible import worldlbank country data", "agent.worldbank.country.data.loadCountries", "SE");
71  
72  			final List<IndicatorElement> indicatorlist = getImportService().getAllIndicators();
73  			final Map<String, String> currentSaved = getImportService().getWorldBankDataMap();
74  
75  			for (final String country : getImportService().getWorldBankCountryMap().keySet()) {
76  				if (importDataForCountries.getPropertyValue().equalsIgnoreCase(country)) {
77  					for (final IndicatorElement indicator : indicatorlist) {
78  						sendCountryIndicatorWorkOrder(currentSaved, indicator, country);
79  					}
80  				}
81  			}
82  
83  		} catch (final JMSException exception) {
84  			LOGGER.warn("jms", exception);
85  		}
86  	}
87  
88  	/**
89  	 * Send country indicator work order.
90  	 *
91  	 * @param currentSaved
92  	 *            the current saved
93  	 * @param indicator
94  	 *            the indicator
95  	 * @param country
96  	 *            the country
97  	 * @throws JMSException
98  	 *             the exception
99  	 */
100 	private void sendCountryIndicatorWorkOrder(final Map<String, String> currentSaved, final IndicatorElement indicator,
101 			final String countryIso2Code) throws JMSException {
102 		if (countryIso2Code != null && countryIso2Code.length() > 0
103 				&& !currentSaved.containsKey(countryIso2Code + '.' + indicator.getId())) {
104 			final List<String> load = new ArrayList<>();
105 			load.add(countryIso2Code);
106 			load.add(indicator.getId());
107 			getJmsSender().send(dataWorkdestination, (Serializable) load);
108 		}
109 	}
110 
111 }