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.riksdagen.workers;
20  
21  import javax.jms.Destination;
22  import javax.jms.JMSException;
23  import javax.jms.Message;
24  import javax.jms.MessageListener;
25  import javax.jms.ObjectMessage;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.beans.factory.annotation.Autowired;
30  import org.springframework.beans.factory.annotation.Qualifier;
31  import org.springframework.stereotype.Service;
32  import org.springframework.transaction.annotation.Transactional;
33  
34  import com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentElement;
35  import com.hack23.cia.service.component.agent.impl.common.jms.JmsSender;
36  import com.hack23.cia.service.external.common.api.ProcessDataStrategy;
37  import com.hack23.cia.service.external.riksdagen.api.DataFailureException;
38  import com.hack23.cia.service.external.riksdagen.api.RiksdagenDocumentApi;
39  
40  /**
41   * The Class RiksdagenLoadDocumentWorkConsumerImpl.
42   */
43  @Service("riksdagenLoadDocumentWorkConsumerImpl")
44  @Transactional
45  final class RiksdagenLoadDocumentWorkConsumerImpl implements
46  MessageListener {
47  
48  	/** The Constant LOGGER. */
49  	private static final Logger LOGGER = LoggerFactory
50  			.getLogger(RiksdagenLoadDocumentWorkConsumerImpl.class);
51  
52  
53  	/** The document element workdestination. */
54  	@Autowired
55  	@Qualifier("com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentElement")
56  	private Destination documentElementWorkdestination;
57  
58  	/** The riksdagen api. */
59  	@Autowired
60  	private RiksdagenDocumentApi riksdagenApi;
61  
62  	/** The jms template. */
63  	@Autowired
64  	private JmsSender jmsSender;
65  
66  
67  	/**
68  	 * Instantiates a new riksdagen load document work consumer impl.
69  	 */
70  	public RiksdagenLoadDocumentWorkConsumerImpl() {
71  		super();
72  	}
73  
74  	@Override
75  	public void onMessage(final Message message) {
76  		try {
77  			final LoadDocumentWork work = (LoadDocumentWork) ((ObjectMessage) message).getObject();
78  
79  			riksdagenApi.processDocumentList(work.getFromDate(),work.getToDate(),new DocumentElementWorkProducer());
80  
81  		} catch (final DataFailureException | RuntimeException | JMSException e) {
82  			LOGGER.warn("Error loading riksdagen document" , e);
83  		}
84  	}
85  
86  
87  	/**
88  	 * The Class DocumentElementWorkProducer.
89  	 */
90  	private class DocumentElementWorkProducer implements ProcessDataStrategy<DocumentElement> {
91  
92  		@Override
93  		public void process(final DocumentElement t) {
94  			try {
95  				jmsSender.send(documentElementWorkdestination, t);
96  			} catch (final JMSException e) {
97  				LOGGER.warn("Error proccessing documentElement",e);
98  			}
99  		}
100 	}
101 
102 }