com.eryansky.modules.disk.service.FileShareManager.java Source code

Java tutorial

Introduction

Here is the source code for com.eryansky.modules.disk.service.FileShareManager.java

Source

/**
 * Copyright (c) 2012-2014 http://www.eryansky.com
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.eryansky.modules.disk.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.Validate;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eryansky.common.orm.Page;
import com.eryansky.common.orm.entity.StatusState;
import com.eryansky.common.orm.hibernate.EntityManager;
import com.eryansky.common.orm.hibernate.HibernateDao;
import com.eryansky.common.orm.hibernate.Parameter;
import com.eryansky.common.utils.StringUtils;
import com.eryansky.common.utils.collections.Collections3;
import com.google.common.collect.Lists;
import com.eryansky.modules.disk.entity.File;
import com.eryansky.modules.disk.entity.FileNotice;
import com.eryansky.modules.disk.entity.FileShare;
import com.eryansky.modules.disk.entity.Folder;
import com.eryansky.modules.disk.entity._enum.FileOperate;
import com.eryansky.modules.disk.entity._enum.FolderAuthorize;
import com.eryansky.modules.disk.web.DiskController.NType;

/**
 * ?
 * 
 * @author xwj 201518 10:06:34
 */
@Service
public class FileShareManager extends EntityManager<FileShare, String> {

    private HibernateDao<FileShare, String> shareDao;
    @Autowired
    private FolderManager folderManager;
    @Autowired
    private FileManager fileManager;
    @Autowired
    private FileNoticeManager fileNoticeManager;

    /**
     * sessionFactory?DAO???.
     */
    @Autowired
    public void setSessionFactory(final SessionFactory sessionFactory) {
        shareDao = new HibernateDao<FileShare, String>(sessionFactory, FileShare.class);
    }

    @Override
    protected HibernateDao<FileShare, String> getEntityDao() {
        return shareDao;
    }

    /**
     * --->??
     * 
     * @modify by xwj 2015121 18:13:16 ?
     * @param file
     *            
     * @param userIds
     *            ?
     */
    public void shareFileToPerson(File file, String shareUserId, List<String> userIds) {
        if (Collections3.isNotEmpty(userIds)) {
            FileShare shareFile = new FileShare();
            shareFile.setFile(file);
            shareFile.setUserId(shareUserId);
            shareFile.setSharedUserList(userIds);
            shareFile.setShareTime(Calendar.getInstance().getTime());
            save(shareFile);

            FileNotice fileNotice = new FileNotice();
            fileNotice.setFile(file);
            fileNotice.setUserId(shareUserId);
            fileNotice.setOperateType(FileOperate.SHARE.getValue());
            fileNotice.setReceiveUserList(userIds);
            fileNotice.setLocation(FolderAuthorize.Share.getValue());
            fileNoticeManager.save(fileNotice);
        }

    }

    /**
     * ?
     * 
     * @param page
     * @param userId
     *            
     * @param fileName
     *            ??
     */
    public Page<FileShare> findSharePage(Page<FileShare> page, String userId, String fileName) {
        if (userId != null) {
            StringBuffer hql = new StringBuffer();
            Parameter parameter = new Parameter(userId, StatusState.DELETE.getValue());
            hql.append(
                    " from FileShare  s  where s.userId  = :p1  and EXISTS ( from File f where f.id = s.file.id  and f.status <> :p2 ");
            if (StringUtils.isNotBlank(fileName)) {
                hql.append(" and  f.name like CONCAT('%', :fileName, '%') ");
                parameter.put("fileName", fileName);
            }
            hql.append(" )  order by s.shareTime desc ");
            page = getEntityDao().findPage(page, hql.toString(), parameter);
        } else {
            page.setTotalCount(0);
        }
        return page;
    }

    /**
     * ?
     * 
     * @param page
     * @param userId
     *            
     * @param fileName
     *            ??
     * @return
     */
    public Page<FileShare> findReceivePage(Page<FileShare> page, String userId, String fileName) {
        if (userId != null) {
            StringBuffer hql = new StringBuffer();
            Parameter parameter = new Parameter(StatusState.DELETE.getValue());
            hql.append(" select s from FileShare s, File  f  where f.id = s.file.id and f.status <> :p1 ");
            if (StringUtils.isNotBlank(fileName)) {
                hql.append("  and f.name like :fileName ");
                parameter.put("fileName", "%" + fileName + "%");
            }
            hql.append("  and  :userId in elements(s.sharedUserList)  ");
            parameter.put("userId", userId);
            hql.append(" order by s.shareTime desc ");
            page = getEntityDao().findPage(page, hql.toString(), parameter);

        } else {
            page.setTotalCount(0);
        }
        return page;
    }

    /**
     * --->?
     * 
     * @param file
     *            
     * @param userId
     *            
     * @param organIds
     *            ?
     */
    public void shareFileToOrgan(File file, String userId, List<String> organIds) {
        if (Collections3.isNotEmpty(organIds)) {
            List<String> sharedFileList = Lists.newArrayList();
            Date nowTime = Calendar.getInstance().getTime();
            for (String organId : organIds) {
                Folder folder = folderManager.initShareForOrgan(organId);
                File newFile = file.copy();
                newFile.setUserId(userId);
                newFile.setCreateTime(nowTime);
                newFile.setShareUserId(null);
                newFile.setFolder(folder);
                fileManager.save(newFile);
                sharedFileList.add(newFile.getId());

            }
            FileShare shareFile = new FileShare();
            shareFile.setFile(file);
            shareFile.setUserId(userId);
            shareFile.setSharedFileList(sharedFileList);
            shareFile.setShareTime(nowTime);
            save(shareFile);

            FileNotice fileNotice = new FileNotice();
            fileNotice.setFile(file);
            fileNotice.setUserId(userId);
            fileNotice.setOperateType(FileOperate.SHARE.getValue());
            fileNotice.setReceiveOrganList(organIds);
            fileNotice.setLocation(FolderAuthorize.Organ.getValue());
            fileNoticeManager.save(fileNotice);
        }

    }

    /**
     * --->?
     * 
     * @param file
     *            
     * @param userId
     *            
     */
    public void shareFileToPublic(File file, String userId) {
        Date nowTime = Calendar.getInstance().getTime();
        Folder folder = folderManager.initShareForPublic();
        File newFile = file.copy();
        newFile.setUserId(userId);
        newFile.setShareUserId(null);
        newFile.setFolder(folder);
        newFile.setCreateTime(nowTime);
        fileManager.save(newFile);

        FileShare shareFile = new FileShare();
        shareFile.setFile(file);
        shareFile.setUserId(userId);
        shareFile.setSharedFileList(Lists.newArrayList(newFile.getId()));
        shareFile.setShareTime(nowTime);
        save(shareFile);

        FileNotice fileNotice = new FileNotice();
        fileNotice.setFile(file);
        fileNotice.setUserId(userId);
        fileNotice.setOperateType(FileOperate.SHARE.getValue());
        fileNotice.setLocation(FolderAuthorize.Public.getValue());
        fileNoticeManager.save(fileNotice);

    }

    /**
     * ?
     * 
     * @param shareId
     */
    public void removeShare(String shareId) {
        if (shareId != null) {
            FileShare fileShare = getById(shareId);
            List<String> sharedFileList = fileShare.getSharedFileList();
            if (Collections3.isNotEmpty(sharedFileList)) {
                fileManager.deleteFolderFiles(sharedFileList);
            }
            delete(fileShare);
        }

    }

    /**
     * ??
     * 
     * @param pageId
     *            Id
     * @param userId
     *            Id
     * @param nodeType
     *            
     * @param nodeId
     *            Id
     */
    public void removeReceive(String pageId, String userId, String nodeType, Integer nodeId) {
        Validate.notNull(pageId, "?[pageId]?null.");
        if (NType.FolderAuthorize.toString().equals(nodeType)
                && FolderAuthorize.ReceivePerson.getValue().equals(nodeId)) {// ,?Id
            FileShare fileShare = getById(pageId);
            List<String> sharedUserList = fileShare.getSharedUserList();
            if (Collections3.isNotEmpty(sharedUserList)) {
                int index = sharedUserList.indexOf(userId);
                if (index > -1) {
                    sharedUserList.remove(index);
                }
            }
            saveOrUpdate(fileShare);
        } else {//  ,?Id
            FileShare fileShare = this.findUniqueShareByFileId(pageId);
            if (fileShare != null) {
                fileShare.getSharedFileList().remove(pageId);
                saveOrUpdate(fileShare);
                fileManager.deleteFile(pageId);
            }
        }

    }

    /**
     * ?Id?
     * 
     * @param fileId
     * @return
     */
    public FileShare findUniqueShareByFileId(String fileId) {
        Validate.notNull(fileId, "?[fileId]?null.");
        StringBuffer hql = new StringBuffer();
        hql.append(" from FileShare s where ?  in elements(s.sharedFileList) ");
        FileShare share = getEntityDao().findUnique(hql.toString(), fileId);
        return share;
    }

}