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.command;
20  
21  import javax.jms.Destination;
22  import javax.jms.JMSException;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  import org.springframework.beans.factory.annotation.Autowired;
27  import org.springframework.beans.factory.annotation.Qualifier;
28  import org.springframework.stereotype.Service;
29  import org.springframework.transaction.annotation.Propagation;
30  import org.springframework.transaction.annotation.Transactional;
31  
32  import com.hack23.cia.model.internal.application.data.impl.DataAgentWorkOrder;
33  import com.hack23.cia.model.internal.application.data.impl.RiksdagenDataSources;
34  import com.hack23.cia.model.internal.application.data.impl.WorldBankDataSources;
35  import com.hack23.cia.service.component.agent.api.DataAgentApi;
36  import com.hack23.cia.service.component.agent.impl.common.jms.JmsSender;
37  
38  /**
39   * The Class DataAgentApiImpl.
40   */
41  @Service
42  @Transactional(propagation=Propagation.REQUIRED)
43  final class DataAgentApiImpl implements DataAgentApi {
44  
45  	/** The Constant LOGGER. */
46  	private static final Logger LOGGER = LoggerFactory
47  			.getLogger(DataAgentApiImpl.class);
48  
49  	/** The jms template. */
50  	@Autowired
51  	private JmsSender jmsSender;
52  
53  	/** The riksdagen api destination. */
54  	@Autowired
55  	@Qualifier("riksdagenApiAgentWorkQueue")
56  	private Destination riksdagenApiDestination;
57  
58  	/** The world bank api destination. */
59  	@Autowired
60  	@Qualifier("worldbankApiAgentWorkQueue")
61  	private Destination worldBankApiDestination;
62  
63  	/**
64  	 * Instantiates a new data agent api impl.
65  	 */
66  	public DataAgentApiImpl() {
67  		super();
68  	}
69  
70  	@Override
71  	public void execute(final DataAgentWorkOrder workOrder) {
72  		try {
73  			sendMessage(workOrder);
74  		} catch (final JMSException e) {
75  			LOGGER.error("Jms exception:", e);
76  		}
77  	}
78  
79  	/**
80  	 * Send message.
81  	 *
82  	 * @param workOrder
83  	 *            the work order
84  	 * @throws JMSException
85  	 *             the JMS exception
86  	 */
87  	public void sendMessage(final DataAgentWorkOrder workOrder)
88  			throws JMSException {
89  
90  		switch (workOrder.getTarget()) {
91  		case MODEL_EXTERNAL_RIKSDAGEN:
92  			for (final RiksdagenDataSources datasource :RiksdagenDataSources.values()) {
93  				jmsSender.send(riksdagenApiDestination,	datasource);
94  			}
95  			break;
96  		case MODEL_EXTERNAL_WORLDBANK:
97  			for (final WorldBankDataSources datasource :WorldBankDataSources.values()) {
98  				jmsSender.send(worldBankApiDestination,datasource);
99  			}
100 			break;
101 		default:
102 			break;
103 		}
104 	}
105 
106 }