Subversion Repositories SmartDukaan

Rev

Rev 9182 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.serving.controllers;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.ServletOutputStream;

import in.shop2020.crm.Activity;
import in.shop2020.crm.Agent;
import in.shop2020.crm.SearchFilter;
import in.shop2020.crm.Ticket;
import in.shop2020.serving.auth.CRMAuthorizingRealm;
import in.shop2020.thrift.clients.CRMClient;
import in.shop2020.util.CRMConstants;

import org.apache.thrift.TException;

/**
 * Action class to display activity details.
 * @author mandeep
 */
public class ActivityInfoController extends BaseController {
    private static final String SEMI_COLON = ";";
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private long activityId;
    private Ticket ticket;
    private Activity activity;
    private String attachment;

    public String index() throws TException
    {
        SearchFilter searchFilter = new SearchFilter();
        searchFilter.setActivityId(activityId);
        crmServiceClient         = new CRMClient().getClient();
        activity = crmServiceClient.getActivities(searchFilter).get(0);

        if (activity != null && activity.isSetTicketId()) {
            searchFilter.setTicketId(activity.getTicketId());
            ticket = crmServiceClient.getTickets(searchFilter).get(0);
        }

        return INDEX;
    }

    public Ticket getTicket() {
        return ticket;
    }

    public String[] getAttachments() {
        String attachments = activity.getAttachments();
        return attachments == null ? new String[0] : attachments.split(SEMI_COLON);
    }

    public Activity getActivity() {
        return activity;
    }

    public void setActivityId(long activityId) {
        this.activityId = activityId;
    }

    public Agent getAgent(long agentId) throws TException {
        return CRMAuthorizingRealm.getAgent(agentId);
    }
    

    public String getAttachment() {
        return attachment;
    }

    public void setAttachment(String attachment) {
        this.attachment = attachment;
    }
    
    public void downloadAttachment() {
        byte[] buffer = null;
        try {
            if (attachment == null || attachment.isEmpty()) {
                throw new Exception("File name is empty");
            }
            System.out.println("attachment ...."+attachment);
            /*String attachmentString = "";
            try {
                attachmentString = URLEncoder.encode(attachment, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                        System.out.println("Unable to encode the attachement file name");
                }
                System.out.println("attachmentString ...."+attachmentString);*/
            String fileName = CRMConstants.ATTACHMENTS_ARCHIVE_DIR + attachment;
            File file = new File(fileName);
            buffer = new byte[(int)file.length()];
            InputStream input = null;
            try {
                int totalBytesRead = 0;
                input = new BufferedInputStream(new FileInputStream(file));
                while(totalBytesRead < buffer.length){
                    int bytesRemaining = buffer.length - totalBytesRead;
                    //input.read() returns -1, 0, or more :
                    int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
                    if (bytesRead > 0){
                        totalBytesRead = totalBytesRead + bytesRead;
                    }
                }
                /*
                 the above style is a bit tricky: it places bytes into the 'buffer' array; 
                 'buffer' is an output parameter;
                 the while loop usually has a single iteration only.
                 */
            }
            finally {
                input.close();
            }
        }
        catch (FileNotFoundException ex) {
            System.out.println("FILE NOT FOUND : " + attachment);
        }
        catch (IOException ex) {
            System.out.println("FILE NOT FOUND : " + attachment);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        response.setHeader("Content-disposition", "inline; filename=" + attachment );

        ServletOutputStream sos;
        try {
            sos = response.getOutputStream();
            sos.write(buffer);
            sos.flush();
        } catch (IOException e) {
            System.out.println("Unable to stream the manifest file");
        }   
    }
}