PasswordMailSendService.java 4.81 KB
Newer Older
Kim Gyeongeun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
package jp.agentec.sinaburocast.service;

import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import javax.mail.MessagingException;

import jp.agentec.sinaburocast.common.SinaburoConstant;
import jp.agentec.sinaburocast.common.util.MailUtil;
import jp.agentec.sinaburocast.common.util.PropertyUtil;
import jp.agentec.sinaburocast.common.util.SinaburoUtil;
import jp.agentec.sinaburocast.entity.Member;
import jp.agentec.sinaburocast.entity.Message;
import jp.agentec.sinaburocast.entity.PasswordMailSend;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.log4j.Logger;
import org.seasar.extension.jdbc.where.SimpleWhere;
import org.seasar.framework.container.annotation.tiger.Component;
import org.seasar.framework.container.annotation.tiger.InstanceType;

@Component(instance=InstanceType.SINGLETON)
public class PasswordMailSendService extends AbstractService<PasswordMailSend> {

	private static final Logger logger = Logger.getLogger(PasswordMailSendService.class);
	public static final String ID_SEQ_NAME = "password_mail_send_id_seq";
	public static final String PASSWORD_MAIL_TITLE = "パスワード確認URLのご送付";


	public MessageService messageService;




	/**
	 * IDを発行して、登録する。
	 */
	public int insertPasswordMailSend(PasswordMailSend passwordMailSend, String insId) {
		passwordMailSend.passwordMailSendId = getSeqNextVal(Integer.class, ID_SEQ_NAME);
		return super.insert(passwordMailSend, insId);
	}


    public PasswordMailSend findById(Integer passwordMailSendId) {
        return select().id(passwordMailSendId).getSingleResult();
    }

//    public PasswordMailSend findBymemberId(Integer memberId) {
//        return select().where(new SimpleWhere().eq("memberId", memberId)).getSingleResult();
//    }

    public List<PasswordMailSend> findAllOrderById() {
        return select().orderBy("passwordMailSendId asc").getResultList();
    }

    public List<PasswordMailSend> findBymemberIdList(Integer memberId) {
        return select().where(new SimpleWhere().eq("memberId", memberId)).getResultList();
    }

    /**
     * ID、パスワード問い合わせのメールを送信し、パスワード確認メール送信TBLにデータ登録
     *
     * @param member ユーザー情報
     * @param email  ユーザーが入力したEmailアドレス
     * @return insertの結果件数。
     * @throws MessagingException
     * @throws UnsupportedEncodingException
     */
	public int insertPasswordMailSendLogic(Member member,String email)throws MessagingException, UnsupportedEncodingException {

    	//事前準備
    	Message message=messageService.findById(SinaburoConstant.MessageCode.PASS_WORD_CONFIRM);
    	List<PasswordMailSend> passwordMailSends = this.findAllByMailType(SinaburoConstant.MailType.PASSWORD);


    	int retValue = 0;

    	//データ設定
    	PasswordMailSend passwordMailSend = new  PasswordMailSend();
    	//onetimeTicketは唯一
    	ArrayList<String> onetimeTicketList = new ArrayList<String>();
    	for (PasswordMailSend pms : passwordMailSends) {
    		onetimeTicketList.add(pms.onetimeTicket);
		}

    	String min = PropertyUtil.getProperty("mail.add_expired_date_min");
    	passwordMailSend.memberId = member.memberId;
    	passwordMailSend.expiredDate = SinaburoUtil.addMin(Integer.parseInt(min));
    	passwordMailSend.mailType=SinaburoConstant.MailType.PASSWORD;

		do {
			passwordMailSend.onetimeTicket = RandomStringUtils.randomAlphanumeric(30);
			//既に登録されていたら作りなおす
		} while (onetimeTicketList.contains(passwordMailSend.onetimeTicket) );

		//登録
    	retValue = insertPasswordMailSend(passwordMailSend,member.loginId);

    	//メール送信
    	String body= MessageFormat.format(message.message.replace("\\r\\n", "\r\n"), passwordMailSend.onetimeTicket);
		MailUtil.send(email, PASSWORD_MAIL_TITLE,body.toString());

    	return retValue;
    }
	
	public List<PasswordMailSend> findAllByMailType(Integer mailType){
		return select().where(new SimpleWhere().eq("mailType", mailType)).getResultList();	
	}
	
	public PasswordMailSend findByTicketAndMailType(String oneTimeTicket,Integer mailType){
		return select().where(new SimpleWhere().eq("onetimeTicket", oneTimeTicket).ge("expiredDate", SinaburoUtil.getTimestamp())).getSingleResult();	
	}
	
	


	/**
	 * OneTypeTicketで検索する。oneTypeTicketが同一でexpiredDateが一時間前より以降
	 * @param passwordMailSendId
	 * @return
	 */
    public PasswordMailSend findByOneTypeTicket(String oneTypeTicket,Integer mailType) {
    	String min = PropertyUtil.getProperty("mail.add_expired_date_min");
        return select().where(new SimpleWhere().eq("onetimeTicket", oneTypeTicket).gt("expiredDate", SinaburoUtil.addMin(Integer.parseInt(min) * -1)).eq("mailType", mailType)).getSingleResult();
    }

}