Subversion Repositories SmartDukaan

Rev

Rev 23122 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.support.controllers;

import in.shop2020.logistics.LogisticsLocationInfo;
import in.shop2020.logistics.LogisticsService.Client;
//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
import in.shop2020.support.utils.ReportsUtils;
//End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
import in.shop2020.thrift.clients.LogisticsClient;

import javax.servlet.http.HttpServletRequest;
//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
import javax.servlet.http.HttpSession;
//End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
//End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
import com.opensymphony.xwork2.ActionSupport;
//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
//End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
import java.util.List;

@SuppressWarnings("serial")
public class PincodeController extends ActionSupport implements ServletRequestAware {
        
        private Long provider;
        private String pincode;
        private String destCode;
        private String exp;
        private String cod;
        private String otg;
        private String stationType;
        //Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
        private String runCompleteUpdate;
        
        private HttpServletRequest request;
    private HttpSession session;
    //End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
        
        private boolean codService = false;
        private boolean expService = false;
        private boolean otgService = false;
        private int stationType_int = 1;
        //Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
        private boolean runCompleteUpdateValue = false;
        
        private File recordsFile;
        //End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
        private static Logger logger = LoggerFactory.getLogger(PincodeController.class);
        //Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
    private String errorMsg = "";
        private String successmsg = "";

    public String index() {
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))
            return "authfail";
        checkForErrors();
        return "authsuccess";
    }
        //End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
    
        @Override
        public void setServletRequest(HttpServletRequest request) {
                // TODO Auto-generated method stub
                //Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
                this.request = request;
        this.session = request.getSession();            
        //End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
        }

        //Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
        public String create(){
                if(runCompleteUpdate!=null && runCompleteUpdate.equals("on")) {
                        runCompleteUpdateValue = true;
                } else {
                        provider = -1L;
                }
                File fileToCreate = null;
                try {
                    fileToCreate = new File("/tmp/", this.recordsFile.getName());
                    FileUtils.copyFile(this.recordsFile, fileToCreate);
                } catch (Exception e) {
                   logger.error("Error while writing file used to the local file system", e);
                   addActionError("Unable to open the Uploaded File. Please check the file format.");
                   return "Error while writing file used to the local file system";
                }
                if(fileToCreate.isFile()){
                        HSSFWorkbook wb = null;
                        try{
                                wb = new HSSFWorkbook(new FileInputStream(fileToCreate));
                        }
                        catch (FileNotFoundException e) {
                    logger.error("Unable to open the Uploaded File", e);
                    addActionError("Unable to open the Uploaded File. Please check the file format.");
                } catch (IOException e) {
                    logger.error("Unable to open the Uploaded File", e);
                    addActionError("Unable to open the Uploaded File. Please check the file format.");
                }
                catch (Exception e){
                        logger.error("Supplied Data is not in specified form. Please check the file format",e);
                        addActionError("Supplied Data is not in specified form. Please check the file format");
                }
                if(checkForErrors())
                    return "Error in updation";
                
                HSSFSheet sheet= wb.getSheetAt(0);
                Iterator<Row> rowIterator = sheet.rowIterator();
                int rowCount = 0;
                List<LogisticsLocationInfo> logisticsLocationInfoList;
                logisticsLocationInfoList = new ArrayList<LogisticsLocationInfo>();
                while(rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    if(rowCount++==0)
                        continue;
                    Iterator<Cell> cellIterator = row.cellIterator();
                    int cellCount= 0;
                    LogisticsLocationInfo logisticsLocationInfo = new LogisticsLocationInfo();
                    while(cellIterator.hasNext()) {
                         
                        Cell cell = cellIterator.next();
                        String cellVal_Str ="";
                        Long cellVal_Num  = 0L;
                        boolean cellVal_bool = false;
                        
                        switch(cell.getCellType()) {
                            case Cell.CELL_TYPE_BOOLEAN:
                                cellVal_bool = cell.getBooleanCellValue();
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                cellVal_Num= (long) cell.getNumericCellValue();
                                break;
                            case Cell.CELL_TYPE_STRING:
                                cellVal_Str = cell.getStringCellValue();
                                break;
                        }
                        switch(cellCount) {
                                case 0:
                                        long provider_id = (long) cellVal_Num;
                                        if (provider_id != provider) break;
                                        logisticsLocationInfo.setProviderId((long) cellVal_Num);
                                        break;
                                case 1:
                                        pincode = cellVal_Str.length() > 0 ? cellVal_Str : cellVal_Num +"";
                                        logisticsLocationInfo.setPinCode(pincode);
                                        break;
                                case 2:
                                        destCode = cellVal_Str.length() > 0 ? cellVal_Str : cellVal_Num +"";
                                        logisticsLocationInfo.setDestinationCode(destCode);
                                        break;
                                case 3:
                                        if(cellVal_Num==0L)
                                                expService = false;
                                        else if(cellVal_Num==1L)
                                                expService = true;
                                        else 
                                                expService = false;
                                        logisticsLocationInfo.setExpAvailable(expService);
                                        break;
                                case 4:
                                        if(cellVal_Num==0L)
                                                codService = false;
                                        else if(cellVal_Num==1L)
                                                codService = true;
                                        else 
                                                codService = false;
                                        logisticsLocationInfo.setCodAvailable(codService);
                                        break;
                                case 5:
                                        if(cellVal_Num==0L)
                                                otgService = false;
                                        else if(cellVal_Num==1L)
                                                otgService = true;
                                        else 
                                                otgService = false;
                                        logisticsLocationInfo.setOtgAvailable(otgService);
                                        break;
                                case 6:
                                        logisticsLocationInfo.setCodLimit((long) cellVal_Num);
                                        break;
                                case 7:
                                        logisticsLocationInfo.setPrepaidLimit((long) cellVal_Num);
                                        break;
                                case 8:
                                        logisticsLocationInfo.setWarehouseId((long) cellVal_Num);
                                        break;
                                case 9:
                                        logisticsLocationInfo.setDeliveryTime((long) cellVal_Num);
                                        break;
                                case 10:
                                        logisticsLocationInfo.setDelivery_delay((long) cellVal_Num);
                                        break;
                                case 11:
                                        logisticsLocationInfo.setZoneCode(cellVal_Str);
                                        break;
                        }
                        if(logisticsLocationInfo.getProviderId()==0l) break;
                       cellCount++;
                    }
                    logisticsLocationInfoList.add(logisticsLocationInfo);
                }
                try{
                Client logisticsClient = new LogisticsClient().getClient();
                    logisticsClient.runLogisticsLocationInfoUpdate(logisticsLocationInfoList, runCompleteUpdateValue, provider);
            }
            catch (TException e) {
                logger.error("Unable to update data", e);
                addActionError(e.getMessage());
            }
            if(checkForErrors())
                    return "Error in updation";
                
                }
                else{
                        addActionError("Uncompatible File Format !!!");
                        if(checkForErrors())
                    return "Error in updation";
                }
                if(!checkForErrors()){
                        successmsg="Pincode Updated Successfully";
                }
                
                return "Pincode Updated Successfully";
        
        }
        //End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
        
        public String addPincode() throws TException{
                setBooleanServiceability();
                Client logisticsClient = new LogisticsClient().getClient();
                logisticsClient.addPincode(provider, pincode, destCode, expService, codService, stationType_int, otgService);
                return null;
        }

        public String updatePincode() throws TException{
                setBooleanServiceability();
                logger.info("provider = " + provider + " pincode " + pincode + " exp = " + expService + " cod = " + codService + " otg = " + otgService);
                Client logisticsClient = new LogisticsClient().getClient();
                logisticsClient.updatePincode(provider, pincode, expService, codService, otgService);
                return null;
        }
        
        public void setBooleanServiceability(){
                
                if(cod!=null && cod.equals("on")) {
                        codService = true;
                }
                if(exp!= null && exp.equals("on")) {
                        expService = true;
                }
                if(otg!= null && otg.equals("on")) {
                        otgService = true;
                }
                if(stationType==null || !stationType.equals("on")) {
                        stationType_int = 0;
                }
        }
        
        public Long getProvider() {
                return provider;
        }

        public void setProvider(Long provider) {
                this.provider = provider;
        }

        public String getPincode() {
                return pincode;
        }

        public void setPincode(String pincode) {
                this.pincode = pincode;
        }

        public String getDestCode() {
                return destCode;
        }

        public void setDestCode(String destCode) {
                this.destCode = destCode;
        }

        public String isExp() {
                return exp;
        }

        public void setExp(String exp) {
                this.exp = exp;
        }

        public String isCod() {
                return cod;
        }

        public void setCod(String cod) {
                this.cod = cod;
        }

        public String isStationType() {
                return stationType;
        }

        public void setStationType(String stationType) {
                this.stationType = stationType;
        }

        public void setOtg(String otg) {
                this.otg = otg;
        }

        public String getOtg() {
                return otg;
        }
        
        //Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
        public String getRunCompleteUpdate() {
                return runCompleteUpdate;
        }

        public void setRunCompleteUpdate(String runCompleteUpdate) {
                this.runCompleteUpdate = runCompleteUpdate;
        }

        public File getRecordsFile() {
                return recordsFile;
        }

        public void setRecordsFile(File recordsFile) {
                this.recordsFile = recordsFile;
        }
        
        public String getErrorMsg(){
        return this.errorMsg;
    }
        
        public String getSuccessMessage(){
                return this.successmsg ;
        }

        private boolean checkForErrors(){
        Collection<String> actionErrors = getActionErrors();
        if(actionErrors != null && !actionErrors.isEmpty()){
            for (String str : actionErrors) {
                errorMsg += "<BR/>" + str;
            }
            return true;
        }
        return false;
    }
        //End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
}