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   package com.hack23.cia.service.impl.agent.sweden;
6   
7   import gnu.trove.THashMap;
8   
9   import java.io.BufferedReader;
10  import java.io.File;
11  import java.io.FileInputStream;
12  import java.io.IOException;
13  import java.io.InputStream;
14  import java.io.StringReader;
15  import java.util.List;
16  import java.util.Map;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.apache.poi.hssf.usermodel.HSSFCell;
21  import org.apache.poi.hssf.usermodel.HSSFRow;
22  import org.apache.poi.hssf.usermodel.HSSFSheet;
23  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
24  import org.apache.poi.poifs.filesystem.POIFSFileSystem;
25  import org.springframework.context.ApplicationContext;
26  import org.springframework.context.ApplicationContextAware;
27  import org.springframework.core.io.Resource;
28  
29  import com.hack23.cia.model.sweden.impl.ParliamentMember;
30  import com.hack23.cia.model.sweden.impl.RegisterInformation;
31  
32  /***
33   * The Class ParliamentMemberRegisterAgentImpl.
34   */
35  public class ParliamentMemberRegisterAgentImpl implements
36          ParliamentMemberRegisterAgent, ApplicationContextAware {
37  
38      /*** The Constant LOGGER. */
39      private static final Log LOGGER = LogFactory
40              .getLog(ParliamentMemberRegisterAgentImpl.class);
41  
42      /*** The application context. */
43      private ApplicationContext applicationContext;
44  
45      /*** The current ledamot register. */
46      private final String currentLedamotRegister = "WEB-INF/Ledamotsregister.augusti.2010.xls";
47  
48      /*** The current ledamot register local. */
49      private final String currentLedamotRegisterLocal = "src/main/webapp/WEB-INF/Ledamotsregister.augusti.2010.xls";
50  
51      /*** The register information map. */
52      private final Map<String, RegisterInformation> registerInformationMap = new THashMap<String, RegisterInformation>();
53  
54      /***
55       * Instantiates a new parliament member register agent impl.
56       *
57       * @throws Exception the exception
58       */
59      public ParliamentMemberRegisterAgentImpl() throws Exception {
60          super();
61      }
62  
63      /***
64       * Extract from lines to list.
65       *
66       * @param str the str
67       * @param list the list
68       */
69      private void extractFromLinesToList(final String str,
70              final List<String> list) {
71          final BufferedReader reader = new BufferedReader(new StringReader(str));
72          String line = null;
73          try {
74              while ((line = reader.readLine()) != null) {
75  
76                  if (line.length() > 0) {
77                      list.add(line);
78                  }
79              }
80          } catch (final IOException e) {
81              LOGGER.warn("Problem parsing line", e);
82          }
83  
84      }
85  
86      /*
87       * (non-Javadoc)
88       *
89       * @see
90       * com.hack23.cia.service.agent.sweden.ParliamentMemberRegisterAgent#init()
91       */
92      @Override
93  	public final void init() throws Exception {
94          final Resource resource = applicationContext
95          .getResource(currentLedamotRegister);
96  
97          InputStream resourceAsStream = null;
98          try {
99              resourceAsStream = new FileInputStream(resource.getFile());
100         } catch (final Exception e) {
101             try {
102             resourceAsStream = new FileInputStream(new File(currentLedamotRegisterLocal));
103             } catch (final Exception e2) {
104                 LOGGER.warn("Couldn't read register", e2);
105             return;
106             }
107         }
108 
109         final POIFSFileSystem fs = new POIFSFileSystem(resourceAsStream);
110         final HSSFWorkbook wb = new HSSFWorkbook(fs);
111         final HSSFSheet sheet = wb.getSheetAt(0);
112 
113         for (int r = 1; r < sheet.getPhysicalNumberOfRows(); r++) {
114             HSSFRow row;
115             row = sheet.getRow(r);
116             if (row != null) {
117                 HSSFCell cell;
118                 final RegisterInformation registerInformation = new RegisterInformation();
119 
120                 cell = row.getCell( 0);
121                 if (cell != null) {
122                     registerInformation.setName(cell.getStringCellValue());
123                 }
124                 cell = row.getCell( 1);
125                 if (cell != null) {
126                     registerInformation.setElectoralArea(cell
127                             .getStringCellValue());
128                 }
129                 cell = row.getCell(2);
130                 if (cell != null) {
131                     registerInformation.setParty(cell.getStringCellValue());
132                 }
133                 cell = row.getCell( 3);
134                 if (cell != null) {
135 
136                     extractFromLinesToList(cell.getStringCellValue(),
137                             registerInformation.getStocks());
138                 }
139                 cell = row.getCell(4);
140                 if (cell != null) {
141                     extractFromLinesToList(cell.getStringCellValue(),
142                             registerInformation.getHouses());
143                 }
144                 cell = row.getCell(5);
145                 if (cell != null) {
146                     extractFromLinesToList(cell.getStringCellValue(),
147                             registerInformation.getIncomeActivity());
148                 }
149                 cell = row.getCell(6);
150                 if (cell != null) {
151                     extractFromLinesToList(cell.getStringCellValue(),
152                             registerInformation.getAgreements());
153                 }
154                 cell = row.getCell(7);
155                 if (cell != null) {
156                     extractFromLinesToList(cell.getStringCellValue(),
157                             registerInformation.getRoles());
158                 }
159                 cell = row.getCell( 8);
160                 if (cell != null) {
161                     extractFromLinesToList(cell.getStringCellValue(),
162                             registerInformation.getPublicRoles());
163                 }
164 
165                 cell = row.getCell( 9);
166                 if (cell != null) {
167                     extractFromLinesToList(cell.getStringCellValue(),
168                             registerInformation.getAllowances());
169                 }
170 
171                 cell = row.getCell(10);
172                 if (cell != null) {
173                     registerInformation.setNothingDeclared(cell
174                             .getStringCellValue());
175                 }
176 
177                 final String key = registerInformation.getName().trim()
178                         + registerInformation.getParty().trim();
179                 LOGGER.info("init, added :" + key);
180                 registerInformationMap.put(key, registerInformation);
181 
182             }
183         }
184 
185     }
186 
187     /*
188      * (non-Javadoc)
189      *
190      * @seecom.hack23.cia.service.agent.sweden.ParliamentMemberRegisterAgent#
191      * lookupRegisterInformation(com.hack23.cia.model.sweden.ParliamentMember)
192      */
193     @Override
194     public final RegisterInformation lookupRegisterInformation(
195             final ParliamentMember member) {
196         LOGGER.info("lookupRegisterInformation: " + member.getName());
197 
198         final RegisterInformation registerInformation = this.registerInformationMap
199                 .get(member.getName().trim() + member.getParty().trim());
200         LOGGER.info("found : " + registerInformation);
201         return registerInformation;
202     }
203 
204     /*
205      * (non-Javadoc)
206      *
207      * @see
208      * org.springframework.context.ApplicationContextAware#setApplicationContext
209      * (org.springframework.context.ApplicationContext)
210      */
211     @Override
212     public final void setApplicationContext(final ApplicationContext arg0) {
213         this.applicationContext = arg0;
214     }
215 }