com.qatickets.service.TicketFileAttachmentService.java Source code

Java tutorial

Introduction

Here is the source code for com.qatickets.service.TicketFileAttachmentService.java

Source

/*
The MIT License (MIT)
    
Copyright (c) 2015 QAXML Pty Ltd
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.qatickets.service;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Date;

import javax.activation.MimetypesFileTypeMap;
import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qatickets.dao.TicketFileAttachmentDao;
import com.qatickets.domain.Ticket;
import com.qatickets.domain.TicketFileAttachment;
import com.qatickets.domain.UserProfile;

@Service
public class TicketFileAttachmentService {

    private static final Log log = LogFactory.getLog(TicketFileAttachmentService.class);

    @Inject
    private TicketFileAttachmentDao dao;

    @Inject
    private AttachmentStorageService s3client;

    @Inject
    private TicketService ticketService;

    @Inject
    private SpamService spamService;

    @Transactional(readOnly = false)
    public void resetUser(int userId) {
        dao.resetUser(userId);
        log.debug("Reset user: " + userId);
    }

    @Transactional(readOnly = false)
    public void save(File incomingFile, int ticketId, UserProfile user) {

        log.debug("Save attachment: " + incomingFile);

        Ticket ticket = ticketService.findById(ticketId);

        if (ticket != null) {

            // save attachment
            TicketFileAttachment a = new TicketFileAttachment();
            a.setDate(new Date());
            a.setFileName(spamService.clean(incomingFile.getName(), 200));
            a.setImage(false);
            a.setTicket(ticket);
            a.setUser(user);
            try {
                a.setContentType(Files.probeContentType(new File(incomingFile.getName()).toPath()));
            } catch (IOException e1) {
                a.setContentType(new MimetypesFileTypeMap().getContentType(incomingFile.getName()));
            }
            a.setSize(incomingFile.length());

            log.debug("Saved attachment: " + a);

            this.dao.save(a);

            // attach to body
            ticket.addAttachment(a);
            this.ticketService.save(ticket);

            // upload to aws
            /*
            File file = new File(SystemUtils.getJavaIoTmpDir(), UUID.randomUUID().toString());
            try {
               multpartFile.transferTo(file);
                
               s3client.save(a, user, file);
                
            } catch (Exception e) {
               log.error("Error: ", e);
            } finally {
               FileUtils.deleteQuietly(file);
            }
            */

        }

    }
    /*
       @Transactional(readOnly = true)
       public URL generateUrl(Account account, int attachmentId, int ticketId) {
          Ticket ticket = this.ticketService.findByAccountAndId(account.getId(), ticketId);
          if (ticket != null) {
     TicketFileAttachment fileAttachment = this.dao.findByTicketAndId(ticket, attachmentId);
     if (fileAttachment != null) {
        return s3client.generateUrl(fileAttachment);
     }
          }
          return null;
       }
        
       @Transactional(readOnly = true)
       public void download(Account account, int attachmentId, int ticketId, HttpServletResponse resp) throws Exception {
          Ticket ticket = this.ticketService.findByAccountAndId(account.getId(), ticketId);
          if (ticket != null) {
     TicketFileAttachment fileAttachment = this.dao.findByTicketAndId(ticket, attachmentId);
     if (fileAttachment != null) {
        
        resp.setContentType(fileAttachment.getContentType());
        resp.setHeader("Content-Disposition", "filename=\"" + fileAttachment.getFileName() + "\"");
        
        s3client.download(resp.getOutputStream(), fileAttachment);
     }
          }
       }
        
       @Transactional(readOnly = false)
       public void delete(int ticketId, UserProfile user, Account account, int attachmentId) {
        
          final Ticket ticket = this.ticketService.findByAccountAndId(account.getId(), ticketId);
        
          if (ticket != null) {
        
     TicketFileAttachment fileAttachment = this.dao.findByTicketAndId(ticket, attachmentId);
        
     log.debug("Delete ticket file attachment: #" + fileAttachment.getId());
        
     this.delete(fileAttachment);
          }
        
       }
        
       public void deleteByTicket(int ticketId) {
        
          final Ticket ticket = this.ticketService.findById(ticketId);
        
          if (ticket.hasFileAttachments()) {
     for (TicketFileAttachment a : ticket.getFileAttachments()) {
        log.debug("Delete ticket file attachment: #" + a.getId());
        this.delete(a);
     }
          }
        
       }
        
       @Transactional(readOnly = false)
       public void delete(TicketFileAttachment fileAttachment) {
        
          if (fileAttachment != null) {
     this.s3client.delete(fileAttachment);
     this.dao.delete(fileAttachment.getId());
          }
        
       }
        
       @Transactional(readOnly = true)
       public int countByTicket(Ticket ticket) {
          return dao.countByTicket(ticket);
       }
    */

}