Subversion Repositories SmartDukaan

Rev

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