Subversion Repositories SmartDukaan

Rev

Rev 3405 | Rev 4490 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3405 Rev 4241
Line 1... Line 1...
1
/**
1
/**
2
 * 
2
 * 
3
 */
3
 */
4
package in.shop2020.serving.controllers;
4
package in.shop2020.serving.controllers;
5
 
5
 
-
 
6
import java.io.BufferedInputStream;
-
 
7
import java.io.File;
-
 
8
import java.io.FileInputStream;
-
 
9
import java.io.FileNotFoundException;
-
 
10
import java.io.IOException;
-
 
11
import java.io.InputStream;
-
 
12
 
-
 
13
import javax.servlet.ServletOutputStream;
-
 
14
 
6
import in.shop2020.crm.Activity;
15
import in.shop2020.crm.Activity;
7
import in.shop2020.crm.Agent;
16
import in.shop2020.crm.Agent;
8
import in.shop2020.crm.SearchFilter;
17
import in.shop2020.crm.SearchFilter;
9
import in.shop2020.crm.Ticket;
18
import in.shop2020.crm.Ticket;
10
import in.shop2020.serving.auth.CRMAuthorizingRealm;
19
import in.shop2020.serving.auth.CRMAuthorizingRealm;
11
import in.shop2020.thrift.clients.CRMClient;
20
import in.shop2020.thrift.clients.CRMClient;
-
 
21
import in.shop2020.util.CRMConstants;
12
 
22
 
13
import org.apache.thrift.TException;
23
import org.apache.thrift.TException;
14
 
24
 
15
/**
25
/**
16
 * Action class to display activity details.
26
 * Action class to display activity details.
17
 * @author mandeep
27
 * @author mandeep
18
 */
28
 */
19
public class ActivityInfoController extends BaseController {
29
public class ActivityInfoController extends BaseController {
-
 
30
    private static final String SEMI_COLON = ";";
20
    /**
31
    /**
21
     * 
32
     * 
22
     */
33
     */
23
    private static final long serialVersionUID = 1L;
34
    private static final long serialVersionUID = 1L;
24
    private long activityId;
35
    private long activityId;
25
    private Ticket ticket;
36
    private Ticket ticket;
26
    private Activity activity;
37
    private Activity activity;
-
 
38
    private String attachment;
27
 
39
 
28
    public String index() throws TException
40
    public String index() throws TException
29
    {
41
    {
30
        SearchFilter searchFilter = new SearchFilter();
42
        SearchFilter searchFilter = new SearchFilter();
31
        searchFilter.setActivityId(activityId);
43
        searchFilter.setActivityId(activityId);
Line 42... Line 54...
42
 
54
 
43
    public Ticket getTicket() {
55
    public Ticket getTicket() {
44
        return ticket;
56
        return ticket;
45
    }
57
    }
46
 
58
 
-
 
59
    public String[] getAttachments() {
-
 
60
        String attachments = activity.getAttachments();
-
 
61
        return attachments == null ? new String[0] : attachments.split(SEMI_COLON);
-
 
62
    }
-
 
63
 
47
    public Activity getActivity() {
64
    public Activity getActivity() {
48
        return activity;
65
        return activity;
49
    }
66
    }
50
 
67
 
51
    public void setActivityId(long activityId) {
68
    public void setActivityId(long activityId) {
Line 53... Line 70...
53
    }
70
    }
54
 
71
 
55
    public Agent getAgent(long agentId) throws TException {
72
    public Agent getAgent(long agentId) throws TException {
56
        return CRMAuthorizingRealm.getAgent(agentId);
73
        return CRMAuthorizingRealm.getAgent(agentId);
57
    }
74
    }
-
 
75
    
-
 
76
 
-
 
77
    public String getAttachment() {
-
 
78
        return attachment;
-
 
79
    }
-
 
80
 
-
 
81
    public void setAttachment(String attachment) {
-
 
82
        this.attachment = attachment;
-
 
83
    }
-
 
84
    
-
 
85
    public void downloadAttachment() {
-
 
86
        String fileName = CRMConstants.ATTACHMENTS_ARCHIVE_DIR + attachment;
-
 
87
        File file = new File(fileName);
-
 
88
        byte[] buffer = new byte[(int)file.length()];
-
 
89
        try {
-
 
90
            InputStream input = null;
-
 
91
            try {
-
 
92
                int totalBytesRead = 0;
-
 
93
                input = new BufferedInputStream(new FileInputStream(file));
-
 
94
                while(totalBytesRead < buffer.length){
-
 
95
                    int bytesRemaining = buffer.length - totalBytesRead;
-
 
96
                    //input.read() returns -1, 0, or more :
-
 
97
                    int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
-
 
98
                    if (bytesRead > 0){
-
 
99
                        totalBytesRead = totalBytesRead + bytesRead;
-
 
100
                    }
-
 
101
                }
-
 
102
                /*
-
 
103
                 the above style is a bit tricky: it places bytes into the 'buffer' array; 
-
 
104
                 'buffer' is an output parameter;
-
 
105
                 the while loop usually has a single iteration only.
-
 
106
                 */
-
 
107
            }
-
 
108
            finally {
-
 
109
                input.close();
-
 
110
            }
-
 
111
        }
-
 
112
        catch (FileNotFoundException ex) {
-
 
113
            System.out.println("FILE NOT FOUND : " + attachment);
-
 
114
        }
-
 
115
        catch (IOException ex) {
-
 
116
            System.out.println("FILE NOT FOUND : " + attachment);
-
 
117
        }
-
 
118
        response.setHeader("Content-disposition", "inline; filename=" + attachment );
-
 
119
 
-
 
120
        ServletOutputStream sos;
-
 
121
        try {
-
 
122
            sos = response.getOutputStream();
-
 
123
            sos.write(buffer);
-
 
124
            sos.flush();
-
 
125
        } catch (IOException e) {
-
 
126
            System.out.println("Unable to stream the manifest file");
-
 
127
        }   
-
 
128
    }
58
}
129
}
59
130