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 java.nio.charset.StandardCharsets;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Scanner;
26  
27  import javax.jms.Destination;
28  import javax.jms.JMSException;
29  
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.beans.factory.annotation.Autowired;
33  import org.springframework.beans.factory.annotation.Qualifier;
34  import org.springframework.stereotype.Service;
35  
36  import com.hack23.cia.model.external.riksdagen.personlista.impl.PersonElement;
37  import com.hack23.cia.model.internal.application.data.impl.RiksdagenDataSources;
38  import com.hack23.cia.service.external.riksdagen.api.DataFailureException;
39  import com.hack23.cia.service.external.riksdagen.api.RiksdagenPersonApi;
40  
41  /**
42   * The Class RiksdagenPersonsWorkGeneratorImpl.
43   */
44  @Service("RiksdagenPersonsWorkGeneratorImpl")
45  final class RiksdagenPersonsWorkGeneratorImpl extends AbstractRiksdagenDataSourcesWorkGenerator {
46  
47  	/** The Constant LOGGER. */
48  	private static final Logger LOGGER = LoggerFactory.getLogger(RiksdagenPersonsWorkGeneratorImpl.class);
49  
50  	/** The person element workdestination. */
51  	@Autowired
52  	@Qualifier("com.hack23.cia.model.external.riksdagen.personlista.impl.PersonElement")
53  	private Destination personElementWorkdestination;
54  
55  	/** The riksdagen api. */
56  	@Autowired
57  	private RiksdagenPersonApi riksdagenApi;
58  
59  	/**
60  	 * Instantiates a new riksdagen persons work generator impl.
61  	 */
62  	public RiksdagenPersonsWorkGeneratorImpl() {
63  		super(RiksdagenDataSources.PERSONS);
64  	}
65  
66  	@Override
67  	public void generateWorkOrders() {
68  		try {
69  			final List<PersonElement> personList = riksdagenApi.getPersonList().getPerson();
70  			final Map<String, String> currentSaved = getImportService().getPersonMap();
71  
72  			for (final PersonElement personElement : personList) {
73  				if (!currentSaved.containsKey(personElement.getId())) {
74  					LOGGER.info("Send Load Order:{}", personElement.getPersonUrlXml());
75  					getJmsSender().send(personElementWorkdestination, personElement);
76  					currentSaved.put(personElement.getId(), personElement.getId());
77  				}
78  			}
79  			for (final String personId : readMissingPersonList()) {
80  				if (!currentSaved.containsKey(personId)) {
81  					LOGGER.info("Send Load Order:{}{}", "http://data.riksdagen.se/person/", personId);
82  					getJmsSender().send(personElementWorkdestination, new PersonElement().withId(personId));
83  				}
84  			}
85  		} catch (final JMSException | DataFailureException exception) {
86  			LOGGER.warn("jms", exception);
87  		}
88  	}
89  
90  	/**
91  	 * Read missing person list.
92  	 *
93  	 * @return the string[]
94  	 */
95  	private static String[] readMissingPersonList() {
96  
97  		final Scanner sc = new Scanner(RiksdagenPersonsWorkGeneratorImpl.class.getResourceAsStream("/personlist.txt"),StandardCharsets.UTF_8.name());
98  		final List<String> lines = new ArrayList<>();
99  		while (sc.hasNextLine()) {
100 			lines.add(sc.nextLine());
101 		}
102 		sc.close();
103 		return lines.toArray(new String[0]);
104 	}
105 
106 }