Subversion Repositories SmartDukaan

Rev

Rev 754 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 754 Rev 756
Line 6... Line 6...
6
import in.shop2020.thrift.clients.HelperServiceClient;
6
import in.shop2020.thrift.clients.HelperServiceClient;
7
import in.shop2020.utils.HelperService;
7
import in.shop2020.utils.HelperService;
8
import in.shop2020.utils.LogisticsUser;
8
import in.shop2020.utils.LogisticsUser;
9
 
9
 
10
import java.io.ByteArrayOutputStream;
10
import java.io.ByteArrayOutputStream;
-
 
11
import java.io.File;
-
 
12
import java.io.FileInputStream;
11
import java.io.IOException;
13
import java.io.IOException;
12
import java.util.Calendar;
14
import java.util.Calendar;
13
import java.util.GregorianCalendar;
15
import java.util.GregorianCalendar;
14
import java.util.HashMap;
16
import java.util.HashMap;
15
import java.util.List;
17
import java.util.List;
Line 26... Line 28...
26
import org.apache.struts2.rest.HttpHeaders;
28
import org.apache.struts2.rest.HttpHeaders;
27
 
29
 
28
public class CourierDetailsController implements ServletResponseAware,
30
public class CourierDetailsController implements ServletResponseAware,
29
		ServletRequestAware {
31
		ServletRequestAware {
30
	private String id;
32
	private String id;
-
 
33
	
31
	//private long warehouseId;
34
	//FIXME: Read this configuration from the config client
32
	//private long providerId;
35
	private String courierDetailsPath = "/CourierDetailReports";
-
 
36
	
33
	private HttpServletRequest request;
37
	private HttpServletRequest request;
34
	private HttpServletResponse response;
38
	private HttpServletResponse response;
35
	private HttpSession session;
39
	private HttpSession session;
36
 
40
 
37
	public String index(){
41
	public String index(){
Line 65... Line 69...
65
			long providerId = ((Long)session.getAttribute("providerId")).longValue();
69
			long providerId = ((Long)session.getAttribute("providerId")).longValue();
66
			
70
			
67
			System.out.println("Warehouse Id is:  " + warehouseId);
71
			System.out.println("Warehouse Id is:  " + warehouseId);
68
			System.out.println("Provider Id is: " + providerId);
72
			System.out.println("Provider Id is: " + providerId);
69
			
73
			
70
			CourierDetailsGenerator courierDetailsGenerator = new CourierDetailsGenerator();
-
 
71
			ByteArrayOutputStream baos = courierDetailsGenerator.generateCourierDetails(warehouseId, providerId);
-
 
72
			response.setContentType("application/vnd.ms-excel");
74
			response.setContentType("application/vnd.ms-excel");
73
			
75
			
74
			Calendar date = new GregorianCalendar();
76
			Calendar date = new GregorianCalendar();
75
			int year = date.get(Calendar.YEAR);
77
			int year = date.get(Calendar.YEAR);
76
			int month = date.get(Calendar.MONTH) +1;
78
			int month = date.get(Calendar.MONTH) +1;
77
			int day = date.get(Calendar.DAY_OF_MONTH);
79
			int day = date.get(Calendar.DAY_OF_MONTH);
78
			
-
 
-
 
80
			String fileName = courierDetailsPath + "/courier-details-"+warehouseId+"-"+ providerId + "-"+year+"-"+ month+"-" + day +".xls";
79
			response.setHeader("Content-disposition", "inline; filename=courier-details-"+warehouseId+"-"+ providerId + "-"+year+"-"+ month+"-" + day +".xls" );
81
			response.setHeader("Content-disposition", "inline; filename=courier-details-"+warehouseId+"-"+ providerId + "-"+year+"-"+ month+"-" + day +".xls" );
80
			
82
			
81
			ServletOutputStream sos;
83
			ServletOutputStream sos;
82
			try {
84
			try {
-
 
85
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
 
86
				baos.write(getBytesFromFile(new File(fileName)));
83
				sos = response.getOutputStream();
87
				sos = response.getOutputStream();
84
				baos.writeTo(sos);
88
				baos.writeTo(sos);
85
				sos.flush();
89
				sos.flush();
86
			} catch (IOException e) {
90
			} catch (IOException e) {
87
				e.printStackTrace();
91
				e.printStackTrace();
Line 91... Line 95...
91
			nfe.printStackTrace();
95
			nfe.printStackTrace();
92
		}
96
		}
93
		return "authfail";
97
		return "authfail";
94
	}
98
	}
95
	
99
	
-
 
100
	// Returns the contents of the file in a byte array.
-
 
101
	public static byte[] getBytesFromFile(File file) throws IOException {
-
 
102
	    FileInputStream is = new FileInputStream(file);
-
 
103
	    
-
 
104
	    // Get the size of the file
-
 
105
	    long length = file.length();
-
 
106
 
-
 
107
	    // You cannot create an array using a long type.
-
 
108
	    // It needs to be an int type.
-
 
109
	    // Before converting to an int type, check
-
 
110
	    // to ensure that file is not larger than Integer.MAX_VALUE.
-
 
111
	    if (length > Integer.MAX_VALUE) {
-
 
112
	        // File is too large
-
 
113
	    }
-
 
114
 
-
 
115
	    // Create the byte array to hold the data
-
 
116
	    byte[] bytes = new byte[(int)length];
-
 
117
 
-
 
118
	    // Read in the bytes
-
 
119
	    int offset = 0;
-
 
120
	    int numRead = 0;
-
 
121
	    while (offset < bytes.length
-
 
122
	           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
-
 
123
	        offset += numRead;
-
 
124
	    }
-
 
125
 
-
 
126
	    // Ensure all the bytes have been read in
-
 
127
	    if (offset < bytes.length) {
-
 
128
	        throw new IOException("Could not completely read file "+file.getName());
-
 
129
	    }
-
 
130
 
-
 
131
	    // Close the input stream and return bytes
-
 
132
	    is.close();
-
 
133
	    return bytes;
-
 
134
	}
-
 
135
	
96
	@Override
136
	@Override
97
	public void setServletResponse(HttpServletResponse response) {
137
	public void setServletResponse(HttpServletResponse response) {
98
		this.response  = response;
138
		this.response  = response;
99
	}
139
	}
100
 
140