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.workgenerator;
20  
21  import javax.jms.Destination;
22  import javax.jms.JMSException;
23  
24  import org.joda.time.DateTime;
25  import org.joda.time.format.DateTimeFormat;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.beans.factory.annotation.Qualifier;
30  import org.springframework.stereotype.Service;
31  
32  import com.hack23.cia.model.internal.application.data.impl.RiksdagenDataSources;
33  import com.hack23.cia.service.component.agent.impl.riksdagen.workers.LoadDocumentWork;
34  
35  /**
36   * The Class RiksdagenDocumentListWorkGeneratorImpl.
37   */
38  @Service("RiksdagenDocumentListWorkGeneratorImpl")
39  final class RiksdagenDocumentListWorkGeneratorImpl extends AbstractRiksdagenDataSourcesWorkGenerator {
40  
41  	/** The Constant LOGGER. */
42  	private static final Logger LOGGER = LoggerFactory.getLogger(RiksdagenDocumentListWorkGeneratorImpl.class);
43  
44  	/** The document element workdestination. */
45  	@Autowired
46  	@Qualifier("com.hack23.cia.service.component.agent.impl.riksdagen.workers.LoadDocumentWork")
47  	private Destination loadDocumentWorkdestination;
48  
49  	/**
50  	 * Instantiates a new riksdagen document list work generator impl.
51  	 */
52  	public RiksdagenDocumentListWorkGeneratorImpl() {
53  		super(RiksdagenDataSources.DOCUMENT_LIST);
54  	}
55  
56  	@Override
57  	public void generateWorkOrders() {
58  		try {
59  
60  			final int startYearForDocumentElement = getImportService().getStartYearForDocumentElement();
61  
62  			final org.joda.time.format.DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");
63  			DateTime fromDateTime = fmt.parseDateTime(startYearForDocumentElement + "-01-01");
64  
65  			DateTime loadedWeekDate = fmt.parseDateTime(startYearForDocumentElement + "-01-01");
66  
67  			final DateTime toDate = new DateTime();
68  			while (loadedWeekDate.isBefore(toDate)) {
69  				loadedWeekDate = loadedWeekDate.plusWeeks(1);
70  
71  				getJmsSender().send(loadDocumentWorkdestination,
72  						new LoadDocumentWork(fmt.print(fromDateTime), fmt.print(loadedWeekDate)));
73  
74  				fromDateTime = fromDateTime.plusWeeks(1);
75  			}
76  
77  		} catch (final JMSException e) {
78  			LOGGER.warn("error generating work for loading documents", e);
79  		}
80  	}
81  
82  }