View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
4   */
5   
6   package com.hack23.cia.service.impl.admin;
7   
8   import gnu.trove.THashMap;
9   
10  import java.util.Date;
11  import java.util.List;
12  import java.util.Map;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  import com.hack23.cia.model.sweden.impl.Ballot;
18  import com.hack23.cia.model.sweden.impl.CommitteeReport;
19  import com.hack23.cia.model.sweden.impl.ParliamentMember;
20  import com.hack23.cia.model.sweden.impl.PoliticalParty;
21  import com.hack23.cia.service.impl.agent.sweden.BallotAgent;
22  import com.hack23.cia.service.impl.agent.sweden.CommitteeReportAgent;
23  import com.hack23.cia.service.impl.agent.sweden.ParliamentMemberAgent;
24  import com.hack23.cia.service.impl.agent.sweden.PoliticalPartyAgent;
25  
26  /***
27   * The Class LoaderTask.
28   */
29  public class LoaderTask implements Runnable {
30  
31      /*** The Constant BET_2007_08. */
32      private static final String BET_2010_11 = "bet=2010"; //$NON-NLS-1$
33  
34      /*** The Constant LOGGER. */
35      private static final Log LOGGER = LogFactory.getLog(LoaderTask.class);
36  
37      /*** The ballot agent. */
38      private final BallotAgent ballotAgent;
39      
40      /*** The commitee report agent. */
41      private final CommitteeReportAgent commiteeReportAgent;
42  
43      /*** The disabled. */
44      private final boolean disabled;
45  
46      /*** The loader service. */
47      private final LoaderService loaderService;
48  
49      /*** The parliament member agent. */
50      private final ParliamentMemberAgent parliamentMemberAgent;
51      
52      /*** The political party agent. */
53      private final PoliticalPartyAgent politicalPartyAgent;
54  
55      /***
56       * Instantiates a new loader task.
57       *
58       * @param commiteeReportAgent the commitee report agent
59       * @param ballotAgent the ballot agent
60       * @param parliamentMemberAgent the parliament member agent
61       * @param politicalPartyAgent the political party agent
62       * @param loaderService the loader service
63       * @param disabled the disabled
64       */
65      public LoaderTask(final CommitteeReportAgent commiteeReportAgent,final BallotAgent ballotAgent,
66              final ParliamentMemberAgent parliamentMemberAgent,final PoliticalPartyAgent politicalPartyAgent,
67              final LoaderService loaderService, final boolean disabled) {
68          super();
69          this.commiteeReportAgent = commiteeReportAgent;
70  		this.ballotAgent = ballotAgent;
71          this.parliamentMemberAgent = parliamentMemberAgent;
72  		this.politicalPartyAgent = politicalPartyAgent;
73          this.loaderService = loaderService;
74          this.disabled = disabled;
75      }
76  
77      /*
78       * (non-Javadoc)
79       * 
80       * @see java.lang.Runnable#run()
81       */
82      @Override
83      public final void run() {
84          LOGGER.info("Job Started"); //$NON-NLS-1$
85  
86          if (!disabled) {
87              try {
88              	
89              	try {            		
90              		this.parliamentMemberAgent.initData();            		
91              	} catch (final Exception e) {
92  					LOGGER.error("Problem importing wiki data" );
93  				}
94  
95              	
96  //            	try {
97  //            		final List<PoliticalParty> politicalParties = politicalPartyAgent.getCurrentList();
98  //            		
99  //            		for (final PoliticalParty politcalParty : politicalParties) {
100 //            			loaderService.addIfNotExist(politcalParty);
101 //            		}            		
102 //            	} catch (final Exception e) {
103 //					LOGGER.error("Problem importing parties" );
104 //				}
105 
106 
107             	try {
108             		final List<CommitteeReport> currentList = commiteeReportAgent
109             		.getCurrentList();
110             		
111             		for (final CommitteeReport commiteeReport : currentList) {
112             			loaderService.addIfNotExist(commiteeReport);
113             		}            		
114             	} catch (final Exception e) {
115 					LOGGER.error("Problem importing part:" );
116 				}
117 
118             	
119             	try {
120             		for (final CommitteeReport commiteeReport : loaderService
121             				.getAllCreatedCommitteeReports()) {
122             			
123             			if (commiteeReport.getHref().contains(BET_2010_11)) {
124             				final Date decidedDateIfAny = commiteeReportAgent
125             				.getDecidedDateIfAny(commiteeReport);
126             				if (decidedDateIfAny != null) {
127             					loaderService.addCommitteeReportInformation(
128             							commiteeReport.getId(), decidedDateIfAny,
129             							ballotAgent
130             							.findBallots(commiteeReport));
131             				}
132             			}
133             		}
134             		
135             		
136             	} catch (final Exception e) {
137 					LOGGER.error("Problem importing part:" );
138 				}
139 
140 
141                 
142             	try {
143             		
144             		for (final Ballot ballot : loaderService.getAllCreatedBallotar()) {
145             			try {
146             				if (ballot.getCommiteeReport().getHref().contains(
147             						BET_2010_11)) {
148             					loaderService.addBallotInformation(ballot.getId(),
149             							ballotAgent.getVoteResult(ballot));
150             				}
151             			} catch (final RuntimeException runtimeException) {
152             				LOGGER
153             				.error(
154             						"Rollback done : Ballot problem " + ballot.getDescription() + " " + ballot.getOverviewHref() + " " + ballot.getVoteResultsHref(), runtimeException); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
155             			}
156             			
157             		}
158             		
159             	} catch (final Exception e) {
160 					LOGGER.error("Problem importing part:" );
161 				}
162 
163             	
164             	try {
165             		
166             		this.loaderService.generateParliamentCharts();
167             		
168             	} catch (final Exception e) {
169 					LOGGER.error("Problem importing part:" );
170 				}
171 
172 
173             	try {
174             		for (final PoliticalParty politicalParty : this.loaderService.getAllPoliticalParties()) {
175             			this.loaderService.generatePoliticalPartyCharts(politicalParty.getId());
176             		}
177             		
178             		for (final Ballot ballot : this.loaderService.getAllBallots()) {
179             			this.loaderService.generateBallotCharts(ballot.getId());
180             		}
181             		
182             		//            	this.loaderService.generateCommitteeReportCharts();
183             		//            	
184             		
185             		for (final ParliamentMember parliamentMember : loaderService.getCurrentList()) {
186             			this.loaderService.generateParliamentMemberCharts(parliamentMember.getId());
187             		}
188             		
189             		
190             	} catch (final Exception e) {
191 					LOGGER.error("Problem importing part:" );
192 				}
193 
194             	
195             	try {
196             		
197             		updateRiksLedamotsHomePagesRef();
198             		updateRiksLedamotsWikiRef();
199             		updateRiksLedamotsEnglishWikiRef();
200             		
201             	} catch (final Exception e) {
202 					LOGGER.error("Problem importing part:" );
203 				}
204 
205             	try {
206                     this.loaderService.deleteParliamentMemberVoteCompareResults();
207                     LOGGER.info("Deleted ParliamentMemberVoteCompareResults"); //$NON-NLS-1$
208                     this.loaderService.createParliamentMemberVoteCompareResults();
209                     LOGGER.info("Created ParliamentMemberVoteCompareResults"); //$NON-NLS-1$            		
210             	} catch (final Exception e) {
211 					LOGGER.error("Problem importing part:" );
212 				}
213             	
214 
215             	
216             } catch (final Exception e) {
217                 LOGGER.error("Job Failed", e); //$NON-NLS-1$
218             }
219         }
220         LOGGER.info("Job Ended"); //$NON-NLS-1$
221     }
222 
223     /***
224      * Update riks ledamots english wiki ref.
225      */
226     private void updateRiksLedamotsEnglishWikiRef() {
227         final Map<Long, String> updates = new THashMap<Long, String>();
228         for (final ParliamentMember parliamentMember : loaderService.getCurrentList()) {
229             if (parliamentMember.getEnglishWikiHref() == null) {
230                 final String href = this.parliamentMemberAgent
231                         .getEnglishWikiHref(parliamentMember);
232                 if (href != null) {
233                     updates.put(parliamentMember.getId(), href);
234                 }
235             }
236         }
237         loaderService.updateParliamentMembersEnglishWikiHref(updates);
238     }
239 
240     /***
241      * Update riks ledamots home pages ref.
242      */
243     private void updateRiksLedamotsHomePagesRef() {
244         final Map<Long, String> updates = new THashMap<Long, String>();
245         for (final ParliamentMember parliamentMember : loaderService.getCurrentList()) {
246             if (parliamentMember.getHref() == null) {
247                 final String href = this.parliamentMemberAgent
248                         .getHref(parliamentMember);
249                 if (href != null) {
250                     updates.put(parliamentMember.getId(), href);
251                 }
252             }
253         }
254         loaderService.updateParliamentMembersHref(updates);
255     }
256 
257     /***
258      * Update riks ledamots wiki ref.
259      */
260     private void updateRiksLedamotsWikiRef() {
261         final Map<Long, String> updates = new THashMap<Long, String>();
262         for (final ParliamentMember parliamentMember : loaderService.getCurrentList()) {
263             if (parliamentMember.getWikiHref() == null) {
264                 final String href = this.parliamentMemberAgent
265                         .getWikiHref(parliamentMember);
266                 if (href != null) {
267                     updates.put(parliamentMember.getId(), href);
268                 }
269             }
270         }
271         loaderService.updateParliamentMembersWikiHref(updates);
272     }
273 }