Subversion Repositories SmartDukaan

Rev

Rev 6612 | Rev 7784 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6322 amar.kumar 1
package in.shop2020.support.controllers;
2
 
3
import in.shop2020.logistics.LogisticsService.Client;
7735 manish.sha 4
//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
5
import in.shop2020.support.utils.ReportsUtils;
6
//End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
6322 amar.kumar 7
import in.shop2020.thrift.clients.LogisticsClient;
8
 
9
import javax.servlet.http.HttpServletRequest;
7735 manish.sha 10
//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
11
import javax.servlet.http.HttpSession;
12
//End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
6322 amar.kumar 13
 
14
import org.apache.struts2.interceptor.ServletRequestAware;
15
import org.apache.thrift.TException;
6612 amar.kumar 16
import org.slf4j.Logger;
17
import org.slf4j.LoggerFactory;
7735 manish.sha 18
//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
19
import org.apache.commons.io.FileUtils;
20
import org.apache.poi.hssf.usermodel.HSSFSheet;
21
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
22
import org.apache.poi.ss.usermodel.Cell;
23
import org.apache.poi.ss.usermodel.Row;
24
//End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
6322 amar.kumar 25
import com.opensymphony.xwork2.ActionSupport;
7735 manish.sha 26
//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
27
import java.io.File;
28
import java.io.FileInputStream;
29
import java.io.FileNotFoundException;
30
import java.io.IOException;
31
import java.util.Collection;
32
import java.util.Iterator;
33
//End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
6322 amar.kumar 34
 
7735 manish.sha 35
@SuppressWarnings("serial")
6322 amar.kumar 36
public class PincodeController extends ActionSupport implements ServletRequestAware {
37
 
38
	private Long provider;
39
	private String pincode;
40
	private String destCode;
41
	private String exp;
42
	private String cod;
6524 rajveer 43
	private String otg;
6322 amar.kumar 44
	private String stationType;
7735 manish.sha 45
	//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
46
	private String runCompleteUpdate;
6322 amar.kumar 47
 
7735 manish.sha 48
	private HttpServletRequest request;
49
    private HttpSession session;
50
    //End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
51
 
6322 amar.kumar 52
	private boolean codService = false;
53
	private boolean expService = false;
6524 rajveer 54
	private boolean otgService = false;
6322 amar.kumar 55
	private int stationType_int = 1;
7735 manish.sha 56
	//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
57
	private boolean runCompleteUpdateValue = false;
6322 amar.kumar 58
 
7735 manish.sha 59
	private File recordsFile;
60
	//End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
6612 amar.kumar 61
	private static Logger logger = LoggerFactory.getLogger(PincodeController.class);
7735 manish.sha 62
	//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
63
    private String errorMsg = "";
64
	private String successmsg = "";
65
 
66
    public String index() {
67
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))
68
            return "authfail";
69
        checkForErrors();
70
        return "authsuccess";
71
    }
72
	//End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
73
 
6322 amar.kumar 74
	@Override
7735 manish.sha 75
	public void setServletRequest(HttpServletRequest request) {
6322 amar.kumar 76
		// TODO Auto-generated method stub
7735 manish.sha 77
		//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
78
		this.request = request;
79
        this.session = request.getSession();		
80
        //End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
81
	}
82
 
83
	//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
84
	public String create(){
85
		if(runCompleteUpdate!=null && runCompleteUpdate.equals("on")) {
86
			runCompleteUpdateValue = true;
87
		}
88
		 File fileToCreate = null;
89
	        try {
90
	            fileToCreate = new File("/tmp/", this.recordsFile.getName());
91
	            FileUtils.copyFile(this.recordsFile, fileToCreate);
92
	        } catch (Exception e) {
93
	           logger.error("Error while writing file used to the local file system", e);
94
	           addActionError("Unable to open the Uploaded File. Please check the file format.");
95
	           return "Error while writing file used to the local file system";
96
	        }
97
		if(fileToCreate.isFile()){
98
			HSSFWorkbook wb = null;
99
			try{
100
				wb = new HSSFWorkbook(new FileInputStream(fileToCreate));
101
			}
102
			catch (FileNotFoundException e) {
103
	            logger.error("Unable to open the Uploaded File", e);
104
	            addActionError("Unable to open the Uploaded File. Please check the file format.");
105
	        } catch (IOException e) {
106
	            logger.error("Unable to open the Uploaded File", e);
107
	            addActionError("Unable to open the Uploaded File. Please check the file format.");
108
	        }
109
	        catch (Exception e){
110
	        	logger.error("Supplied Data is not in specified form. Please check the file format",e);
111
	        	addActionError("Supplied Data is not in specified form. Please check the file format");
112
	        }
113
	        if(checkForErrors())
114
	            return "Error in updation";
115
	        try{
116
		        Client logisticsClient = new LogisticsClient().getClient();
117
		        if(runCompleteUpdateValue){
118
		        	logisticsClient.runCompleteUpdateForPincode();
119
		        }
120
	        }
121
	        catch (TException e) {
122
                logger.error("Unable to complete the operation", e);
123
                addActionError(e.getMessage());
124
            }
125
	        if(checkForErrors())
126
	            return "Error in updation";
127
	        HSSFSheet sheet= wb.getSheetAt(0);
128
	        Iterator<Row> rowIterator = sheet.rowIterator();
129
	        int rowCount = 0;
130
	        while(rowIterator.hasNext()) {
131
	            Row row = rowIterator.next();
132
	            if(rowCount++==0)
133
	            	continue;
134
	            Iterator<Cell> cellIterator = row.cellIterator();
135
	            int cellCount= 0;
136
	            while(cellIterator.hasNext()) {
137
 
138
	                Cell cell = cellIterator.next();
139
	                String cellVal_Str ="";
140
	                Long cellVal_Num  = 0L;
141
	                boolean cellVal_bool = false;
142
 
143
	                switch(cell.getCellType()) {
144
	                    case Cell.CELL_TYPE_BOOLEAN:
145
	                    	cellVal_bool = cell.getBooleanCellValue();
146
	                        System.out.print(cell.getBooleanCellValue() + "\t\t");
147
	                        break;
148
	                    case Cell.CELL_TYPE_NUMERIC:
149
	                    	cellVal_Num= (long) cell.getNumericCellValue();
150
	                        System.out.print(cell.getNumericCellValue() + "\t\t");
151
	                        break;
152
	                    case Cell.CELL_TYPE_STRING:
153
	                    	cellVal_Str = cell.getStringCellValue();
154
	                        System.out.print(cell.getStringCellValue() + "\t\t");
155
	                        break;
156
	                }
157
	                switch(cellCount) {
158
	                	case 0:
159
	                		provider = (long) cellVal_Num;
160
	                		break;
161
	                	case 1:
162
	                		pincode = cellVal_Str.length() > 0 ? cellVal_Str : cellVal_Num +"";
163
	                		break;
164
	                	case 2:
165
	                		destCode = cellVal_Str.length() > 0 ? cellVal_Str : cellVal_Num +"";
166
	                		break;
167
	                	case 3:
168
	                		if(cellVal_Num==0L)
169
	                			expService = false;
170
	                		else if(cellVal_Num==1L)
171
	                			expService = true;
172
	                		else 
173
	                			expService = false;
174
	                		break;
175
	                	case 4:
176
	                		if(cellVal_Num==0L)
177
	                			codService = false;
178
	                		else if(cellVal_Num==1L)
179
	                			codService = true;
180
	                		else 
181
	                			codService = false;
182
	                		break;
183
	                	case 5:
184
	                		stationType_int = Integer.parseInt(cellVal_Num.toString());
185
	                		break;
186
	                	case 6:
187
	                		if(cellVal_Num==0L)
188
	                			otgService = false;
189
	                		else if(cellVal_Num==1L)
190
	                			otgService = true;
191
	                		else 
192
	                			otgService = false;
193
	                		break;
194
	                }
195
	               cellCount++;
196
	            }
197
	            try{
198
	            	Client logisticsClient = new LogisticsClient().getClient();
199
		            boolean isRecordAvaliable = logisticsClient.getServiceableLocationAsPerProviderIdAndPincode(provider, pincode);
200
	                if(isRecordAvaliable){
201
	                	logisticsClient.updatePincode(provider, pincode, expService, codService, otgService);
202
	                }
203
	                else{
204
	                	logisticsClient.addPincode(provider, pincode, destCode, expService, codService, stationType_int, otgService);
205
	                }
206
	            }
207
	            catch (TException e) {
208
	                logger.error("Unable to update data", e);
209
	                addActionError(e.getMessage());
210
	            }
211
	            if(checkForErrors())
212
		            return "Error in updation";
213
	        }
214
		}
215
		else{
216
			addActionError("Uncompatible File Format !!!");
217
			if(checkForErrors())
218
	            return "Error in updation";
219
		}
220
		if(!checkForErrors()){
221
			successmsg="Pincode Updated Successfully";
222
		}
6322 amar.kumar 223
 
7735 manish.sha 224
		return "Pincode Updated Successfully";
225
 
6322 amar.kumar 226
	}
7735 manish.sha 227
	//End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
228
 
6322 amar.kumar 229
	public String addPincode() throws TException{
230
		setBooleanServiceability();
231
		Client logisticsClient = new LogisticsClient().getClient();
6524 rajveer 232
		logisticsClient.addPincode(provider, pincode, destCode, expService, codService, stationType_int, otgService);
6322 amar.kumar 233
		return null;
234
	}
235
 
236
	public String updatePincode() throws TException{
237
		setBooleanServiceability();
6612 amar.kumar 238
		logger.info("provider = " + provider + " pincode " + pincode + " exp = " + expService + " cod = " + codService + " otg = " + otgService);
6322 amar.kumar 239
		Client logisticsClient = new LogisticsClient().getClient();
6524 rajveer 240
		logisticsClient.updatePincode(provider, pincode, expService, codService, otgService);
6322 amar.kumar 241
		return null;
242
	}
243
 
244
	public void setBooleanServiceability(){
245
 
246
		if(cod!=null && cod.equals("on")) {
247
			codService = true;
248
		}
249
		if(exp!= null && exp.equals("on")) {
250
			expService = true;
251
		}
6524 rajveer 252
		if(otg!= null && otg.equals("on")) {
253
			otgService = true;
254
		}
6322 amar.kumar 255
		if(stationType==null || !stationType.equals("on")) {
256
			stationType_int = 0;
257
		}
258
	}
259
 
260
	public Long getProvider() {
261
		return provider;
262
	}
263
 
264
	public void setProvider(Long provider) {
265
		this.provider = provider;
266
	}
267
 
268
	public String getPincode() {
269
		return pincode;
270
	}
271
 
272
	public void setPincode(String pincode) {
273
		this.pincode = pincode;
274
	}
275
 
276
	public String getDestCode() {
277
		return destCode;
278
	}
279
 
280
	public void setDestCode(String destCode) {
281
		this.destCode = destCode;
282
	}
283
 
284
	public String isExp() {
285
		return exp;
286
	}
287
 
288
	public void setExp(String exp) {
289
		this.exp = exp;
290
	}
291
 
292
	public String isCod() {
293
		return cod;
294
	}
295
 
296
	public void setCod(String cod) {
297
		this.cod = cod;
298
	}
299
 
300
	public String isStationType() {
301
		return stationType;
302
	}
303
 
304
	public void setStationType(String stationType) {
305
		this.stationType = stationType;
306
	}
6524 rajveer 307
 
308
	public void setOtg(String otg) {
309
		this.otg = otg;
310
	}
311
 
312
	public String getOtg() {
313
		return otg;
314
	}
6322 amar.kumar 315
 
7735 manish.sha 316
	//Start:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
317
	public String getRunCompleteUpdate() {
318
		return runCompleteUpdate;
319
	}
320
 
321
	public void setRunCompleteUpdate(String runCompleteUpdate) {
322
		this.runCompleteUpdate = runCompleteUpdate;
323
	}
324
 
325
	public File getRecordsFile() {
326
		return recordsFile;
327
	}
328
 
329
	public void setRecordsFile(File recordsFile) {
330
		this.recordsFile = recordsFile;
331
	}
6322 amar.kumar 332
 
7735 manish.sha 333
	public String getErrorMsg(){
334
        return this.errorMsg;
335
    }
336
 
337
	public String getSuccessMessage(){
338
		return this.successmsg ;
339
	}
340
 
341
	private boolean checkForErrors(){
342
        Collection<String> actionErrors = getActionErrors();
343
        if(actionErrors != null && !actionErrors.isEmpty()){
344
            for (String str : actionErrors) {
345
                errorMsg += "<BR/>" + str;
346
            }
347
            return true;
348
        }
349
        return false;
350
    }
351
	//End:- Added/Modified by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
6322 amar.kumar 352
}