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.data;
20  
21  import java.text.ParseException;
22  import java.text.SimpleDateFormat;
23  import java.util.ArrayList;
24  import java.util.Date;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.Map;
28  import java.util.concurrent.ConcurrentHashMap;
29  
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.beans.factory.annotation.Autowired;
33  import org.springframework.stereotype.Component;
34  import org.springframework.transaction.annotation.Propagation;
35  import org.springframework.transaction.annotation.Transactional;
36  
37  import com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentElement;
38  import com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentType;
39  import com.hack23.cia.model.external.riksdagen.votering.impl.VoteDataEmbeddedId;
40  import com.hack23.cia.model.internal.application.system.impl.ApplicationConfiguration;
41  import com.hack23.cia.model.internal.application.system.impl.ConfigurationGroup;
42  import com.hack23.cia.service.data.api.ApplicationConfigurationService;
43  import com.hack23.cia.service.data.api.CommitteeProposalComponentDataDAO;
44  import com.hack23.cia.service.data.api.DocumentContentDataDAO;
45  import com.hack23.cia.service.data.api.DocumentElementDAO;
46  import com.hack23.cia.service.data.api.DocumentStatusContainerDAO;
47  import com.hack23.cia.service.data.api.PersonDataDAO;
48  import com.hack23.cia.service.data.api.VoteDataDAO;
49  
50  /**
51   * The Class RiksdagenImportServiceImpl.
52   */
53  @Component("RiksdagenImportService")
54  @Transactional(propagation = Propagation.MANDATORY)
55  final class RiksdagenImportServiceImpl implements RiksdagenImportService {
56  
57  	/** The Constant LOGGER. */
58  	private static final Logger LOGGER = LoggerFactory.getLogger(RiksdagenImportServiceImpl.class);
59  
60  	/** The Constant RIKSDAGEN_JAVA_SIMPLE_DATE_FORMAT. */
61  	private static final String RIKSDAGEN_JAVA_SIMPLE_DATE_FORMAT = "yyyy-MM-dd";
62  
63  	/** The Constant RIKSDAGEN_JAVA_SIMPLE_DATE_TIME_FORMAT. */
64  	private static final String RIKSDAGEN_JAVA_SIMPLE_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
65  
66  	/** The application configuration service. */
67  	@Autowired
68  	private ApplicationConfigurationService applicationConfigurationService;
69  
70  	/** The committee proposal component data dao. */
71  	@Autowired
72  	private CommitteeProposalComponentDataDAO committeeProposalComponentDataDAO;
73  
74  	/** The document content data dao. */
75  	@Autowired
76  	private DocumentContentDataDAO documentContentDataDAO;
77  
78  	/** The document element dao. */
79  	@Autowired
80  	private DocumentElementDAO documentElementDAO;
81  
82  	/** The document status container dao. */
83  	@Autowired
84  	private DocumentStatusContainerDAO documentStatusContainerDAO;
85  
86  	/** The person data dao. */
87  	@Autowired
88  	private PersonDataDAO personDataDAO;
89  
90  	/** The vote data dao. */
91  	@Autowired
92  	private VoteDataDAO voteDataDAO;
93  
94  	/**
95  	 * Instantiates a new riksdagen import service impl.
96  	 */
97  	public RiksdagenImportServiceImpl() {
98  		super();
99  	}
100 
101 	/**
102 	 * Creates the map from list.
103 	 *
104 	 * @param all
105 	 *            the all
106 	 * @return the map
107 	 */
108 	private static Map<String, String> createMapFromList(final List<String> all) {
109 		final Map<String, String> map = new ConcurrentHashMap<>();
110 
111 		for (final String documentElement : all) {
112 			map.put(documentElement, documentElement);
113 		}
114 		return map;
115 	}
116 
117 	/**
118 	 * Creates the map from list vote.
119 	 *
120 	 * @param list
121 	 *            the list
122 	 * @return the map
123 	 */
124 	private static Map<String, String> createMapFromListVote(final List<VoteDataEmbeddedId> list) {
125 		final Map<String, String> map = new ConcurrentHashMap<>();
126 
127 		for (final VoteDataEmbeddedId documentElement : list) {
128 			map.put(documentElement.getBallotId(), documentElement.getBallotId());
129 		}
130 		return map;
131 	}
132 
133 	@Override
134 	public List<String> getAvaibleCommitteeProposal() {
135 		return documentStatusContainerDAO.getAvaibleCommitteeProposal();
136 	}
137 
138 	@Override
139 	public List<String> getAvaibleDocumentContent() {
140 		return documentElementDAO.getAvaibleDocumentContent();
141 	}
142 
143 	@Override
144 	public Map<String, String> getCommitteeProposalComponentDataMap() {
145 		return createMapFromList(committeeProposalComponentDataDAO.getIdList());
146 	}
147 
148 	@Override
149 	public Map<String, String> getDocumentContentMap() {
150 		return createMapFromList(documentContentDataDAO.getIdList());
151 	}
152 
153 	@Override
154 	public Map<String, String> getDocumentElementMap() {
155 		final List<String> all = documentElementDAO.getIdList();
156 		return createMapFromList(all);
157 	}
158 
159 	@Override
160 	public Map<String, String> getDocumentElementMap(final Date after, final List<DocumentType> downloadType,
161 			final boolean onlyWithDocStatus) {
162 		final List<DocumentElement> all = documentElementDAO.getAll();
163 
164 		final List<String> documentTypeValues = new ArrayList<>();
165 
166 		for (final DocumentType docType : downloadType) {
167 			documentTypeValues.add(docType.value());
168 		}
169 
170 		final Map<String, String> map = new ConcurrentHashMap<>();
171 
172 		for (final DocumentElement documentElement : all) {
173 			if (checkIncludeDate(after, documentTypeValues, documentElement) && checkIncludeStatus(onlyWithDocStatus, documentElement)) {
174 				map.put(documentElement.getId(), documentElement.getDocumentType());
175 			}
176 		}
177 		return map;
178 	}
179 
180 	/**
181 	 * Check include status.
182 	 *
183 	 * @param onlyWithDocStatus
184 	 *            the only with doc status
185 	 * @param documentElement
186 	 *            the document element
187 	 * @return true, if successful
188 	 */
189 	private static boolean checkIncludeStatus(final boolean onlyWithDocStatus, final DocumentElement documentElement) {
190 		return !onlyWithDocStatus || documentElement.getDocumentStatusUrlXml() != null;
191 	}
192 
193 	/**
194 	 * Check include date.
195 	 *
196 	 * @param after
197 	 *            the after
198 	 * @param documentTypeValues
199 	 *            the document type values
200 	 * @param documentElement
201 	 *            the document element
202 	 * @return true, if successful
203 	 */
204 	private static boolean checkIncludeDate(final Date after, final List<String> documentTypeValues,
205 			final DocumentElement documentElement) {
206 		try {
207 			if (documentElement.getMadePublicDate() != null && documentElement.getMadePublicDate().length() > 0) {
208 				return getDate(documentElement.getMadePublicDate()).after(after)
209 						&& documentTypeValues.contains(documentElement.getDocumentType());
210 			} else {
211 				return getDate(documentElement.getCreatedDate()).after(after)
212 						&& documentTypeValues.contains(documentElement.getDocumentType());				
213 			}			
214 		} catch (final ParseException e) {
215 			LOGGER.warn("Problem getting date from{} : exception:{}", documentElement, e);
216 			return false;
217 		}
218 	}
219 
220 	/**
221 	 * Gets the date.
222 	 *
223 	 * @param date
224 	 *            the date
225 	 * @return the date
226 	 * @throws ParseException
227 	 *             the parse exception
228 	 */
229 	private static Date getDate(final String date) throws ParseException {
230 		if (RIKSDAGEN_JAVA_SIMPLE_DATE_FORMAT.length() > date.length()) {
231 			return new SimpleDateFormat(RIKSDAGEN_JAVA_SIMPLE_DATE_TIME_FORMAT, Locale.ENGLISH).parse(date);
232 		} else {
233 			return new SimpleDateFormat(RIKSDAGEN_JAVA_SIMPLE_DATE_FORMAT, Locale.ENGLISH).parse(date);
234 		}
235 	}
236 
237 	@Override
238 	public Map<String, String> getDocumentStatusContainerMap() {
239 		return createMapFromList(documentStatusContainerDAO.getIdList());
240 	}
241 
242 	@Override
243 	public Map<String, String> getLoadedBallotIdMap() {
244 		return createMapFromListVote(voteDataDAO.getBallotIdList());
245 	}
246 
247 	@Override
248 	public Map<String, String> getPersonMap() {
249 		return createMapFromList(personDataDAO.getIdList());
250 	}
251 
252 	@Override
253 	public int getStartYearForDocumentElement() {
254 		final ApplicationConfiguration registeredUsersGetAdminConfig = applicationConfigurationService
255 				.checkValueOrLoadDefault("Load Riksdagen documents from year", "Load Riksdagen documents from year",
256 						ConfigurationGroup.AGENT, RiksdagenImportService.class.getSimpleName(),
257 						"Riksdagen Import Service", "Responsible import Riksdagen data",
258 						"agent.riksdagen.documents.loadfromyear", "2000");
259 
260 		return documentElementDAO
261 				.getMissingDocumentStartFromYear(Integer.parseInt(registeredUsersGetAdminConfig.getPropertyValue()));
262 	}
263 
264 }