123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- /*
- * Copyright (C) 2004-2015 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * L2J Server is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.gameserver.instancemanager;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
- import com.l2jserver.gameserver.ThreadPoolManager;
- import com.l2jserver.gameserver.idfactory.IdFactory;
- import com.l2jserver.gameserver.instancemanager.tasks.MessageDeletionTask;
- import com.l2jserver.gameserver.model.L2World;
- import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
- import com.l2jserver.gameserver.model.entity.Message;
- import com.l2jserver.gameserver.network.serverpackets.ExNoticePostArrived;
- /**
- * @author Migi, DS
- */
- public final class MailManager
- {
- private static final Logger _log = Logger.getLogger(MailManager.class.getName());
-
- private final Map<Integer, Message> _messages = new ConcurrentHashMap<>();
-
- protected MailManager()
- {
- load();
- }
-
- private void load()
- {
- int count = 0;
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- Statement ps = con.createStatement();
- ResultSet rs = ps.executeQuery("SELECT * FROM messages ORDER BY expiration"))
- {
- while (rs.next())
- {
-
- final Message msg = new Message(rs);
-
- int msgId = msg.getId();
- _messages.put(msgId, msg);
-
- count++;
-
- long expiration = msg.getExpiration();
-
- if (expiration < System.currentTimeMillis())
- {
- ThreadPoolManager.getInstance().scheduleGeneral(new MessageDeletionTask(msgId), 10000);
- }
- else
- {
- ThreadPoolManager.getInstance().scheduleGeneral(new MessageDeletionTask(msgId), expiration - System.currentTimeMillis());
- }
- }
- }
- catch (SQLException e)
- {
- _log.log(Level.WARNING, getClass().getSimpleName() + ": Error loading from database:" + e.getMessage(), e);
- }
- _log.info(getClass().getSimpleName() + ": Successfully loaded " + count + " messages.");
- }
-
- public final Message getMessage(int msgId)
- {
- return _messages.get(msgId);
- }
-
- public final Collection<Message> getMessages()
- {
- return _messages.values();
- }
-
- public final boolean hasUnreadPost(L2PcInstance player)
- {
- final int objectId = player.getObjectId();
- for (Message msg : getMessages())
- {
- if ((msg != null) && (msg.getReceiverId() == objectId) && msg.isUnread())
- {
- return true;
- }
- }
- return false;
- }
-
- public final int getInboxSize(int objectId)
- {
- int size = 0;
- for (Message msg : getMessages())
- {
- if ((msg != null) && (msg.getReceiverId() == objectId) && !msg.isDeletedByReceiver())
- {
- size++;
- }
- }
- return size;
- }
-
- public final int getOutboxSize(int objectId)
- {
- int size = 0;
- for (Message msg : getMessages())
- {
- if ((msg != null) && (msg.getSenderId() == objectId) && !msg.isDeletedBySender())
- {
- size++;
- }
- }
- return size;
- }
-
- public final List<Message> getInbox(int objectId)
- {
- final List<Message> inbox = new ArrayList<>();
- for (Message msg : getMessages())
- {
- if ((msg != null) && (msg.getReceiverId() == objectId) && !msg.isDeletedByReceiver())
- {
- inbox.add(msg);
- }
- }
- return inbox;
- }
-
- public final List<Message> getOutbox(int objectId)
- {
- final List<Message> outbox = new ArrayList<>();
- for (Message msg : getMessages())
- {
- if ((msg != null) && (msg.getSenderId() == objectId) && !msg.isDeletedBySender())
- {
- outbox.add(msg);
- }
- }
- return outbox;
- }
-
- public void sendMessage(Message msg)
- {
- _messages.put(msg.getId(), msg);
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- PreparedStatement ps = Message.getStatement(msg, con))
- {
- ps.execute();
- }
- catch (SQLException e)
- {
- _log.log(Level.WARNING, getClass().getSimpleName() + ": Error saving message:" + e.getMessage(), e);
- }
-
- final L2PcInstance receiver = L2World.getInstance().getPlayer(msg.getReceiverId());
- if (receiver != null)
- {
- receiver.sendPacket(ExNoticePostArrived.valueOf(true));
- }
-
- ThreadPoolManager.getInstance().scheduleGeneral(new MessageDeletionTask(msg.getId()), msg.getExpiration() - System.currentTimeMillis());
- }
-
- public final void markAsReadInDb(int msgId)
- {
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("UPDATE messages SET isUnread = 'false' WHERE messageId = ?"))
- {
- ps.setInt(1, msgId);
- ps.execute();
- }
- catch (SQLException e)
- {
- _log.log(Level.WARNING, getClass().getSimpleName() + ": Error marking as read message:" + e.getMessage(), e);
- }
- }
-
- public final void markAsDeletedBySenderInDb(int msgId)
- {
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("UPDATE messages SET isDeletedBySender = 'true' WHERE messageId = ?"))
- {
- ps.setInt(1, msgId);
- ps.execute();
- }
- catch (SQLException e)
- {
- _log.log(Level.WARNING, getClass().getSimpleName() + ": Error marking as deleted by sender message:" + e.getMessage(), e);
- }
- }
-
- public final void markAsDeletedByReceiverInDb(int msgId)
- {
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("UPDATE messages SET isDeletedByReceiver = 'true' WHERE messageId = ?"))
- {
- ps.setInt(1, msgId);
- ps.execute();
- }
- catch (SQLException e)
- {
- _log.log(Level.WARNING, getClass().getSimpleName() + ": Error marking as deleted by receiver message:" + e.getMessage(), e);
- }
- }
-
- public final void removeAttachmentsInDb(int msgId)
- {
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("UPDATE messages SET hasAttachments = 'false' WHERE messageId = ?"))
- {
- ps.setInt(1, msgId);
- ps.execute();
- }
- catch (SQLException e)
- {
- _log.log(Level.WARNING, getClass().getSimpleName() + ": Error removing attachments in message:" + e.getMessage(), e);
- }
- }
-
- public final void deleteMessageInDb(int msgId)
- {
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("DELETE FROM messages WHERE messageId = ?"))
- {
- ps.setInt(1, msgId);
- ps.execute();
- }
- catch (SQLException e)
- {
- _log.log(Level.WARNING, getClass().getSimpleName() + ": Error deleting message:" + e.getMessage(), e);
- }
-
- _messages.remove(msgId);
- IdFactory.getInstance().releaseId(msgId);
- }
-
- /**
- * Gets the single instance of {@code MailManager}.
- * @return single instance of {@code MailManager}
- */
- public static MailManager getInstance()
- {
- return SingletonHolder._instance;
- }
-
- private static class SingletonHolder
- {
- protected static final MailManager _instance = new MailManager();
- }
- }
|