Subversion Repositories SmartDukaan

Rev

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

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