/**
*
*/
package com.cin.service;
import java.rmi.RemoteException;
import java.util.List;
import java.util.logging.Level;
import javax.naming.NamingException;
import com.cin.dto.GenericResponse;
import com.cin.dto.IndustryDTO;
import com.cin.dto.OccupationDTO;
import com.cin.dto.StatisticsDTO;
import com.cin.dto.UserDTO;
import com.cin.ejb.transactionejb.TransactionRemote;
import com.cin.ejb.transactionejb.UserFactory;
import com.cin.exceptions.PersistenceException;
import com.cin.jndi.lookup.EJBLookup;
import com.cin.log.EventLogger;
/**
* @author arunsanjay
*
*/
public class StatisticsService {
private static int resultSize = 5;
static TransactionRemote aRemote;
static UserFactory userFactory;
static {
try {
aRemote = EJBLookup.getInstance().getTransactionEJB();
userFactory = EJBLookup.getInstance().getUserFactory();
}
catch(NamingException pException) {
EventLogger.getInstance().log(Level.SEVERE, pException.getMessage());
}
catch(RemoteException pException) {
EventLogger.getInstance().log(Level.SEVERE, pException.getMessage());
}
}
public GenericResponse calculateWeeklyWage(StatisticsDTO pDTO) throws PersistenceException {
List<UserDTO> theUsers = aRemote.findForAvgWage(pDTO);
GenericResponse aResponse = new GenericResponse();
if(theUsers != null && !theUsers.isEmpty()) {
StaticDataService aStaticService = new StaticDataService();
int anAvgWage = aStaticService.calculateMeanWeekWage(theUsers);
pDTO.setAvgWeekWage(anAvgWage);
aResponse.setStatistics(pDTO);
aResponse.setResponseStatus(true);
}
else {
aResponse.setErrorMessage("No users found for criteria");
}
return aResponse;
}
public GenericResponse calculateAverageIncome(StatisticsDTO pStatistics) throws PersistenceException {
GenericResponse aResponse = new GenericResponse();
StatisticsDTO statistics = new StatisticsDTO();
List<UserDTO> theUsers = userFactory.findForIncomeStatistics(pStatistics);
double income = 0;
int avgIncome = 0;
if(theUsers != null && !theUsers.isEmpty()) {
for(UserDTO user : theUsers) {
income = income + user.getIncome();
}
avgIncome = (int)income/theUsers.size();
statistics.setAvgIncome(avgIncome);
aResponse.setStatistics(statistics);
aResponse.setResponseStatus(true);
}
else {
aResponse.setResponseStatus(false);
aResponse.setErrorMessage("No users found for given criteria");
}
return aResponse;
}
/**
* Finds largest industries in a given state.
* @param stateName
* @param resultSize Maximum number of industries returned.
* @return List sorted by industry size in descending order.
*/
public List<IndustryDTO> getLargestIndustriesInState(String stateName) throws PersistenceException {
// TODO: add error checking: statename = null
List<IndustryDTO> list;
String zipCode = null;
String stateAbbv = aRemote.getStateAbbv(stateName);
list = aRemote.getLargestIndustries(resultSize, stateAbbv, zipCode);
return list;
}
/**
* Finds largest occupations in a given state.
* @param stateName
* @param resultSize Maximum number of occupations returned.
* @return List sorted by occupation size in descending order.
*/
public List<OccupationDTO> getLargestOccupationsInState(String stateName) throws PersistenceException {
// TODO: add error checking: statename = null
List<OccupationDTO> list;
String zipCode = null;
String stateAbbv = aRemote.getStateAbbv(stateName);
list = aRemote.getLargestOccupations(resultSize, stateAbbv, zipCode);
return list;
}
public List<String> getPrincipleStateForIndustry(int industryCode){
// TODO: add error checking: industry code exists in DB
Integer occupationCode = null;
List<String> states = aRemote.getPrincipleStates(resultSize, industryCode, occupationCode);
return states;
}
public List<String> getPrincipleStateForOccupation(int occupationCode){
// TODO: add error checking: occupation code exists in DB
Integer industryCode = null;
List<String> states = aRemote.getPrincipleStates(resultSize, industryCode, occupationCode);
return states;
}
}